{"version":3,"sources":["../../src/core/owned-signal.ts"],"names":["_ownedSignalIdCounter","_createOwnedSignal","initialValue","sc","createSignalV2","scheduleEffect","getter","setter","component","getComponent","key","componentRegistry","container","effs","i","eff","idx","__resetOwnedSignalCounterForTest"],"mappings":"kJA8CA,IAAIA,CAAAA,CAAwB,EASrB,SAASC,CAAAA,CACZC,EACoB,CACpB,IAAMC,EAAKC,CAAAA,CAAeF,CAAAA,CAAcG,CAAc,CAAA,CAChDC,CAAAA,CAASH,EAAG,OAAA,CAIZI,GAAAA,CAASJ,EAAG,OAAA,CAEZK,CAAAA,CAAYC,GAAa,CAC/B,GAAID,EAAW,CACX,IAAME,EAAM,CAAA,QAAA,EAAWV,CAAAA,EAAuB,GAC9CW,CAAAA,CAAkB,QAAA,CAASH,EAAWE,CAAAA,CAAK,IAAM,CAK7C,IAAME,CAAAA,CAAYT,EAAG,YAAA,CACfU,CAAAA,CAAOD,EAAU,OAAA,CACvB,IAAA,IAASE,EAAI,CAAA,CAAGA,CAAAA,CAAID,EAAK,MAAA,CAAQC,CAAAA,EAAAA,CAAK,CAClC,IAAMC,CAAAA,CAAMF,EAAKC,CAAC,CAAA,CAClB,GAAIC,CAAAA,EAAOA,CAAAA,CAAI,KAAM,CACjB,IAAMC,EAAMD,CAAAA,CAAI,IAAA,CAAK,QAAQH,CAAS,CAAA,CAClCI,IAAQ,EAAA,EAAID,CAAAA,CAAI,KAAK,MAAA,CAAOC,CAAAA,CAAK,CAAC,EAC1C,CACJ,CACAH,CAAAA,CAAK,MAAA,CAAS,EAClB,CAAC,EACL,CAEA,OAAO,CAACP,EAAQC,GAAM,CAC1B,CAGO,SAASU,CAAAA,EAAyC,CACrDjB,CAAAA,CAAwB,EAC5B","file":"owned-signal.mjs","sourcesContent":["/**\n * @internal\n *\n * `_createOwnedSignal` — compile-time escape hatch emitted by Phase 7 of\n * vite-plugin-flexium (`use-substitution` pass). Never call from handwritten\n * application code; use `use(initialValue)` instead. This module is exposed\n * at the package sub-path `flexium/core/owned-signal` so the compiler-emitted\n * import does not depend on `flexium`'s public surface.\n *\n * Semantics:\n *   1. Creates a V2 signal directly via `createSignalV2` — no hook bookkeeping.\n *   2. If invoked inside a component render, registers a cleanup hook with the\n *      Phase 4 componentRegistry so the signal's subscriber list is cleared on\n *      unmount (or via FinalizationRegistry GC as a backstop).\n *   3. Returns `[getter, setter]` — same tuple shape as `createSignal()`. The\n *      compiler emits an immediate `[0]()` call to recover the value form:\n *\n *        const __sig0 = _createOwnedSignal(0);\n *        const count   = __sig0[0]();\n *        const setCount = __sig0[1];\n *\n *   4. Outside a component context (top-level module, SSR top-level, raw\n *      tests) the signal is unowned — same fallback as `createSignal()`.\n *\n * V1/V2 compatibility:\n *   Always uses `createSignalV2` regardless of the `FLEXIUM_REACTIVITY_V2`\n *   flag. The compile-time substitution path is intentionally V2-only; when\n *   V1 mode is active, the vite-plugin-flexium optimizer is typically off\n *   and userland code stays on the runtime `use()` fallback path.\n */\n\nimport { createSignalV2 } from './reactive-v2'\nimport { scheduleEffect } from './scheduler'\nimport { getComponent } from './hook'\nimport { componentRegistry } from './component-registry'\nimport type { Setter } from './use'\n\n/**\n * Module-scope counter for unique per-signal registry keys.\n *\n * Each registered owned signal gets its own `__owned:N` key so multiple signals\n * within the same component each have independent cleanup entries. The counter\n * never resets across the lifetime of a dev server — that is correct (uniqueness\n * is monotonic) and the integer is bounded by JS number precision (2^53 entries\n * is unreachable in practice).\n */\nlet _ownedSignalIdCounter = 0\n\n/**\n * @internal — called only by vite-plugin-flexium Phase 7 compile output.\n *\n * Creates a component-owned signal whose effect subscribers are cleared when\n * the owning component unmounts. Outside a component, behaves like\n * `createSignal()`.\n */\nexport function _createOwnedSignal<T>(\n    initialValue: T\n): [() => T, Setter<T>] {\n    const sc = createSignalV2(initialValue, scheduleEffect)\n    const getter = sc._getter\n    // SignalContainerV2._setter already handles functional updates + Object.is\n    // dedup + trigger internally (Phase 5/6) — exposing it directly avoids a\n    // wrapping closure allocation per signal.\n    const setter = sc._setter as Setter<T>\n\n    const component = getComponent()\n    if (component) {\n        const key = `__owned:${_ownedSignalIdCounter++}`\n        componentRegistry.register(component, key, () => {\n            // Bidirectional unlink: walk each subscriber's `deps` array,\n            // remove this signal so the effect won't see a dangling pointer\n            // after the component is gone. Then clear the signal's subscriber\n            // list so any future triggerV2() finds an empty array.\n            const container = sc._v2Container\n            const effs = container.effects\n            for (let i = 0; i < effs.length; i++) {\n                const eff = effs[i]\n                if (eff && eff.deps) {\n                    const idx = eff.deps.indexOf(container)\n                    if (idx !== -1) eff.deps.splice(idx, 1)\n                }\n            }\n            effs.length = 0\n        })\n    }\n\n    return [getter, setter]\n}\n\n/** @internal — test helper to reset the per-signal counter between tests. */\nexport function __resetOwnedSignalCounterForTest(): void {\n    _ownedSignalIdCounter = 0\n}\n"]}