@raphy-js/raph 1.0.1-dev.20 → 1.0.1-dev.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/dist/{core-CyilRD7s.js → core-CClhT8WR.js} +389 -400
  2. package/dist/core-cPQWYD4K.cjs +1 -0
  3. package/dist/debug/api/Raph.cjs +1 -1
  4. package/dist/debug/api/Raph.js +46 -47
  5. package/dist/debug/api/RaphFacadeSession.cjs +1 -1
  6. package/dist/debug/api/RaphFacadeSession.js +85 -89
  7. package/dist/debug/api/RaphReactivityInstance.cjs +1 -1
  8. package/dist/debug/api/RaphReactivityInstance.js +27 -30
  9. package/dist/debug/path/DataPath.cjs +1 -1
  10. package/dist/debug/path/DataPath.js +198 -202
  11. package/dist/debug/path/PathRouter.cjs +1 -1
  12. package/dist/debug/path/PathRouter.js +44 -50
  13. package/dist/debug/path/RaphKernel.cjs +1 -1
  14. package/dist/debug/path/RaphKernel.js +257 -228
  15. package/dist/debug/path/data-path.utils.cjs +1 -1
  16. package/dist/debug/path/data-path.utils.js +41 -41
  17. package/dist/debug/path/derived/DerivedRegistry.cjs +1 -1
  18. package/dist/debug/path/derived/DerivedRegistry.js +52 -66
  19. package/dist/debug/path/derived/RaphDerivedHandle.cjs +1 -1
  20. package/dist/debug/path/derived/RaphDerivedHandle.js +19 -22
  21. package/dist/debug/path/derived/derived-collection.utils.cjs +1 -1
  22. package/dist/debug/path/derived/derived-collection.utils.js +70 -70
  23. package/dist/debug/path/meta/MetaController.cjs +1 -1
  24. package/dist/debug/path/meta/MetaController.js +57 -65
  25. package/dist/debug/path/meta/RaphMeta.cjs +1 -1
  26. package/dist/debug/path/meta/RaphMeta.js +26 -32
  27. package/dist/debug/path/meta/RaphMetaStore.cjs +1 -1
  28. package/dist/debug/path/meta/RaphMetaStore.js +24 -30
  29. package/dist/debug/path/storage/AdapterPathStore.cjs +1 -1
  30. package/dist/debug/path/storage/AdapterPathStore.js +49 -54
  31. package/dist/debug/path/storage/DefaultDataAdapter.cjs +1 -1
  32. package/dist/debug/path/storage/DefaultDataAdapter.js +46 -56
  33. package/dist/debug/path/storage/PathEventHub.cjs +1 -1
  34. package/dist/debug/path/storage/PathEventHub.js +74 -61
  35. package/dist/debug/path/storage/PathStore.cjs +1 -1
  36. package/dist/debug/path/storage/PathStore.js +89 -99
  37. package/dist/debug/path/storage/createDataDraft.cjs +1 -1
  38. package/dist/debug/path/storage/createDataDraft.js +24 -28
  39. package/dist/debug/reactivity/disposal.cjs +1 -0
  40. package/dist/debug/reactivity/disposal.js +32 -0
  41. package/dist/debug/reactivity/engine.cjs +1 -1
  42. package/dist/debug/reactivity/engine.js +787 -392
  43. package/dist/debug/reactivity/reactive-object.cjs +1 -1
  44. package/dist/debug/reactivity/reactive-object.js +81 -96
  45. package/dist/debug/reactivity/scope.cjs +1 -1
  46. package/dist/debug/reactivity/scope.js +221 -117
  47. package/dist/debug/reactivity/watch.cjs +1 -1
  48. package/dist/debug/reactivity/watch.js +1 -1
  49. package/dist/debug/runtime/RaphRuntime.cjs +1 -1
  50. package/dist/debug/runtime/RaphRuntime.js +764 -411
  51. package/dist/debug/runtime/diagnostics/RaphDebug.cjs +1 -1
  52. package/dist/debug/runtime/diagnostics/RaphDebug.js +90 -100
  53. package/dist/debug/runtime/ownership/RaphEffectScope.cjs +1 -1
  54. package/dist/debug/runtime/ownership/RaphEffectScope.js +215 -82
  55. package/dist/debug/runtime/ownership/RaphNode.cjs +1 -1
  56. package/dist/debug/runtime/ownership/RaphNode.js +595 -136
  57. package/dist/debug/runtime/ownership/RaphScope.cjs +1 -1
  58. package/dist/debug/runtime/ownership/RaphScope.js +203 -68
  59. package/dist/debug/runtime/phases/RaphFrameLoop.cjs +1 -1
  60. package/dist/debug/runtime/phases/RaphFrameLoop.js +13 -17
  61. package/dist/debug/runtime/phases/RaphPhasePipeline.cjs +1 -1
  62. package/dist/debug/runtime/phases/RaphPhasePipeline.js +618 -357
  63. package/dist/debug/runtime/phases/RaphPhaseTaskRecord.cjs +1 -0
  64. package/dist/debug/runtime/phases/RaphPhaseTaskRecord.js +176 -0
  65. package/dist/debug/runtime/phases/RaphPhaseWatch.cjs +1 -0
  66. package/dist/debug/runtime/phases/RaphPhaseWatch.js +20 -0
  67. package/dist/debug/runtime/reactivity/PathHandle.cjs +1 -1
  68. package/dist/debug/runtime/reactivity/PathHandle.js +50 -60
  69. package/dist/debug/runtime/reactivity/RaphMetaSource.cjs +1 -1
  70. package/dist/debug/runtime/reactivity/RaphMetaSource.js +28 -32
  71. package/dist/debug/runtime/reactivity/RaphSelector.cjs +1 -0
  72. package/dist/debug/runtime/reactivity/RaphSelector.js +503 -0
  73. package/dist/debug/runtime/reactivity/RaphWorkGraph.cjs +1 -1
  74. package/dist/debug/runtime/reactivity/RaphWorkGraph.js +541 -379
  75. package/dist/debug/runtime/reactivity/RuntimeReactivity.cjs +1 -1
  76. package/dist/debug/runtime/reactivity/RuntimeReactivity.js +484 -90
  77. package/dist/debug/runtime/reactivity/RuntimeWatch.cjs +1 -1
  78. package/dist/debug/runtime/reactivity/RuntimeWatch.js +33 -44
  79. package/dist/debug/runtime/reactivity/disposal-sweep.utils.cjs +1 -0
  80. package/dist/debug/runtime/reactivity/disposal-sweep.utils.js +42 -0
  81. package/dist/entrypoints/runtime.d.ts +2 -1
  82. package/dist/machine-testing.cjs +1 -1
  83. package/dist/machine-testing.js +1 -1
  84. package/dist/machine.cjs +1 -1
  85. package/dist/machine.js +216 -231
  86. package/dist/path/RaphKernel.d.ts +24 -0
  87. package/dist/path/storage/PathEventHub.d.ts +14 -0
  88. package/dist/path/storage/PathStore.d.ts +4 -4
  89. package/dist/reactivity/disposal.d.ts +14 -0
  90. package/dist/reactivity/engine.d.ts +177 -4
  91. package/dist/reactivity/scope.d.ts +108 -30
  92. package/dist/runtime/RaphRuntime.d.ts +236 -17
  93. package/dist/runtime/ownership/RaphEffectScope.d.ts +70 -1
  94. package/dist/runtime/ownership/RaphNode.d.ts +276 -6
  95. package/dist/runtime/ownership/RaphScope.d.ts +144 -14
  96. package/dist/runtime/phases/RaphPhasePipeline.d.ts +202 -28
  97. package/dist/runtime/phases/RaphPhaseTaskRecord.d.ts +195 -0
  98. package/dist/runtime/phases/RaphPhaseWatch.d.ts +22 -0
  99. package/dist/runtime/phases/phase.types.d.ts +83 -4
  100. package/dist/runtime/reactivity/RaphSelector.d.ts +338 -0
  101. package/dist/runtime/reactivity/RaphWorkGraph.d.ts +163 -25
  102. package/dist/runtime/reactivity/RuntimeReactivity.d.ts +258 -10
  103. package/dist/runtime/reactivity/RuntimeWatch.d.ts +8 -8
  104. package/dist/runtime/reactivity/disposal-sweep.utils.d.ts +19 -0
  105. package/dist/runtime/reactivity/runtime-reactivity.types.d.ts +48 -3
  106. package/package.json +10 -2
  107. package/dist/core-ugchZNqC.cjs +0 -1
