@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,38 +1,33 @@
1
- var h = Object.defineProperty;
2
- var u = (n, t, i) => t in n ? h(n, t, { enumerable: !0, configurable: !0, writable: !0, value: i }) : n[t] = i;
3
- var p = (n, t, i) => u(n, typeof t != "symbol" ? t + "" : t, i);
4
- import { pathSegmentKey as x } from "./pathSegmentKey.js";
5
- class m {
6
- constructor() {
7
- // Корень trie разделяет exact и indexed-wildcard transitions.
8
- p(this, "_root", {});
9
- }
1
+ import { pathSegmentKey as p } from "./pathSegmentKey.js";
2
+ class u {
3
+ // Корень trie разделяет exact и indexed-wildcard transitions.
4
+ _root = {};
10
5
  /**
11
6
  * Регистрирует route mask и возвращает функцию удаления.
12
7
  */
13
- add(t, i) {
8
+ add(d, n) {
14
9
  let e = this._root;
15
10
  const o = [];
16
- for (const c of t.segments)
17
- if (c.kind === "wildcard") {
18
- const d = e.indexedWildcard ?? (e.indexedWildcard = {});
19
- o.push({ child: d, parent: e, indexedWildcard: !0 }), e = d;
11
+ for (const i of d.segments)
12
+ if (i.kind === "wildcard") {
13
+ const t = e.indexedWildcard ??= {};
14
+ o.push({ child: t, parent: e, indexedWildcard: !0 }), e = t;
20
15
  } else {
21
- const d = x(c), a = e.exact ?? (e.exact = /* @__PURE__ */ new Map());
22
- let s = a.get(d);
23
- s === void 0 && (s = {}, a.set(d, s)), o.push({ child: s, key: d, parent: e, indexedWildcard: !1 }), e = s;
16
+ const t = p(i), a = e.exact ??= /* @__PURE__ */ new Map();
17
+ let l = a.get(t);
18
+ l === void 0 && (l = {}, a.set(t, l)), o.push({ child: l, key: t, parent: e, indexedWildcard: !1 }), e = l;
24
19
  }
25
- const r = t.deep ? e.deep ?? (e.deep = /* @__PURE__ */ new Set()) : e.end ?? (e.end = /* @__PURE__ */ new Set());
26
- r.add(i);
27
- let l = !0;
20
+ const r = d.deep ? e.deep ??= /* @__PURE__ */ new Set() : e.end ??= /* @__PURE__ */ new Set();
21
+ r.add(n);
22
+ let s = !0;
28
23
  return () => {
29
- if (l) {
30
- l = !1, r.delete(i), r.size === 0 && (t.deep ? e.deep = void 0 : e.end = void 0);
31
- for (let c = o.length - 1; c >= 0; c--) {
32
- const d = o[c];
33
- if (!v(d.child))
24
+ if (s) {
25
+ s = !1, r.delete(n), r.size === 0 && (d.deep ? e.deep = void 0 : e.end = void 0);
26
+ for (let i = o.length - 1; i >= 0; i--) {
27
+ const t = o[i];
28
+ if (!x(t.child))
34
29
  break;
35
- d.indexedWildcard ? d.parent.indexedWildcard = void 0 : (d.parent.exact.delete(d.key), d.parent.exact.size === 0 && (d.parent.exact = void 0));
30
+ t.indexedWildcard ? t.parent.indexedWildcard = void 0 : (t.parent.exact.delete(t.key), t.parent.exact.size === 0 && (t.parent.exact = void 0));
36
31
  }
37
32
  }
38
33
  };
@@ -40,21 +35,20 @@ class m {
40
35
  /**
41
36
  * Возвращает routes, чьи masks соответствуют указанному событию.
42
37
  */
43
- match(t) {
44
- var o;
45
- let i = [this._root];
38
+ match(d) {
39
+ let n = [this._root];
46
40
  const e = /* @__PURE__ */ new Set();
47
- for (let r = 0; r <= t.length; r++) {
48
- const l = r === t.length, c = [];
49
- for (const d of i) {
50
- if (f(e, d.deep), l) {
51
- f(e, d.end);
41
+ for (let o = 0; o <= d.length; o++) {
42
+ const r = o === d.length, s = [];
43
+ for (const i of n) {
44
+ if (f(e, i.deep), r) {
45
+ f(e, i.end);
52
46
  continue;
53
47
  }
54
- const a = t[r], s = (o = d.exact) == null ? void 0 : o.get(x(a));
55
- s !== void 0 && c.push(s), d.indexedWildcard !== void 0 && (a.kind === "index" || a.kind === "selector") && c.push(d.indexedWildcard);
48
+ const t = d[o], a = i.exact?.get(p(t));
49
+ a !== void 0 && s.push(a), i.indexedWildcard !== void 0 && (t.kind === "index" || t.kind === "selector") && s.push(i.indexedWildcard);
56
50
  }
57
- if (l || (i = c, i.length === 0))
51
+ if (r || (n = s, n.length === 0))
58
52
  break;
59
53
  }
60
54
  return e;
@@ -63,24 +57,24 @@ class m {
63
57
  * При замене root возвращает все зарегистрированные path masks.
64
58
  */
65
59
  matchAll() {
66
- const t = /* @__PURE__ */ new Set(), i = [this._root];
67
- for (; i.length; ) {
68
- const e = i.pop();
69
- if (f(t, e.deep), f(t, e.end), e.indexedWildcard !== void 0 && i.push(e.indexedWildcard), e.exact !== void 0)
60
+ const d = /* @__PURE__ */ new Set(), n = [this._root];
61
+ for (; n.length; ) {
62
+ const e = n.pop();
63
+ if (f(d, e.deep), f(d, e.end), e.indexedWildcard !== void 0 && n.push(e.indexedWildcard), e.exact !== void 0)
70
64
  for (const o of e.exact.values())
71
- i.push(o);
65
+ n.push(o);
72
66
  }
73
- return t;
67
+ return d;
74
68
  }
75
69
  }
76
- function f(n, t) {
77
- if (t !== void 0)
78
- for (const i of t)
79
- n.add(i);
70
+ function f(c, d) {
71
+ if (d !== void 0)
72
+ for (const n of d)
73
+ c.add(n);
80
74
  }
81
- function v(n) {
82
- return n.deep === void 0 && n.end === void 0 && n.exact === void 0 && n.indexedWildcard === void 0;
75
+ function x(c) {
76
+ return c.deep === void 0 && c.end === void 0 && c.exact === void 0 && c.indexedWildcard === void 0;
83
77
  }
84
78
  export {
85
- m as PathRouter
79
+ u as PathRouter
86
80
  };
@@ -1 +1 @@
1
- "use strict";var A=Object.defineProperty;var z=(m,e,t)=>e in m?A(m,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):m[e]=t;var p=(m,e,t)=>z(m,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const k=require("./data-path.utils.cjs"),R=require("./DataPath.cjs"),f=require("./derived/derived-collection.utils.cjs"),I=require("./derived/DerivedRegistry.cjs"),M=require("./derived/RaphDerivedHandle.cjs"),E=require("./meta/MetaController.cjs"),C=require("./storage/AdapterPathStore.cjs"),S=require("./storage/DefaultDataAdapter.cjs"),F=require("./storage/PathStore.cjs"),b=class b{constructor(e={}){p(this,"id");p(this,"_paths");p(this,"_derivedController");p(this,"_runtimes");p(this,"_metaController");p(this,"_batchDepth",0);p(this,"_runtimeMutationDepth",0);p(this,"_stabilizing",!1);p(this,"_derivedWriteDepth",0);if(this.id=e.id??`kernel-${b._kernelCounter++}`,e.adapter!==void 0){if(e.initial!==void 0||e.adapterOptions!==void 0)throw new TypeError("[Raph] A custom adapter cannot be combined with initial or adapterOptions.");this._paths=new C.AdapterPathStore(e.adapter)}else if(e.adapterOptions!==void 0)this._paths=new C.AdapterPathStore(new S.DefaultDataAdapter(e.initial,e.adapterOptions));else{const t=this._paths=new F.PathStore;e.initial!==void 0&&t.set("",e.initial)}}get disposed(){return this._paths===void 0}getDerivedSnapshot(){var t;const e=this._derivedController;return{registrations:(e==null?void 0:e.size)??0,errors:[...(e==null?void 0:e.values())??[]].filter(r=>r.status==="error").length,pending:((t=e==null?void 0:e.pending)==null?void 0:t.size)??0,stabilizing:this._stabilizing}}get meta(){return this._metadata.facade}get(e,t){return this._store.get(e??"",t)}peek(e,t){return this._store.peek(e??"",t)}has(e,t){return this._store.has(e,t)}set(e,t,r){var s;if(this._runtimeMutationDepth===0&&this._runtimes!==void 0){this._withAttachedRuntimeBoundary(()=>this.set(e,t,r));return}this._assertWritablePath("set",e,t,r);const i=this._store;if((((s=this._metaController)==null?void 0:s.size)??0)===0){if(this._derivedController===void 0){i.set(e,t,r);return}this._mutateDerived(i,"set",e,t,r);return}this._mutateWithMetaLifecycle(()=>{this._derivedController===void 0?i.set(e,t,r):this._mutateDerived(i,"set",e,t,r),this._pruneMissingMetaOwners(e,"owner-replaced",r)})}merge(e,t,r){var s;if(this._runtimeMutationDepth===0&&this._runtimes!==void 0){this._withAttachedRuntimeBoundary(()=>this.merge(e,t,r));return}this._assertWritablePath("merge",e,t,r);const i=this._store;if((((s=this._metaController)==null?void 0:s.size)??0)===0){if(this._derivedController===void 0){i.merge(e,t,r);return}this._mutateDerived(i,"merge",e,t,r);return}this._mutateWithMetaLifecycle(()=>{this._derivedController===void 0?i.merge(e,t,r):this._mutateDerived(i,"merge",e,t,r),this._pruneMissingMetaOwners(e,"owner-replaced",r)})}delete(e,t){var i;if(this._runtimeMutationDepth===0&&this._runtimes!==void 0){this._withAttachedRuntimeBoundary(()=>this.delete(e,t));return}this._assertWritablePath("delete",e,void 0,t);const r=this._store;if((((i=this._metaController)==null?void 0:i.size)??0)===0){if(this._derivedController===void 0){r.delete(e,t);return}this._mutateDerived(r,"delete",e,void 0,t);return}r.has(e,t)&&this._mutateWithMetaLifecycle(()=>{this._derivedController===void 0?r.delete(e,t):this._mutateDerived(r,"delete",e,void 0,t);const s=this._metaPath(e,t);this._metaController.deleteSubtree(s,"owner-delete"),this._pruneMissingMetaOwners(s,"owner-delete")})}batch(e){var o;if(this._runtimeMutationDepth===0&&this._runtimes!==void 0)return this._withAttachedRuntimeBoundary(()=>this.batch(e));const t=this._store;if(this._derivedController===void 0&&!((o=this._metaController)!=null&&o.hasWatchers))return t.batch(e);let r,i,s=!1;try{r=t.batch(()=>this._runDerivedBatch(e))}catch(l){s=!0,i=l}if(s)throw i;return r}watch(e,t){return this._store.watch(e,t)}getMeta(e,t,r){var i;return this._store,t!==void 0&&this._assertMetaNamespace(t),(i=this._metaController)==null?void 0:i.get(this._metaPath(e,r),t)}hasMeta(e,t,r){var i;return this._store,t!==void 0&&this._assertMetaNamespace(t),((i=this._metaController)==null?void 0:i.has(this._metaPath(e,r),t))??!1}setMeta(e,t,r,i){if(this._runtimeMutationDepth===0&&this._runtimes!==void 0){this._withAttachedRuntimeBoundary(()=>this.setMeta(e,t,r,i));return}this._assertMetaNamespace(t);const s=this._exactMetaPath(e,i);this._assertMetaOwner(s),this._metadata.set(s,t,r,i)}mergeMeta(e,t,r,i){if(this._runtimeMutationDepth===0&&this._runtimes!==void 0){this._withAttachedRuntimeBoundary(()=>this.mergeMeta(e,t,r,i));return}this._assertMetaNamespace(t);const s=this._exactMetaPath(e,i);this._assertMetaOwner(s),this._metadata.merge(s,t,r,i)}deleteMeta(e,t,r){var s;if(this._runtimeMutationDepth===0&&this._runtimes!==void 0){this._withAttachedRuntimeBoundary(()=>this.deleteMeta(e,t,r));return}t!==void 0&&this._assertMetaNamespace(t);const i=this._exactMetaPath(e,r);(s=this._metaController)==null||s.delete(i,t,r)}_watchMeta(e,t,r={},i){return this._store,r.namespace!==void 0&&this._assertMetaNamespace(r.namespace),this._metadata.watch(e,t,r,i)}derive(e){var d,u,g;if(this._store,e.strategy!==void 0&&e.strategy.kind!=="full"&&e.strategy.kind!=="collection-by-key"&&e.strategy.kind!=="filter-by-key")throw new Error(`[Raph] Unsupported derived strategy "${String(e.strategy.kind)}".`);if(e.id!==void 0&&((d=this._derivedController)!=null&&d.has(e.id)))throw new Error(`[Raph] Duplicate derived id "${e.id}".`);const t=k.parseDataPath(e.from),r=k.parseDataPath(e.to);if(P(t,r))throw new Error("[Raph] Derived source and target must not overlap.");const i=this._derivedController??new I.DerivedRegistry,s=e.id??i.nextId(),o={...e,from:t.canonical,to:r.canonical},l=new M.RaphDerivedHandle(this,s,o),h=((u=e.strategy)==null?void 0:u.kind)==="collection-by-key"||((g=e.strategy)==null?void 0:g.kind)==="filter-by-key"?e.strategy:void 0;this._derivedController=i,i.register(l,t,h);try{e.immediate!==!1&&l.recompute()}catch(a){throw this._removeDerived(l),l._disposeFromKernel(),a}return l}_attachRuntime(e){this._store,(this._runtimes??(this._runtimes=new Set)).add(e)}_detachRuntime(e){const t=this._runtimes;t!==void 0&&(t.delete(e),t.size===0&&(this._runtimes=void 0))}dispose(){var t,r;if(this._paths===void 0)return;const e=[];if(this._runtimes!==void 0)for(const i of[...this._runtimes])try{i.dispose()}catch(s){e.push(s)}if(this._runtimes=void 0,(t=this._derivedController)==null||t.dispose(),this._derivedController=void 0,(r=this._metaController)==null||r.dispose(),this._metaController=void 0,this._paths=void 0,e.length===1)throw e[0];if(e.length>1)throw new AggregateError(e,`[Raph] Kernel "${this.id}" disposal failed.`)}_recomputeDerived(e){if(!(e.paused||e.disposed))try{this._recomputeDerivedFull(e),e._recordSuccess()}catch(t){throw e._recordFailure(t),t}}_removeDerived(e){const t=this._derivedController;t!==void 0&&(t.remove(e),t.size===0&&(this._derivedController=void 0))}get _store(){const e=this._paths;if(e!==void 0)return e;throw new Error(`[Raph] Kernel "${this.id}" is disposed.`)}get _metadata(){return this._store,this._metaController??(this._metaController=new E.MetaController(this,e=>this.has(e),()=>this._batchDepth!==0||this._stabilizing))}_withAttachedRuntimeBoundary(e){var l,h,d;const t=this._runtimes===void 0?[]:[...this._runtimes];if(t.length===0)return e();const r=[],i=[];let s,o=!1;this._runtimeMutationDepth++;try{for(const u of t)(l=u._beginKernelBatch)==null||l.call(u),r.push(u);try{s=e()}catch(u){o=!0,i.push(u)}}catch(u){o=!0,i.push(u)}finally{this._runtimeMutationDepth--;for(let u=r.length-1;u>=0;u--)try{(d=(h=r[u])._endKernelBatch)==null||d.call(h)}catch(g){i.push(g)}}if(i.length===1)throw i[0];if(i.length>1)throw new AggregateError(i,o?"[Raph] Kernel mutation and Runtime publication failed.":"[Raph] Runtime publication failed after a Kernel mutation.");return s}_assertWritablePath(e,t,r,i){const s=this._derivedController;if(s===void 0||this._derivedWriteDepth!==0)return;const o=k.parseDataPath(t,i);for(const l of s.values()){const h=k.parseDataPath(l.options.to);if(!P(o,h))continue;const d=o.segments.length<h.segments.length;if(!(e==="merge"&&d&&!q(r,h.segments[o.segments.length])))throw new M.RaphDerivedTargetWriteError(o.canonical)}}_writeDerivedTarget(e,t){this._derivedWriteDepth++;try{this.set(e,t)}finally{this._derivedWriteDepth--}}_mutateDerived(e,t,r,i,s){const o=k.parseDataPath(r,s);t==="delete"&&!e.has(r,s)||e.batch(()=>{t==="set"?e.set(r,i,s):t==="merge"?e.merge(r,i,s):e.delete(r,s),this._queueDerived(o),this._batchDepth===0&&!this._stabilizing&&this._stabilizeDerived()})}_runDerivedBatch(e){var u;let t=!1,r,i=!1,s,o=!1,l,h;this._batchDepth++;try{h=e()}catch(g){t=!0,r=g}finally{if(this._batchDepth--,this._batchDepth===0){if(!this._stabilizing)try{this._stabilizeDerived()}catch(g){i=!0,s=g}try{(u=this._metaController)==null||u.publish()}catch(g){o=!0,l=g}}}const d=[];if(t&&d.push(r),i&&d.push(s),o&&d.push(l),d.length>1)throw new AggregateError(d,"[Raph] Kernel batch publication failed.");if(d.length===1)throw d[0];return h}_mutateWithMetaLifecycle(e){if(this._batchDepth!==0||this._stabilizing){e();return}this.batch(e)}_pruneMissingMetaOwners(e,t,r){var i;(i=this._metaController)==null||i.pruneMissing(this._metaPath(e,r),t)}_metaPath(e,t){return R.DataPath.from(e,t)}_exactMetaPath(e,t){const r=this._metaPath(e,t);if(r.segments().some(i=>i.kind==="wildcard"))throw new Error("[RaphMeta] wildcard paths are read-only observation masks.");return r}_assertMetaNamespace(e){if(this._store,!e.trim())throw new Error("[RaphMeta] namespace must not be empty.")}_assertMetaOwner(e){if(!this.has(e))throw new Error(`[RaphMeta] Owner data path does not exist: "${e}".`)}_queueDerived(e){var t;(t=this._derivedController)==null||t.queue(e)}_queueDerivedReplacement(e,t){var r;(r=this._derivedController)==null||r.queueReplacement(e,t)}_replaceDerivedTarget(e,t,r){const i=k.parseDataPath(e);this._store.set(i.canonical,t),this._queueDerivedReplacement(i,r)}_stabilizeDerived(){const e=this._derivedController,t=e==null?void 0:e.pending;if(t===void 0||t.size===0)return;this._stabilizing=!0;let r=0;try{for(;t.size!==0;){const i=t.values().next().value;if(t.delete(i),!(i.paused||i.disposed)){try{this._recomputePendingDerived(i),i._recordSuccess()}catch(s){throw i._recordFailure(s),s}if(++r>1e4)throw new Error("[Raph] Derived stabilization exceeded 10000 recomputations.")}}}catch(i){throw t.clear(),i}finally{this._stabilizing=!1,e==null||e.clearPendingIfEmpty()}}_recomputePendingDerived(e){var r;const t=(r=this._derivedController)==null?void 0:r.state(e);if(t===void 0||!t.initialized||t.full){this._recomputeDerivedFull(e);return}t.kind==="filter-by-key"?this._recomputeDerivedFilter(e,t):this._recomputeDerivedCollection(e,t)}_recomputeDerivedFull(e){var h;const t=(h=this._derivedController)==null?void 0:h.state(e);t!==void 0&&(t.full=!0);const r=this.get(e.options.from),i=e.options.compute(r);let s=i,o,l;t!==void 0&&(r===void 0&&(i===void 0||Array.isArray(i)&&i.length===0)?(o=new Map,l=new Map):(t.kind==="filter-by-key"?(f.validateFullFilter(e.id,t.key,r,i),s=f.cloneMaterializedValue(i)):f.validateFullCollection(e.id,t.key,r,i),o=f.collectionIndex(r,t.key,"source"),l=f.collectionIndex(s,t.key,"output"))),this._writeDerivedTarget(e.options.to,s),t!==void 0&&(t.sourceIndex=o,t.targetIndex=l,t.initialized=!0,t.full=!1,t.keys.clear())}_recomputeDerivedCollection(e,t){t.full=!0;const r=this.get(e.options.from),i=this.get(e.options.to);if(!Array.isArray(r)||!Array.isArray(i))throw f.collectionError(e.id,"requires array source and target");(t.sourceIndex===void 0||f.indexesAreStale(r,t.key,t.sourceIndex,t.keys))&&(t.sourceIndex=f.collectionIndex(r,t.key,"source"));const s=t.targetIndex;if(s===void 0)throw f.collectionError(e.id,"has no materialized target index");const o=[],l=[];for(const a of t.keys){const v=t.sourceIndex.get(a);v===void 0?l.push(a):o.push({key:a,index:v,item:r[v]})}o.sort((a,v)=>a.index-v.index);const h=o.map(a=>a.item),d=h.length===0?[]:e.options.compute(h);f.validateIncrementalCollection(e.id,t.key,o.map(a=>a.key),d);const u=d;if(l.length!==0||o.some(a=>!s.has(a.key))){const a=new Map;for(let _=0;_<o.length;_++)a.set(o[_].key,u[_]);const v=new Map;for(const[_,y]of s)v.set(_,i[y]);const n=f.collectionKeys(r,t.key,"source"),c=[];c.length=n.length;for(let _=0;_<n.length;_++){const y=n[_];if(y===void 0)continue;const D=a.has(y)?a.get(y):v.get(y);if(D===void 0)throw f.collectionError(e.id,`is missing target item for key "${String(y)}"`);c[_]=D}const w=f.collectionIndex(c,t.key,"target");this._replaceDerivedTarget(e.options.to,c,t.keys),t.targetIndex=w}else for(let a=0;a<o.length;a++)this._writeDerivedTarget(f.keyedPath(e.options.to,t.key,o[a].key),u[a]);t.keys.clear(),t.full=!1}_recomputeDerivedFilter(e,t){t.full=!0;const r=this.get(e.options.from),i=this.get(e.options.to);if(!Array.isArray(r)||!Array.isArray(i))throw f.filterError(e.id,"requires array source and target");(t.sourceIndex===void 0||f.indexesAreStale(r,t.key,t.sourceIndex,t.keys))&&(t.sourceIndex=f.collectionIndex(r,t.key,"source"));const s=t.targetIndex;if(s===void 0)throw f.filterError(e.id,"has no materialized target index");const o=[];for(const n of t.keys){const c=t.sourceIndex.get(n);c!==void 0&&o.push({key:n,index:c,item:r[c]})}o.sort((n,c)=>n.index-c.index);const l=o.map(n=>n.item),h=l.length===0?[]:e.options.compute(l);f.validateIncrementalFilter(e.id,t.key,o.map(n=>n.key),h);const d=new Map,u=h,g=f.collectionKeys(u,t.key,"output");for(let n=0;n<g.length;n++){const c=g[n];c!==void 0&&d.set(c,f.cloneMaterializedValue(u[n]))}const a=new Set;let v=!1;for(const n of t.keys){const c=s.has(n),w=d.has(n);!c&&!w||(a.add(n),c!==w&&(v=!0))}if(v&&t.keys.size===1&&i.length===s.size&&this._patchSingleFilterMembership(e,t,i,s,d,a)){t.keys.clear(),t.full=!1;return}if(v){const n=new Map;for(const[_,y]of s)n.set(_,i[y]);const c=[];for(const _ of f.collectionKeys(r,t.key,"source"))if(_!==void 0){if(t.keys.has(_)){d.has(_)&&c.push(d.get(_));continue}n.has(_)&&c.push(n.get(_))}const w=f.collectionIndex(c,t.key,"target");this._replaceDerivedTarget(e.options.to,c,a),t.targetIndex=w}else for(const[n,c]of d)this._writeDerivedTarget(f.keyedPath(e.options.to,t.key,n),c);t.keys.clear(),t.full=!1}_patchSingleFilterMembership(e,t,r,i,s,o){const l=t.sourceIndex;if(l===void 0)return!1;const h=t.keys.values().next().value;if(h===void 0)return!1;const d=i.get(h),u=s.has(h);if(d!==void 0===u)return!1;const g=r.slice();let a;if(d!==void 0)a=d,g.splice(a,1);else{const v=l.get(h);if(v===void 0)return!1;let n=0,c=g.length;for(;n<c;){const w=n+c>>>1,_=g[w];if(typeof _!="object"||_===null||Array.isArray(_))return!1;const y=_[t.key];if(typeof y!="string"&&typeof y!="number")return!1;const D=l.get(y);if(D===void 0)return!1;D<v?n=w+1:c=w}a=n,g.splice(a,0,s.get(h))}if(this._replaceDerivedTarget(e.options.to,g,o),d!==void 0){i.delete(h);for(const[v,n]of i)n>a&&i.set(v,n-1)}else{for(const[v,n]of i)n>=a&&i.set(v,n+1);i.set(h,a)}return!0}};p(b,"_kernelCounter",0);let x=b;function P(m,e){const t=Math.min(m.segments.length,e.segments.length);for(let r=0;r<t;r++)if(!O(m.segments[r],e.segments[r]))return!1;return!0}function q(m,e){return typeof m!="object"||m===null||Array.isArray(m)||e.kind!=="key"?!0:Object.hasOwn(m,e.key)}function O(m,e){return m.kind!==e.kind?!1:m.kind==="key"&&e.kind==="key"?m.key===e.key:m.kind==="index"&&e.kind==="index"?m.index===e.index:m.kind==="selector"&&e.kind==="selector"&&m.key===e.key&&Object.is(m.value,e.value)}exports.RaphKernel=x;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const w=require("./data-path.utils.cjs"),C=require("./DataPath.cjs"),_=require("./derived/derived-collection.utils.cjs"),R=require("./derived/DerivedRegistry.cjs"),b=require("./derived/RaphDerivedHandle.cjs"),P=require("./meta/MetaController.cjs"),M=require("./storage/AdapterPathStore.cjs"),z=require("./storage/DefaultDataAdapter.cjs"),A=require("./storage/PathStore.cjs");class D{static _kernelCounter=0;id;_paths;_derivedController;_runtimes;_metaController;_soleRuntime;_pathBatch;_batchDepth=0;_runtimeMutationDepth=0;_stabilizing=!1;_derivedWriteDepth=0;constructor(e={}){if(this.id=e.id??`kernel-${D._kernelCounter++}`,e.adapter!==void 0){if(e.initial!==void 0||e.adapterOptions!==void 0)throw new TypeError("[Raph] A custom adapter cannot be combined with initial or adapterOptions.");this._paths=new M.AdapterPathStore(e.adapter)}else if(e.adapterOptions!==void 0)this._paths=new M.AdapterPathStore(new z.DefaultDataAdapter(e.initial,e.adapterOptions));else{const t=this._paths=new A.PathStore;this._pathBatch=t._batchHub,e.initial!==void 0&&t.set("",e.initial)}}get disposed(){return this._paths===void 0}getDerivedSnapshot(){const e=this._derivedController;return{registrations:e?.size??0,errors:[...e?.values()??[]].filter(t=>t.status==="error").length,pending:e?.pending?.size??0,stabilizing:this._stabilizing}}get meta(){return this._metadata.facade}get(e,t){return this._store.get(e??"",t)}peek(e,t){return this._store.peek(e??"",t)}has(e,t){return this._store.has(e,t)}set(e,t,r){if(this._runtimeMutationDepth===0&&this._runtimes!==void 0){this._withAttachedRuntimeBoundary(()=>this.set(e,t,r));return}this._assertWritablePath("set",e,t,r);const i=this._store;if((this._metaController?.size??0)===0){if(this._derivedController===void 0){i.set(e,t,r);return}this._mutateDerived(i,"set",e,t,r);return}this._mutateWithMetaLifecycle(()=>{this._derivedController===void 0?i.set(e,t,r):this._mutateDerived(i,"set",e,t,r),this._pruneMissingMetaOwners(e,"owner-replaced",r)})}merge(e,t,r){if(this._runtimeMutationDepth===0&&this._runtimes!==void 0){this._withAttachedRuntimeBoundary(()=>this.merge(e,t,r));return}this._assertWritablePath("merge",e,t,r);const i=this._store;if((this._metaController?.size??0)===0){if(this._derivedController===void 0){i.merge(e,t,r);return}this._mutateDerived(i,"merge",e,t,r);return}this._mutateWithMetaLifecycle(()=>{this._derivedController===void 0?i.merge(e,t,r):this._mutateDerived(i,"merge",e,t,r),this._pruneMissingMetaOwners(e,"owner-replaced",r)})}delete(e,t){if(this._runtimeMutationDepth===0&&this._runtimes!==void 0){this._withAttachedRuntimeBoundary(()=>this.delete(e,t));return}this._assertWritablePath("delete",e,void 0,t);const r=this._store;if((this._metaController?.size??0)===0){if(this._derivedController===void 0){r.delete(e,t);return}this._mutateDerived(r,"delete",e,void 0,t);return}r.has(e,t)&&this._mutateWithMetaLifecycle(()=>{this._derivedController===void 0?r.delete(e,t):this._mutateDerived(r,"delete",e,void 0,t);const i=this._metaPath(e,t);this._metaController.deleteSubtree(i,"owner-delete"),this._pruneMissingMetaOwners(i,"owner-delete")})}batch(e){if(this._runtimeMutationDepth===0&&this._runtimes!==void 0)return this._withAttachedRuntimeBoundary(()=>this.batch(e));const t=this._store;if(this._derivedController===void 0&&!this._metaController?.hasWatchers)return t.batch(e);let r,i,s=!1;try{r=t.batch(()=>this._runDerivedBatch(e))}catch(o){s=!0,i=o}if(s)throw i;return r}_openSoleRuntimeBatch(e){const t=this._pathBatch;if(!(this._soleRuntime!==e||t===void 0||this._runtimeMutationDepth!==0||this._derivedController!==void 0||this._metaController?.hasWatchers===!0))return this._runtimeMutationDepth++,e._beginKernelBatch?.(),t._openBatch(),t}_closeSoleRuntimeBatch(e,t,r,i){let s=r,o=i;try{t._closeBatch(r,i)}catch(n){s=!0,o=n}this._runtimeMutationDepth--;try{e._endKernelBatch?.()}catch(n){throw s?new AggregateError([o,n],"[Raph] Kernel mutation and Runtime publication failed."):n}if(s)throw o}watch(e,t){return this._store.watch(e,t)}getMeta(e,t,r){return this._store,t!==void 0&&this._assertMetaNamespace(t),this._metaController?.get(this._metaPath(e,r),t)}hasMeta(e,t,r){return this._store,t!==void 0&&this._assertMetaNamespace(t),this._metaController?.has(this._metaPath(e,r),t)??!1}setMeta(e,t,r,i){if(this._runtimeMutationDepth===0&&this._runtimes!==void 0){this._withAttachedRuntimeBoundary(()=>this.setMeta(e,t,r,i));return}this._assertMetaNamespace(t);const s=this._exactMetaPath(e,i);this._assertMetaOwner(s),this._metadata.set(s,t,r,i)}mergeMeta(e,t,r,i){if(this._runtimeMutationDepth===0&&this._runtimes!==void 0){this._withAttachedRuntimeBoundary(()=>this.mergeMeta(e,t,r,i));return}this._assertMetaNamespace(t);const s=this._exactMetaPath(e,i);this._assertMetaOwner(s),this._metadata.merge(s,t,r,i)}deleteMeta(e,t,r){if(this._runtimeMutationDepth===0&&this._runtimes!==void 0){this._withAttachedRuntimeBoundary(()=>this.deleteMeta(e,t,r));return}t!==void 0&&this._assertMetaNamespace(t);const i=this._exactMetaPath(e,r);this._metaController?.delete(i,t,r)}_watchMeta(e,t,r={},i){return this._store,r.namespace!==void 0&&this._assertMetaNamespace(r.namespace),this._metadata.watch(e,t,r,i)}derive(e){if(this._store,e.strategy!==void 0&&e.strategy.kind!=="full"&&e.strategy.kind!=="collection-by-key"&&e.strategy.kind!=="filter-by-key")throw new Error(`[Raph] Unsupported derived strategy "${String(e.strategy.kind)}".`);if(e.id!==void 0&&this._derivedController?.has(e.id))throw new Error(`[Raph] Duplicate derived id "${e.id}".`);const t=w.parseDataPath(e.from),r=w.parseDataPath(e.to);if(x(t,r))throw new Error("[Raph] Derived source and target must not overlap.");const i=this._derivedController??new R.DerivedRegistry,s=e.id??i.nextId(),o={...e,from:t.canonical,to:r.canonical},n=new b.RaphDerivedHandle(this,s,o),c=e.strategy?.kind==="collection-by-key"||e.strategy?.kind==="filter-by-key"?e.strategy:void 0;this._derivedController=i,i.register(n,t,c);try{e.immediate!==!1&&n.recompute()}catch(h){throw this._removeDerived(n),n._disposeFromKernel(),h}return n}_attachRuntime(e){this._store;const t=this._runtimes??=new Set;t.add(e),this._soleRuntime=t.size===1?e:void 0}_detachRuntime(e){const t=this._runtimes;t!==void 0&&(t.delete(e),t.size===0&&(this._runtimes=void 0),this._soleRuntime=t.size===1?t.values().next().value:void 0)}dispose(){if(this._paths===void 0)return;const e=[];if(this._runtimes!==void 0)for(const t of[...this._runtimes])try{t.dispose()}catch(r){e.push(r)}if(this._runtimes=void 0,this._soleRuntime=void 0,this._derivedController?.dispose(),this._derivedController=void 0,this._metaController?.dispose(),this._metaController=void 0,this._paths=void 0,this._pathBatch=void 0,e.length===1)throw e[0];if(e.length>1)throw new AggregateError(e,`[Raph] Kernel "${this.id}" disposal failed.`)}_recomputeDerived(e){if(!(e.paused||e.disposed))try{this._recomputeDerivedFull(e),e._recordSuccess()}catch(t){throw e._recordFailure(t),t}}_removeDerived(e){const t=this._derivedController;t!==void 0&&(t.remove(e),t.size===0&&(this._derivedController=void 0))}get _store(){const e=this._paths;if(e!==void 0)return e;throw new Error(`[Raph] Kernel "${this.id}" is disposed.`)}get _metadata(){return this._store,this._metaController??=new P.MetaController(this,e=>this.has(e),()=>this._batchDepth!==0||this._stabilizing)}_withAttachedRuntimeBoundary(e){const t=this._runtimes===void 0?[]:[...this._runtimes];if(t.length===0)return e();const r=[],i=[];let s,o=!1;this._runtimeMutationDepth++;try{for(const n of t)n._beginKernelBatch?.(),r.push(n);try{s=e()}catch(n){o=!0,i.push(n)}}catch(n){o=!0,i.push(n)}finally{this._runtimeMutationDepth--;for(let n=r.length-1;n>=0;n--)try{r[n]._endKernelBatch?.()}catch(c){i.push(c)}}if(i.length===1)throw i[0];if(i.length>1)throw new AggregateError(i,o?"[Raph] Kernel mutation and Runtime publication failed.":"[Raph] Runtime publication failed after a Kernel mutation.");return s}_assertWritablePath(e,t,r,i){const s=this._derivedController;if(s===void 0||this._derivedWriteDepth!==0)return;const o=w.parseDataPath(t,i);for(const n of s.values()){const c=w.parseDataPath(n.options.to);if(!x(o,c))continue;const h=o.segments.length<c.segments.length;if(!(e==="merge"&&h&&!I(r,c.segments[o.segments.length])))throw new b.RaphDerivedTargetWriteError(o.canonical)}}_writeDerivedTarget(e,t){this._derivedWriteDepth++;try{this.set(e,t)}finally{this._derivedWriteDepth--}}_mutateDerived(e,t,r,i,s){const o=w.parseDataPath(r,s);t==="delete"&&!e.has(r,s)||e.batch(()=>{t==="set"?e.set(r,i,s):t==="merge"?e.merge(r,i,s):e.delete(r,s),this._queueDerived(o),this._batchDepth===0&&!this._stabilizing&&this._stabilizeDerived()})}_runDerivedBatch(e){let t=!1,r,i=!1,s,o=!1,n,c;this._batchDepth++;try{c=e()}catch(v){t=!0,r=v}finally{if(this._batchDepth--,this._batchDepth===0){if(!this._stabilizing)try{this._stabilizeDerived()}catch(v){i=!0,s=v}try{this._metaController?.publish()}catch(v){o=!0,n=v}}}const h=[];if(t&&h.push(r),i&&h.push(s),o&&h.push(n),h.length>1)throw new AggregateError(h,"[Raph] Kernel batch publication failed.");if(h.length===1)throw h[0];return c}_mutateWithMetaLifecycle(e){if(this._batchDepth!==0||this._stabilizing){e();return}this.batch(e)}_pruneMissingMetaOwners(e,t,r){this._metaController?.pruneMissing(this._metaPath(e,r),t)}_metaPath(e,t){return C.DataPath.from(e,t)}_exactMetaPath(e,t){const r=this._metaPath(e,t);if(r.segments().some(i=>i.kind==="wildcard"))throw new Error("[RaphMeta] wildcard paths are read-only observation masks.");return r}_assertMetaNamespace(e){if(this._store,!e.trim())throw new Error("[RaphMeta] namespace must not be empty.")}_assertMetaOwner(e){if(!this.has(e))throw new Error(`[RaphMeta] Owner data path does not exist: "${e}".`)}_queueDerived(e){this._derivedController?.queue(e)}_queueDerivedReplacement(e,t){this._derivedController?.queueReplacement(e,t)}_replaceDerivedTarget(e,t,r){const i=w.parseDataPath(e);this._store.set(i.canonical,t),this._queueDerivedReplacement(i,r)}_stabilizeDerived(){const e=this._derivedController,t=e?.pending;if(t===void 0||t.size===0)return;this._stabilizing=!0;let r=0;try{for(;t.size!==0;){const i=t.values().next().value;if(t.delete(i),!(i.paused||i.disposed)){try{this._recomputePendingDerived(i),i._recordSuccess()}catch(s){throw i._recordFailure(s),s}if(++r>1e4)throw new Error("[Raph] Derived stabilization exceeded 10000 recomputations.")}}}catch(i){throw t.clear(),i}finally{this._stabilizing=!1,e?.clearPendingIfEmpty()}}_recomputePendingDerived(e){const t=this._derivedController?.state(e);if(t===void 0||!t.initialized||t.full){this._recomputeDerivedFull(e);return}t.kind==="filter-by-key"?this._recomputeDerivedFilter(e,t):this._recomputeDerivedCollection(e,t)}_recomputeDerivedFull(e){const t=this._derivedController?.state(e);t!==void 0&&(t.full=!0);const r=this.get(e.options.from),i=e.options.compute(r);let s=i,o,n;t!==void 0&&(r===void 0&&(i===void 0||Array.isArray(i)&&i.length===0)?(o=new Map,n=new Map):(t.kind==="filter-by-key"?(_.validateFullFilter(e.id,t.key,r,i),s=_.cloneMaterializedValue(i)):_.validateFullCollection(e.id,t.key,r,i),o=_.collectionIndex(r,t.key,"source"),n=_.collectionIndex(s,t.key,"output"))),this._writeDerivedTarget(e.options.to,s),t!==void 0&&(t.sourceIndex=o,t.targetIndex=n,t.initialized=!0,t.full=!1,t.keys.clear())}_recomputeDerivedCollection(e,t){t.full=!0;const r=this.get(e.options.from),i=this.get(e.options.to);if(!Array.isArray(r)||!Array.isArray(i))throw _.collectionError(e.id,"requires array source and target");(t.sourceIndex===void 0||_.indexesAreStale(r,t.key,t.sourceIndex,t.keys))&&(t.sourceIndex=_.collectionIndex(r,t.key,"source"));const s=t.targetIndex;if(s===void 0)throw _.collectionError(e.id,"has no materialized target index");const o=[],n=[];for(const d of t.keys){const f=t.sourceIndex.get(d);f===void 0?n.push(d):o.push({key:d,index:f,item:r[f]})}o.sort((d,f)=>d.index-f.index);const c=o.map(d=>d.item),h=c.length===0?[]:e.options.compute(c);_.validateIncrementalCollection(e.id,t.key,o.map(d=>d.key),h);const v=h;if(n.length!==0||o.some(d=>!s.has(d.key))){const d=new Map;for(let u=0;u<o.length;u++)d.set(o[u].key,v[u]);const f=new Map;for(const[u,g]of s)f.set(u,i[g]);const a=_.collectionKeys(r,t.key,"source"),l=[];l.length=a.length;for(let u=0;u<a.length;u++){const g=a[u];if(g===void 0)continue;const k=d.has(g)?d.get(g):f.get(g);if(k===void 0)throw _.collectionError(e.id,`is missing target item for key "${String(g)}"`);l[u]=k}const y=_.collectionIndex(l,t.key,"target");this._replaceDerivedTarget(e.options.to,l,t.keys),t.targetIndex=y}else for(let d=0;d<o.length;d++)this._writeDerivedTarget(_.keyedPath(e.options.to,t.key,o[d].key),v[d]);t.keys.clear(),t.full=!1}_recomputeDerivedFilter(e,t){t.full=!0;const r=this.get(e.options.from),i=this.get(e.options.to);if(!Array.isArray(r)||!Array.isArray(i))throw _.filterError(e.id,"requires array source and target");(t.sourceIndex===void 0||_.indexesAreStale(r,t.key,t.sourceIndex,t.keys))&&(t.sourceIndex=_.collectionIndex(r,t.key,"source"));const s=t.targetIndex;if(s===void 0)throw _.filterError(e.id,"has no materialized target index");const o=[];for(const a of t.keys){const l=t.sourceIndex.get(a);l!==void 0&&o.push({key:a,index:l,item:r[l]})}o.sort((a,l)=>a.index-l.index);const n=o.map(a=>a.item),c=n.length===0?[]:e.options.compute(n);_.validateIncrementalFilter(e.id,t.key,o.map(a=>a.key),c);const h=new Map,v=c,p=_.collectionKeys(v,t.key,"output");for(let a=0;a<p.length;a++){const l=p[a];l!==void 0&&h.set(l,_.cloneMaterializedValue(v[a]))}const d=new Set;let f=!1;for(const a of t.keys){const l=s.has(a),y=h.has(a);!l&&!y||(d.add(a),l!==y&&(f=!0))}if(f&&t.keys.size===1&&i.length===s.size&&this._patchSingleFilterMembership(e,t,i,s,h,d)){t.keys.clear(),t.full=!1;return}if(f){const a=new Map;for(const[u,g]of s)a.set(u,i[g]);const l=[];for(const u of _.collectionKeys(r,t.key,"source"))if(u!==void 0){if(t.keys.has(u)){h.has(u)&&l.push(h.get(u));continue}a.has(u)&&l.push(a.get(u))}const y=_.collectionIndex(l,t.key,"target");this._replaceDerivedTarget(e.options.to,l,d),t.targetIndex=y}else for(const[a,l]of h)this._writeDerivedTarget(_.keyedPath(e.options.to,t.key,a),l);t.keys.clear(),t.full=!1}_patchSingleFilterMembership(e,t,r,i,s,o){const n=t.sourceIndex;if(n===void 0)return!1;const c=t.keys.values().next().value;if(c===void 0)return!1;const h=i.get(c),v=s.has(c);if(h!==void 0===v)return!1;const p=r.slice();let d;if(h!==void 0)d=h,p.splice(d,1);else{const f=n.get(c);if(f===void 0)return!1;let a=0,l=p.length;for(;a<l;){const y=a+l>>>1,u=p[y];if(typeof u!="object"||u===null||Array.isArray(u))return!1;const g=u[t.key];if(typeof g!="string"&&typeof g!="number")return!1;const k=n.get(g);if(k===void 0)return!1;k<f?a=y+1:l=y}d=a,p.splice(d,0,s.get(c))}if(this._replaceDerivedTarget(e.options.to,p,o),h!==void 0){i.delete(c);for(const[f,a]of i)a>d&&i.set(f,a-1)}else{for(const[f,a]of i)a>=d&&i.set(f,a+1);i.set(c,d)}return!0}}function x(m,e){const t=Math.min(m.segments.length,e.segments.length);for(let r=0;r<t;r++)if(!E(m.segments[r],e.segments[r]))return!1;return!0}function I(m,e){return typeof m!="object"||m===null||Array.isArray(m)||e.kind!=="key"?!0:Object.hasOwn(m,e.key)}function E(m,e){return m.kind!==e.kind?!1:m.kind==="key"&&e.kind==="key"?m.key===e.key:m.kind==="index"&&e.kind==="index"?m.index===e.index:m.kind==="selector"&&e.kind==="selector"&&m.key===e.key&&Object.is(m.value,e.value)}exports.RaphKernel=D;