{"version":3,"sources":["../src/core/component-registry.ts"],"names":["ComponentWeakRegistry","key","entry","instance","cleanup","token","componentRegistry"],"mappings":"AA6BO,IAAMA,CAAAA,CAAN,KAA4B,CAI/B,WAAA,EAAc,CAHd,IAAA,CAAiB,QAAA,CAAW,IAAI,GAAA,CAI5B,IAAA,CAAK,UAAA,CAAa,IAAI,oBAAA,CAA8BC,GAAQ,CACxD,IAAMC,CAAAA,CAAQ,IAAA,CAAK,QAAA,CAAS,GAAA,CAAID,CAAG,CAAA,CAC9BC,IAGLA,CAAAA,CAAM,OAAA,EAAQ,CACd,IAAA,CAAK,QAAA,CAAS,MAAA,CAAOD,CAAG,CAAA,EAC5B,CAAC,EACL,CAEA,IAAI,IAAA,EAAe,CACf,OAAO,IAAA,CAAK,QAAA,CAAS,IACzB,CAEA,GAAA,CAAIA,CAAAA,CAAsB,CACtB,OAAO,IAAA,CAAK,QAAA,CAAS,GAAA,CAAIA,CAAG,CAChC,CAYA,QAAA,CAASE,CAAAA,CAAkBF,CAAAA,CAAaG,CAAAA,CAA2B,CAC3D,IAAA,CAAK,SAAS,GAAA,CAAIH,CAAG,CAAA,EAAG,IAAA,CAAK,WAAWA,CAAG,CAAA,CAC/C,IAAMI,CAAAA,CAAQ,EAAC,CACTH,CAAAA,CAAe,CACjB,GAAA,CAAK,IAAI,OAAA,CAAQC,CAAQ,CAAA,CACzB,QAAAC,CAAAA,CACA,KAAA,CAAAC,CACJ,CAAA,CACA,IAAA,CAAK,QAAA,CAAS,GAAA,CAAIJ,CAAAA,CAAKC,CAAK,CAAA,CAC5B,IAAA,CAAK,UAAA,CAAW,QAAA,CAASC,CAAAA,CAAUF,CAAAA,CAAKI,CAAK,EACjD,CAMA,UAAA,CAAWJ,CAAAA,CAAmB,CAC1B,IAAMC,EAAQ,IAAA,CAAK,QAAA,CAAS,GAAA,CAAID,CAAG,EAC9BC,CAAAA,GACL,IAAA,CAAK,UAAA,CAAW,UAAA,CAAWA,CAAAA,CAAM,KAAK,CAAA,CACtC,IAAA,CAAK,SAAS,MAAA,CAAOD,CAAG,CAAA,CACxBC,CAAAA,CAAM,OAAA,EAAQ,EAClB,CAOA,MAAA,CAAOD,EAAqC,CACxC,OAAO,IAAA,CAAK,QAAA,CAAS,GAAA,CAAIA,CAAG,CAAA,EAAG,GAAA,EAAO,IAC1C,CAGA,cAAA,EAAuB,CACnB,IAAA,CAAK,SAAS,KAAA,GAClB,CACJ,CAAA,CAMaK,EAAoB,IAAIN","file":"chunk-A7E5TGHB.mjs","sourcesContent":["/**\n * ComponentWeakRegistry\n *\n * Tracks live component instances via WeakRef. When the JS engine GCs an\n * instance that was never explicitly unregistered, a FinalizationRegistry\n * callback fires to run emergency cleanup (stop leaked effects, release\n * global keys, etc.).\n *\n * Two paths:\n *   - Explicit (preferred): the renderer calls `unregister(key)` during normal\n *     unmount. Cleanup runs synchronously, the entry is removed, and the\n *     finalizer is detached.\n *   - GC-triggered (fallback): if a reference is dropped without calling\n *     `unregister` (a bug, but we want to recover gracefully), the\n *     FinalizationRegistry fires the callback asynchronously after the\n *     instance is collected.\n *\n * Browser support: WeakRef + FinalizationRegistry available in\n * Chrome 84+, Firefox 79+, Safari 14.1+ (already required by flexium's\n * existing Sprint 2 globalRegistry work).\n */\n\ninterface Entry {\n    ref: WeakRef<object>\n    cleanup: () => void\n    /** FinalizationRegistry unregister token — opaque object identity. */\n    token: object\n}\n\nexport class ComponentWeakRegistry {\n    private readonly _entries = new Map<string, Entry>()\n    private readonly _finalizer: FinalizationRegistry<string>\n\n    constructor() {\n        this._finalizer = new FinalizationRegistry<string>((key) => {\n            const entry = this._entries.get(key)\n            if (!entry) return\n            // GC-triggered: instance was collected without explicit unregister.\n            // Run emergency cleanup, then drop the entry.\n            entry.cleanup()\n            this._entries.delete(key)\n        })\n    }\n\n    get size(): number {\n        return this._entries.size\n    }\n\n    has(key: string): boolean {\n        return this._entries.has(key)\n    }\n\n    /**\n     * Register `instance` under `key`.\n     *\n     * `cleanup` is called either via explicit `unregister(key)` or via the\n     * FinalizationRegistry when `instance` is GC'd.\n     *\n     * Re-registering an existing key first unregisters the previous binding\n     * (its cleanup fires immediately), so callers don't have to worry about\n     * stale entries when a component re-mounts under the same key.\n     */\n    register(instance: object, key: string, cleanup: () => void): void {\n        if (this._entries.has(key)) this.unregister(key)\n        const token = {}\n        const entry: Entry = {\n            ref: new WeakRef(instance),\n            cleanup,\n            token,\n        }\n        this._entries.set(key, entry)\n        this._finalizer.register(instance, key, token)\n    }\n\n    /**\n     * Explicitly unregister and run cleanup synchronously.\n     * Idempotent — safe to call multiple times for the same key.\n     */\n    unregister(key: string): void {\n        const entry = this._entries.get(key)\n        if (!entry) return\n        this._finalizer.unregister(entry.token)\n        this._entries.delete(key)\n        entry.cleanup()\n    }\n\n    /**\n     * Return the WeakRef for `key`, or null if not registered.\n     * Callers should `.deref()` to obtain the instance (may be null if the\n     * underlying object was collected between `getRef` and `deref`).\n     */\n    getRef(key: string): WeakRef<object> | null {\n        return this._entries.get(key)?.ref ?? null\n    }\n\n    /** Reset all registrations without running cleanups — test helper only. */\n    __resetForTest(): void {\n        this._entries.clear()\n    }\n}\n\n/**\n * Module-level singleton. The renderer registers/unregisters component\n * instances against this. Tests can call `__resetForTest()` for determinism.\n */\nexport const componentRegistry = new ComponentWeakRegistry()\n"]}