@@ -1,47 +1,47 @@
1
1
  function k(x, S) {
2
- const m = /* @__PURE__ */ new WeakMap(), v = /* @__PURE__ */ new WeakMap(), w = (n, u, a = /* @__PURE__ */ new Set()) => {
2
+ const w = /* @__PURE__ */ new WeakMap(), m = /* @__PURE__ */ new WeakMap(), v = (n, u, a = /* @__PURE__ */ new Set()) => {
3
3
  if (!a.has(n)) {
4
4
  a.add(n), n.changed.add(u), Array.isArray(n.source) && S(n.proxy);
5
5
  for (const [c, l] of n.parents)
6
6
  for (const r of l)
7
- w(c, r, a);
7
+ v(c, r, a);
8
8
  }
9
9
  }, b = (n, u, a) => {
10
10
  if (!n || typeof n != "object")
11
11
  return n;
12
12
  const c = Object.getPrototypeOf(n);
13
- let l = v.get(n) ?? m.get(n);
13
+ let l = m.get(n) ?? w.get(n);
14
14
  if (!l) {
15
- const r = n, g = Array.isArray(r), i = g ? [] : Object.create(c);
16
- g && (i.length = r.length), l = { source: r, proxy: null, values: /* @__PURE__ */ new Map(), removed: /* @__PURE__ */ new Set(), changed: /* @__PURE__ */ new Set(), parents: /* @__PURE__ */ new Map() };
17
- const t = l, O = (s) => !t.removed.has(s) && (t.values.has(s) || Reflect.has(r, s)), d = (s) => t.removed.has(s) ? void 0 : t.values.has(s) ? t.values.get(s) : Reflect.get(r, s);
15
+ const r = n, f = Array.isArray(r), i = f ? [] : Object.create(c);
16
+ f && (i.length = r.length), l = { source: r, proxy: null, values: /* @__PURE__ */ new Map(), removed: /* @__PURE__ */ new Set(), changed: /* @__PURE__ */ new Set(), parents: /* @__PURE__ */ new Map() };
17
+ const t = l, O = (s) => !t.removed.has(s) && (t.values.has(s) || Reflect.has(r, s)), g = (s) => t.removed.has(s) ? void 0 : t.values.has(s) ? t.values.get(s) : Reflect.get(r, s);
18
18
  t.proxy = new Proxy(i, {
19
- get: (s, e) => b(d(e), t, e),
19
+ get: (s, e) => b(g(e), t, e),
20
20
  has: (s, e) => O(e),
21
- set: (s, e, f) => {
22
- if (g && e === "length") {
23
- const o = Number(f);
21
+ set: (s, e, d) => {
22
+ if (f && e === "length") {
23
+ const o = Number(d);
24
24
  if (!Number.isInteger(o) || o < 0 || o > 4294967295)
25
25
  throw new RangeError("Invalid array length");
26
- for (let h = o; h < Number(d("length")); h++)
26
+ for (let h = o; h < Number(g("length")); h++)
27
27
  t.removed.add(String(h)), t.values.delete(String(h));
28
28
  i.length = o;
29
- } else if (g && typeof e == "string" && /^(?:0|[1-9]\d*)$/.test(e)) {
29
+ } else if (f && typeof e == "string" && /^(?:0|[1-9]\d*)$/.test(e)) {
30
30
  const o = Number(e);
31
- o < 4294967295 && o >= Number(d("length")) && (t.values.set("length", o + 1), i.length = o + 1);
31
+ o < 4294967295 && o >= Number(g("length")) && (t.values.set("length", o + 1), i.length = o + 1);
32
32
  }
33
- return t.values.set(e, f), t.removed.delete(e), w(t, e), !0;
33
+ return t.values.set(e, d), t.removed.delete(e), v(t, e), !0;
34
34
  },
35
- deleteProperty: (s, e) => g && e === "length" ? !1 : (t.values.delete(e), t.removed.add(e), w(t, e), !0),
35
+ deleteProperty: (s, e) => f && e === "length" ? !1 : (t.values.delete(e), t.removed.add(e), v(t, e), !0),
36
36
  ownKeys: () => [.../* @__PURE__ */ new Set([...Reflect.ownKeys(r), ...t.values.keys()])].filter((s) => !t.removed.has(s)),
37
37
  getOwnPropertyDescriptor: (s, e) => {
38
- if (g && e === "length")
39
- return { value: d(e), writable: !0, enumerable: !1, configurable: !1 };
40
- const f = Object.getOwnPropertyDescriptor(r, e);
41
- if (!(t.removed.has(e) || !f && !t.values.has(e)))
42
- return { value: b(d(e), t, e), writable: !0, enumerable: (f == null ? void 0 : f.enumerable) ?? !0, configurable: !0 };
38
+ if (f && e === "length")
39
+ return { value: g(e), writable: !0, enumerable: !1, configurable: !1 };
40
+ const d = Object.getOwnPropertyDescriptor(r, e);
41
+ if (!(t.removed.has(e) || !d && !t.values.has(e)))
42
+ return { value: b(g(e), t, e), writable: !0, enumerable: d?.enumerable ?? !0, configurable: !0 };
43
43
  }
44
- }), m.set(r, t), v.set(t.proxy, t);
44
+ }), w.set(r, t), m.set(t.proxy, t);
45
45
  }
46
46
  if (u && a !== void 0) {
47
47
  let r = l.parents.get(u);
@@ -52,16 +52,12 @@ function k(x, S) {
52
52
  return {
53
53
  root: b(x),
54
54
  unchangedArray(n, u) {
55
- const a = v.get(n);
55
+ const a = m.get(n);
56
56
  if (!a)
57
57
  return null;
58
- for (const c of a.changed) {
59
- if (a.values.has(c) || a.removed.has(c))
58
+ for (const c of a.changed)
59
+ if (a.values.has(c) || a.removed.has(c) || w.get(a.source[c])?.changed.has(u))
60
60
  return null;
61
- const l = m.get(a.source[c]);
62
- if (l != null && l.changed.has(u))
63
- return null;
64
- }
65
61
  return a.source;
66
62
  }
67
63
  };
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function l(r){return{error:r,hasUnrouted:!0}}function s(r,n){if(r===void 0||r.length===0)return;if(r.length===1)return r[0];const i=[];let e,t=!1;for(const o of r)i.push(o.error),t||=o.hasUnrouted,o.completed!==void 0&&(e??=[]).push(o.completed.error);const u=new AggregateError(i,n);return{error:u,hasUnrouted:t,...e===void 0?{}:{completed:{error:t?e.length===1?e[0]:new AggregateError(e,n):u}}}}function d(r){if(r!==void 0)throw r.error}exports.combineDisposalFailures=s;exports.throwDisposalFailure=d;exports.unroutedDisposalFailure=l;
@@ -0,0 +1,32 @@
1
+ function d(r) {
2
+ return { error: r, hasUnrouted: !0 };
3
+ }
4
+ function l(r, n) {
5
+ if (r === void 0 || r.length === 0)
6
+ return;
7
+ if (r.length === 1)
8
+ return r[0];
9
+ const i = [];
10
+ let o, t = !1;
11
+ for (const e of r)
12
+ i.push(e.error), t ||= e.hasUnrouted, e.completed !== void 0 && (o ??= []).push(e.completed.error);
13
+ const u = new AggregateError(i, n);
14
+ return {
15
+ error: u,
16
+ hasUnrouted: t,
17
+ ...o === void 0 ? {} : {
18
+ completed: {
19
+ error: t ? o.length === 1 ? o[0] : new AggregateError(o, n) : u
20
+ }
21
+ }
22
+ };
23
+ }
24
+ function c(r) {
25
+ if (r !== void 0)
26
+ throw r.error;
27
+ }
28
+ export {
29
+ l as combineDisposalFailures,
30
+ c as throwDisposalFailure,
31
+ d as unroutedDisposalFailure
32
+ };
@@ -1 +1 @@
1
- "use strict";var z=Object.defineProperty;var P=(c,e,t)=>e in c?z(c,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):c[e]=t;var g=(c,e,t)=>P(c,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const B=require("./reactive-object.cjs"),R=require("./scope.cjs"),G=require("./watch.cjs"),w=1,d=2,C=4,b=8,W=16,y=32,x=64,O=128,m=256,v=512,E=w|d,_=16,M=2;function U(c){c.dependencyLookupDebt===void 0&&c.dependencyIndex===void 0||(c.dependencyIndex=void 0,c.dependencyLookupDebt=void 0,c.dependencyLookupSearches=void 0)}function Q(c,e){if(e<=_||c.flags&O||c.dependencyIndex!==void 0)return;const t=(c.dependencyLookupSearches??0)+1,i=(c.dependencyLookupDebt??0)+e;if(c.dependencyLookupSearches=t,c.dependencyLookupDebt=i,t<M||i<c.depCount)return;const s=new Map;for(let n=c.firstDep;n!==null;n=n.nextDep)s.set(n.dep,n);c.dependencyIndex=s,c.dependencyLookupDebt=void 0,c.dependencyLookupSearches=void 0}function j(c,e,t){var r;const i=c.firstSub;if(i===null)return;if(i===c.lastSub)return i.sub===e?i:void 0;const s=c.subSearchCursor??i;let n=1;if(s.sub===e)return c.subSearchCursor=s.nextSub??i,s;if(t){const l=(r=e.dependencyIndex)==null?void 0:r.get(c);if(l!==void 0)return c.subSearchCursor=l.nextSub??i,l;if(e.dependencyIndex!==void 0)return}let a=s.nextSub??i;for(;a!==s;){if(++n,a.sub===e)return c.subSearchCursor=a.nextSub??i,t&&Q(e,n),a;a=a.nextSub??i}t&&Q(e,n)}class N{constructor(e={}){g(this,"adaptiveDependencyIndex");g(this,"activeSub",null);g(this,"batchDepth",0);g(this,"effectReadIndex",0);g(this,"effectWriteIndex",0);g(this,"invalidationDepth",0);g(this,"dependencyDepth",0);g(this,"flushingEffects",!1);g(this,"effectQueue",new Array(16).fill(void 0));g(this,"invalidationStack",new Array(512).fill(null));g(this,"dependencyStack",[]);g(this,"handleReaders",this.createHandleReaders());g(this,"effectScheduler");g(this,"flushEffects");g(this,"_effectImmediate");this.adaptiveDependencyIndex=e.adaptiveDependencyIndex??!1,this.effectScheduler=e.effectScheduler,this._effectImmediate=this.effect.bind(this),this.signal=this.signal.bind(this),this.computed=this.computed.bind(this),this.reactive=this.reactive.bind(this),this.watch=this.watch.bind(this),this.batch=this.batch.bind(this),this.untracked=this.untracked.bind(this),this.scope=this.scope.bind(this),this._signalAccess=this._signalAccess.bind(this),this._computedAccess=this._computedAccess.bind(this),this._ownedSignal=this._ownedSignal.bind(this),this._ownedComputed=this._ownedComputed.bind(this),this._isTracking=this._isTracking.bind(this),this._isFlushingEffects=this._isFlushingEffects.bind(this),this._startBatch=this._startBatch.bind(this),this._endBatch=this._endBatch.bind(this),this.effect=this.effectScheduler===void 0?this._effectImmediate:this.createScheduledEffect.bind(this),this.flushEffects=this.effectScheduler===void 0?this.flushSyncEffects:this.flushExternalEffects}signal(e){let t=e,i;const s=()=>{const a=i;if(a!==void 0)return this.readSignalValue(a);const r=this.activeSub;return r===null||(i=this.materializeSignal(t,r)),t},n=a=>{const r=i;if(r!==void 0){this.writeSignalValue(r,a);return}t!==a&&(t=a)};return{get:s,peek:this.handleReaders.peek,set:n}}reactive(e){return B.reactiveObject(this,e,()=>this.createReactivePropertySourceBridge())}computed(e){if(typeof e=="function")return this.createComputedHandle(e);const t=this.createComputedHandle(e.get);return t.set=e.set,t}effect(e,t){const i=this.allocateGraphRecord(void 0,e,d|w|y);t!==void 0&&(i.onStop=t);const s=this.activeSub;s!==null&&s.flags&y&&R.appendScopeChild(s,i);try{this.executeEffect(i)}catch(n){try{this.disposeEffect(i)}catch(a){throw new AggregateError([n,a],"Effect registration and cleanup failed.")}throw n}return this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects(),()=>this.disposeEffect(i)}watch(e,t,i){return G.watchWithReactivity(this.effect,this.untracked,e,t,i)}batch(e){++this.batchDepth;let t;try{t=e()}catch(i){if(!--this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex)try{this.flushEffects()}catch(s){throw new AggregateError([i,s],"Batch operation and effect queue processing failed.")}throw i}return!--this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects(),t}untracked(e){if(this.activeSub===null)return e();const t=this.activeSub;this.activeSub=null;try{return e()}finally{this.activeSub=t}}scope(){return new R.ScopeNode(this)}allocateGraphRecord(e,t,i){return{flags:i,firstSub:null,lastSub:null,firstDep:null,tailDep:null,depCount:0,evaluate:t,cachedValue:e}}createReactivePropertySourceBridge(){return{batch:this.batch,create:()=>{if(this.activeSub===null)throw new Error("[Raph] Reactive property source requires a tracked read.");return{firstSub:null,lastSub:null}},isTracking:this._isTracking,track:e=>{const t=this.activeSub;t!==null&&this.trackDependency(e,t)},trigger:e=>{const t=e.firstSub;t!==null&&(this.propagateInvalidation(t),!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects())}}}materializeSignal(e,t,i){const s=this.allocateGraphRecord(e,void 0,0);return i!==void 0&&(s.sourceHooks=i,s.flags|=v),t.flags&O?this.appendInitialDependency(s,t):this.trackDependency(s,t),s}createComputedHandle(e,t){const i={evaluate:e,get:this.handleReaders.initializeComputed,peek:this.handleReaders.peek};return t!==void 0&&(i.sourceHooks=t),i}createScheduledEffect(e,t){const i=this.allocateGraphRecord(void 0,e,d|w|y);let s=!1;t!==void 0&&(i.onStop=t);const n=this.activeSub;n!==null&&n.flags&y&&R.appendScopeChild(n,i);const a=()=>{if(!(!(i.flags&x)||i.evaluate===void 0)){i.flags&=~x,s=!0;try{this.executeEffect(i)}catch(r){if(!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex)try{this.flushEffects()}catch(l){throw new AggregateError([r,l],"Effect execution and queue processing failed.")}throw r}!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects()}};try{const r=this.effectScheduler.bind(a);r.cancelPending=()=>i.evaluate===void 0||!s?!1:(this.settleCancelledDependencies(i),i.flags&=~(x|E|C),!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects(),!0),r.rearmPending=()=>{i.evaluate!==void 0&&(i.flags|=d|x,r.schedule())},i.effectTask=r,this.requestEffectTask(i)}catch(r){try{this.disposeEffect(i)}catch(l){throw new AggregateError([r,l],"Effect registration and cleanup failed.")}throw r}return!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects(),()=>this.disposeEffect(i)}readSignalValue(e){const t=this.activeSub;let i=null;return t!==null&&(i=this.trackDependency(e,t)),e.flags&d&&this.commitSignalValue(e,i),e.cachedValue}writeSignalValue(e,t){if(e.flags&d||this.batchDepth){const s=!!(e.flags&d);if((e.flags&d?e.pendingValue:e.cachedValue)===t)return;if(e.pendingValue=t,e.cachedValue===t)e.flags&=~d,e.pendingAuthor=void 0;else{e.flags|=d;const r=this.activeSub!==null&&this.activeSub.flags&b?this.activeSub:null;s?e.pendingAuthor!==r&&(e.pendingAuthor=null):e.pendingAuthor=r}const a=e.firstSub;a!=null&&(!s||this.activeSub!==null&&this.activeSub.flags&b||e.flags&v)&&this.propagateInvalidation(a,!0);return}if(e.cachedValue===t)return;e.cachedValue=t;const i=e.firstSub;i!=null&&(this.propagateInvalidation(i),!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects())}readComputedValue(e){const t=e.flags,i=t&(W|E|m),s=i===0?e.cachedValue:i&W?this.evaluateFirstValue(e):this.refreshComputedValue(e,t);if(e.evaluate===void 0)return s;const n=this.activeSub;if(n!==null){const a=n.tailDep;n.flags&O?(a===null||a.dep!==e)&&this.appendInitialDependency(e,n):this.trackDependency(e,n)}return s}evaluateFirstValue(e){const t=e.flags&v,i=e.evaluate;e.flags=t|b|O;const s=this.activeSub;this.activeSub=e;try{const n=i();return e.evaluate!==i?(e.cachedValue=void 0,e.flags=0,n):(e.cachedValue=n,e.flags=t|(e.flags&C?d:0),n)}catch(n){throw e.evaluate!==i?(e.cachedValue=void 0,e.flags=0,n):(e.flags=t|m,s!==null&&this.trackDependency(e,s),n)}finally{this.activeSub=s}}refreshComputedValue(e,t,i=null){try{if(t&(d|C|m))return this.recomputeValue(e,i)||(e.flags&=~E),e.cachedValue;const s=e.firstDep;return(s===null||!this.refreshDependencies(e,s)||!this.recomputeValue(e,i))&&(e.flags&=~E),e.cachedValue}catch(s){throw this.activeSub!==null&&this.trackDependency(e,this.activeSub),s}}commitSignalValue(e,t){const i=e.cachedValue;e.cachedValue=e.pendingValue,e.flags&=~d;const s=e.pendingAuthor;return e.pendingAuthor=void 0,i===e.cachedValue?!1:(e.firstSub!==null&&this.confirmSubUpdates(e.firstSub,t,s??null),!0)}trackDependency(e,t){const i=t.tailDep;if(i!==null){const n=i.nextDep;return n!==null&&n.dep===e?(t.tailDep=n,n):i.dep===e?i:this.reconcileDependency(e,t,i,n)}const s=t.firstDep;return s===null?t.tailDep=this.insertDependency(e,t,null):s.dep===e?(t.tailDep=s,s):this.reconcileDependency(e,t,null,s)}reconcileDependency(e,t,i,s){if(t.depCount<=_){for(let l=s;l!==null;l=l.nextDep)if(l.dep===e)return this.reorderDependency(t,l,i),t.tailDep=l,l;for(let l=i;l!==null;l=l.prevDep)if(l.dep===e)return l;return s!==null?(t.tailDep=this.reuseDepLink(s,e),s):t.tailDep=this.insertDependency(e,t,i)}let n=0,a=s;for(;a!==null&&n<_;){const l=a;if(l.dep===e)return this.reorderDependency(t,l,i),t.tailDep=l,l;a=l.nextDep,++n}let r=i;for(;r!==null&&n<_;){const l=r;if(l.dep===e)return l;r=l.prevDep,++n}if(a!==null||r!==null){const l=j(e,t,this.adaptiveDependencyIndex);if(l!==void 0)return this.reorderDependency(t,l,i),t.tailDep=l,l}return s!==null?(t.tailDep=this.reuseDepLink(s,e),s):t.tailDep=this.insertDependency(e,t,i)}appendInitialDependency(e,t){var a;const i=t.tailDep,s=e.lastSub,n={dep:e,sub:t,prevSub:s,nextSub:null,prevDep:i,nextDep:null};return s===null?e.firstSub=n:s.nextSub=n,e.lastSub=n,i===null?t.firstDep=n:i.nextDep=n,t.tailDep=n,++t.depCount,(a=t.dependencyIndex)==null||a.set(e,n),e.flags&v&&this.attachDependencyCleanup(n),n}insertDependency(e,t,i){var r;const s=e.lastSub,n={dep:e,sub:t,prevSub:s,nextSub:null,prevDep:i,nextDep:null};s===null?e.firstSub=n:s.nextSub=n,e.lastSub=n;const a=i===null?t.firstDep:i.nextDep;return n.nextDep=a,a!==null&&(a.prevDep=n),i===null?t.firstDep=n:i.nextDep=n,++t.depCount,(r=t.dependencyIndex)==null||r.set(e,n),e.flags&v&&this.attachDependencyCleanup(n),n}reuseDepLink(e,t){const i=e.sub.dependencyIndex;this.detachSubLink(e);const s=t.lastSub;return e.dep=t,e.prevSub=s,e.nextSub=null,s===null?t.firstSub=e:s.nextSub=e,t.lastSub=e,i==null||i.set(t,e),t.flags&v&&this.attachDependencyCleanup(e),e}reorderDependency(e,t,i){if(t.prevDep===i)return;const s=t.prevDep,n=t.nextDep;s===null?e.firstDep=n:s.nextDep=n,n!==null&&(n.prevDep=s);const a=i===null?e.firstDep:i.nextDep;t.prevDep=i,t.nextDep=a,a!==null&&(a.prevDep=t),i===null?e.firstDep=t:i.nextDep=t}pruneUnreadDependencies(e){const t=e.tailDep;let i=t===null?e.firstDep:t.nextDep;if(i!==null){for(t===null?e.firstDep=null:t.nextDep=null;i!==null;){const s=i.nextDep;this.detachSubLink(i),--e.depCount,i=s}e.depCount<=_&&U(e)}}detachSubLink(e){var t;if((t=e.sub.dependencyIndex)==null||t.delete(e.dep),e.dep.flags&v){const i=e.dependencyCleanup;e.dependencyCleanup=void 0,i==null||i()}e.dep.subSearchCursor===e&&(e.dep.subSearchCursor=e.nextSub??(e.dep.firstSub===e?void 0:e.dep.firstSub??void 0)),e.prevSub===null?e.dep.firstSub=e.nextSub:e.prevSub.nextSub=e.nextSub,e.nextSub===null?e.dep.lastSub=e.prevSub:e.nextSub.prevSub=e.prevSub}attachDependencyCleanup(e){var n,a;const t=e.dep.sourceHooks,i=t==null?void 0:t.onDependency;if(i===void 0)return;const s=((n=e.sub.sourceHooks)==null?void 0:n.dependencyOwner)??((a=e.sub.effectTask)==null?void 0:a.dependencyOwner);s!==void 0&&(e.dependencyCleanup=i(s))}releaseSourceSubs(e){if(!(e.flags&v)){let i=e.firstSub;for(;i!=null;){const s=i.nextSub,n=i.sub;this.detachDependencyLink(i),this.detachSubLink(i);const a=n.flags;(a&(E|b))===w&&(n.flags=a&~w|d,a&y&&this.enqueueEffect(n,e)),i=s}e.firstSub=e.lastSub=null,e.subSearchCursor=void 0;return}let t=e.firstSub;for(;t!=null;){const i=t.nextSub;this.detachDependencyLink(t),this.detachSubLink(t),t=i}e.firstSub=e.lastSub=null,e.subSearchCursor=void 0}detachDependencyLink(e){const t=e.sub,i=e.prevDep,s=e.nextDep;i===null?t.firstDep=s:i.nextDep=s,s!==null&&(s.prevDep=i),t.tailDep===e&&(t.tailDep=i),--t.depCount,t.depCount<=_&&U(t)}enqueueEffect(e,t){var s,n;const i=t!==void 0&&t.flags&v?t.sourceHooks.token:void 0;i!==void 0&&((n=(s=e.effectTask)==null?void 0:s.notify)==null||n.call(s,i)),!(e.flags&x)&&(e.flags|=x,this.effectQueue[this.effectWriteIndex]=e,++this.effectWriteIndex)}propagateInvalidation(e,t=!1){var n,a,r,l,h,D,u,o,I,A,L,q,F;const i=this.invalidationDepth;let s=i;for(let p=e;p!==null;p=p.nextSub){const f=p.sub,S=f.flags;let k=0;if(S&(E|b)?S&b?k=this.invalidateReadDependency(p,f,S):!t&&!(S&d)&&(f.flags=S&~w|d):(k=S&~C|(t?w:d),f.flags=k),k===0){if(S&y&&S&x){const T=p.dep.flags&v?p.dep.sourceHooks.token:void 0;T!==void 0&&((a=(n=f.effectTask)==null?void 0:n.notify)==null||a.call(n,T))}else f.flags&v&&((l=(r=f.sourceHooks).onInvalidate)==null||l.call(r,f.firstSub!==null),f.firstSub!==null&&(this.invalidationStack[s++]=f.firstSub));continue}k&y?(!(k&b)||((h=f.effectTask)==null?void 0:h.allowScheduleDuringRun)===!0)&&this.enqueueEffect(f,p.dep):(!(S&E)&&f.flags&v&&((u=(D=f.sourceHooks).onInvalidate)==null||u.call(D,f.firstSub!==null)),f.firstSub!==null&&(this.invalidationStack[s++]=f.firstSub))}for(;s!==i;){let p=this.invalidationStack[--s];do{const f=p.sub,S=f.flags;let k=0;if(S&(E|b)?S&b&&(k=this.invalidateReadDependency(p,f,S)):(k=S&~C|w,f.flags=k),k===0){if(S&y&&S&x){const T=p.dep.flags&v?p.dep.sourceHooks.token:void 0;T!==void 0&&((I=(o=f.effectTask)==null?void 0:o.notify)==null||I.call(o,T))}else if(f.flags&v&&((L=(A=f.sourceHooks).onInvalidate)==null||L.call(A,f.firstSub!==null),f.firstSub!==null)){p.nextSub!==null&&(this.invalidationStack[s++]=p.nextSub),p=f.firstSub;continue}}else if(k&y)this.enqueueEffect(f,p.dep);else if(!(S&E)&&f.flags&v&&((F=(q=f.sourceHooks).onInvalidate)==null||F.call(q,f.firstSub!==null)),f.firstSub!==null){p.nextSub!==null&&(this.invalidationStack[s++]=p.nextSub),p=f.firstSub;continue}p=p.nextSub}while(p!==null)}this.invalidationDepth=i}invalidateReadDependency(e,t,i){const s=t.tailDep;if(s===null)return 0;if(e!==s){for(let a=e.prevDep;a!==null;a=a.prevDep)if(a===s)return 0}const n=i|C|w;return t.flags=n,n}confirmSubUpdates(e,t,i=null){var s,n;for(let a=e;a!==null;a=a.nextSub){if(a===t||a.sub===i)continue;const r=a.sub,l=r.flags;l&d||(r.flags=l&~w|d,l&y?this.enqueueEffect(r,a.dep):l&E||r.flags&v&&((n=(s=r.sourceHooks).onInvalidate)==null||n.call(s,r.firstSub!==null)))}}recomputeValue(e,t=null){const i=e.evaluate;e.tailDep=null;const s=e.flags&~C|b;e.flags=s;const n=this.activeSub;this.activeSub=e;let a;try{a=i()}catch(u){throw e.evaluate!==i?(e.cachedValue=void 0,e.flags=0,u):(this.restoreComputedFailure(e,t),u)}finally{this.activeSub=n}if(e.evaluate!==i)return e.cachedValue=void 0,e.flags=0,!1;const r=e.flags&C,l=s&-268,h=e.tailDep;(h===null?e.firstDep:h.nextDep)!==null&&this.pruneUnreadDependencies(e);const D=e.cachedValue;return e.cachedValue=a,e.flags=s&v|(r?d:l),D===a?!1:(e.firstSub!==null&&this.confirmSubUpdates(e.firstSub,t),!0)}restoreComputedFailure(e,t){e.flags=e.flags&-144|m,this.pruneUnreadDependencies(e),t!==null&&this.restoreFailedDependency(t.sub)}refreshDependencies(e,t){const i=this.dependencyDepth;try{return this.walkDependencyChain(e,t)}catch(s){this.restoreFailedDependency(e);for(let n=i;n<this.dependencyDepth;n++)this.restoreFailedDependency(this.dependencyStack[n].sub);throw this.dependencyDepth=i,this.dependencyStack.length=i,s}}settleCancelledDependencies(e){for(let t=e.firstDep;t!==null;t=t.nextDep){const i=t.dep,s=i.flags;s&(W|E|m)&&(i.evaluate===void 0?s&d&&this.commitSignalValue(i,t):this.refreshComputedValue(i,s,t))}}walkDependencyChain(e,t){const i=this.dependencyDepth;let s=i,n=!1;e:for(;;){if(e.flags&d)n=!0;else{const a=t.dep,r=a.flags;if(r&(d|m))this.dependencyDepth=s,n=a.evaluate===void 0?this.commitSignalValue(a,t):this.recomputeValue(a,t);else if(r&w){if(a.firstDep!==null){this.dependencyStack[s++]=t,e=a,t=a.firstDep;continue e}this.dependencyDepth=s,n=this.recomputeValue(a,t)}else n=!1}if(!n&&t.nextDep!==null){t=t.nextDep;continue}if(n){for(;s!==i;){this.dependencyDepth=--s;const a=this.dependencyStack[s];if(n=this.recomputeValue(e,a),e=a.sub,!n){if(a.nextDep!==null){t=a.nextDep;continue e}break}}if(n)return this.dependencyDepth=i,!0}for(;s!==i;){this.dependencyDepth=--s;const a=this.dependencyStack[s];if(e.flags&=~w,e=a.sub,a.nextDep!==null){t=a.nextDep;continue e}}return e.flags&=~w,this.dependencyDepth=i,!1}}restoreFailedDependency(e){e.flags&=-16,e.flags&y||(e.flags|=m)}executeEffect(e){const t=e.flags;if(!(t&E))return;if(!(t&(d|C))){const h=e.firstDep;try{if(h===null||!this.refreshDependencies(e,h)){e.flags&=~E;return}}catch(D){throw e.flags&=-80,D}}const i=e.evaluate;if(i===void 0){e.flags&=~E;return}let s;if(e.scopeLastChild!==void 0)try{this.disposeNestedEffects(e)}catch(h){s=[h]}const n=typeof e.cachedValue=="function"?e.cachedValue:void 0;if(e.cachedValue=void 0,e.flags&=~C,n!==void 0)try{this.untracked(n)}catch(h){s??(s=[]),s.push(h)}if(s!==void 0){try{this.disposeEffect(e)}catch(h){s.push(h)}R.throwScopeErrors(s,"Effect cleanup failed.")}if(e.evaluate===void 0)return;e.tailDep=null,e.flags=e.flags&~C|b;const a=this.activeSub;this.activeSub=e;let r;try{r=i()}catch(h){this.activeSub=a,e.flags&=~b,this.failEffectRun(e,h,s)}finally{this.activeSub=a,e.flags&=~b}const l=e.tailDep;if((l===null?e.firstDep:l.nextDep)!==null&&this.pruneUnreadDependencies(e),typeof r=="function")if(e.evaluate===void 0)try{this.untracked(r)}catch(h){s??(s=[]),s.push(h)}else e.cachedValue=r;e.flags&x?e.flags=e.flags&-7|w:e.flags&=-8}failEffectRun(e,t,i){const s=i??[];s.push(t),this.pruneUnreadDependencies(e);try{this.disposeNestedEffects(e)}catch(n){s.push(n)}throw e.flags&=-72,s.length===1?s[0]:new AggregateError(s,"Effect execution failed.")}flushSyncEffects(){if(!(this.flushingEffects||this.activeSub!==null&&(this.activeSub.flags&(y|b))===(y|b))){this.flushingEffects=!0;try{for(;this.effectReadIndex!==this.effectWriteIndex;){const e=this.effectReadIndex,t=this.effectQueue[e];this.effectQueue[e]=void 0,++this.effectReadIndex,t.flags&=~x,this.executeEffect(t)}}catch(e){this.drainQueueAfterError(e,!1)}finally{this.effectReadIndex=0,this.effectWriteIndex=0,this.flushingEffects=!1}}}flushExternalEffects(){if(!(this.flushingEffects||this.activeSub!==null&&(this.activeSub.flags&(y|b))===(y|b))){this.flushingEffects=!0;try{for(;this.effectReadIndex!==this.effectWriteIndex;){const e=this.effectReadIndex,t=this.effectQueue[e];this.effectQueue[e]=void 0,++this.effectReadIndex;const i=t.effectTask;i===void 0?(t.flags&=~x,this.executeEffect(t)):t.evaluate!==void 0?this.scheduleEffectTask(t,i):t.flags&=~x}}catch(e){this.drainQueueAfterError(e,!0)}finally{this.effectReadIndex=0,this.effectWriteIndex=0,this.flushingEffects=!1}}}drainQueueAfterError(e,t){const i=[e];for(;this.effectReadIndex!==this.effectWriteIndex;){const s=this.effectReadIndex++,n=this.effectQueue[s];this.effectQueue[s]=void 0;try{const a=t?n.effectTask:void 0;a!==void 0&&n.evaluate!==void 0?this.scheduleEffectTask(n,a):(n.flags&=~x,this.executeEffect(n))}catch(a){i.push(a)}}throw i.length===1?e:new AggregateError(i,"Effect queue processing failed.")}requestEffectTask(e){const t=e.effectTask;t===void 0||e.evaluate===void 0||(e.flags|=x,this.scheduleEffectTask(e,t))}scheduleEffectTask(e,t){try{t.schedule()}catch(i){throw e.flags&=-72,i}}disposeEffect(e){if(e.evaluate===void 0)return;this.activeSub===e&&(this.activeSub=null);const t=e.effectTask,i=e.onStop,s=typeof e.cachedValue=="function"?e.cachedValue:void 0;e.evaluate=void 0,e.flags=0,e.cachedValue=void 0,e.effectTask=void 0,i!==void 0&&(e.onStop=void 0),R.detachScopeChild(e);let n=e.firstDep;for(;n!==null;){const r=n.nextDep;this.detachSubLink(n),n=r}e.firstDep=e.tailDep=null,e.depCount=0,U(e);let a;if(t!==void 0)try{t.dispose()}catch(r){a=[r]}try{this.disposeNestedEffects(e)}catch(r){a??(a=[]),a.push(r)}if(s!==void 0)try{this.untracked(s)}catch(r){a??(a=[]),a.push(r)}if(i!==void 0)try{this.untracked(i)}catch(r){a??(a=[]),a.push(r)}this.invalidationStack.length=this.invalidationDepth,this.dependencyStack.length=this.dependencyDepth,a!==void 0&&R.throwScopeErrors(a,"Effect disposal failed.")}disposeNestedEffects(e){let t;for(;e.scopeLastChild!==void 0;)try{this.disposeEffect(e.scopeLastChild)}catch(i){t??(t=[]),t.push(i)}t!==void 0&&R.throwScopeErrors(t,"Nested effect disposal failed.")}static materializeComputed(e){e.flags=W|(e.sourceHooks===void 0?0:v),e.firstSub=null,e.lastSub=null,e.firstDep=null,e.tailDep=null,e.depCount=0,e.cachedValue=void 0}createHandleReaders(){const e=this;function t(){return e.readComputedValue(this)}return{initializeComputed(){const i=this;return N.materializeComputed(i),this.get=t,e.readComputedValue(i)},initializeRaw(){const i=this;N.materializeComputed(i);const s=()=>e.readComputedValue(i);return this.read=s,s()},peek(){return e.peekHandle(this)}}}peekHandle(e){const t=this.activeSub;if(t===null)return e.get();this.activeSub=null;try{return e.get()}finally{this.activeSub=t}}_signalAccess(e){const t=this;let i=e,s;function n(a){if(arguments.length!==0){const h=s;h!==void 0?t.writeSignalValue(h,a):i!==a&&(i=a);return}const r=s;if(r!==void 0)return t.readSignalValue(r);const l=t.activeSub;return l===null||(s=t.materializeSignal(i,l)),i}return{read:n,write:n}}_computedAccess(e){return{evaluate:e,read:this.handleReaders.initializeRaw}}_ownedSignal(e,t){const i=this;let s=e,n,a=!1;const r=()=>{const u=n;if(u!==void 0)return this.readSignalValue(u);const o=this.activeSub;if(o===null)return s;const I=s;return n=this.materializeSignal(I,o,t),s=void 0,I},l=t===void 0?u=>{const o=n;o!==void 0?this.writeSignalValue(o,u):s!==u&&(s=u)}:u=>{var I,A;const o=n;if(o!==void 0){const L=o.flags&d?o.pendingValue:o.cachedValue;L!==u&&((I=t.onChange)==null||I.call(t,L,u,o.firstSub!==null)),this.writeSignalValue(o,u);return}s!==u&&((A=t.onChange)==null||A.call(t,s,u,!1),s=u)},D={source:{get:r,peek:this.handleReaders.peek,set:l},dispose(){var o;if(a)return!0;a=!0,s=void 0;let u;try{(o=t==null?void 0:t.onDispose)==null||o.call(t,(n==null?void 0:n.firstSub)!=null)}catch(I){(u??(u=[])).push(I)}if(n!==void 0){try{i.releaseSourceSubs(n)}catch(I){(u??(u=[])).push(I)}n.cachedValue=void 0,n.pendingValue=void 0,n.pendingAuthor=void 0,n.flags=0}if(n=void 0,(u==null?void 0:u.length)===1)throw u[0];if(u!==void 0&&u.length>1)throw new AggregateError(u,"Owned signal disposal failed.");return!0}};return t!==void 0&&(D.hasSubscribers=()=>(n==null?void 0:n.firstSub)!=null),D}_ownedComputed(e,t){const i=this,s=this.createComputedHandle(e,t);let n=!1;const a={source:s,dispose(){var h;if(n)return!0;n=!0,i.activeSub===s&&(i.activeSub=null);const r=[];try{(h=t==null?void 0:t.onDispose)==null||h.call(t,s.firstSub!=null)}catch(D){r.push(D)}if(s.flags!==void 0)try{i.releaseSourceSubs(s)}catch(D){r.push(D)}let l=s.firstDep;for(;l!=null;){const D=l.nextDep;i.detachSubLink(l),l=D}if(s.firstDep=s.tailDep=null,s.depCount=0,U(s),s.evaluate=void 0,s.cachedValue=void 0,s.flags=0,r.length===1)throw r[0];if(r.length>1)throw new AggregateError(r,"Owned computed disposal failed.");return!0}};return t!==void 0&&(a.hasSubscribers=()=>s.firstSub!=null),a}_startBatch(){++this.batchDepth}_endBatch(){!--this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects()}_isTracking(){return this.activeSub!==null}_isFlushingEffects(){return this.flushingEffects}}let K;function V(){return K??(K=new N)}const Y=c=>V().signal(c),X=c=>V().reactive(c);function Z(c){return V().computed(c)}const J=(c,e)=>V().effect(c,e),$=(c,e,t)=>V().watch(c,e,t),H=c=>V().batch(c),ee=c=>V().untracked(c),te=()=>V().scope();exports.ScopeNode=R.ScopeNode;exports.RaphReactivity=N;exports.batch=H;exports.computed=Z;exports.effect=J;exports.reactive=X;exports.scope=te;exports.signal=Y;exports.untracked=ee;exports.watch=$;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const H=require("./reactive-object.cjs"),D=require("./scope.cjs"),O=require("./watch.cjs"),g=1,f=2,y=4,p=8,T=16,d=32,v=64,w=128,x=256,o=512,N=2048,k=4096,R=8192,m=N|k,A=1e4,b=g|f,C=16,U=2;function L(c){c.dependencyLookupDebt===void 0&&c.dependencyIndex===void 0||(c.dependencyIndex=void 0,c.dependencyLookupDebt=void 0,c.dependencyLookupSearches=void 0)}function W(c,e){if(e<=C||c.flags&w||c.dependencyIndex!==void 0)return;const t=(c.dependencyLookupSearches??0)+1,i=(c.dependencyLookupDebt??0)+e;if(c.dependencyLookupSearches=t,c.dependencyLookupDebt=i,t<U||i<c.depCount)return;const s=new Map;for(let n=c.firstDep;n!==null;n=n.nextDep)s.set(n.dep,n);c.dependencyIndex=s,c.dependencyLookupDebt=void 0,c.dependencyLookupSearches=void 0}function q(c,e,t){const i=c.firstSub;if(i===null)return;if(i===c.lastSub)return i.sub===e?i:void 0;const s=c.subSearchCursor??i;let n=1;if(s.sub===e)return c.subSearchCursor=s.nextSub??i,s;if(t){const a=e.dependencyIndex?.get(c);if(a!==void 0)return c.subSearchCursor=a.nextSub??i,a;if(e.dependencyIndex!==void 0)return}let r=s.nextSub??i;for(;r!==s;){if(++n,r.sub===e)return c.subSearchCursor=r.nextSub??i,t&&W(e,n),r;r=r.nextSub??i}t&&W(e,n)}class _{adaptiveDependencyIndex;activeSub=null;batchDepth=0;effectReadIndex=0;effectWriteIndex=0;invalidationDepth=0;dependencyDepth=0;checkingDepth=0;flushingEffects=!1;effectQueue=new Array(16).fill(void 0);invalidationStack=new Array(512).fill(null);dependencyStack=[];handleReaders=this.createHandleReaders();effectScheduler;flushEffects;_effectImmediate;constructor(e={}){this.adaptiveDependencyIndex=e.adaptiveDependencyIndex??!1,this.effectScheduler=e.effectScheduler,this._effectImmediate=this.effect.bind(this),this.signal=this.signal.bind(this),this.computed=this.computed.bind(this),this.reactive=this.reactive.bind(this),this.watch=this.watch.bind(this),this.batch=this.batch.bind(this),this.untracked=this.untracked.bind(this),this.scope=this.scope.bind(this),this._signalAccess=this._signalAccess.bind(this),this._computedAccess=this._computedAccess.bind(this),this._ownedSignal=this._ownedSignal.bind(this),this._ownedComputed=this._ownedComputed.bind(this),this._isTracking=this._isTracking.bind(this),this._isFlushingEffects=this._isFlushingEffects.bind(this),this._startBatch=this._startBatch.bind(this),this._endBatch=this._endBatch.bind(this),this.effect=this.effectScheduler===void 0?this._effectImmediate:this.createScheduledEffect.bind(this),this.flushEffects=this.effectScheduler===void 0?this.flushSyncEffects:this.flushExternalEffects}signal(e){let t=e,i;const s=()=>{const r=i;if(r!==void 0)return this.readSignalValue(r);const a=this.activeSub;return a===null||(i=this.materializeSignal(t,a)),t},n=r=>{const a=i;if(a!==void 0){this.writeSignalValue(a,r);return}t!==r&&(t=r)};return{get:s,peek:this.handleReaders.peek,set:n}}reactive(e){return H.reactiveObject(this,e,()=>this.createReactivePropertySourceBridge())}computed(e){if(typeof e=="function")return this.createComputedHandle(e);const t=this.createComputedHandle(e.get);return t.set=e.set,t}effect(e,t){const i=this.allocateGraphRecord(void 0,e,f|g|d);t!==void 0&&(i.onStop=t);const s=this.activeSub;s!==null&&s.flags&d&&D.appendScopeChild(s,i);try{this.executeEffect(i)}catch(n){try{this.disposeEffect(i)}catch(r){throw new AggregateError([n,r],"Effect registration and cleanup failed.")}throw n}return this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects(),()=>this.disposeEffect(i)}watch(e,t,i){return O.watchWithReactivity(this.effect,this.untracked,e,t,i)}batch(e){++this.batchDepth;let t;try{t=e()}catch(i){if(!--this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex)try{this.flushEffects()}catch(s){throw new AggregateError([i,s],"Batch operation and effect queue processing failed.")}throw i}return!--this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects(),t}untracked(e){if(this.activeSub===null)return e();const t=this.activeSub;this.activeSub=null;try{return e()}finally{this.activeSub=t}}scope(){return new D.ScopeNode(this)}allocateGraphRecord(e,t,i){return{flags:i,firstSub:null,lastSub:null,firstDep:null,tailDep:null,depCount:0,evaluate:t,cachedValue:e}}createReactivePropertySourceBridge(){return{batch:this.batch,create:()=>{if(this.activeSub===null)throw new Error("[Raph] Reactive property source requires a tracked read.");return{firstSub:null,lastSub:null}},isTracking:this._isTracking,track:e=>{const t=this.activeSub;t!==null&&this.trackDependency(e,t)},trigger:e=>{const t=e.firstSub;t!==null&&(this.propagateInvalidation(t),!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects())}}}materializeSignal(e,t,i){const s=this.allocateGraphRecord(e,void 0,0);return i!==void 0&&(s.sourceHooks=i,s.flags|=o),t.flags&w?this.appendInitialDependency(s,t):this.trackDependency(s,t),s}createComputedHandle(e,t){const i={evaluate:e,get:this.handleReaders.initializeComputed,peek:this.handleReaders.peek};return t!==void 0&&(i.sourceHooks=t),i}createScheduledEffect(e,t){const i=this.allocateGraphRecord(void 0,e,f|g|d);let s=!1;t!==void 0&&(i.onStop=t);const n=this.activeSub;n!==null&&n.flags&d&&D.appendScopeChild(n,i);const r=()=>{if(!(!(i.flags&v)||i.evaluate===void 0)){i.flags&=~v,s=!0;try{this.executeEffect(i)}catch(a){if(!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex)try{this.flushEffects()}catch(l){throw new AggregateError([a,l],"Effect execution and queue processing failed.")}throw a}!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects()}};try{const a=this.effectScheduler.bind(r);a.cancelPending=()=>i.evaluate===void 0||!s?!1:(this.settleCancelledDependencies(i),i.flags&=~(v|b|y),!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects(),!0),a.rearmPending=()=>{i.evaluate!==void 0&&(i.flags|=f|v,a.schedule())},i.effectTask=a,this.requestEffectTask(i)}catch(a){try{this.disposeEffect(i)}catch(l){throw new AggregateError([a,l],"Effect registration and cleanup failed.")}throw a}return!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects(),()=>this.disposeEffect(i)}readSignalValue(e){const t=this.activeSub;let i=null;return t!==null&&(i=this.trackDependency(e,t)),e.flags&f&&this.commitSignalValue(e,i),e.cachedValue}writeSignalValue(e,t){if(e.flags&f||this.batchDepth){const s=!!(e.flags&f);if((e.flags&f?e.pendingValue:e.cachedValue)===t)return;if(e.pendingValue=t,e.cachedValue===t)e.flags&=~f,e.pendingAuthor=void 0;else{e.flags|=f;const a=this.activeSub!==null&&this.activeSub.flags&p?this.activeSub:null;s?e.pendingAuthor!==a&&(e.pendingAuthor=null):e.pendingAuthor=a}const r=e.firstSub;r!=null&&(!s||this.activeSub!==null&&this.activeSub.flags&p||e.flags&o)&&this.propagateInvalidation(r,!0);return}if(e.cachedValue===t)return;e.cachedValue=t;const i=e.firstSub;i!=null&&(this.propagateInvalidation(i),!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects())}readComputedValue(e){const t=e.flags;if((t&(p|w))===(p|w))throw new Error("Computed value is already being evaluated.");const i=t&(T|b|x),s=i===0?e.cachedValue:i&T?this.evaluateFirstValue(e):this.refreshComputedValue(e,t);if(e.evaluate===void 0)return s;const n=this.activeSub;if(n!==null){const r=n.tailDep;n.flags&w?(r===null||r.dep!==e)&&this.appendInitialDependency(e,n):this.trackDependency(e,n)}return s}evaluateFirstValue(e){const t=e.flags&o,i=e.evaluate;e.flags=t|p|w;const s=this.activeSub;this.activeSub=e;let n;try{if(n=i(),e.evaluate!==i)return e.cachedValue=void 0,e.flags=0,n;e.flags=t|(e.flags&y?f:0)}catch(r){throw e.evaluate!==i?(e.cachedValue=void 0,e.flags=0,r):(e.flags=t|x,s!==null&&this.trackDependency(e,s),r)}finally{this.activeSub=s}return e.flags&f?this.refreshComputedValue(e,e.flags):(e.cachedValue=n,n)}refreshComputedValue(e,t,i=null){try{if(t&(f|y|x))return this.recomputeValue(e,i),e.cachedValue;const s=e.firstDep;return s!==null&&this.refreshDependencies(e,s)?this.recomputeValue(e,i):e.flags&=~b,e.cachedValue}catch(s){throw this.activeSub!==null&&this.trackDependency(e,this.activeSub),s}}commitSignalValue(e,t){const i=e.cachedValue;e.cachedValue=e.pendingValue,e.flags&=~f;const s=e.pendingAuthor;return e.pendingAuthor=void 0,i===e.cachedValue?!1:(e.firstSub!==null&&this.confirmSubUpdates(e.firstSub,t,s??null),!0)}trackDependency(e,t){const i=t.tailDep;if(i!==null){const n=i.nextDep;return n!==null&&n.dep===e?(t.tailDep=n,n):i.dep===e?i:this.reconcileDependency(e,t,i,n)}const s=t.firstDep;return s===null?t.tailDep=this.insertDependency(e,t,null):s.dep===e?(t.tailDep=s,s):this.reconcileDependency(e,t,null,s)}reconcileDependency(e,t,i,s){if(t.depCount<=C){for(let l=s;l!==null;l=l.nextDep)if(l.dep===e)return this.reorderDependency(t,l,i),t.tailDep=l,l;for(let l=i;l!==null;l=l.prevDep)if(l.dep===e)return l;return s!==null?(t.tailDep=this.reuseDepLink(s,e),s):t.tailDep=this.insertDependency(e,t,i)}let n=0,r=s;for(;r!==null&&n<C;){const l=r;if(l.dep===e)return this.reorderDependency(t,l,i),t.tailDep=l,l;r=l.nextDep,++n}let a=i;for(;a!==null&&n<C;){const l=a;if(l.dep===e)return l;a=l.prevDep,++n}if(r!==null||a!==null){const l=q(e,t,this.adaptiveDependencyIndex);if(l!==void 0)return this.reorderDependency(t,l,i),t.tailDep=l,l}return s!==null?(t.tailDep=this.reuseDepLink(s,e),s):t.tailDep=this.insertDependency(e,t,i)}appendInitialDependency(e,t){const i=t.tailDep,s=e.lastSub,n={dep:e,sub:t,prevSub:s,nextSub:null,prevDep:i,nextDep:null};return s===null?e.firstSub=n:s.nextSub=n,e.lastSub=n,i===null?t.firstDep=n:i.nextDep=n,t.tailDep=n,++t.depCount,t.dependencyIndex?.set(e,n),e.flags&o&&this.attachDependencyCleanup(n),n}insertDependency(e,t,i){const s=e.lastSub,n={dep:e,sub:t,prevSub:s,nextSub:null,prevDep:i,nextDep:null};s===null?e.firstSub=n:s.nextSub=n,e.lastSub=n;const r=i===null?t.firstDep:i.nextDep;return n.nextDep=r,r!==null&&(r.prevDep=n),i===null?t.firstDep=n:i.nextDep=n,++t.depCount,t.dependencyIndex?.set(e,n),e.flags&o&&this.attachDependencyCleanup(n),n}reuseDepLink(e,t){const i=e.sub.dependencyIndex;this.detachSubLink(e);const s=t.lastSub;return e.dep=t,e.prevSub=s,e.nextSub=null,s===null?t.firstSub=e:s.nextSub=e,t.lastSub=e,i?.set(t,e),t.flags&o&&this.attachDependencyCleanup(e),e}reorderDependency(e,t,i){if(t.prevDep===i)return;const s=t.prevDep,n=t.nextDep;s===null?e.firstDep=n:s.nextDep=n,n!==null&&(n.prevDep=s);const r=i===null?e.firstDep:i.nextDep;t.prevDep=i,t.nextDep=r,r!==null&&(r.prevDep=t),i===null?e.firstDep=t:i.nextDep=t}pruneUnreadDependencies(e){const t=e.tailDep;let i=t===null?e.firstDep:t.nextDep;if(i===null)return;t===null?e.firstDep=null:t.nextDep=null;for(let n=i;n!==null;n=n.nextDep)n.prevDep=null,--e.depCount;e.depCount<=C&&L(e);let s;for(;i!==null;){const n=i.nextDep;try{this.detachSubLink(i,!0)}catch(r){this.unlinkSubLink(i,!0),s??=[],s.push(r)}i=n}s!==void 0&&D.throwScopeErrors(s,"Dependency cleanup failed.")}detachSubLink(e,t=!1){e.sub.dependencyIndex?.delete(e.dep);let i;e.dep.flags&o&&(i=e.dependencyCleanup,e.dependencyCleanup=void 0,i?.()),this.unlinkSubLink(e,t||i!==void 0)}unlinkSubLink(e,t){t&&((e.prevSub===null?e.dep.firstSub!==e:e.prevSub.nextSub!==e)||(e.nextSub===null?e.dep.lastSub!==e:e.nextSub.prevSub!==e))||(e.dep.subSearchCursor===e&&(e.dep.subSearchCursor=e.nextSub??(e.dep.firstSub===e?void 0:e.dep.firstSub??void 0)),e.prevSub===null?e.dep.firstSub=e.nextSub:e.prevSub.nextSub=e.nextSub,e.nextSub===null?e.dep.lastSub=e.prevSub:e.nextSub.prevSub=e.prevSub)}attachDependencyCleanup(e){const i=e.dep.sourceHooks?.onDependency;if(i===void 0)return;const s=e.sub.sourceHooks?.dependencyOwner??e.sub.effectTask?.dependencyOwner;s!==void 0&&(e.dependencyCleanup=i(s))}releaseSourceSubs(e){if(!(e.flags&o)){let s=e.firstSub;for(;s!=null;){const n=s.nextSub,r=s.sub;this.detachDependencyLink(s),this.detachSubLink(s);const a=r.flags;(a&(b|p))===g&&(r.flags=a&~g|f,a&d&&this.enqueueEffect(r,e)),s=n}e.firstSub=e.lastSub=null,e.subSearchCursor=void 0;return}let t=e.firstSub,i;for(;t!=null;){const s=t.nextSub;this.detachDependencyLink(t);try{this.detachSubLink(t,!0)}catch(n){this.unlinkSubLink(t,!0),i??=[],i.push(n)}t=s}e.firstSub=e.lastSub=null,e.subSearchCursor=void 0,i!==void 0&&D.throwScopeErrors(i,"Source dependency cleanup failed.")}detachDependencyLink(e){const t=e.sub,i=e.prevDep,s=e.nextDep;(i===null?t.firstDep!==e:i.nextDep!==e)||(i===null?t.firstDep=s:i.nextDep=s,s!==null&&(s.prevDep=i),t.tailDep===e&&(t.tailDep=i),--t.depCount,t.depCount<=C&&L(t))}enqueueEffect(e,t){const i=t!==void 0&&t.flags&o?t.sourceHooks.token:void 0;i!==void 0&&e.effectTask?.notify?.(i),!(e.flags&v)&&(e.flags|=v,this.effectQueue[this.effectWriteIndex]=e,++this.effectWriteIndex)}propagateInvalidation(e,t=!1){this.checkingDepth!==0&&this.markActiveChecks(e);const i=this.invalidationDepth;let s=i;for(let n=e;n!==null;n=n.nextSub){const r=n.sub,a=r.flags;let l=0;if(a&(b|p)?a&p?l=this.invalidateReadDependency(n,r,a):!t&&!(a&f)&&(r.flags=a&~g|f):(l=a&~y|(t?g:f),r.flags=l),l===0){if(a&d&&a&v){const u=n.dep.flags&o?n.dep.sourceHooks.token:void 0;u!==void 0&&r.effectTask?.notify?.(u)}else r.flags&(o|R)&&(r.flags&o&&r.sourceHooks.onInvalidate?.(r.firstSub!==null),r.flags&=~R,r.firstSub!==null&&(this.invalidationStack[s++]=r.firstSub));continue}l&d?(!(l&p)||r.effectTask?.allowScheduleDuringRun===!0)&&this.enqueueEffect(r,n.dep):(!(a&b)&&r.flags&o&&r.sourceHooks.onInvalidate?.(r.firstSub!==null),r.firstSub!==null&&(this.invalidationStack[s++]=r.firstSub))}for(;s!==i;){let n=this.invalidationStack[--s];do{const r=n.sub,a=r.flags;let l=0;if(a&(b|p)?a&p&&(l=this.invalidateReadDependency(n,r,a)):(l=a&~y|g,r.flags=l),l===0){if(a&d&&a&v){const u=n.dep.flags&o?n.dep.sourceHooks.token:void 0;u!==void 0&&r.effectTask?.notify?.(u)}else if(r.flags&(o|R)&&(r.flags&o&&r.sourceHooks.onInvalidate?.(r.firstSub!==null),r.flags&=~R,r.firstSub!==null)){n.nextSub!==null&&(this.invalidationStack[s++]=n.nextSub),n=r.firstSub;continue}}else if(l&d)this.enqueueEffect(r,n.dep);else if(!(a&b)&&r.flags&o&&r.sourceHooks.onInvalidate?.(r.firstSub!==null),r.firstSub!==null){n.nextSub!==null&&(this.invalidationStack[s++]=n.nextSub),n=r.firstSub;continue}n=n.nextSub}while(n!==null)}this.invalidationDepth=i}markActiveChecks(e){const t=[e],i=new Set;for(;t.length!==0;)for(let s=t.pop();s!==null;s=s.nextSub){const n=s.sub;i.has(n)||(i.add(n),n.flags&N&&(n.flags|=k),!(n.flags&d)&&n.firstSub!==null&&t.push(n.firstSub))}}invalidateReadDependency(e,t,i){const s=t.tailDep;if(s===null)return 0;if(e!==s){for(let r=e.prevDep;r!==null;r=r.prevDep)if(r===s)return 0}const n=i|y|g;return t.flags=n,n}confirmSubUpdates(e,t,i=null){for(let s=e;s!==null;s=s.nextSub){if(s===t||s.sub===i)continue;const n=s.sub,r=n.flags;r&f||(n.flags=r&~g|f,r&d?this.enqueueEffect(n,s.dep):r&b||n.flags&o&&n.sourceHooks.onInvalidate?.(n.firstSub!==null))}}recomputeValue(e,t=null){const i=e.cachedValue;for(let s=0;s<A;s++){const n=e.evaluate;e.tailDep=null;const r=e.flags&-6149|p;e.flags=r;const a=this.activeSub;this.activeSub=e;let l;try{l=n()}catch(h){if(e.evaluate!==n)throw e.cachedValue=void 0,e.flags=0,h;this.failComputedValue(e,t,h)}finally{this.activeSub=a}if(e.evaluate!==n)return e.cachedValue=void 0,e.flags=0,!1;const u=e.tailDep;if((u===null?e.firstDep:u.nextDep)!==null)try{this.pruneUnreadDependencies(e)}catch(h){this.failComputedValue(e,t,h)}if(e.evaluate!==n)return e.cachedValue=void 0,e.flags=0,!1;const I=e.flags&y;if(e.flags=r&-8460,I){e.flags|=f;continue}return e.cachedValue=l,i===l?!1:(e.firstSub!==null&&this.confirmSubUpdates(e.firstSub,t),!0)}return this.failComputedValue(e,t,new Error(`Computed settlement exceeded ${A} attempts.`))}failComputedValue(e,t,i){try{this.restoreComputedFailure(e,t)}catch(s){throw new AggregateError([i,s],"Computed evaluation and dependency cleanup failed.")}throw i}restoreComputedFailure(e,t){e.evaluate!==void 0&&(e.flags=e.flags&-6288|x);try{e.evaluate!==void 0&&this.pruneUnreadDependencies(e)}finally{e.evaluate===void 0&&(e.cachedValue=void 0,e.flags=0),t!==null&&this.restoreFailedDependency(t.sub)}}refreshDependencies(e,t){const i=this.dependencyDepth;try{return this.walkDependencyChain(e,t)}catch(s){this.markRecoveryDependencies(e),this.restoreFailedDependency(e);for(let n=i;n<this.dependencyDepth;n++)this.restoreFailedDependency(this.dependencyStack[n].sub);throw this.dependencyDepth=i,this.dependencyStack.length=i,s}}settleCancelledDependencies(e){for(let t=e.firstDep;t!==null;t=t.nextDep){const i=t.dep,s=i.flags;s&(T|b|x)&&(i.evaluate===void 0?s&f&&this.commitSignalValue(i,t):this.refreshComputedValue(i,s,t))}}walkDependencyChain(e,t){const i=this.dependencyDepth;let s=i,n=!1,r;e.flags=e.flags&~k|N,++this.checkingDepth;try{e:for(;;){if(e.flags&k){r??=new Map;const a=(r.get(e)??0)+1;if(r.set(e,a),a>A)throw new Error(`Dependency check exceeded ${A} restarts.`);e.flags&=~k,e.firstDep!==null&&(t=e.firstDep)}if(e.flags&f)n=!0;else{const a=t.dep,l=a.flags;if(l&(f|x))this.dependencyDepth=s,n=a.evaluate===void 0?this.commitSignalValue(a,t):this.recomputeValue(a,t);else if(l&g){if(a.firstDep!==null){this.dependencyStack[s++]=t,e=a,e.flags=e.flags&~k|N,t=a.firstDep;continue e}this.dependencyDepth=s,n=this.recomputeValue(a,t)}else n=!1}if(!n&&t.nextDep!==null){t=t.nextDep;continue}if(n){for(;s!==i;){this.dependencyDepth=--s;const a=this.dependencyStack[s];if(e.flags&=~m,n=this.recomputeValue(e,a),e=a.sub,!n){if(a.nextDep!==null){t=a.nextDep;continue e}break}}if(n)return this.dependencyDepth=i,!0}for(;s!==i;){if(e.flags&(f|k))continue e;this.dependencyDepth=--s;const a=this.dependencyStack[s];if(e.flags&=~(g|m),e=a.sub,a.nextDep!==null){t=a.nextDep;continue e}}if(e.flags&(f|k))continue e;return e.flags&=~(g|m),this.dependencyDepth=i,!1}}finally{e.flags&=~m;for(let a=i;a<s;a++)this.dependencyStack[a].sub.flags&=~m;--this.checkingDepth}}restoreFailedDependency(e){if(e.evaluate===void 0){e.cachedValue=void 0,e.flags=0;return}e.flags&=-6160,e.flags&d||(e.flags|=x)}executeEffect(e){const t=e.flags;if(!(t&b))return;if(!(t&(f|y))){const u=e.firstDep;try{if(u===null||!this.refreshDependencies(e,u)){e.flags&=~b;return}}catch(I){throw e.flags&=-80,I}}const i=e.evaluate;if(i===void 0){e.flags&=~b;return}let s;if(e.scopeLastChild!==void 0)try{this.disposeNestedEffects(e)}catch(u){s=[u]}const n=typeof e.cachedValue=="function"?e.cachedValue:void 0;if(e.cachedValue=void 0,e.flags&=~y,n!==void 0)try{this.untracked(n)}catch(u){s??=[],s.push(u)}if(s!==void 0){try{this.disposeEffect(e)}catch(u){s.push(u)}D.throwScopeErrors(s,"Effect cleanup failed.")}if(e.evaluate===void 0)return;e.tailDep=null,e.flags=e.flags&~y|p;const r=this.activeSub;this.activeSub=e;let a;try{a=i()}catch(u){this.activeSub=r,e.flags&=~p,this.failEffectRun(e,u,s)}finally{this.activeSub=r,e.flags&=~p}const l=e.tailDep;if((l===null?e.firstDep:l.nextDep)!==null&&this.pruneUnreadDependencies(e),typeof a=="function")if(e.evaluate===void 0)try{this.untracked(a)}catch(u){s??=[],s.push(u)}else e.cachedValue=a;e.flags&v?e.flags=e.flags&-7|g:e.flags&=-8}failEffectRun(e,t,i){const s=i??[];s.push(t),this.pruneUnreadDependencies(e);try{this.disposeNestedEffects(e)}catch(n){s.push(n)}throw e.flags&=-72,s.length===1?s[0]:new AggregateError(s,"Effect execution failed.")}flushSyncEffects(){if(!(this.flushingEffects||this.activeSub!==null&&(this.activeSub.flags&(d|p))===(d|p))){this.flushingEffects=!0;try{for(;this.effectReadIndex!==this.effectWriteIndex;){const e=this.effectReadIndex,t=this.effectQueue[e];this.effectQueue[e]=void 0,++this.effectReadIndex,t.flags&=~v,this.executeEffect(t)}}catch(e){this.drainQueueAfterError(e,!1)}finally{this.effectReadIndex=0,this.effectWriteIndex=0,this.flushingEffects=!1}}}flushExternalEffects(){if(!(this.flushingEffects||this.activeSub!==null&&(this.activeSub.flags&(d|p))===(d|p))){this.flushingEffects=!0;try{for(;this.effectReadIndex!==this.effectWriteIndex;){const e=this.effectReadIndex,t=this.effectQueue[e];this.effectQueue[e]=void 0,++this.effectReadIndex;const i=t.effectTask;i===void 0?(t.flags&=~v,this.executeEffect(t)):t.evaluate!==void 0?this.scheduleEffectTask(t,i):t.flags&=~v}}catch(e){this.drainQueueAfterError(e,!0)}finally{this.effectReadIndex=0,this.effectWriteIndex=0,this.flushingEffects=!1}}}markRecoveryDependencies(e){const t=[e],i=new Set;for(;t.length!==0;){const s=t.pop();if(!i.has(s)){i.add(s);for(let n=s.firstDep;n!==null;n=n.nextDep){const r=n.dep;r.evaluate!==void 0&&!(r.flags&d)&&(r.flags|=R,t.push(r))}}}}drainQueueAfterError(e,t){const i=[e];for(;this.effectReadIndex!==this.effectWriteIndex;){const s=this.effectReadIndex++,n=this.effectQueue[s];this.effectQueue[s]=void 0;try{const r=t?n.effectTask:void 0;r!==void 0&&n.evaluate!==void 0?this.scheduleEffectTask(n,r):(n.flags&=~v,this.executeEffect(n))}catch(r){i.push(r)}}throw i.length===1?e:new AggregateError(i,"Effect queue processing failed.")}requestEffectTask(e){const t=e.effectTask;t===void 0||e.evaluate===void 0||(e.flags|=v,this.scheduleEffectTask(e,t))}scheduleEffectTask(e,t){try{t.schedule()}catch(i){throw e.flags&=-72,i}}disposeEffect(e){if(e.evaluate===void 0)return;this.activeSub===e&&(this.activeSub=null);const t=e.effectTask,i=e.onStop,s=typeof e.cachedValue=="function"?e.cachedValue:void 0;e.evaluate=void 0,e.flags=0,e.cachedValue=void 0,e.effectTask=void 0,i!==void 0&&(e.onStop=void 0),D.detachScopeChild(e);let n;e.tailDep=null;try{this.pruneUnreadDependencies(e)}catch(r){n=[r]}if(e.firstDep=e.tailDep=null,e.depCount=0,L(e),t!==void 0)try{t.dispose()}catch(r){n??=[],n.push(r)}try{this.disposeNestedEffects(e)}catch(r){n??=[],n.push(r)}if(s!==void 0)try{this.untracked(s)}catch(r){n??=[],n.push(r)}if(i!==void 0)try{this.untracked(i)}catch(r){n??=[],n.push(r)}this.invalidationStack.length!==this.invalidationDepth&&(this.invalidationStack.length=this.invalidationDepth),this.dependencyStack.length!==this.dependencyDepth&&(this.dependencyStack.length=this.dependencyDepth),n!==void 0&&D.throwScopeErrors(n,"Effect disposal failed.")}disposeNestedEffects(e){let t;for(;e.scopeLastChild!==void 0;)try{this.disposeEffect(e.scopeLastChild)}catch(i){t??=[],t.push(i)}t!==void 0&&D.throwScopeErrors(t,"Nested effect disposal failed.")}static materializeComputed(e){e.flags=T|(e.sourceHooks===void 0?0:o),e.firstSub=null,e.lastSub=null,e.firstDep=null,e.tailDep=null,e.depCount=0,e.cachedValue=void 0}createHandleReaders(){const e=this;function t(){return e.readComputedValue(this)}return{initializeComputed(){const i=this;return _.materializeComputed(i),this.get=t,e.readComputedValue(i)},initializeRaw(){const i=this;_.materializeComputed(i);const s=()=>e.readComputedValue(i);return this.read=s,s()},peek(){return e.peekHandle(this)}}}peekHandle(e){const t=this.activeSub;if(t===null)return e.get();this.activeSub=null;try{return e.get()}finally{this.activeSub=t}}_signalAccess(e){const t=this;let i=e,s;function n(r){if(arguments.length!==0){const u=s;u!==void 0?t.writeSignalValue(u,r):i!==r&&(i=r);return}const a=s;if(a!==void 0)return t.readSignalValue(a);const l=t.activeSub;return l===null||(s=t.materializeSignal(i,l)),i}return{read:n,write:n}}_computedAccess(e){return{evaluate:e,read:this.handleReaders.initializeRaw}}_ownedSignal(e,t){const i=this;let s=e,n,r=!1;const a=()=>{const h=n;if(h!==void 0)return this.readSignalValue(h);const S=this.activeSub;if(S===null)return s;const V=s;return n=this.materializeSignal(V,S,t),s=void 0,V},l=t===void 0?h=>{const S=n;S!==void 0?this.writeSignalValue(S,h):s!==h&&(s=h)}:h=>{const S=n;if(S!==void 0){const V=S.flags&f?S.pendingValue:S.cachedValue;V!==h&&t.onChange?.(V,h,S.firstSub!==null),this.writeSignalValue(S,h);return}s!==h&&(t.onChange?.(s,h,!1),s=h)},I={source:{get:a,peek:this.handleReaders.peek,set:l},dispose(){if(r)return!0;r=!0,s=void 0;let h;try{t?.onDispose?.(n?.firstSub!=null)}catch(S){(h??=[]).push(S)}if(n!==void 0){try{i.releaseSourceSubs(n)}catch(S){(h??=[]).push(S)}n.cachedValue=void 0,n.pendingValue=void 0,n.pendingAuthor=void 0,n.flags=0}if(n=void 0,h?.length===1)throw h[0];if(h!==void 0&&h.length>1)throw new AggregateError(h,"Owned signal disposal failed.");return!0}};return t!==void 0&&(I.hasSubscribers=()=>n?.firstSub!=null),I}_ownedComputed(e,t){const i=this,s=this.createComputedHandle(e,t);let n=!1;const r={source:s,dispose(){if(n)return!0;n=!0,i.activeSub===s&&(i.activeSub=null);const a=[];try{t?.onDispose?.(s.firstSub!=null)}catch(u){a.push(u)}if(s.flags!==void 0)try{i.releaseSourceSubs(s)}catch(u){a.push(u)}const l=s.flags!==void 0;if(s.evaluate=void 0,s.cachedValue=void 0,s.flags=0,s.tailDep=null,l)try{i.pruneUnreadDependencies(s)}catch(u){a.push(u)}if(s.firstDep=s.tailDep=null,s.depCount=0,L(s),a.length===1)throw a[0];if(a.length>1)throw new AggregateError(a,"Owned computed disposal failed.");return!0}};return t!==void 0&&(r.hasSubscribers=()=>s.firstSub!=null),r}_startBatch(){++this.batchDepth}_endBatch(){!--this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects()}_isTracking(){return this.activeSub!==null}_isFlushingEffects(){return this.flushingEffects}_allocateTaskEffect(e,t){const i=this.allocateGraphRecord(void 0,e,f|g|d),s=this.activeSub;return s!==null&&s.flags&d&&D.appendScopeChild(s,i),i.effectTask=t,i}_requestTaskEffect(e,t){t?this.requestEffectTask(e):e.effectTask!==void 0&&e.evaluate!==void 0&&(e.flags|=v),!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects()}_beginTaskEffectRun(e){return!(e.flags&v)||e.evaluate===void 0?!1:(e.flags&=~v,!0)}_finishTaskEffectRun(e){const t=this.activeSub;this.activeSub=null;try{try{this.executeEffect(e)}catch(i){if(!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex)try{this.flushEffects()}catch(s){throw new AggregateError([i,s],"Effect execution and queue processing failed.")}throw i}!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects()}finally{this.activeSub=t}}_cancelTaskEffect(e){return e.evaluate===void 0?!1:(this.settleCancelledDependencies(e),e.flags&=-72,!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects(),!0)}_rearmTaskEffect(e){const t=e.effectTask;t===void 0||e.evaluate===void 0||(e.flags|=f|v,t.schedule())}_disposeTaskEffect(e){this.disposeEffect(e)}_trackSignalNode(e,t){const i=this.activeSub;if(i!==null)return this.materializeSignal(e,i,t)}_readSignalNode(e){return this.readSignalValue(e)}_peekSignalNode(e){const t=this.activeSub;if(t===null)return this.readSignalValue(e);this.activeSub=null;try{return this.readSignalValue(e)}finally{this.activeSub=t}}_visibleSignalValue(e){return e.flags&f?e.pendingValue:e.cachedValue}_writeSignalNode(e,t){this.writeSignalValue(e,t)}_signalNodeHasSubscribers(e){return e.firstSub!==null}_releaseSignalNode(e){try{this.releaseSourceSubs(e)}finally{e.cachedValue=void 0,e.pendingValue!==void 0&&(e.pendingValue=void 0),e.pendingAuthor!=null&&(e.pendingAuthor=void 0),e.flags=0}}_findSubscriberOwner(e,t,i){for(let s=e.firstSub;s!==null;s=s.nextSub){const n=s.sub,r=n.sourceHooks?.dependencyOwner??n.effectTask?.dependencyOwner;if(r!==void 0&&t(r,i))return r}}_activeSubscriberOwner(){const e=this.activeSub;if(e!==null)return e.sourceHooks?.dependencyOwner??e.effectTask?.dependencyOwner}_bumpSignalNode(e){return e.firstSub===null?!1:(this.writeSignalValue(e,(e.flags&f?e.pendingValue:e.cachedValue)+1),!this.batchDepth&&this.effectReadIndex!==this.effectWriteIndex&&this.flushEffects(),!0)}_trackCounterNode(e){const t=this.activeSub;if(t===null)return;const i={flags:o,firstSub:null,lastSub:null,evaluate:void 0,cachedValue:0,sourceHooks:e};return t.flags&w?this.appendInitialDependency(i,t):this.trackDependency(i,t),i}_activeEffectTask(){const e=this.activeSub;return e===null?void 0:e.effectTask}}let z;function E(){return z??=new _}const F=c=>E().signal(c),Q=c=>E().reactive(c);function P(c){return E().computed(c)}const B=(c,e)=>E().effect(c,e),G=(c,e,t)=>E().watch(c,e,t),M=c=>E().batch(c),K=c=>E().untracked(c),Y=()=>E().scope();exports.ScopeNode=D.ScopeNode;exports.RaphReactivity=_;exports.batch=M;exports.computed=P;exports.effect=B;exports.reactive=Q;exports.scope=Y;exports.signal=F;exports.untracked=K;exports.watch=G;