mutts 1.0.1 → 1.0.3

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 (111) hide show
  1. package/README.md +36 -6
  2. package/dist/chunks/_tslib-BgjropY9.js +81 -0
  3. package/dist/chunks/_tslib-BgjropY9.js.map +1 -0
  4. package/dist/chunks/_tslib-Mzh1rNsX.esm.js +75 -0
  5. package/dist/chunks/_tslib-Mzh1rNsX.esm.js.map +1 -0
  6. package/dist/chunks/{decorator-8qjFb7dw.js → decorator-DLvrD0UF.js} +103 -14
  7. package/dist/chunks/decorator-DLvrD0UF.js.map +1 -0
  8. package/dist/chunks/{decorator-AbRkXM5O.esm.js → decorator-DqiszP7i.esm.js} +100 -15
  9. package/dist/chunks/decorator-DqiszP7i.esm.js.map +1 -0
  10. package/dist/chunks/index-DzUDtFc7.esm.js +4841 -0
  11. package/dist/chunks/index-DzUDtFc7.esm.js.map +1 -0
  12. package/dist/chunks/index-HNVqPzjz.js +4891 -0
  13. package/dist/chunks/index-HNVqPzjz.js.map +1 -0
  14. package/dist/decorator.d.ts +57 -0
  15. package/dist/decorator.esm.js +1 -1
  16. package/dist/decorator.js +1 -1
  17. package/dist/destroyable.d.ts +43 -1
  18. package/dist/destroyable.esm.js +19 -1
  19. package/dist/destroyable.esm.js.map +1 -1
  20. package/dist/destroyable.js +19 -1
  21. package/dist/destroyable.js.map +1 -1
  22. package/dist/devtools/devtools.html +9 -0
  23. package/dist/devtools/devtools.js +5 -0
  24. package/dist/devtools/devtools.js.map +1 -0
  25. package/dist/devtools/manifest.json +8 -0
  26. package/dist/devtools/panel.css +72 -0
  27. package/dist/devtools/panel.html +31 -0
  28. package/dist/devtools/panel.js +13048 -0
  29. package/dist/devtools/panel.js.map +1 -0
  30. package/dist/eventful.d.ts +10 -1
  31. package/dist/eventful.esm.js +5 -27
  32. package/dist/eventful.esm.js.map +1 -1
  33. package/dist/eventful.js +15 -37
  34. package/dist/eventful.js.map +1 -1
  35. package/dist/index.d.ts +18 -14
  36. package/dist/index.esm.js +4 -3
  37. package/dist/index.esm.js.map +1 -1
  38. package/dist/index.js +44 -5
  39. package/dist/index.js.map +1 -1
  40. package/dist/indexable.d.ts +213 -1
  41. package/dist/indexable.esm.js +203 -3
  42. package/dist/indexable.esm.js.map +1 -1
  43. package/dist/indexable.js +204 -2
  44. package/dist/indexable.js.map +1 -1
  45. package/dist/mutts.umd.js +1 -1
  46. package/dist/mutts.umd.js.map +1 -1
  47. package/dist/mutts.umd.min.js +1 -1
  48. package/dist/mutts.umd.min.js.map +1 -1
  49. package/dist/promiseChain.d.ts +10 -0
  50. package/dist/promiseChain.esm.js +6 -0
  51. package/dist/promiseChain.esm.js.map +1 -1
  52. package/dist/promiseChain.js +6 -0
  53. package/dist/promiseChain.js.map +1 -1
  54. package/dist/reactive.d.ts +774 -33
  55. package/dist/reactive.esm.js +4 -1458
  56. package/dist/reactive.esm.js.map +1 -1
  57. package/dist/reactive.js +53 -1474
  58. package/dist/reactive.js.map +1 -1
  59. package/dist/std-decorators.d.ts +35 -0
  60. package/dist/std-decorators.esm.js +36 -1
  61. package/dist/std-decorators.esm.js.map +1 -1
  62. package/dist/std-decorators.js +36 -1
  63. package/dist/std-decorators.js.map +1 -1
  64. package/docs/ai/api-reference.md +133 -0
  65. package/docs/ai/manual.md +105 -0
  66. package/docs/iterableWeak.md +646 -0
  67. package/docs/mixin.md +229 -0
  68. package/docs/reactive/advanced.md +1280 -0
  69. package/docs/reactive/collections.md +767 -0
  70. package/docs/reactive/core.md +973 -0
  71. package/docs/reactive.md +21 -2688
  72. package/package.json +18 -5
  73. package/src/decorator.ts +266 -0
  74. package/src/destroyable.ts +199 -0
  75. package/src/eventful.ts +77 -0
  76. package/src/index.d.ts +9 -0
  77. package/src/index.ts +9 -0
  78. package/src/indexable.ts +484 -0
  79. package/src/introspection.ts +59 -0
  80. package/src/iterableWeak.ts +233 -0
  81. package/src/mixins.ts +123 -0
  82. package/src/promiseChain.ts +110 -0
  83. package/src/reactive/array.ts +414 -0
  84. package/src/reactive/change.ts +134 -0
  85. package/src/reactive/debug.ts +517 -0
  86. package/src/reactive/deep-touch.ts +268 -0
  87. package/src/reactive/deep-watch-state.ts +82 -0
  88. package/src/reactive/deep-watch.ts +168 -0
  89. package/src/reactive/effect-context.ts +94 -0
  90. package/src/reactive/effects.ts +1333 -0
  91. package/src/reactive/index.ts +75 -0
  92. package/src/reactive/interface.ts +223 -0
  93. package/src/reactive/map.ts +171 -0
  94. package/src/reactive/mapped.ts +130 -0
  95. package/src/reactive/memoize.ts +107 -0
  96. package/src/reactive/non-reactive-state.ts +49 -0
  97. package/src/reactive/non-reactive.ts +43 -0
  98. package/src/reactive/project.project.md +93 -0
  99. package/src/reactive/project.ts +335 -0
  100. package/src/reactive/proxy-state.ts +27 -0
  101. package/src/reactive/proxy.ts +285 -0
  102. package/src/reactive/record.ts +196 -0
  103. package/src/reactive/register.ts +421 -0
  104. package/src/reactive/set.ts +144 -0
  105. package/src/reactive/tracking.ts +101 -0
  106. package/src/reactive/types.ts +358 -0
  107. package/src/reactive/zone.ts +208 -0
  108. package/src/std-decorators.ts +217 -0
  109. package/src/utils.ts +117 -0
  110. package/dist/chunks/decorator-8qjFb7dw.js.map +0 -1
  111. package/dist/chunks/decorator-AbRkXM5O.esm.js.map +0 -1
@@ -0,0 +1,4841 @@
1
+ import { i as isConstructor, R as ReflectGet, d as decorator, b as ReflectSet, c as isOwnAccessor, r as renamed } from './decorator-DqiszP7i.esm.js';
2
+ import { Indexable, setAt, getAt, ArrayReadForward, forwardArray } from '../indexable.esm.js';
3
+ import { a as __setFunctionName, b as __esDecorate, c as __runInitializers, d as __classPrivateFieldSet, _ as __classPrivateFieldGet } from './_tslib-Mzh1rNsX.esm.js';
4
+
5
+ /// <reference lib="esnext.collection" />
6
+ var _a, _b;
7
+ /**
8
+ * Uses weak references but still may iterate through them
9
+ * Note: The behavior is highly dependant on the garbage collector - some entries are perhaps deemed to be collected: don't resuscitate them
10
+ */
11
+ class IterableWeakMap {
12
+ constructor(entries) {
13
+ this.uuids = new WeakMap();
14
+ this.refs = {};
15
+ this[_a] = 'IterableWeakMap';
16
+ // Create a FinalizationRegistry to clean up refs when keys are garbage collected
17
+ this.registry = new FinalizationRegistry((uuid) => {
18
+ delete this.refs[uuid];
19
+ });
20
+ if (entries)
21
+ for (const [k, v] of entries)
22
+ this.set(k, v);
23
+ }
24
+ createIterator(cb) {
25
+ const { refs } = this;
26
+ return (function* () {
27
+ for (const uuid of Object.keys(refs)) {
28
+ const [keyRef, value] = refs[uuid];
29
+ const key = keyRef.deref();
30
+ if (key)
31
+ yield cb(key, value);
32
+ else
33
+ delete refs[uuid];
34
+ }
35
+ return undefined;
36
+ })();
37
+ }
38
+ clear() {
39
+ // Unregister all keys from the FinalizationRegistry
40
+ for (const uuid of Object.keys(this.refs)) {
41
+ const key = this.refs[uuid][0].deref();
42
+ if (key)
43
+ this.registry.unregister(key);
44
+ }
45
+ this.uuids = new WeakMap();
46
+ this.refs = {};
47
+ }
48
+ delete(key) {
49
+ const uuid = this.uuids.get(key);
50
+ if (!uuid)
51
+ return false;
52
+ delete this.refs[uuid];
53
+ this.uuids.delete(key);
54
+ this.registry.unregister(key);
55
+ return true;
56
+ }
57
+ forEach(callbackfn, thisArg) {
58
+ for (const [k, v] of this)
59
+ callbackfn.call(thisArg ?? this, v, k, thisArg ?? this);
60
+ }
61
+ get(key) {
62
+ const uuid = this.uuids.get(key);
63
+ if (!uuid)
64
+ return undefined;
65
+ return this.refs[uuid][1];
66
+ }
67
+ has(key) {
68
+ return this.uuids.has(key);
69
+ }
70
+ set(key, value) {
71
+ let uuid = this.uuids.get(key);
72
+ if (uuid) {
73
+ this.refs[uuid][1] = value;
74
+ }
75
+ else {
76
+ uuid = crypto.randomUUID();
77
+ this.uuids.set(key, uuid);
78
+ this.refs[uuid] = [new WeakRef(key), value];
79
+ // Register key for cleanup when garbage collected
80
+ this.registry.register(key, uuid, key);
81
+ }
82
+ return this;
83
+ }
84
+ get size() {
85
+ return [...this].length;
86
+ }
87
+ entries() {
88
+ return this.createIterator((key, value) => [key, value]);
89
+ }
90
+ keys() {
91
+ return this.createIterator((key, _value) => key);
92
+ }
93
+ values() {
94
+ return this.createIterator((_key, value) => value);
95
+ }
96
+ [Symbol.iterator]() {
97
+ return this.entries();
98
+ }
99
+ }
100
+ _a = Symbol.toStringTag;
101
+ /**
102
+ * Uses weak references but still may iterate through them
103
+ * Note: The behavior is highly dependant on the garbage collector - some entries are perhaps deemed to be collected: don't resuscitate them
104
+ */
105
+ class IterableWeakSet {
106
+ constructor(entries) {
107
+ this.uuids = new WeakMap();
108
+ this.refs = {};
109
+ this[_b] = 'IterableWeakSet';
110
+ // Create a FinalizationRegistry to clean up refs when values are garbage collected
111
+ this.registry = new FinalizationRegistry((uuid) => {
112
+ delete this.refs[uuid];
113
+ });
114
+ if (entries)
115
+ for (const k of entries)
116
+ this.add(k);
117
+ }
118
+ createIterator(cb) {
119
+ const { refs } = this;
120
+ return (function* () {
121
+ for (const uuid of Object.keys(refs)) {
122
+ const key = refs[uuid].deref();
123
+ if (key)
124
+ yield cb(key);
125
+ else
126
+ delete refs[uuid];
127
+ }
128
+ return undefined;
129
+ })();
130
+ }
131
+ clear() {
132
+ // Unregister all values from the FinalizationRegistry
133
+ for (const uuid of Object.keys(this.refs)) {
134
+ const value = this.refs[uuid].deref();
135
+ if (value)
136
+ this.registry.unregister(value);
137
+ }
138
+ this.uuids = new WeakMap();
139
+ this.refs = {};
140
+ }
141
+ add(value) {
142
+ let uuid = this.uuids.get(value);
143
+ if (!uuid) {
144
+ uuid = crypto.randomUUID();
145
+ this.uuids.set(value, uuid);
146
+ this.refs[uuid] = new WeakRef(value);
147
+ // Register value for cleanup when garbage collected
148
+ this.registry.register(value, uuid, value);
149
+ }
150
+ return this;
151
+ }
152
+ delete(value) {
153
+ const uuid = this.uuids.get(value);
154
+ if (!uuid)
155
+ return false;
156
+ delete this.refs[uuid];
157
+ this.uuids.delete(value);
158
+ this.registry.unregister(value);
159
+ return true;
160
+ }
161
+ forEach(callbackfn, thisArg) {
162
+ for (const value of this)
163
+ callbackfn.call(thisArg ?? this, value, value, thisArg ?? this);
164
+ }
165
+ has(value) {
166
+ return this.uuids.has(value);
167
+ }
168
+ get size() {
169
+ return [...this].length;
170
+ }
171
+ entries() {
172
+ return this.createIterator((key) => [key, key]);
173
+ }
174
+ keys() {
175
+ return this.createIterator((key) => key);
176
+ }
177
+ values() {
178
+ return this.createIterator((key) => key);
179
+ }
180
+ [Symbol.iterator]() {
181
+ return this.keys();
182
+ }
183
+ union(other) {
184
+ const others = {
185
+ [Symbol.iterator]() {
186
+ return other.keys();
187
+ },
188
+ };
189
+ const that = this;
190
+ return new Set((function* () {
191
+ yield* that;
192
+ for (const value of others)
193
+ if (!that.has(value))
194
+ yield value;
195
+ })());
196
+ }
197
+ intersection(other) {
198
+ const that = this;
199
+ return new Set((function* () {
200
+ for (const value of that)
201
+ if (other.has(value))
202
+ yield value;
203
+ })());
204
+ }
205
+ difference(other) {
206
+ const that = this;
207
+ return new Set((function* () {
208
+ for (const value of that)
209
+ if (!other.has(value))
210
+ yield value;
211
+ })());
212
+ }
213
+ symmetricDifference(other) {
214
+ const others = {
215
+ [Symbol.iterator]() {
216
+ return other.keys();
217
+ },
218
+ };
219
+ const that = this;
220
+ return new Set((function* () {
221
+ for (const value of that)
222
+ if (!other.has(value))
223
+ yield value;
224
+ for (const value of others)
225
+ if (!that.has(value))
226
+ yield value;
227
+ })());
228
+ }
229
+ isSubsetOf(other) {
230
+ for (const value of this)
231
+ if (!other.has(value))
232
+ return false;
233
+ return true;
234
+ }
235
+ isSupersetOf(other) {
236
+ const others = {
237
+ [Symbol.iterator]() {
238
+ return other.keys();
239
+ },
240
+ };
241
+ for (const value of others)
242
+ if (!this.has(value))
243
+ return false;
244
+ return true;
245
+ }
246
+ isDisjointFrom(other) {
247
+ for (const value of this)
248
+ if (other.has(value))
249
+ return false;
250
+ return true;
251
+ }
252
+ }
253
+ _b = Symbol.toStringTag;
254
+
255
+ /**
256
+ * Creates a mixin that can be used both as a class (extends) and as a function (mixin)
257
+ *
258
+ * This function supports:
259
+ * - Using mixins as base classes: `class MyClass extends MyMixin`
260
+ * - Using mixins as functions: `class MyClass extends MyMixin(SomeBase)`
261
+ * - Composing mixins: `const Composed = MixinA(MixinB)`
262
+ * - Type-safe property inference for all patterns
263
+ *
264
+ * @param mixinFunction - The function that creates the mixin
265
+ * @param unwrapFunction - Optional function to unwrap reactive objects for method calls
266
+ * @returns A mixin that can be used both as a class and as a function
267
+ */
268
+ function mixin(mixinFunction, unwrapFunction) {
269
+ /**
270
+ * Cache for mixin results to ensure the same base class always returns the same mixed class
271
+ */
272
+ const mixinCache = new WeakMap();
273
+ // Apply the mixin to Object as the base class
274
+ const MixedBase = mixinFunction(Object);
275
+ mixinCache.set(Object, MixedBase);
276
+ // Create the proxy that handles both constructor and function calls
277
+ return new Proxy(MixedBase, {
278
+ // Handle `MixinClass(SomeBase)` - use as mixin function
279
+ apply(_target, _thisArg, args) {
280
+ if (args.length === 0) {
281
+ throw new Error('Mixin requires a base class');
282
+ }
283
+ const baseClass = args[0];
284
+ if (typeof baseClass !== 'function') {
285
+ throw new Error('Mixin requires a constructor function');
286
+ }
287
+ // Check if it's a valid constructor or a mixin
288
+ if (!isConstructor(baseClass) &&
289
+ !(baseClass && typeof baseClass === 'function' && baseClass.prototype)) {
290
+ throw new Error('Mixin requires a valid constructor');
291
+ }
292
+ // Check cache first
293
+ const cached = mixinCache.get(baseClass);
294
+ if (cached) {
295
+ return cached;
296
+ }
297
+ let usedBase = baseClass;
298
+ if (unwrapFunction) {
299
+ // Create a proxied base class that handles method unwrapping
300
+ const ProxiedBaseClass = class extends baseClass {
301
+ };
302
+ // Proxy the prototype methods to handle unwrapping
303
+ const originalPrototype = baseClass.prototype;
304
+ const proxiedPrototype = new Proxy(originalPrototype, {
305
+ get(target, prop, receiver) {
306
+ const value = ReflectGet(target, prop, receiver);
307
+ // Only wrap methods that are likely to access private fields
308
+ // Skip symbols and special properties that the reactive system needs
309
+ if (typeof value === 'function' &&
310
+ typeof prop === 'string' &&
311
+ !['constructor', 'toString', 'valueOf'].includes(prop)) {
312
+ // Return a wrapped version that uses unwrapped context
313
+ return function (...args) {
314
+ // Use the unwrapping function if provided, otherwise use this
315
+ const context = unwrapFunction(this);
316
+ return value.apply(context, args);
317
+ };
318
+ }
319
+ return value;
320
+ },
321
+ });
322
+ // Set the proxied prototype
323
+ Object.setPrototypeOf(ProxiedBaseClass.prototype, proxiedPrototype);
324
+ usedBase = ProxiedBaseClass;
325
+ }
326
+ // Create the mixed class using the proxied base class
327
+ const mixedClass = mixinFunction(usedBase);
328
+ // Cache the result
329
+ mixinCache.set(baseClass, mixedClass);
330
+ return mixedClass;
331
+ },
332
+ });
333
+ }
334
+
335
+ // biome-ignore-all lint/suspicious/noConfusingVoidType: Type 'void' is not assignable to type 'ScopedCallback | undefined'.
336
+ // Argument of type '() => void' is not assignable to parameter of type '(dep: DependencyFunction) => ScopedCallback | undefined'.
337
+ // Track native reactivity
338
+ const nativeReactive = Symbol('native-reactive');
339
+ /**
340
+ * Symbol to mark individual objects as non-reactive
341
+ */
342
+ const nonReactiveMark = Symbol('non-reactive');
343
+ /**
344
+ * Symbol to mark class properties as non-reactive
345
+ */
346
+ const unreactiveProperties = Symbol('unreactive-properties');
347
+ /**
348
+ * Symbol for prototype forwarding in reactive objects
349
+ */
350
+ const prototypeForwarding = Symbol('prototype-forwarding');
351
+ /**
352
+ * Symbol representing all properties in reactive tracking
353
+ */
354
+ const allProps = Symbol('all-props');
355
+ // Symbol to mark functions with their root function
356
+ const rootFunction = Symbol('root-function');
357
+ /**
358
+ * Structured error codes for machine-readable diagnosis
359
+ */
360
+ var ReactiveErrorCode;
361
+ (function (ReactiveErrorCode) {
362
+ ReactiveErrorCode["CycleDetected"] = "CYCLE_DETECTED";
363
+ ReactiveErrorCode["MaxDepthExceeded"] = "MAX_DEPTH_EXCEEDED";
364
+ ReactiveErrorCode["MaxReactionExceeded"] = "MAX_REACTION_EXCEEDED";
365
+ ReactiveErrorCode["WriteInComputed"] = "WRITE_IN_COMPUTED";
366
+ ReactiveErrorCode["TrackingError"] = "TRACKING_ERROR";
367
+ })(ReactiveErrorCode || (ReactiveErrorCode = {}));
368
+ /**
369
+ * Error class for reactive system errors
370
+ */
371
+ class ReactiveError extends Error {
372
+ constructor(message, debugInfo) {
373
+ super(message);
374
+ this.debugInfo = debugInfo;
375
+ this.name = 'ReactiveError';
376
+ }
377
+ }
378
+ // biome-ignore-start lint/correctness/noUnusedFunctionParameters: Interface declaration with empty defaults
379
+ /**
380
+ * Global options for the reactive system
381
+ */
382
+ const options = {
383
+ /**
384
+ * Debug purpose: called when an effect is entered
385
+ * @param effect - The effect that is entered
386
+ */
387
+ enter: (_effect) => { },
388
+ /**
389
+ * Debug purpose: called when an effect is left
390
+ * @param effect - The effect that is left
391
+ */
392
+ leave: (_effect) => { },
393
+ /**
394
+ * Debug purpose: called when an effect is chained
395
+ * @param target - The effect that is being triggered
396
+ * @param caller - The effect that is calling the target
397
+ */
398
+ chain: (_targets, _caller) => { },
399
+ /**
400
+ * Debug purpose: called when an effect chain is started
401
+ * @param target - The effect that is being triggered
402
+ */
403
+ beginChain: (_targets) => { },
404
+ /**
405
+ * Debug purpose: called when an effect chain is ended
406
+ */
407
+ endChain: () => { },
408
+ garbageCollected: (_fn) => { },
409
+ /**
410
+ * Debug purpose: called when an object is touched
411
+ * @param obj - The object that is touched
412
+ * @param evolution - The type of change
413
+ * @param props - The properties that changed
414
+ * @param deps - The dependencies that changed
415
+ */
416
+ touched: (_obj, _evolution, _props, _deps) => { },
417
+ /**
418
+ * Debug purpose: called when an effect is skipped because it's already running
419
+ * @param effect - The effect that is already running
420
+ * @param runningChain - The array of effects from the detected one to the currently running one
421
+ */
422
+ skipRunningEffect: (_effect, _runningChain) => { },
423
+ /**
424
+ * Debug purpose: maximum effect chain (like call stack max depth)
425
+ * Used to prevent infinite loops
426
+ * @default 100
427
+ */
428
+ maxEffectChain: 100,
429
+ /**
430
+ * Debug purpose: maximum effect reaction (like call stack max depth)
431
+ * Used to prevent infinite loops
432
+ * @default 'throw'
433
+ */
434
+ maxEffectReaction: 'throw',
435
+ /**
436
+ * How to handle cycles detected in effect batches
437
+ * - 'throw': Throw an error with cycle information (default, recommended for development)
438
+ * - 'warn': Log a warning and break the cycle by executing one effect
439
+ * - 'break': Silently break the cycle by executing one effect (recommended for production)
440
+ * - 'strict': Prevent cycle creation by checking graph before execution (throws error)
441
+ * @default 'throw'
442
+ */
443
+ cycleHandling: 'throw',
444
+ /**
445
+ * Maximum depth for deep watching traversal
446
+ * Used to prevent infinite recursion in circular references
447
+ * @default 100
448
+ */
449
+ maxDeepWatchDepth: 100,
450
+ /**
451
+ * Only react on instance members modification (not inherited properties)
452
+ * For instance, do not track class methods
453
+ * @default true
454
+ */
455
+ instanceMembers: true,
456
+ /**
457
+ * Ignore accessors (getters and setters) and only track direct properties
458
+ * @default true
459
+ */
460
+ ignoreAccessors: true,
461
+ /**
462
+ * Enable recursive touching when objects with the same prototype are replaced
463
+ * When enabled, replacing an object with another of the same prototype triggers
464
+ * recursive diffing instead of notifying parent effects
465
+ * @default true
466
+ */
467
+ recursiveTouching: true,
468
+ /**
469
+ * Default async execution mode for effects that return Promises
470
+ * - 'cancel': Cancel previous async execution when dependencies change (default, enables async zone)
471
+ * - 'queue': Queue next execution to run after current completes (enables async zone)
472
+ * - 'ignore': Ignore new executions while async work is running (enables async zone)
473
+ * - false: Disable async zone and async mode handling (effects run concurrently)
474
+ *
475
+ * **When truthy:** Enables async zone (Promise.prototype wrapping) for automatic context
476
+ * preservation in Promise callbacks. Warning: This modifies Promise.prototype globally.
477
+ * Only enable if no other library modifies Promise.prototype.
478
+ *
479
+ * **When false:** Async zone is disabled. Use `tracked()` manually in Promise callbacks.
480
+ *
481
+ * Can be overridden per-effect via EffectOptions
482
+ * @default 'cancel'
483
+ */
484
+ asyncMode: 'cancel',
485
+ // biome-ignore lint/suspicious/noConsole: This is the whole point here
486
+ warn: (...args) => console.warn(...args),
487
+ /**
488
+ * Configuration for the introspection system
489
+ */
490
+ introspection: {
491
+ /**
492
+ * Whether to keep a history of mutations for debugging
493
+ * @default false
494
+ */
495
+ enableHistory: false,
496
+ /**
497
+ * Number of mutations to keep in history
498
+ * @default 50
499
+ */
500
+ historySize: 50,
501
+ },
502
+ /**
503
+ * Configuration for zone hooks - control which async APIs are hooked
504
+ * Each option controls whether the corresponding async API is wrapped to preserve effect context
505
+ * Only applies when asyncMode is enabled (truthy)
506
+ */
507
+ zones: {
508
+ /**
509
+ * Hook setTimeout to preserve effect context
510
+ * @default true
511
+ */
512
+ setTimeout: true,
513
+ /**
514
+ * Hook setInterval to preserve effect context
515
+ * @default true
516
+ */
517
+ setInterval: true,
518
+ /**
519
+ * Hook requestAnimationFrame (runs in untracked context when hooked)
520
+ * @default true
521
+ */
522
+ requestAnimationFrame: true,
523
+ /**
524
+ * Hook queueMicrotask to preserve effect context
525
+ * @default true
526
+ */
527
+ queueMicrotask: true,
528
+ },
529
+ };
530
+
531
+ /**
532
+ * Effect context stack for nested tracking (front = active, next = parent)
533
+ */
534
+ const stack = [];
535
+ function captureEffectStack() {
536
+ return stack.slice();
537
+ }
538
+ function isRunning(effect) {
539
+ const rootEffect = getRoot(effect);
540
+ // Check if the effect is directly in the stack
541
+ const rootIndex = stack.indexOf(rootEffect);
542
+ if (rootIndex !== -1) {
543
+ return stack.slice(0, rootIndex + 1).reverse();
544
+ }
545
+ // Check if any effect in the stack is a descendant of this effect
546
+ // (i.e., walk up the parent chain from each stack effect to see if we reach this effect)
547
+ for (let i = 0; i < stack.length; i++) {
548
+ const stackEffect = stack[i];
549
+ let current = stackEffect;
550
+ const visited = new WeakSet();
551
+ const ancestorChain = [];
552
+ // TODO: That's perhaps a lot of computations for an `assert`
553
+ // Walk up the parent chain to find if this effect is an ancestor
554
+ while (current && !visited.has(current)) {
555
+ visited.add(current);
556
+ const currentRoot = getRoot(current);
557
+ ancestorChain.push(currentRoot);
558
+ if (currentRoot === rootEffect) {
559
+ // Found a descendant - build the full chain from ancestor to active
560
+ // The ancestorChain contains [descendant, parent, ..., ancestor] (walking up)
561
+ // We need [ancestor (effect), ..., parent, descendant, ...stack from descendant to active]
562
+ const chainFromAncestor = ancestorChain.reverse(); // [ancestor, ..., descendant]
563
+ // Prepend the actual effect we're checking (in case current is a wrapper)
564
+ if (chainFromAncestor[0] !== rootEffect) {
565
+ chainFromAncestor.unshift(rootEffect);
566
+ }
567
+ // Append the rest of the stack from the descendant to the active effect
568
+ const stackFromDescendant = stack.slice(0, i + 1).reverse(); // [descendant, ..., active]
569
+ // Remove duplicate descendant (it's both at end of chainFromAncestor and start of stackFromDescendant)
570
+ if (chainFromAncestor.length > 0 && stackFromDescendant.length > 0) {
571
+ stackFromDescendant.shift(); // Remove duplicate descendant
572
+ }
573
+ return [...chainFromAncestor, ...stackFromDescendant];
574
+ }
575
+ current = effectParent.get(current);
576
+ }
577
+ }
578
+ return false;
579
+ }
580
+ function withEffectStack(snapshot, fn) {
581
+ const previousStack = stack.slice();
582
+ assignStack(snapshot);
583
+ try {
584
+ return fn();
585
+ }
586
+ finally {
587
+ assignStack(previousStack);
588
+ }
589
+ }
590
+ function getActiveEffect() {
591
+ return stack[0];
592
+ }
593
+ /**
594
+ * Executes a function with a specific effect context
595
+ * @param effect - The effect to use as context
596
+ * @param fn - The function to execute
597
+ * @param keepParent - Whether to keep the parent effect context
598
+ * @returns The result of the function
599
+ */
600
+ function withEffect(effect, fn) {
601
+ // console.log('[Mutts] withEffect', effect ? 'Active' : 'NULL');
602
+ if (getRoot(effect) === getRoot(getActiveEffect()))
603
+ return fn();
604
+ stack.unshift(effect);
605
+ try {
606
+ return fn();
607
+ }
608
+ finally {
609
+ const recoveredEffect = stack.shift();
610
+ if (recoveredEffect !== effect)
611
+ throw new ReactiveError('[reactive] Effect stack mismatch');
612
+ }
613
+ }
614
+ function assignStack(values) {
615
+ stack.length = 0;
616
+ stack.push(...values);
617
+ }
618
+
619
+ const objectToProxy = new WeakMap();
620
+ const proxyToObject = new WeakMap();
621
+ function storeProxyRelationship(target, proxy) {
622
+ objectToProxy.set(target, proxy);
623
+ proxyToObject.set(proxy, target);
624
+ }
625
+ function getExistingProxy(target) {
626
+ return objectToProxy.get(target);
627
+ }
628
+ function trackProxyObject(proxy, target) {
629
+ proxyToObject.set(proxy, target);
630
+ }
631
+ function unwrap(obj) {
632
+ let current = obj;
633
+ while (current && typeof current === 'object' && current !== null && proxyToObject.has(current)) {
634
+ current = proxyToObject.get(current);
635
+ }
636
+ return current;
637
+ }
638
+ function isReactive(obj) {
639
+ return proxyToObject.has(obj);
640
+ }
641
+
642
+ // Track which effects are watching which reactive objects for cleanup
643
+ const effectToReactiveObjects = new WeakMap();
644
+ // Track effects per reactive object and property
645
+ const watchers = new WeakMap();
646
+ // runEffect -> set<stop>
647
+ const effectChildren = new WeakMap();
648
+ // Track parent effect relationships for hierarchy traversal (used in deep touch filtering)
649
+ const effectParent = new WeakMap();
650
+ /**
651
+ * Marks a function with its root function for effect tracking
652
+ * @param fn - The function to mark
653
+ * @param root - The root function
654
+ * @returns The marked function
655
+ */
656
+ function markWithRoot(fn, root) {
657
+ // Mark fn with the new root
658
+ return Object.defineProperty(fn, rootFunction, {
659
+ value: getRoot(root),
660
+ writable: false,
661
+ });
662
+ }
663
+ /**
664
+ * Gets the root function of a function for effect tracking
665
+ * @param fn - The function to get the root of
666
+ * @returns The root function
667
+ */
668
+ function getRoot(fn) {
669
+ return fn?.[rootFunction] || fn;
670
+ }
671
+ // Flag to disable dependency tracking for the current active effect (not globally)
672
+ const trackingDisabledEffects = new WeakSet();
673
+ let globalTrackingDisabled = false;
674
+ function getTrackingDisabled() {
675
+ const active = getActiveEffect();
676
+ if (!active)
677
+ return globalTrackingDisabled;
678
+ return trackingDisabledEffects.has(getRoot(active));
679
+ }
680
+ function setTrackingDisabled(value) {
681
+ const active = getActiveEffect();
682
+ if (!active) {
683
+ globalTrackingDisabled = value;
684
+ return;
685
+ }
686
+ const root = getRoot(active);
687
+ if (value)
688
+ trackingDisabledEffects.add(root);
689
+ else
690
+ trackingDisabledEffects.delete(root);
691
+ }
692
+ /**
693
+ * Marks a property as a dependency of the current effect
694
+ * @param obj - The object containing the property
695
+ * @param prop - The property name (defaults to allProps)
696
+ */
697
+ function dependant(obj, prop = allProps) {
698
+ obj = unwrap(obj);
699
+ const currentActiveEffect = getActiveEffect();
700
+ // Early return if no active effect, tracking disabled, or invalid prop
701
+ if (!currentActiveEffect ||
702
+ getTrackingDisabled() ||
703
+ (typeof prop === 'symbol' && prop !== allProps))
704
+ return;
705
+ registerDependency(obj, prop, currentActiveEffect);
706
+ }
707
+ function registerDependency(obj, prop, currentActiveEffect) {
708
+ let objectWatchers = watchers.get(obj);
709
+ if (!objectWatchers) {
710
+ objectWatchers = new Map();
711
+ watchers.set(obj, objectWatchers);
712
+ }
713
+ let deps = objectWatchers.get(prop);
714
+ if (!deps) {
715
+ deps = new Set();
716
+ objectWatchers.set(prop, deps);
717
+ }
718
+ deps.add(currentActiveEffect);
719
+ // Track which reactive objects this effect is watching
720
+ const effectObjects = effectToReactiveObjects.get(currentActiveEffect);
721
+ if (effectObjects) {
722
+ effectObjects.add(obj);
723
+ }
724
+ else {
725
+ effectToReactiveObjects.set(currentActiveEffect, new Set([obj]));
726
+ }
727
+ }
728
+
729
+ /**
730
+ * Debug utilities for the reactivity system
731
+ * - Captures effect metadata (names, parent relationships)
732
+ * - Records cause → consequence edges with object/prop labels
733
+ * - Provides graph data for tooling (DevTools panel, etc.)
734
+ */
735
+ const EXTERNAL_SOURCE = Symbol('external-source');
736
+ let devtoolsEnabled = false;
737
+ // Registry for debugging (populated lazily when DevTools are enabled)
738
+ const debugEffectRegistry = new Set();
739
+ const debugObjectRegistry = new Set();
740
+ // Human-friendly names
741
+ const effectNames = new WeakMap();
742
+ const objectNames = new WeakMap();
743
+ let effectCounter = 0;
744
+ let objectCounter = 0;
745
+ const triggerGraph = new Map();
746
+ function ensureEffectName(effect) {
747
+ let name = effectNames.get(effect);
748
+ if (!name) {
749
+ const root = getRoot(effect);
750
+ name = root?.name?.trim() || `effect_${++effectCounter}`;
751
+ effectNames.set(effect, name);
752
+ }
753
+ return name;
754
+ }
755
+ function ensureObjectName(obj) {
756
+ let name = objectNames.get(obj);
757
+ if (!name) {
758
+ const ctorName = obj?.constructor?.name;
759
+ const base = ctorName && ctorName !== 'Object' ? ctorName : 'object';
760
+ name = `${base}_${++objectCounter}`;
761
+ objectNames.set(obj, name);
762
+ }
763
+ return name;
764
+ }
765
+ function describeProp(obj, prop) {
766
+ const objectName = ensureObjectName(obj);
767
+ if (prop === allProps)
768
+ return `${objectName}.*`;
769
+ if (typeof prop === 'symbol')
770
+ return `${objectName}.${prop.description ?? prop.toString()}`;
771
+ return `${objectName}.${String(prop)}`;
772
+ }
773
+ function addEffectToRegistry(effect) {
774
+ if (!effect || debugEffectRegistry.has(effect))
775
+ return;
776
+ debugEffectRegistry.add(effect);
777
+ const deps = effectToReactiveObjects.get(effect);
778
+ if (deps) {
779
+ for (const obj of deps) {
780
+ documentObject(obj);
781
+ }
782
+ }
783
+ }
784
+ function documentObject(obj) {
785
+ if (!debugObjectRegistry.has(obj)) {
786
+ dbRegisterObject(obj);
787
+ }
788
+ }
789
+ function dbRegisterObject(obj) {
790
+ debugObjectRegistry.add(obj);
791
+ ensureObjectName(obj);
792
+ }
793
+ function ensureParentChains(effects) {
794
+ const queue = Array.from(effects);
795
+ for (let i = 0; i < queue.length; i++) {
796
+ const effect = queue[i];
797
+ const parent = effectParent.get(effect);
798
+ if (parent && !effects.has(parent)) {
799
+ effects.add(parent);
800
+ queue.push(parent);
801
+ }
802
+ }
803
+ }
804
+ function ensureTriggerContainers(source) {
805
+ let targetMap = triggerGraph.get(source);
806
+ if (!targetMap) {
807
+ targetMap = new Map();
808
+ triggerGraph.set(source, targetMap);
809
+ }
810
+ return targetMap;
811
+ }
812
+ function ensureTriggerRecord(source, target, label, obj, prop, evolution) {
813
+ const targetMap = ensureTriggerContainers(source);
814
+ let labelMap = targetMap.get(target);
815
+ if (!labelMap) {
816
+ labelMap = new Map();
817
+ targetMap.set(target, labelMap);
818
+ }
819
+ let record = labelMap.get(label);
820
+ if (!record) {
821
+ record = { label, object: obj, prop, evolution, count: 0, lastTriggered: Date.now() };
822
+ labelMap.set(label, record);
823
+ }
824
+ return record;
825
+ }
826
+ /**
827
+ * Assign a debug-friendly name to an effect (shown in DevTools)
828
+ */
829
+ function setEffectName(effect, name) {
830
+ effectNames.set(effect, name);
831
+ }
832
+ /**
833
+ * Assign a debug-friendly name to a reactive object
834
+ */
835
+ function setObjectName(obj, name) {
836
+ objectNames.set(obj, name);
837
+ debugObjectRegistry.add(obj);
838
+ }
839
+ /**
840
+ * Register an effect so it appears in the DevTools graph
841
+ */
842
+ function registerEffectForDebug(effect) {
843
+ if (!effect || !devtoolsEnabled)
844
+ return;
845
+ addEffectToRegistry(effect);
846
+ }
847
+ /**
848
+ * Register a reactive object so it appears in the DevTools graph
849
+ */
850
+ function registerObjectForDebug(obj) {
851
+ if (!devtoolsEnabled)
852
+ return;
853
+ documentObject(obj);
854
+ }
855
+ /**
856
+ * Records a cause → consequence relationship between effects.
857
+ * @param source - The effect performing the write (undefined if external/user input)
858
+ * @param target - The effect that re-ran because of the write
859
+ * @param obj - The reactive object that changed
860
+ * @param prop - The property that changed
861
+ * @param evolution - The type of change (set/add/del/bunch)
862
+ */
863
+ function recordTriggerLink(source, target, obj, prop, evolution) {
864
+ if (options.introspection.enableHistory) {
865
+ addToMutationHistory(source, target, obj, prop, evolution);
866
+ }
867
+ if (!devtoolsEnabled)
868
+ return;
869
+ addEffectToRegistry(target);
870
+ if (source)
871
+ addEffectToRegistry(source);
872
+ const descriptor = describeProp(obj, prop);
873
+ const record = ensureTriggerRecord(source ?? EXTERNAL_SOURCE, target, descriptor, obj, prop, evolution);
874
+ record.count += 1;
875
+ record.lastTriggered = Date.now();
876
+ documentObject(obj);
877
+ }
878
+ /**
879
+ * Traces back the chain of triggers that led to a specific effect
880
+ * @param effect The effect to trace back
881
+ * @param limit Max depth
882
+ */
883
+ function getTriggerChain(effect, limit = 5) {
884
+ const chain = [];
885
+ let current = effect;
886
+ for (let i = 0; i < limit; i++) {
887
+ // Find who triggered 'current'
888
+ // We need to reverse search the triggerGraph (source -> target)
889
+ // This is expensive O(Edges) but okay for error reporting
890
+ let foundSource;
891
+ let foundReason = '';
892
+ search: for (const [source, targetMap] of triggerGraph) {
893
+ for (const [target, labelMap] of targetMap) {
894
+ if (target === current) {
895
+ // Found a source! Use the most recent trigger record
896
+ let lastTime = 0;
897
+ for (const record of labelMap.values()) {
898
+ if (record.lastTriggered > lastTime) {
899
+ lastTime = record.lastTriggered;
900
+ foundReason = record.label;
901
+ foundSource = source === EXTERNAL_SOURCE ? undefined : source;
902
+ }
903
+ }
904
+ if (foundSource || foundReason)
905
+ break search;
906
+ }
907
+ }
908
+ }
909
+ if (foundSource) {
910
+ chain.push(`${ensureEffectName(foundSource)} -> (${foundReason}) -> ${ensureEffectName(current)}`);
911
+ current = foundSource;
912
+ }
913
+ else if (foundReason) {
914
+ chain.push(`External -> (${foundReason}) -> ${ensureEffectName(current)}`);
915
+ break;
916
+ }
917
+ else {
918
+ break;
919
+ }
920
+ }
921
+ return chain.reverse();
922
+ }
923
+ function buildEffectNodes(allEffects) {
924
+ const nodes = [];
925
+ const nodeByEffect = new Map();
926
+ const ordered = Array.from(allEffects);
927
+ for (const effect of ordered) {
928
+ const label = ensureEffectName(effect);
929
+ const node = {
930
+ id: `effect_${nodes.length}`,
931
+ label,
932
+ type: 'effect',
933
+ depth: 0,
934
+ debugName: label,
935
+ };
936
+ nodes.push(node);
937
+ nodeByEffect.set(effect, node);
938
+ }
939
+ const depthCache = new Map();
940
+ const computeDepth = (effect) => {
941
+ if (!effect)
942
+ return 0;
943
+ const cached = depthCache.get(effect);
944
+ if (cached !== undefined)
945
+ return cached;
946
+ const parent = effectParent.get(effect);
947
+ const depth = computeDepth(parent) + (parent ? 1 : 0);
948
+ depthCache.set(effect, depth);
949
+ return depth;
950
+ };
951
+ for (const [effect, node] of nodeByEffect) {
952
+ node.depth = computeDepth(effect);
953
+ const parent = effectParent.get(effect);
954
+ if (parent) {
955
+ const parentNode = nodeByEffect.get(parent);
956
+ if (parentNode) {
957
+ node.parentId = parentNode.id;
958
+ }
959
+ }
960
+ }
961
+ return { nodes, nodeByEffect };
962
+ }
963
+ /**
964
+ * Builds a graph representing current reactive state (effects, objects, and trigger edges)
965
+ */
966
+ function buildReactivityGraph() {
967
+ const nodes = [];
968
+ const edges = [];
969
+ const nodeIds = new Map();
970
+ const allEffects = new Set(debugEffectRegistry);
971
+ ensureParentChains(allEffects);
972
+ const { nodes: effectNodes, nodeByEffect } = buildEffectNodes(allEffects);
973
+ for (const node of effectNodes)
974
+ nodes.push(node);
975
+ for (const [effect, node] of nodeByEffect) {
976
+ nodeIds.set(effect, node.id);
977
+ }
978
+ // Object nodes (optional, used for dependency inspection)
979
+ for (const obj of debugObjectRegistry) {
980
+ const id = `object_${nodes.length}`;
981
+ nodes.push({ id, label: ensureObjectName(obj), type: 'state', debugName: objectNames.get(obj) });
982
+ nodeIds.set(obj, id);
983
+ }
984
+ // External source node (user/system outside of effects)
985
+ if (triggerGraph.has(EXTERNAL_SOURCE)) {
986
+ const externalId = `effect_external`;
987
+ nodes.push({ id: externalId, label: 'External', type: 'external', depth: 0 });
988
+ nodeIds.set(EXTERNAL_SOURCE, externalId);
989
+ }
990
+ // Dependency edges (effect → object)
991
+ for (const effect of allEffects) {
992
+ const effectId = nodeIds.get(effect);
993
+ if (!effectId)
994
+ continue;
995
+ const deps = effectToReactiveObjects.get(effect);
996
+ if (!deps)
997
+ continue;
998
+ for (const obj of deps) {
999
+ const objId = nodeIds.get(obj);
1000
+ if (!objId)
1001
+ continue;
1002
+ edges.push({
1003
+ id: `${effectId}->${objId}`,
1004
+ source: effectId,
1005
+ target: objId,
1006
+ type: 'dependency',
1007
+ label: 'depends',
1008
+ });
1009
+ }
1010
+ }
1011
+ // Cause edges (effect/object/prop → effect)
1012
+ for (const [source, targetMap] of triggerGraph) {
1013
+ for (const [targetEffect, labelMap] of targetMap) {
1014
+ const targetId = nodeIds.get(targetEffect);
1015
+ if (!targetId)
1016
+ continue;
1017
+ const sourceId = nodeIds.get(source);
1018
+ if (!sourceId)
1019
+ continue;
1020
+ for (const record of labelMap.values()) {
1021
+ edges.push({
1022
+ id: `${sourceId}->${targetId}:${record.label}`,
1023
+ source: sourceId,
1024
+ target: targetId,
1025
+ type: 'cause',
1026
+ label: record.count > 1 ? `${record.label} (${record.count})` : record.label,
1027
+ count: record.count,
1028
+ });
1029
+ }
1030
+ }
1031
+ }
1032
+ return {
1033
+ nodes,
1034
+ edges,
1035
+ meta: {
1036
+ generatedAt: Date.now(),
1037
+ devtoolsEnabled,
1038
+ },
1039
+ };
1040
+ }
1041
+ /**
1042
+ * Enables the DevTools bridge and exposes the debug API on window.
1043
+ * Call as early as possible in development builds.
1044
+ */
1045
+ function enableDevTools() {
1046
+ if (typeof window === 'undefined')
1047
+ return;
1048
+ if (devtoolsEnabled)
1049
+ return;
1050
+ devtoolsEnabled = true;
1051
+ // @ts-expect-error - global window extension
1052
+ window.__MUTTS_DEVTOOLS__ = {
1053
+ getGraph: buildReactivityGraph,
1054
+ setEffectName,
1055
+ setObjectName,
1056
+ registerEffect: registerEffectForDebug,
1057
+ registerObject: registerObjectForDebug,
1058
+ };
1059
+ }
1060
+ function isDevtoolsEnabled() {
1061
+ return devtoolsEnabled;
1062
+ }
1063
+ const mutationHistory = [];
1064
+ let mutationCounter = 0;
1065
+ function addToMutationHistory(source, target, obj, prop, evolution) {
1066
+ const record = {
1067
+ id: ++mutationCounter,
1068
+ timestamp: Date.now(),
1069
+ source: source ? ensureEffectName(source) : 'External',
1070
+ target: ensureEffectName(target),
1071
+ objectName: ensureObjectName(obj),
1072
+ prop: String(prop),
1073
+ type: evolution.type,
1074
+ };
1075
+ mutationHistory.push(record);
1076
+ if (mutationHistory.length > options.introspection.historySize) {
1077
+ mutationHistory.shift();
1078
+ }
1079
+ }
1080
+
1081
+ /**
1082
+ * Zone-like async context preservation for reactive effects
1083
+ *
1084
+ * Automatically preserves effect context across async boundaries:
1085
+ * - Promise methods: .then(), .catch(), .finally()
1086
+ * - Timers: setTimeout(), setInterval()
1087
+ * - Animation: requestAnimationFrame() (if available) - runs in untracked context
1088
+ * - Microtasks: queueMicrotask() (if available)
1089
+ *
1090
+ * **IMPORTANT:** This module is opt-in via `reactiveOptions.asyncMode` (truthy = enabled, false = disabled).
1091
+ * By default, async zone is ENABLED with 'cancel' mode.
1092
+ *
1093
+ * When disabled (asyncMode = false), use `tracked()` manually in async callbacks.
1094
+ * When enabled (asyncMode = 'cancel' | 'queue' | 'ignore'), async entry points are wrapped ONCE.
1095
+ */
1096
+ let zoneHooked = false;
1097
+ // Store original Promise methods at module load time (before any wrapping)
1098
+ // This ensures we always have the true originals, even if wrapping happens multiple times
1099
+ const originalPromiseThen = Object.getOwnPropertyDescriptor(Promise.prototype, 'then')?.value || Promise.prototype.then;
1100
+ const originalPromiseCatch = Object.getOwnPropertyDescriptor(Promise.prototype, 'catch')?.value || Promise.prototype.catch;
1101
+ const originalPromiseFinally = Object.getOwnPropertyDescriptor(Promise.prototype, 'finally')?.value || Promise.prototype.finally;
1102
+ // Store original timer functions at module load time
1103
+ const originalSetTimeout = globalThis.setTimeout;
1104
+ const originalSetInterval = globalThis.setInterval;
1105
+ const originalRequestAnimationFrame = typeof globalThis.requestAnimationFrame !== 'undefined'
1106
+ ? globalThis.requestAnimationFrame
1107
+ : undefined;
1108
+ const originalQueueMicrotask = typeof globalThis.queueMicrotask !== 'undefined' ? globalThis.queueMicrotask : undefined;
1109
+ // Store batch function to avoid circular dependency
1110
+ let batchFn;
1111
+ /**
1112
+ * Check the asyncMode option and hook Promise.prototype once if enabled
1113
+ * Called lazily on first effect creation
1114
+ * asyncMode being truthy enables async zone, false disables it
1115
+ *
1116
+ * @param batch - Optional batch function injection from effects.ts to avoid circular dependency
1117
+ */
1118
+ function ensureZoneHooked(batch) {
1119
+ if (batch)
1120
+ batchFn = batch;
1121
+ if (zoneHooked || !options.asyncMode)
1122
+ return;
1123
+ hookZone();
1124
+ zoneHooked = true;
1125
+ }
1126
+ /**
1127
+ * Hook Promise.prototype methods to preserve effect context
1128
+ */
1129
+ function hookZone() {
1130
+ // biome-ignore lint/suspicious/noThenProperty: Intentional wrapping for zone functionality
1131
+ Promise.prototype.then = function (onFulfilled, onRejected) {
1132
+ const capturedStack = captureEffectStack();
1133
+ return originalPromiseThen.call(this, wrapCallback(onFulfilled, capturedStack), wrapCallback(onRejected, capturedStack));
1134
+ };
1135
+ Promise.prototype.catch = function (onRejected) {
1136
+ const capturedStack = captureEffectStack();
1137
+ return originalPromiseCatch.call(this, wrapCallback(onRejected, capturedStack));
1138
+ };
1139
+ Promise.prototype.finally = function (onFinally) {
1140
+ const capturedStack = captureEffectStack();
1141
+ return originalPromiseFinally.call(this, wrapCallback(onFinally, capturedStack));
1142
+ };
1143
+ // Hook setTimeout - preserve original function properties for Node.js compatibility
1144
+ const wrappedSetTimeout = ((callback, delay, ...args) => {
1145
+ const capturedStack = options.zones.setTimeout ? captureEffectStack() : undefined;
1146
+ return originalSetTimeout.apply(globalThis, [
1147
+ wrapCallback(callback, capturedStack),
1148
+ delay,
1149
+ ...args,
1150
+ ]);
1151
+ });
1152
+ Object.assign(wrappedSetTimeout, originalSetTimeout);
1153
+ globalThis.setTimeout = wrappedSetTimeout;
1154
+ // Hook setInterval - preserve original function properties for Node.js compatibility
1155
+ const wrappedSetInterval = ((callback, delay, ...args) => {
1156
+ const capturedStack = options.zones.setInterval ? captureEffectStack() : undefined;
1157
+ return originalSetInterval.apply(globalThis, [
1158
+ wrapCallback(callback, capturedStack),
1159
+ delay,
1160
+ ...args,
1161
+ ]);
1162
+ });
1163
+ Object.assign(wrappedSetInterval, originalSetInterval);
1164
+ globalThis.setInterval = wrappedSetInterval;
1165
+ // Hook requestAnimationFrame if available
1166
+ if (originalRequestAnimationFrame) {
1167
+ globalThis.requestAnimationFrame = ((callback) => {
1168
+ const capturedStack = options.zones.requestAnimationFrame ? captureEffectStack() : undefined;
1169
+ return originalRequestAnimationFrame.call(globalThis, wrapCallback(callback, capturedStack));
1170
+ });
1171
+ }
1172
+ // Hook queueMicrotask if available
1173
+ if (originalQueueMicrotask) {
1174
+ globalThis.queueMicrotask = ((callback) => {
1175
+ const capturedStack = options.zones.queueMicrotask ? captureEffectStack() : undefined;
1176
+ originalQueueMicrotask.call(globalThis, wrapCallback(callback, capturedStack));
1177
+ });
1178
+ }
1179
+ }
1180
+ /**
1181
+ * Wraps a callback to restore effect context and ensure batching
1182
+ */
1183
+ function wrapCallback(callback, capturedStack) {
1184
+ if (!callback)
1185
+ return undefined;
1186
+ // If no stack to restore and no batch function, direct call (optimization)
1187
+ if ((!capturedStack || !capturedStack.length) && !batchFn) {
1188
+ return callback;
1189
+ }
1190
+ return ((...args) => {
1191
+ const execute = () => {
1192
+ if (capturedStack?.length) {
1193
+ return withEffectStack(capturedStack, () => callback(...args));
1194
+ }
1195
+ return callback(...args);
1196
+ };
1197
+ if (batchFn) {
1198
+ return batchFn(execute, 'immediate');
1199
+ }
1200
+ return execute();
1201
+ });
1202
+ }
1203
+ /**
1204
+ * Manually enable/disable the zone (for testing)
1205
+ */
1206
+ function setZoneEnabled(enabled) {
1207
+ if (enabled && !zoneHooked) {
1208
+ hookZone();
1209
+ zoneHooked = true;
1210
+ }
1211
+ else if (!enabled && zoneHooked) {
1212
+ // Restore original Promise methods
1213
+ // biome-ignore lint/suspicious/noThenProperty: Restoring original methods
1214
+ Promise.prototype.then = originalPromiseThen;
1215
+ Promise.prototype.catch = originalPromiseCatch;
1216
+ Promise.prototype.finally = originalPromiseFinally;
1217
+ // Restore original timer functions
1218
+ globalThis.setTimeout = originalSetTimeout;
1219
+ globalThis.setInterval = originalSetInterval;
1220
+ if (originalRequestAnimationFrame) {
1221
+ globalThis.requestAnimationFrame = originalRequestAnimationFrame;
1222
+ }
1223
+ if (originalQueueMicrotask) {
1224
+ globalThis.queueMicrotask = originalQueueMicrotask;
1225
+ }
1226
+ zoneHooked = false;
1227
+ }
1228
+ }
1229
+ /**
1230
+ * Check if zone is currently hooked
1231
+ */
1232
+ function isZoneEnabled() {
1233
+ return zoneHooked;
1234
+ }
1235
+
1236
+ /**
1237
+ * Finds a cycle in a sequence of functions by looking for the first repetition
1238
+ */
1239
+ function findCycleInChain(roots) {
1240
+ const seen = new Map();
1241
+ for (let i = 0; i < roots.length; i++) {
1242
+ const root = roots[i];
1243
+ if (seen.has(root)) {
1244
+ return roots.slice(seen.get(root));
1245
+ }
1246
+ seen.set(root, i);
1247
+ }
1248
+ return null;
1249
+ }
1250
+ /**
1251
+ * Formats a list of function roots into a readable trace
1252
+ */
1253
+ function formatRoots(roots, limit = 20) {
1254
+ const names = roots.map((r) => r.name || '<anonymous>');
1255
+ if (names.length <= limit)
1256
+ return names.join(' → ');
1257
+ const start = names.slice(0, 5);
1258
+ const end = names.slice(-10);
1259
+ return `${start.join(' → ')} ... (${names.length - 15} more) ... ${end.join(' → ')}`;
1260
+ }
1261
+ /**
1262
+ * Registers a debug callback that is called when the current effect is triggered by a dependency change
1263
+ *
1264
+ * This function is useful for debugging purposes as it pin-points exactly which reactive property
1265
+ * change triggered the effect. The callback receives information about:
1266
+ * - The object that changed
1267
+ * - The type of change (evolution)
1268
+ * - The specific property that changed
1269
+ *
1270
+ * **Note:** The tracker callback is automatically removed after being called once. If you need
1271
+ * to track multiple triggers, call `trackEffect` again within the effect.
1272
+ *
1273
+ * @param onTouch - Callback function that receives (obj, evolution, prop) when the effect is triggered
1274
+ * @throws {Error} If called outside of an effect context
1275
+ *
1276
+ * @example
1277
+ * ```typescript
1278
+ * const state = reactive({ count: 0, name: 'John' })
1279
+ *
1280
+ * effect(() => {
1281
+ * // Register a tracker to see what triggers this effect
1282
+ * trackEffect((obj, evolution, prop) => {
1283
+ * console.log(`Effect triggered by:`, {
1284
+ * object: obj,
1285
+ * change: evolution.type,
1286
+ * property: prop
1287
+ * })
1288
+ * })
1289
+ *
1290
+ * // Access reactive properties
1291
+ * console.log(state.count, state.name)
1292
+ * })
1293
+ *
1294
+ * state.count = 5
1295
+ * // Logs: Effect triggered by: { object: state, change: 'set', property: 'count' }
1296
+ * ```
1297
+ */
1298
+ function trackEffect(onTouch) {
1299
+ const activeEffect = getActiveEffect();
1300
+ if (!activeEffect)
1301
+ throw new Error('Not in an effect');
1302
+ if (!effectTrackers.has(activeEffect))
1303
+ effectTrackers.set(activeEffect, new Set([onTouch]));
1304
+ else
1305
+ effectTrackers.get(activeEffect).add(onTouch);
1306
+ }
1307
+ const effectTrackers = new WeakMap();
1308
+ const opaqueEffects = new WeakSet();
1309
+ // Dependency graph: tracks which effects trigger which other effects
1310
+ // Uses roots (Function) as keys for consistency
1311
+ const effectTriggers = new WeakMap();
1312
+ const effectTriggeredBy = new WeakMap();
1313
+ // Transitive closures: track all indirect relationships
1314
+ // causesClosure: for each effect, all effects that trigger it (directly or indirectly)
1315
+ // consequencesClosure: for each effect, all effects that it triggers (directly or indirectly)
1316
+ const causesClosure = new WeakMap();
1317
+ const consequencesClosure = new WeakMap();
1318
+ // Debug: Capture where an effect was created
1319
+ const effectCreationStacks = new WeakMap();
1320
+ /**
1321
+ * Gets or creates an IterableWeakSet for a closure map
1322
+ */
1323
+ function getOrCreateClosure(closure, root) {
1324
+ let set = closure.get(root);
1325
+ if (!set) {
1326
+ set = new IterableWeakSet();
1327
+ closure.set(root, set);
1328
+ }
1329
+ return set;
1330
+ }
1331
+ /**
1332
+ * Adds an edge to the dependency graph: callerRoot → targetRoot
1333
+ * Also maintains transitive closures
1334
+ * @param callerRoot - Root function of the effect that triggers
1335
+ * @param targetRoot - Root function of the effect being triggered
1336
+ */
1337
+ function addGraphEdge(callerRoot, targetRoot) {
1338
+ // Skip if edge already exists
1339
+ const triggers = effectTriggers.get(callerRoot);
1340
+ if (triggers?.has(targetRoot)) {
1341
+ return; // Edge already exists
1342
+ }
1343
+ // Add to forward graph: callerRoot → targetRoot
1344
+ if (!triggers) {
1345
+ const newTriggers = new IterableWeakSet();
1346
+ newTriggers.add(targetRoot);
1347
+ effectTriggers.set(callerRoot, newTriggers);
1348
+ }
1349
+ else {
1350
+ triggers.add(targetRoot);
1351
+ }
1352
+ // Add to reverse graph: targetRoot ← callerRoot
1353
+ let triggeredBy = effectTriggeredBy.get(targetRoot);
1354
+ if (!triggeredBy) {
1355
+ triggeredBy = new IterableWeakSet();
1356
+ effectTriggeredBy.set(targetRoot, triggeredBy);
1357
+ }
1358
+ triggeredBy.add(callerRoot);
1359
+ // Update transitive closures
1360
+ // When U→V is added, we need to propagate the relationship:
1361
+ // 1. Add U to causesClosure(V) and V to consequencesClosure(U) (direct relationship)
1362
+ // 2. For each X in causesClosure(U): add V to consequencesClosure(X) and X to causesClosure(V)
1363
+ // 3. For each Y in consequencesClosure(V): add U to causesClosure(Y) and Y to consequencesClosure(U)
1364
+ // Note: Self-loops (U→U) are not added to closures - if an effect appears in its own closure,
1365
+ // it means there's an indirect cycle that should be detected
1366
+ // Self-loops are explicitly ignored - an effect reading and writing the same property
1367
+ // (e.g., obj.prop++) should not create a dependency relationship or appear in closures
1368
+ if (callerRoot === targetRoot) {
1369
+ return;
1370
+ }
1371
+ const uConsequences = getOrCreateClosure(consequencesClosure, callerRoot);
1372
+ const vCauses = getOrCreateClosure(causesClosure, targetRoot);
1373
+ // 1. Add direct relationship
1374
+ uConsequences.add(targetRoot);
1375
+ vCauses.add(callerRoot);
1376
+ // 2. For each X in causesClosure(U): X→U→V means X→V
1377
+ const uCausesSet = causesClosure.get(callerRoot);
1378
+ if (uCausesSet) {
1379
+ for (const x of uCausesSet) {
1380
+ // Skip if this would create a self-loop
1381
+ if (x === targetRoot)
1382
+ continue;
1383
+ const xConsequences = getOrCreateClosure(consequencesClosure, x);
1384
+ xConsequences.add(targetRoot);
1385
+ vCauses.add(x);
1386
+ }
1387
+ }
1388
+ // 3. For each Y in consequencesClosure(V): U→V→Y means U→Y
1389
+ const vConsequencesSet = consequencesClosure.get(targetRoot);
1390
+ if (vConsequencesSet) {
1391
+ for (const y of vConsequencesSet) {
1392
+ // Skip if this would create a self-loop
1393
+ if (y === callerRoot)
1394
+ continue;
1395
+ const yCauses = getOrCreateClosure(causesClosure, y);
1396
+ yCauses.add(callerRoot);
1397
+ uConsequences.add(y);
1398
+ }
1399
+ }
1400
+ // 4. Cross-product: for each X in causesClosure(U) and Y in consequencesClosure(V): X→Y
1401
+ if (uCausesSet && vConsequencesSet) {
1402
+ for (const x of uCausesSet) {
1403
+ const xConsequences = getOrCreateClosure(consequencesClosure, x);
1404
+ for (const y of vConsequencesSet) {
1405
+ // Skip if this would create a self-loop
1406
+ if (x === y)
1407
+ continue;
1408
+ xConsequences.add(y);
1409
+ const yCauses = getOrCreateClosure(causesClosure, y);
1410
+ yCauses.add(x);
1411
+ }
1412
+ }
1413
+ }
1414
+ }
1415
+ /**
1416
+ * Checks if there's a path from start to end in the dependency graph, excluding a specific node
1417
+ * Uses BFS to find any path that doesn't go through the excluded node
1418
+ * @param start - Starting node
1419
+ * @param end - Target node
1420
+ * @param exclude - Node to exclude from the path
1421
+ * @returns true if a path exists without going through the excluded node
1422
+ */
1423
+ function hasPathExcluding(start, end, exclude) {
1424
+ if (start === end)
1425
+ return true;
1426
+ if (start === exclude)
1427
+ return false;
1428
+ const visited = new Set();
1429
+ const queue = [start];
1430
+ visited.add(start);
1431
+ visited.add(exclude); // Pre-mark excluded node as visited to skip it
1432
+ while (queue.length > 0) {
1433
+ const current = queue.shift();
1434
+ const triggers = effectTriggers.get(current);
1435
+ if (!triggers)
1436
+ continue;
1437
+ for (const next of triggers) {
1438
+ if (next === end)
1439
+ return true;
1440
+ if (!visited.has(next)) {
1441
+ visited.add(next);
1442
+ queue.push(next);
1443
+ }
1444
+ }
1445
+ }
1446
+ return false;
1447
+ }
1448
+ /**
1449
+ * Removes all edges involving the given effect from the dependency graph
1450
+ * Also cleans up transitive closures by propagating cleanup to all affected effects
1451
+ * Called when an effect is stopped/cleaned up
1452
+ * @param effect - The effect being cleaned up
1453
+ */
1454
+ function cleanupEffectFromGraph(effect) {
1455
+ const root = getRoot(effect);
1456
+ // Get closures before removing direct edges (needed for propagation)
1457
+ const rootCauses = causesClosure.get(root);
1458
+ const rootConsequences = consequencesClosure.get(root);
1459
+ // Remove from effectTriggers (outgoing edges)
1460
+ const triggers = effectTriggers.get(root);
1461
+ if (triggers) {
1462
+ // Remove this root from all targets' effectTriggeredBy sets
1463
+ for (const targetRoot of triggers) {
1464
+ const triggeredBy = effectTriggeredBy.get(targetRoot);
1465
+ triggeredBy?.delete(root);
1466
+ }
1467
+ effectTriggers.delete(root);
1468
+ }
1469
+ // Remove from effectTriggeredBy (incoming edges)
1470
+ const triggeredBy = effectTriggeredBy.get(root);
1471
+ if (triggeredBy) {
1472
+ // Remove this root from all sources' effectTriggers sets
1473
+ for (const sourceRoot of triggeredBy) {
1474
+ const triggers = effectTriggers.get(sourceRoot);
1475
+ triggers?.delete(root);
1476
+ }
1477
+ effectTriggeredBy.delete(root);
1478
+ }
1479
+ // Propagate closure cleanup to all affected effects
1480
+ // When removing B from A → B → C:
1481
+ // - Remove B from causesClosure(C) and consequencesClosure(A)
1482
+ // - For each X in causesClosure(B): remove C from consequencesClosure(X) if B was the only path
1483
+ // - For each Y in consequencesClosure(B): remove A from causesClosure(Y) if B was the only path
1484
+ // - Remove transitive relationships that depended on B
1485
+ if (rootCauses) {
1486
+ // For each X that triggers root: remove root from X's consequences
1487
+ // Only remove root's consequences if no alternate path exists
1488
+ for (const causeRoot of rootCauses) {
1489
+ const causeConsequences = consequencesClosure.get(causeRoot);
1490
+ if (causeConsequences) {
1491
+ // Remove root itself (it's being cleaned up)
1492
+ causeConsequences.delete(root);
1493
+ // Only remove consequences of root if there's no alternate path from causeRoot to them
1494
+ if (rootConsequences) {
1495
+ for (const consequence of rootConsequences) {
1496
+ // Check if causeRoot can still reach consequence without going through root
1497
+ if (!hasPathExcluding(causeRoot, consequence, root)) {
1498
+ causeConsequences.delete(consequence);
1499
+ }
1500
+ }
1501
+ }
1502
+ }
1503
+ }
1504
+ }
1505
+ if (rootConsequences) {
1506
+ // For each Y that root triggers: remove root from Y's causes
1507
+ // Only remove root's causes if no alternate path exists
1508
+ for (const consequenceRoot of rootConsequences) {
1509
+ const consequenceCauses = causesClosure.get(consequenceRoot);
1510
+ if (consequenceCauses) {
1511
+ // Remove root itself (it's being cleaned up)
1512
+ consequenceCauses.delete(root);
1513
+ // Only remove causes of root if there's no alternate path from them to consequenceRoot
1514
+ if (rootCauses) {
1515
+ for (const cause of rootCauses) {
1516
+ // Check if cause can still reach consequenceRoot without going through root
1517
+ if (!hasPathExcluding(cause, consequenceRoot, root)) {
1518
+ consequenceCauses.delete(cause);
1519
+ }
1520
+ }
1521
+ }
1522
+ }
1523
+ }
1524
+ }
1525
+ // Cross-product cleanup: for each X in causesClosure(B) and Y in consequencesClosure(B),
1526
+ // remove X→Y if B was the only path connecting them
1527
+ if (rootCauses && rootConsequences) {
1528
+ for (const x of rootCauses) {
1529
+ const xConsequences = consequencesClosure.get(x);
1530
+ if (xConsequences) {
1531
+ for (const y of rootConsequences) {
1532
+ // Check if there's still a path from X to Y without going through root
1533
+ // Use BFS to find any path that doesn't include root
1534
+ if (!hasPathExcluding(x, y, root)) {
1535
+ xConsequences.delete(y);
1536
+ const yCauses = causesClosure.get(y);
1537
+ yCauses?.delete(x);
1538
+ }
1539
+ }
1540
+ }
1541
+ }
1542
+ }
1543
+ // Finally, delete the closures for this effect
1544
+ causesClosure.delete(root);
1545
+ consequencesClosure.delete(root);
1546
+ }
1547
+ // Track currently executing effects to prevent re-execution
1548
+ // These are all the effects triggered under `activeEffect`
1549
+ let batchQueue;
1550
+ const batchCleanups = new Set();
1551
+ /**
1552
+ * Computes and caches in-degrees for all effects in the batch
1553
+ * Called once when batch starts or when new effects are added
1554
+ */
1555
+ function computeAllInDegrees(batch) {
1556
+ const activeEffect = getActiveEffect();
1557
+ const activeRoot = activeEffect ? getRoot(activeEffect) : null;
1558
+ // Reset all in-degrees
1559
+ batch.inDegrees.clear();
1560
+ for (const [root] of batch.all) {
1561
+ let inDegree = 0;
1562
+ const causes = causesClosure.get(root);
1563
+ if (causes) {
1564
+ for (const causeRoot of causes) {
1565
+ // Only count if it's in the batch and not the active/self effect
1566
+ if (batch.all.has(causeRoot) && causeRoot !== activeRoot && causeRoot !== root) {
1567
+ inDegree++;
1568
+ }
1569
+ }
1570
+ }
1571
+ batch.inDegrees.set(root, inDegree);
1572
+ }
1573
+ }
1574
+ /**
1575
+ * Decrements in-degrees of all effects that depend on the executed effect
1576
+ * Called after an effect is executed to update the cached in-degrees
1577
+ */
1578
+ function decrementInDegreesForExecuted(batch, executedRoot) {
1579
+ // Get all effects that this executed effect triggers
1580
+ const consequences = consequencesClosure.get(executedRoot);
1581
+ if (!consequences)
1582
+ return;
1583
+ for (const consequenceRoot of consequences) {
1584
+ // Only update if it's still in the batch
1585
+ if (batch.all.has(consequenceRoot)) {
1586
+ const currentDegree = batch.inDegrees.get(consequenceRoot) ?? 0;
1587
+ if (currentDegree > 0) {
1588
+ batch.inDegrees.set(consequenceRoot, currentDegree - 1);
1589
+ }
1590
+ }
1591
+ }
1592
+ }
1593
+ /**
1594
+ * Computes the in-degree (number of dependencies) for an effect in the current batch
1595
+ * Uses causesClosure to count all effects (directly or indirectly) that trigger this effect
1596
+ * @param root - Root function of the effect
1597
+ * @param batchEffects - Map of all effects in current batch (todos - effects that still need execution)
1598
+ * @returns Number of effects in batch that trigger this effect (directly or indirectly)
1599
+ *
1600
+ * TODO: Optimization - For large graphs with small batches, iterating over all causes in the closure
1601
+ * can be expensive. Consider maintaining a separate "batch causes" set or caching in-degrees.
1602
+ */
1603
+ /* function computeInDegreeInBatch(
1604
+ root: Function,
1605
+ batchEffects: Map<Function, ScopedCallback>
1606
+ ): number {
1607
+ let inDegree = 0
1608
+ const activeEffect = getActiveEffect()
1609
+ const activeRoot = activeEffect ? getRoot(activeEffect) : null
1610
+
1611
+ // Count effects in batch that trigger this effect (directly or indirectly)
1612
+ // Using causesClosure which contains all transitive causes
1613
+ // Note: batchEffects only contains effects that still need execution (todos),
1614
+ // so we don't need to check if causes have been executed - they're not in the map if executed
1615
+ const causes = causesClosure.get(root)
1616
+ if (causes) {
1617
+ for (const causeRoot of causes) {
1618
+ // Only count if it's in the batch (still needs execution)
1619
+ // BUT: don't count the currently executing effect (active effect)
1620
+ // This handles the case where an effect is triggered during another effect's execution
1621
+ // Note: Self-loops are ignored - they should not appear in closures, but we check to be safe
1622
+ if (batchEffects.has(causeRoot) && causeRoot !== activeRoot && causeRoot !== root) {
1623
+ inDegree++
1624
+ }
1625
+ }
1626
+ }
1627
+
1628
+ return inDegree
1629
+ }
1630
+
1631
+ /**
1632
+ * Finds a path from startRoot to endRoot in the dependency graph
1633
+ * Uses DFS to find the path through direct edges
1634
+ * @param startRoot - Starting effect root
1635
+ * @param endRoot - Target effect root
1636
+ * @param visited - Set of visited nodes (for recursion)
1637
+ * @param path - Current path being explored
1638
+ * @returns Path from startRoot to endRoot, or empty array if no path exists
1639
+ */
1640
+ function findPath(startRoot, endRoot, visited = new Set(), path = []) {
1641
+ if (startRoot === endRoot) {
1642
+ return [...path, endRoot];
1643
+ }
1644
+ if (visited.has(startRoot)) {
1645
+ return [];
1646
+ }
1647
+ visited.add(startRoot);
1648
+ const newPath = [...path, startRoot];
1649
+ const triggers = effectTriggers.get(startRoot);
1650
+ if (triggers) {
1651
+ for (const targetRoot of triggers) {
1652
+ const result = findPath(targetRoot, endRoot, visited, newPath);
1653
+ if (result.length > 0) {
1654
+ return result;
1655
+ }
1656
+ }
1657
+ }
1658
+ return [];
1659
+ }
1660
+ /**
1661
+ * Gets the cycle path when adding an edge would create a cycle
1662
+ * @param callerRoot - Root of the effect that triggers
1663
+ * @param targetRoot - Root of the effect being triggered
1664
+ * @returns Array of effect roots forming the cycle, or empty array if no cycle
1665
+ */
1666
+ function getCyclePathForEdge(callerRoot, targetRoot) {
1667
+ // Find path from targetRoot back to callerRoot (this is the existing path)
1668
+ // Then adding callerRoot -> targetRoot completes the cycle
1669
+ const path = findPath(targetRoot, callerRoot);
1670
+ if (path.length > 0) {
1671
+ // The cycle is: callerRoot -> targetRoot -> ... -> callerRoot
1672
+ return [callerRoot, ...path];
1673
+ }
1674
+ return [];
1675
+ }
1676
+ /**
1677
+ * Checks if adding an edge would create a cycle
1678
+ * Uses causesClosure to check if callerRoot is already a cause of targetRoot
1679
+ * Self-loops (callerRoot === targetRoot) are explicitly ignored and return false
1680
+ * @param callerRoot - Root of the effect that triggers
1681
+ * @param targetRoot - Root of the effect being triggered
1682
+ * @returns true if adding this edge would create a cycle
1683
+ */
1684
+ function wouldCreateCycle(callerRoot, targetRoot) {
1685
+ // Self-loops are explicitly ignored - an effect reading and writing the same property
1686
+ // (e.g., obj.prop++) should not create a dependency relationship
1687
+ if (callerRoot === targetRoot) {
1688
+ return false;
1689
+ }
1690
+ // Check if targetRoot already triggers callerRoot (directly or indirectly)
1691
+ // This would create a cycle: callerRoot -> targetRoot -> ... -> callerRoot
1692
+ // Using consequencesClosure: if targetRoot triggers callerRoot, then callerRoot is in consequencesClosure(targetRoot)
1693
+ const targetConsequences = consequencesClosure.get(targetRoot);
1694
+ if (targetConsequences?.has(callerRoot)) {
1695
+ return true; // Cycle detected: targetRoot -> ... -> callerRoot, and we're adding callerRoot -> targetRoot
1696
+ }
1697
+ return false;
1698
+ }
1699
+ /**
1700
+ * Adds an effect to the batch queue
1701
+ * @param effect - The effect to add
1702
+ * @param caller - The active effect that triggered this one (optional)
1703
+ * @param immediate - If true, don't create edges in the dependency graph
1704
+ */
1705
+ function addToBatch(effect, caller, immediate) {
1706
+ if (!batchQueue)
1707
+ return;
1708
+ const root = getRoot(effect);
1709
+ // 1. Add to batch first (needed for cycle detection)
1710
+ batchQueue.all.set(root, effect);
1711
+ // 2. Add to global graph (if caller exists and not immediate) - USE ROOTS ONLY
1712
+ // When immediate is true, don't create edges - the effect is not considered as a consequence
1713
+ if (caller && !immediate) {
1714
+ const callerRoot = getRoot(caller);
1715
+ // Check for cycle BEFORE adding edge
1716
+ // We check if adding callerRoot -> root would create a cycle
1717
+ // This means checking if root already triggers callerRoot (directly or transitively)
1718
+ if (wouldCreateCycle(callerRoot, root)) {
1719
+ // Cycle detected! Get the full cycle path for debugging
1720
+ const cyclePath = getCyclePathForEdge(callerRoot, root);
1721
+ const cycleMessage = cyclePath.length > 0
1722
+ ? `Cycle detected: ${cyclePath.map((r) => r.name || r.toString()).join(' → ')}`
1723
+ : `Cycle detected: ${callerRoot.name || callerRoot.toString()} → ${root.name || root.toString()} (and back)`;
1724
+ const cycleHandling = options.cycleHandling;
1725
+ // In strict mode, we throw immediately on detection
1726
+ if (cycleHandling === 'strict') {
1727
+ batchQueue.all.delete(root);
1728
+ const causalChain = getTriggerChain(effect);
1729
+ const creationStack = effectCreationStacks.get(root);
1730
+ throw new ReactiveError(`[reactive] Strict Cycle Prevention: ${cycleMessage}`, {
1731
+ code: ReactiveErrorCode.CycleDetected,
1732
+ cycle: cyclePath.map((r) => r.name || r.toString()),
1733
+ details: cycleMessage,
1734
+ causalChain,
1735
+ creationStack,
1736
+ });
1737
+ }
1738
+ switch (cycleHandling) {
1739
+ case 'throw': {
1740
+ // Remove from batch before throwing
1741
+ batchQueue.all.delete(root);
1742
+ const causalChain = getTriggerChain(effect);
1743
+ const creationStack = effectCreationStacks.get(root);
1744
+ throw new ReactiveError(`[reactive] ${cycleMessage}`, {
1745
+ code: ReactiveErrorCode.CycleDetected,
1746
+ cycle: cyclePath.map((r) => r.name || r.toString()),
1747
+ details: cycleMessage,
1748
+ causalChain,
1749
+ creationStack,
1750
+ });
1751
+ }
1752
+ case 'warn':
1753
+ options.warn(`[reactive] ${cycleMessage}`);
1754
+ // Don't add the edge, break the cycle
1755
+ batchQueue.all.delete(root);
1756
+ return;
1757
+ case 'break':
1758
+ // Silently break cycle, don't add the edge
1759
+ batchQueue.all.delete(root);
1760
+ return;
1761
+ }
1762
+ }
1763
+ addGraphEdge(callerRoot, root); // Add to persistent graph using roots
1764
+ }
1765
+ }
1766
+ /**
1767
+ * Adds a cleanup function to be called when the current batch of effects completes
1768
+ * @param cleanup - The cleanup function to add
1769
+ */
1770
+ function addBatchCleanup(cleanup) {
1771
+ if (!batchQueue)
1772
+ cleanup();
1773
+ else
1774
+ batchCleanups.add(cleanup);
1775
+ }
1776
+ /**
1777
+ * Semantic alias for `addBatchCleanup` - defers work to the end of the current reactive batch.
1778
+ *
1779
+ * Use this when an effect needs to perform an action that would modify state the effect depends on,
1780
+ * which would create a reactive cycle. The deferred callback runs after all effects complete.
1781
+ *
1782
+ * @param callback - The callback to defer until after the current batch completes
1783
+ *
1784
+ * @example
1785
+ * ```typescript
1786
+ * effect(() => {
1787
+ * processData()
1788
+ *
1789
+ * // Defer to avoid cycle (createMovement modifies state this effect reads)
1790
+ * defer(() => {
1791
+ * createMovement(data)
1792
+ * })
1793
+ * })
1794
+ * ```
1795
+ */
1796
+ const defer = addBatchCleanup;
1797
+ /**
1798
+ * Gets a cycle path for debugging
1799
+ * Uses DFS to find cycles in the batch
1800
+ * @param batchQueue - The batch queue
1801
+ * @returns Array of effect roots forming a cycle
1802
+ */
1803
+ function getCyclePath(batchQueue) {
1804
+ // If all effects have in-degree > 0, there must be a cycle
1805
+ // Use DFS to find it
1806
+ const visited = new Set();
1807
+ const recursionStack = new Set();
1808
+ const path = [];
1809
+ for (const [root] of batchQueue.all) {
1810
+ if (visited.has(root))
1811
+ continue;
1812
+ const cycle = findCycle(root, visited, recursionStack, path, batchQueue);
1813
+ if (cycle.length > 0) {
1814
+ return cycle;
1815
+ }
1816
+ }
1817
+ return [];
1818
+ }
1819
+ function findCycle(root, visited, recursionStack, path, batchQueue) {
1820
+ if (recursionStack.has(root)) {
1821
+ // Found a cycle! Return the path from the cycle start to root
1822
+ const cycleStart = path.indexOf(root);
1823
+ return path.slice(cycleStart).concat([root]);
1824
+ }
1825
+ if (visited.has(root)) {
1826
+ return [];
1827
+ }
1828
+ visited.add(root);
1829
+ recursionStack.add(root);
1830
+ path.push(root);
1831
+ // Follow edges to effects in the batch
1832
+ // Use direct edges (effectTriggers) for cycle detection
1833
+ const triggers = effectTriggers.get(root);
1834
+ if (triggers) {
1835
+ for (const targetRoot of triggers) {
1836
+ if (batchQueue.all.has(targetRoot)) {
1837
+ const cycle = findCycle(targetRoot, visited, recursionStack, path, batchQueue);
1838
+ if (cycle.length > 0) {
1839
+ return cycle;
1840
+ }
1841
+ }
1842
+ }
1843
+ }
1844
+ path.pop();
1845
+ recursionStack.delete(root);
1846
+ return [];
1847
+ }
1848
+ /**
1849
+ * Executes the next effect in dependency order (using cached in-degrees)
1850
+ * Finds an effect with in-degree 0 and executes it
1851
+ * @returns The return value of the executed effect, or null if batch is complete
1852
+ */
1853
+ function executeNext(effectuatedRoots) {
1854
+ // Find an effect with in-degree 0 using cached values
1855
+ let nextEffect = null;
1856
+ let nextRoot = null;
1857
+ // Find an effect with in-degree 0 (no dependencies in batch that still need execution)
1858
+ // Using cached in-degrees for O(n) lookup instead of O(n²)
1859
+ for (const [root, effect] of batchQueue.all) {
1860
+ const inDegree = batchQueue.inDegrees.get(root) ?? 0;
1861
+ if (inDegree === 0) {
1862
+ nextEffect = effect;
1863
+ nextRoot = root;
1864
+ break;
1865
+ }
1866
+ }
1867
+ if (!nextEffect) {
1868
+ // No effect with in-degree 0 - there must be a cycle
1869
+ // If all effects have dependencies, it means there's a circular dependency
1870
+ if (batchQueue.all.size > 0) {
1871
+ let cycle = getCyclePath(batchQueue);
1872
+ // If we couldn't find a cycle path using direct edges, try using closures
1873
+ // (transitive relationships) - if all effects have in-degree > 0, there must be a cycle
1874
+ if (cycle.length === 0) {
1875
+ // Try to find a cycle using consequencesClosure (transitive relationships)
1876
+ // Note: Self-loops are ignored - we only look for cycles between different effects
1877
+ for (const [root] of batchQueue.all) {
1878
+ const consequences = consequencesClosure.get(root);
1879
+ if (consequences) {
1880
+ // Check if any consequence in the batch also has root as a consequence
1881
+ for (const consequence of consequences) {
1882
+ // Skip self-loops - they are ignored
1883
+ if (consequence === root)
1884
+ continue;
1885
+ if (batchQueue.all.has(consequence)) {
1886
+ const consequenceConsequences = consequencesClosure.get(consequence);
1887
+ if (consequenceConsequences?.has(root)) {
1888
+ // Found cycle: root -> consequence -> root
1889
+ cycle = [root, consequence, root];
1890
+ break;
1891
+ }
1892
+ }
1893
+ }
1894
+ if (cycle.length > 0)
1895
+ break;
1896
+ }
1897
+ }
1898
+ }
1899
+ const cycleMessage = cycle.length > 0
1900
+ ? `Cycle detected: ${cycle.map((r) => r.name || '<anonymous>').join(' → ')}`
1901
+ : 'Cycle detected in effect batch - all effects have dependencies that prevent execution';
1902
+ const cycleHandling = options.cycleHandling;
1903
+ switch (cycleHandling) {
1904
+ case 'throw':
1905
+ throw new ReactiveError(`[reactive] ${cycleMessage}`);
1906
+ case 'warn': {
1907
+ options.warn(`[reactive] ${cycleMessage}`);
1908
+ // Break the cycle by executing one effect anyway
1909
+ const firstEffect = batchQueue.all.values().next().value;
1910
+ if (firstEffect) {
1911
+ const firstRoot = getRoot(firstEffect);
1912
+ batchQueue.all.delete(firstRoot);
1913
+ batchQueue.inDegrees.delete(firstRoot);
1914
+ return firstEffect();
1915
+ }
1916
+ break;
1917
+ }
1918
+ case 'break': {
1919
+ // Silently break cycle
1920
+ const firstEffect2 = batchQueue.all.values().next().value;
1921
+ if (firstEffect2) {
1922
+ const firstRoot2 = getRoot(firstEffect2);
1923
+ batchQueue.all.delete(firstRoot2);
1924
+ batchQueue.inDegrees.delete(firstRoot2);
1925
+ return firstEffect2();
1926
+ }
1927
+ break;
1928
+ }
1929
+ }
1930
+ }
1931
+ return null; // Batch complete
1932
+ }
1933
+ effectuatedRoots.push(getRoot(nextEffect));
1934
+ // Execute the effect
1935
+ const result = nextEffect();
1936
+ // Remove from batch and update in-degrees of dependents
1937
+ batchQueue.all.delete(nextRoot);
1938
+ batchQueue.inDegrees.delete(nextRoot);
1939
+ decrementInDegreesForExecuted(batchQueue, nextRoot);
1940
+ return result;
1941
+ }
1942
+ // Track which sub-effects have been executed to prevent infinite loops
1943
+ // These are all the effects triggered under `activeEffect` and all their sub-effects
1944
+ function batch(effect, immediate) {
1945
+ if (!Array.isArray(effect))
1946
+ effect = [effect];
1947
+ const roots = effect.map(getRoot);
1948
+ if (batchQueue) {
1949
+ // Nested batch - add to existing
1950
+ options?.chain(roots, getRoot(getActiveEffect()));
1951
+ const caller = getActiveEffect();
1952
+ for (let i = 0; i < effect.length; i++) {
1953
+ addToBatch(effect[i], caller, immediate === 'immediate');
1954
+ }
1955
+ if (immediate) {
1956
+ // Execute immediately (before batch returns)
1957
+ for (let i = 0; i < effect.length; i++) {
1958
+ try {
1959
+ effect[i]();
1960
+ }
1961
+ finally {
1962
+ const root = getRoot(effect[i]);
1963
+ batchQueue.all.delete(root);
1964
+ }
1965
+ }
1966
+ }
1967
+ // Otherwise, effects will be picked up in next executeNext() call
1968
+ }
1969
+ else {
1970
+ // New batch - initialize
1971
+ options.beginChain(roots);
1972
+ batchQueue = {
1973
+ all: new Map(),
1974
+ inDegrees: new Map(),
1975
+ };
1976
+ // Add initial effects
1977
+ const caller = getActiveEffect();
1978
+ for (let i = 0; i < effect.length; i++) {
1979
+ addToBatch(effect[i], caller, immediate === 'immediate');
1980
+ }
1981
+ const effectuatedRoots = [];
1982
+ computeAllInDegrees(batchQueue);
1983
+ if (immediate) {
1984
+ // Execute immediately (before batch returns)
1985
+ const firstReturn = {};
1986
+ try {
1987
+ for (let i = 0; i < effect.length; i++) {
1988
+ try {
1989
+ const rv = effect[i]();
1990
+ if (rv !== undefined && !('value' in firstReturn))
1991
+ firstReturn.value = rv;
1992
+ }
1993
+ finally {
1994
+ const root = getRoot(effect[i]);
1995
+ batchQueue.all.delete(root);
1996
+ }
1997
+ }
1998
+ // After immediate execution, execute any effects that were triggered during execution
1999
+ // This is important for @atomic decorator - effects triggered inside should still run
2000
+ while (batchQueue.all.size > 0) {
2001
+ if (effectuatedRoots.length > options.maxEffectChain) {
2002
+ const cycle = findCycleInChain(effectuatedRoots);
2003
+ const trace = formatRoots(effectuatedRoots);
2004
+ const message = cycle
2005
+ ? `Max effect chain reached (cycle detected: ${formatRoots(cycle)})`
2006
+ : `Max effect chain reached (trace: ${trace})`;
2007
+ const queuedRoots = batchQueue ? Array.from(batchQueue.all.keys()) : [];
2008
+ const queued = queuedRoots.map((r) => r.name || '<anonymous>');
2009
+ const debugInfo = {
2010
+ code: ReactiveErrorCode.MaxDepthExceeded,
2011
+ effectuatedRoots,
2012
+ cycle,
2013
+ trace,
2014
+ maxEffectChain: options.maxEffectChain,
2015
+ queued: queued.slice(0, 50),
2016
+ queuedCount: queued.length,
2017
+ // Try to get causation for the last effect
2018
+ causalChain: effectuatedRoots.length > 0
2019
+ ? getTriggerChain(batchQueue.all.get(effectuatedRoots[effectuatedRoots.length - 1]))
2020
+ : [],
2021
+ };
2022
+ switch (options.maxEffectReaction) {
2023
+ case 'throw':
2024
+ throw new ReactiveError(`[reactive] ${message}`, debugInfo);
2025
+ case 'debug':
2026
+ // biome-ignore lint/suspicious/noDebugger: This is the whole point here
2027
+ debugger;
2028
+ throw new ReactiveError(`[reactive] ${message}`, debugInfo);
2029
+ case 'warn':
2030
+ options.warn(`[reactive] ${message} (queued: ${queued.slice(0, 10).join(', ')}${queued.length > 10 ? ', …' : ''})`);
2031
+ break;
2032
+ }
2033
+ }
2034
+ if (!batchQueue || batchQueue.all.size === 0)
2035
+ break;
2036
+ const rv = executeNext(effectuatedRoots);
2037
+ // If executeNext() returned null but batch is not empty, it means a cycle was detected
2038
+ // and an error was thrown, so we won't reach here
2039
+ if (rv !== undefined && !('value' in firstReturn))
2040
+ firstReturn.value = rv;
2041
+ }
2042
+ const cleanups = Array.from(batchCleanups);
2043
+ batchCleanups.clear();
2044
+ for (const cleanup of cleanups)
2045
+ cleanup();
2046
+ return firstReturn.value;
2047
+ }
2048
+ finally {
2049
+ batchQueue = undefined;
2050
+ options.endChain();
2051
+ }
2052
+ }
2053
+ else {
2054
+ // Execute in dependency order
2055
+ const firstReturn = {};
2056
+ try {
2057
+ while (batchQueue.all.size > 0) {
2058
+ if (effectuatedRoots.length > options.maxEffectChain) {
2059
+ const cycle = findCycleInChain(effectuatedRoots);
2060
+ const trace = formatRoots(effectuatedRoots);
2061
+ const message = cycle
2062
+ ? `Max effect chain reached (cycle detected: ${formatRoots(cycle)})`
2063
+ : `Max effect chain reached (trace: ${trace})`;
2064
+ const queuedRoots = batchQueue ? Array.from(batchQueue.all.keys()) : [];
2065
+ const queued = queuedRoots.map((r) => r.name || '<anonymous>');
2066
+ const debugInfo = {
2067
+ code: ReactiveErrorCode.MaxDepthExceeded,
2068
+ effectuatedRoots,
2069
+ cycle,
2070
+ trace,
2071
+ maxEffectChain: options.maxEffectChain,
2072
+ queued: queued.slice(0, 50),
2073
+ queuedCount: queued.length,
2074
+ // Try to get causation for the last effect
2075
+ causalChain: effectuatedRoots.length > 0
2076
+ ? getTriggerChain(batchQueue.all.get(effectuatedRoots[effectuatedRoots.length - 1]))
2077
+ : [],
2078
+ };
2079
+ switch (options.maxEffectReaction) {
2080
+ case 'throw':
2081
+ throw new ReactiveError(`[reactive] ${message}`, debugInfo);
2082
+ case 'debug':
2083
+ // biome-ignore lint/suspicious/noDebugger: This is the whole point here
2084
+ debugger;
2085
+ throw new ReactiveError(`[reactive] ${message}`, debugInfo);
2086
+ case 'warn':
2087
+ options.warn(`[reactive] ${message} (queued: ${queued.slice(0, 10).join(', ')}${queued.length > 10 ? ', …' : ''})`);
2088
+ break;
2089
+ }
2090
+ }
2091
+ const rv = executeNext(effectuatedRoots);
2092
+ // executeNext() returns null when batch is complete or cycle detected (throws error)
2093
+ // But functions can legitimately return null, so we check batchQueue.all.size instead
2094
+ if (batchQueue.all.size === 0) {
2095
+ // Batch complete
2096
+ break;
2097
+ }
2098
+ // If executeNext() returned null but batch is not empty, it means a cycle was detected
2099
+ // and an error was thrown, so we won't reach here
2100
+ if (rv !== undefined && !('value' in firstReturn))
2101
+ firstReturn.value = rv;
2102
+ // Note: executeNext() already removed it from batchQueue, so we track by count
2103
+ }
2104
+ const cleanups = Array.from(batchCleanups);
2105
+ batchCleanups.clear();
2106
+ for (const cleanup of cleanups)
2107
+ cleanup();
2108
+ return firstReturn.value;
2109
+ }
2110
+ finally {
2111
+ batchQueue = undefined;
2112
+ options.endChain();
2113
+ }
2114
+ }
2115
+ }
2116
+ }
2117
+ /**
2118
+ * Decorator that makes methods atomic - batches all effects triggered within the method
2119
+ */
2120
+ const atomic = decorator({
2121
+ method(original) {
2122
+ return function (...args) {
2123
+ return batch(markWithRoot(() => original.apply(this, args), original), 'immediate');
2124
+ };
2125
+ },
2126
+ default(original) {
2127
+ return function (...args) {
2128
+ return batch(markWithRoot(() => original.apply(this, args), original), 'immediate');
2129
+ };
2130
+ },
2131
+ });
2132
+ const fr = new FinalizationRegistry((f) => f());
2133
+ /**
2134
+ * @param fn - The effect function to run - provides the cleaner
2135
+ * @returns The cleanup function
2136
+ */
2137
+ /**
2138
+ * Creates a reactive effect that automatically re-runs when dependencies change
2139
+ * @param fn - The effect function that provides dependencies and may return a cleanup function or Promise
2140
+ * @param options - Options for effect execution
2141
+ * @returns A cleanup function to stop the effect
2142
+ */
2143
+ function effect(
2144
+ //biome-ignore lint/suspicious/noConfusingVoidType: We have to
2145
+ fn, effectOptions) {
2146
+ // Ensure zone is hooked if asyncZone option is enabled (lazy initialization)
2147
+ // Inject batch function to allow atomic game loops in requestAnimationFrame
2148
+ ensureZoneHooked(batch);
2149
+ // Use per-effect asyncMode or fall back to global option
2150
+ const asyncMode = effectOptions?.asyncMode ?? options.asyncMode ?? 'cancel';
2151
+ if (options.introspection.enableHistory) {
2152
+ const stack = new Error().stack;
2153
+ if (stack) {
2154
+ // Clean up the stack trace to remove internal frames
2155
+ const cleanStack = stack.split('\n').slice(2).join('\n');
2156
+ effectCreationStacks.set(getRoot(fn), cleanStack);
2157
+ }
2158
+ }
2159
+ let cleanup = null;
2160
+ // capture the parent effect at creation time for ascend
2161
+ const parentsForAscend = captureEffectStack();
2162
+ const tracked = markWithRoot((cb) => withEffect(runEffect, cb), fn);
2163
+ const ascend = (cb) => withEffectStack(parentsForAscend, cb);
2164
+ let effectStopped = false;
2165
+ let hasReacted = false;
2166
+ let runningPromise = null;
2167
+ let cancelPrevious = null;
2168
+ function runEffect() {
2169
+ // Clear previous dependencies
2170
+ if (cleanup) {
2171
+ const prevCleanup = cleanup;
2172
+ cleanup = null;
2173
+ withEffect(undefined, () => prevCleanup());
2174
+ }
2175
+ // Handle async modes when effect is retriggered
2176
+ if (runningPromise) {
2177
+ if (asyncMode === 'cancel' && cancelPrevious) {
2178
+ // Cancel previous execution
2179
+ cancelPrevious();
2180
+ cancelPrevious = null;
2181
+ runningPromise = null;
2182
+ }
2183
+ else if (asyncMode === 'ignore') {
2184
+ // Ignore new execution while async work is running
2185
+ return;
2186
+ }
2187
+ // Note: 'queue' mode not yet implemented
2188
+ }
2189
+ // The effect has been stopped after having been planned
2190
+ if (effectStopped)
2191
+ return;
2192
+ options.enter(getRoot(fn));
2193
+ let reactionCleanup;
2194
+ let result;
2195
+ try {
2196
+ result = withEffect(runEffect, () => fn({ tracked, ascend, reaction: hasReacted }));
2197
+ if (result &&
2198
+ typeof result !== 'function' &&
2199
+ (typeof result !== 'object' || !('then' in result)))
2200
+ throw new ReactiveError(`[reactive] Effect returned a non-function value: ${result}`);
2201
+ // Check if result is a Promise (async effect)
2202
+ if (result && typeof result === 'object' && typeof result.then === 'function') {
2203
+ const originalPromise = result;
2204
+ // Create a cancellation promise that we can reject
2205
+ let cancelReject = null;
2206
+ const cancelPromise = new Promise((_, reject) => {
2207
+ cancelReject = reject;
2208
+ });
2209
+ const cancelError = new ReactiveError('[reactive] Effect canceled due to dependency change');
2210
+ // Race between the actual promise and cancellation
2211
+ // If canceled, the race rejects, which will propagate through any promise chain
2212
+ runningPromise = Promise.race([originalPromise, cancelPromise]);
2213
+ // Store the cancellation function
2214
+ cancelPrevious = () => {
2215
+ if (cancelReject) {
2216
+ cancelReject(cancelError);
2217
+ }
2218
+ };
2219
+ // Wrap the original promise chain so cancellation propagates
2220
+ // This ensures that when we cancel, the original promise's .catch() handlers are triggered
2221
+ // We do this by rejecting the race promise, which makes the original promise chain see the rejection
2222
+ // through the zone-wrapped .then()/.catch() handlers
2223
+ }
2224
+ else {
2225
+ // Synchronous result - treat as cleanup function
2226
+ reactionCleanup = result;
2227
+ }
2228
+ }
2229
+ finally {
2230
+ hasReacted = true;
2231
+ options.leave(fn);
2232
+ }
2233
+ // Create cleanup function for next run
2234
+ cleanup = () => {
2235
+ cleanup = null;
2236
+ reactionCleanup?.();
2237
+ // Remove this effect from all reactive objects it's watching
2238
+ const effectObjects = effectToReactiveObjects.get(runEffect);
2239
+ if (effectObjects) {
2240
+ for (const reactiveObj of effectObjects) {
2241
+ const objectWatchers = watchers.get(reactiveObj);
2242
+ if (objectWatchers) {
2243
+ for (const [prop, deps] of objectWatchers.entries()) {
2244
+ deps.delete(runEffect);
2245
+ if (deps.size === 0) {
2246
+ objectWatchers.delete(prop);
2247
+ }
2248
+ }
2249
+ if (objectWatchers.size === 0) {
2250
+ watchers.delete(reactiveObj);
2251
+ }
2252
+ }
2253
+ }
2254
+ effectToReactiveObjects.delete(runEffect);
2255
+ }
2256
+ // Invoke all child stops (recursive via subEffectCleanup calling its own mainCleanup)
2257
+ const children = effectChildren.get(runEffect);
2258
+ if (children) {
2259
+ for (const childCleanup of children)
2260
+ childCleanup();
2261
+ effectChildren.delete(runEffect);
2262
+ }
2263
+ };
2264
+ }
2265
+ // Mark the runEffect callback with the original function as its root
2266
+ markWithRoot(runEffect, fn);
2267
+ // Register strict mode if enabled
2268
+ if (effectOptions?.opaque) {
2269
+ opaqueEffects.add(runEffect);
2270
+ }
2271
+ if (isDevtoolsEnabled()) {
2272
+ registerEffectForDebug(runEffect);
2273
+ }
2274
+ batch(runEffect, 'immediate');
2275
+ const parent = parentsForAscend[0];
2276
+ // Store parent relationship for hierarchy traversal
2277
+ effectParent.set(runEffect, parent);
2278
+ // Only ROOT effects are registered for GC cleanup and zone tracking
2279
+ const isRootEffect = !parent;
2280
+ const stopEffect = () => {
2281
+ if (effectStopped)
2282
+ return;
2283
+ effectStopped = true;
2284
+ // Cancel any running async work
2285
+ if (cancelPrevious) {
2286
+ cancelPrevious();
2287
+ cancelPrevious = null;
2288
+ runningPromise = null;
2289
+ }
2290
+ cleanup?.();
2291
+ // Clean up dependency graph edges
2292
+ cleanupEffectFromGraph(runEffect);
2293
+ fr.unregister(stopEffect);
2294
+ };
2295
+ if (isRootEffect) {
2296
+ const callIfCollected = () => stopEffect();
2297
+ fr.register(callIfCollected, () => {
2298
+ stopEffect();
2299
+ options.garbageCollected(fn);
2300
+ }, stopEffect);
2301
+ return callIfCollected;
2302
+ }
2303
+ // Register this effect to be stopped when the parent effect is cleaned up
2304
+ let children = effectChildren.get(parent);
2305
+ if (!children) {
2306
+ children = new Set();
2307
+ effectChildren.set(parent, children);
2308
+ }
2309
+ const subEffectCleanup = () => {
2310
+ children.delete(subEffectCleanup);
2311
+ if (children.size === 0) {
2312
+ effectChildren.delete(parent);
2313
+ }
2314
+ // Execute this child effect cleanup (which triggers its own mainCleanup)
2315
+ stopEffect();
2316
+ };
2317
+ children.add(subEffectCleanup);
2318
+ return subEffectCleanup;
2319
+ }
2320
+ /**
2321
+ * Executes a function without tracking dependencies but maintains parent cleanup relationship
2322
+ * Effects created inside will still be cleaned up when the parent effect is destroyed
2323
+ * @param fn - The function to execute
2324
+ */
2325
+ function untracked(fn) {
2326
+ // Store current tracking state and temporarily disable it
2327
+ // This prevents the parent effect from tracking dependencies during fn execution
2328
+ const wasTrackingDisabled = getTrackingDisabled();
2329
+ setTrackingDisabled(true);
2330
+ try {
2331
+ return fn();
2332
+ }
2333
+ finally {
2334
+ // Restore tracking state
2335
+ setTrackingDisabled(wasTrackingDisabled);
2336
+ }
2337
+ }
2338
+ /**
2339
+ * Executes a function from a virgin/root context - no parent effect, no tracking
2340
+ * Creates completely independent effects that won't be cleaned up by any parent
2341
+ * @param fn - The function to execute
2342
+ */
2343
+ function root(fn) {
2344
+ let rv;
2345
+ withEffect(undefined, () => {
2346
+ rv = fn();
2347
+ });
2348
+ return rv;
2349
+ }
2350
+ function biDi(received, get, set) {
2351
+ if (typeof get !== 'function') {
2352
+ set = get.set;
2353
+ get = get.get;
2354
+ }
2355
+ const root = getRoot(received);
2356
+ effect(markWithRoot(() => {
2357
+ received(get());
2358
+ }, root));
2359
+ return atomic((value) => {
2360
+ set(value);
2361
+ if (batchQueue?.all.has(root)) {
2362
+ // Remove the effect from the batch queue so it doesn't execute
2363
+ // This prevents circular updates in bidirectional bindings
2364
+ batchQueue.all.delete(root);
2365
+ }
2366
+ });
2367
+ }
2368
+
2369
+ // Track which objects contain which other objects (back-references)
2370
+ const objectParents = new WeakMap();
2371
+ // Track which objects have deep watchers
2372
+ const objectsWithDeepWatchers = new WeakSet();
2373
+ // Track deep watchers per object
2374
+ const deepWatchers = new WeakMap();
2375
+ // Track which effects are doing deep watching
2376
+ const effectToDeepWatchedObjects = new WeakMap();
2377
+ /**
2378
+ * Add a back-reference from child to parent
2379
+ */
2380
+ function addBackReference(child, parent, prop) {
2381
+ let parents = objectParents.get(child);
2382
+ if (!parents) {
2383
+ parents = new Set();
2384
+ objectParents.set(child, parents);
2385
+ }
2386
+ parents.add({ parent, prop });
2387
+ }
2388
+ /**
2389
+ * Remove a back-reference from child to parent
2390
+ */
2391
+ function removeBackReference(child, parent, prop) {
2392
+ const parents = objectParents.get(child);
2393
+ if (parents) {
2394
+ for (const entry of parents) {
2395
+ if (entry.parent === parent && entry.prop === prop) {
2396
+ parents.delete(entry);
2397
+ break;
2398
+ }
2399
+ }
2400
+ if (parents.size === 0) {
2401
+ objectParents.delete(child);
2402
+ }
2403
+ }
2404
+ }
2405
+ /**
2406
+ * Check if an object needs back-references (has deep watchers or parents with deep watchers)
2407
+ */
2408
+ function needsBackReferences(obj) {
2409
+ // Fast path: check if object itself has deep watchers
2410
+ if (objectsWithDeepWatchers.has(obj))
2411
+ return true;
2412
+ // Slow path: check if any parent has deep watchers (recursive)
2413
+ return hasParentWithDeepWatchers(obj);
2414
+ }
2415
+ /**
2416
+ * Bubble up changes through the back-reference chain
2417
+ */
2418
+ function bubbleUpChange(changedObject, evolution) {
2419
+ const parents = objectParents.get(changedObject);
2420
+ if (!parents)
2421
+ return;
2422
+ for (const { parent } of parents) {
2423
+ // Trigger deep watchers on parent
2424
+ const parentDeepWatchers = deepWatchers.get(parent);
2425
+ if (parentDeepWatchers)
2426
+ for (const watcher of parentDeepWatchers)
2427
+ batch(watcher);
2428
+ // Continue bubbling up
2429
+ bubbleUpChange(parent);
2430
+ }
2431
+ }
2432
+ function hasParentWithDeepWatchers(obj) {
2433
+ const parents = objectParents.get(obj);
2434
+ if (!parents)
2435
+ return false;
2436
+ for (const { parent } of parents) {
2437
+ if (objectsWithDeepWatchers.has(parent))
2438
+ return true;
2439
+ if (hasParentWithDeepWatchers(parent))
2440
+ return true;
2441
+ }
2442
+ return false;
2443
+ }
2444
+
2445
+ const states = new WeakMap();
2446
+ function addState(obj, evolution) {
2447
+ obj = unwrap(obj);
2448
+ const next = {};
2449
+ const state = getState(obj);
2450
+ if (state)
2451
+ Object.assign(state, { evolution, next });
2452
+ states.set(obj, next);
2453
+ }
2454
+ /**
2455
+ * Gets the current state of a reactive object for evolution tracking
2456
+ * @param obj - The reactive object
2457
+ * @returns The current state object
2458
+ */
2459
+ function getState(obj) {
2460
+ obj = unwrap(obj);
2461
+ let state = states.get(obj);
2462
+ if (!state) {
2463
+ state = {};
2464
+ states.set(obj, state);
2465
+ }
2466
+ return state;
2467
+ }
2468
+ function collectEffects(obj, evolution, effects, objectWatchers, ...keyChains) {
2469
+ const sourceEffect = getActiveEffect();
2470
+ for (const keys of keyChains)
2471
+ for (const key of keys) {
2472
+ const deps = objectWatchers.get(key);
2473
+ if (deps)
2474
+ for (const effect of deps) {
2475
+ const runningChain = isRunning(effect);
2476
+ if (runningChain) {
2477
+ options.skipRunningEffect(effect, runningChain);
2478
+ continue;
2479
+ }
2480
+ effects.add(effect);
2481
+ const trackers = effectTrackers.get(effect);
2482
+ recordTriggerLink(sourceEffect, effect, obj, key, evolution);
2483
+ if (trackers) {
2484
+ for (const tracker of trackers)
2485
+ tracker(obj, evolution, key);
2486
+ trackers.delete(effect);
2487
+ }
2488
+ }
2489
+ }
2490
+ }
2491
+ /**
2492
+ * Triggers effects for a single property change
2493
+ * @param obj - The object that changed
2494
+ * @param evolution - The type of change
2495
+ * @param prop - The property that changed
2496
+ */
2497
+ function touched1(obj, evolution, prop) {
2498
+ touched(obj, evolution, [prop]);
2499
+ }
2500
+ /**
2501
+ * Triggers effects for property changes
2502
+ * @param obj - The object that changed
2503
+ * @param evolution - The type of change
2504
+ * @param props - The properties that changed
2505
+ */
2506
+ function touched(obj, evolution, props) {
2507
+ obj = unwrap(obj);
2508
+ addState(obj, evolution);
2509
+ const objectWatchers = watchers.get(obj);
2510
+ if (objectWatchers) {
2511
+ // Note: we have to collect effects to remove duplicates in the specific case when no batch is running
2512
+ const effects = new Set();
2513
+ if (props)
2514
+ collectEffects(obj, evolution, effects, objectWatchers, [allProps], props);
2515
+ else
2516
+ collectEffects(obj, evolution, effects, objectWatchers, objectWatchers.keys());
2517
+ options.touched(obj, evolution, props, effects);
2518
+ batch(Array.from(effects));
2519
+ }
2520
+ // Bubble up changes if this object has deep watchers
2521
+ if (objectsWithDeepWatchers.has(obj)) {
2522
+ bubbleUpChange(obj);
2523
+ }
2524
+ }
2525
+ /**
2526
+ * Triggers only opaque effects for property changes
2527
+ * Used by deep-touch to ensure opaque listeners are notified even when deep optimization is active
2528
+ */
2529
+ function touchedOpaque(obj, evolution, prop) {
2530
+ obj = unwrap(obj);
2531
+ const objectWatchers = watchers.get(obj);
2532
+ if (!objectWatchers)
2533
+ return;
2534
+ const deps = objectWatchers.get(prop);
2535
+ if (!deps)
2536
+ return;
2537
+ const effects = new Set();
2538
+ const sourceEffect = getActiveEffect();
2539
+ for (const effect of deps) {
2540
+ if (!opaqueEffects.has(effect))
2541
+ continue;
2542
+ const runningChain = isRunning(effect);
2543
+ if (runningChain) {
2544
+ options.skipRunningEffect(effect, runningChain);
2545
+ continue;
2546
+ }
2547
+ effects.add(effect);
2548
+ const trackers = effectTrackers.get(effect);
2549
+ recordTriggerLink(sourceEffect, effect, obj, prop, evolution);
2550
+ if (trackers) {
2551
+ for (const tracker of trackers)
2552
+ tracker(obj, evolution, prop);
2553
+ trackers.delete(effect);
2554
+ }
2555
+ }
2556
+ if (effects.size > 0) {
2557
+ options.touched(obj, evolution, [prop], effects);
2558
+ batch(Array.from(effects));
2559
+ }
2560
+ }
2561
+
2562
+ const nonReactiveObjects = new WeakSet();
2563
+ const immutables = new Set();
2564
+ const absent = Symbol('absent');
2565
+ function markNonReactive(...obj) {
2566
+ for (const o of obj) {
2567
+ try {
2568
+ Object.defineProperty(o, nonReactiveMark, {
2569
+ value: true,
2570
+ writable: false,
2571
+ enumerable: false,
2572
+ configurable: false,
2573
+ });
2574
+ }
2575
+ catch { }
2576
+ if (!(nonReactiveMark in o))
2577
+ nonReactiveObjects.add(o);
2578
+ }
2579
+ return obj[0];
2580
+ }
2581
+ function nonReactiveClass(...cls) {
2582
+ for (const c of cls)
2583
+ if (c)
2584
+ c.prototype[nonReactiveMark] = true;
2585
+ return cls[0];
2586
+ }
2587
+ function isNonReactive(obj) {
2588
+ if (obj === null || typeof obj !== 'object')
2589
+ return true;
2590
+ if (nonReactiveObjects.has(obj))
2591
+ return true;
2592
+ if (obj[nonReactiveMark])
2593
+ return true;
2594
+ for (const fn of immutables)
2595
+ if (fn(obj))
2596
+ return true;
2597
+ return false;
2598
+ }
2599
+ function registerNativeReactivity(originalClass, reactiveClass) {
2600
+ originalClass.prototype[nativeReactive] = reactiveClass;
2601
+ nonReactiveClass(reactiveClass);
2602
+ }
2603
+ nonReactiveClass(Date, RegExp, Error, Promise, Function);
2604
+ if (typeof window !== 'undefined') {
2605
+ markNonReactive(window, document);
2606
+ nonReactiveClass(Node, Element, HTMLElement, EventTarget);
2607
+ }
2608
+
2609
+ function isObject$1(value) {
2610
+ return typeof value === 'object' && value !== null;
2611
+ }
2612
+ function isObjectLike(value) {
2613
+ return isObject$1(value);
2614
+ }
2615
+ function getPrototypeToken(value) {
2616
+ if (!isObjectLike(value))
2617
+ return undefined;
2618
+ if (Array.isArray(value))
2619
+ return Array.prototype;
2620
+ try {
2621
+ return value.constructor;
2622
+ }
2623
+ catch {
2624
+ return undefined;
2625
+ }
2626
+ }
2627
+ function shouldRecurseTouch(oldValue, newValue) {
2628
+ if (oldValue === newValue)
2629
+ return false;
2630
+ if (!isObjectLike(oldValue) || !isObjectLike(newValue))
2631
+ return false;
2632
+ if (isNonReactive(oldValue) || isNonReactive(newValue))
2633
+ return false;
2634
+ return getPrototypeToken(oldValue) === getPrototypeToken(newValue);
2635
+ }
2636
+ /**
2637
+ * Centralized function to handle property change notifications with optional recursive touch
2638
+ * @param targetObj - The object whose property changed
2639
+ * @param prop - The property that changed
2640
+ * @param oldValue - The old value (before change)
2641
+ * @param newValue - The new value (after change)
2642
+ * @param hadProperty - Whether the property existed before (for add vs set)
2643
+ */
2644
+ function notifyPropertyChange(targetObj, prop, oldValue, newValue, hadProperty) {
2645
+ const evolution = { type: hadProperty ? 'set' : 'add', prop };
2646
+ if (options.recursiveTouching &&
2647
+ oldValue !== undefined &&
2648
+ shouldRecurseTouch(oldValue, newValue)) {
2649
+ const unwrappedObj = unwrap(targetObj);
2650
+ const origin = { obj: unwrappedObj, prop };
2651
+ // Deep touch: only notify nested property changes with origin filtering
2652
+ // Don't notify direct property change - the whole point is to avoid parent effects re-running
2653
+ dispatchNotifications(recursiveTouch(oldValue, newValue, new WeakMap(), [], origin));
2654
+ // Notify opaque listeners (like memoize) that always want to know about identity changes
2655
+ touchedOpaque(targetObj, evolution, prop);
2656
+ }
2657
+ else {
2658
+ touched1(targetObj, evolution, prop);
2659
+ }
2660
+ }
2661
+ function hasVisitedPair(visited, oldObj, newObj) {
2662
+ let mapped = visited.get(oldObj);
2663
+ if (!mapped) {
2664
+ mapped = new WeakSet();
2665
+ visited.set(oldObj, mapped);
2666
+ }
2667
+ if (mapped.has(newObj))
2668
+ return true;
2669
+ mapped.add(newObj);
2670
+ return false;
2671
+ }
2672
+ function collectObjectKeys(obj) {
2673
+ const keys = new Set(Reflect.ownKeys(obj));
2674
+ let proto = Object.getPrototypeOf(obj);
2675
+ // Continue walking while prototype exists and doesn't have its own constructor
2676
+ // This stops at Object.prototype (has own constructor) and class prototypes (have own constructor)
2677
+ // but continues for data prototypes (Object.create({}), Object.create(instance), etc.)
2678
+ while (proto && !Object.hasOwn(proto, 'constructor')) {
2679
+ for (const key of Reflect.ownKeys(proto))
2680
+ keys.add(key);
2681
+ proto = Object.getPrototypeOf(proto);
2682
+ }
2683
+ return keys;
2684
+ }
2685
+ function recursiveTouch(oldValue, newValue, visited = new WeakMap(), notifications = [], origin) {
2686
+ if (!shouldRecurseTouch(oldValue, newValue))
2687
+ return notifications;
2688
+ if (!isObjectLike(oldValue) || !isObjectLike(newValue))
2689
+ return notifications;
2690
+ if (hasVisitedPair(visited, oldValue, newValue))
2691
+ return notifications;
2692
+ if (Array.isArray(oldValue) && Array.isArray(newValue)) {
2693
+ diffArrayElements(oldValue, newValue, visited, notifications, origin);
2694
+ return notifications;
2695
+ }
2696
+ diffObjectProperties(oldValue, newValue, visited, notifications, origin);
2697
+ return notifications;
2698
+ }
2699
+ function diffArrayElements(oldArray, newArray, _visited, notifications, origin) {
2700
+ const local = [];
2701
+ const oldLength = oldArray.length;
2702
+ const newLength = newArray.length;
2703
+ const max = Math.max(oldLength, newLength);
2704
+ for (let index = 0; index < max; index++) {
2705
+ const hasOld = index < oldLength;
2706
+ const hasNew = index < newLength;
2707
+ if (hasOld && !hasNew) {
2708
+ local.push({ target: oldArray, evolution: { type: 'del', prop: index }, prop: index, origin });
2709
+ continue;
2710
+ }
2711
+ if (!hasOld && hasNew) {
2712
+ local.push({ target: oldArray, evolution: { type: 'add', prop: index }, prop: index, origin });
2713
+ continue;
2714
+ }
2715
+ if (!hasOld || !hasNew)
2716
+ continue;
2717
+ const oldEntry = unwrap(oldArray[index]);
2718
+ const newEntry = unwrap(newArray[index]);
2719
+ if (!Object.is(oldEntry, newEntry)) {
2720
+ local.push({ target: oldArray, evolution: { type: 'set', prop: index }, prop: index, origin });
2721
+ }
2722
+ }
2723
+ if (oldLength !== newLength)
2724
+ local.push({
2725
+ target: oldArray,
2726
+ evolution: { type: 'set', prop: 'length' },
2727
+ prop: 'length',
2728
+ origin,
2729
+ });
2730
+ notifications.push(...local);
2731
+ }
2732
+ function diffObjectProperties(oldObj, newObj, visited, notifications, origin) {
2733
+ const oldKeys = collectObjectKeys(oldObj);
2734
+ const newKeys = collectObjectKeys(newObj);
2735
+ const local = [];
2736
+ for (const key of oldKeys)
2737
+ if (!newKeys.has(key))
2738
+ local.push({ target: oldObj, evolution: { type: 'del', prop: key }, prop: key, origin });
2739
+ for (const key of newKeys)
2740
+ if (!oldKeys.has(key))
2741
+ local.push({ target: oldObj, evolution: { type: 'add', prop: key }, prop: key, origin });
2742
+ for (const key of newKeys) {
2743
+ if (!oldKeys.has(key))
2744
+ continue;
2745
+ const oldEntry = unwrap(oldObj[key]);
2746
+ const newEntry = unwrap(newObj[key]);
2747
+ if (shouldRecurseTouch(oldEntry, newEntry)) {
2748
+ recursiveTouch(oldEntry, newEntry, visited, notifications, origin);
2749
+ }
2750
+ else if (!Object.is(oldEntry, newEntry)) {
2751
+ local.push({ target: oldObj, evolution: { type: 'set', prop: key }, prop: key, origin });
2752
+ }
2753
+ }
2754
+ notifications.push(...local);
2755
+ }
2756
+ /**
2757
+ * Checks if an effect or any of its ancestors is in the allowed set
2758
+ */
2759
+ function hasAncestorInSet(effect, allowedSet) {
2760
+ let current = effect;
2761
+ const visited = new WeakSet();
2762
+ while (current && !visited.has(current)) {
2763
+ visited.add(current);
2764
+ if (allowedSet.has(current))
2765
+ return true;
2766
+ current = effectParent.get(current);
2767
+ }
2768
+ return false;
2769
+ }
2770
+ function dispatchNotifications(notifications) {
2771
+ if (!notifications.length)
2772
+ return;
2773
+ const combinedEffects = new Set();
2774
+ // Extract origin from first notification (all should have the same origin from a single deep touch)
2775
+ const origin = notifications[0]?.origin;
2776
+ let allowedEffects;
2777
+ // If origin exists, compute allowed effects (those that depend on origin.obj[origin.prop])
2778
+ if (origin) {
2779
+ allowedEffects = new Set();
2780
+ const originWatchers = watchers.get(origin.obj);
2781
+ if (originWatchers) {
2782
+ const originEffects = new Set();
2783
+ collectEffects(origin.obj, { type: 'set', prop: origin.prop }, originEffects, originWatchers, [allProps], [origin.prop]);
2784
+ for (const effect of originEffects)
2785
+ allowedEffects.add(effect);
2786
+ }
2787
+ // If no allowed effects, skip all notifications (no one should be notified)
2788
+ if (allowedEffects.size === 0)
2789
+ return;
2790
+ }
2791
+ for (const { target, evolution, prop } of notifications) {
2792
+ if (!isObjectLike(target))
2793
+ continue;
2794
+ const obj = unwrap(target);
2795
+ addState(obj, evolution);
2796
+ const objectWatchers = watchers.get(obj);
2797
+ let currentEffects;
2798
+ const propsArray = [prop];
2799
+ if (objectWatchers) {
2800
+ currentEffects = new Set();
2801
+ collectEffects(obj, evolution, currentEffects, objectWatchers, [allProps], propsArray);
2802
+ // Filter effects by ancestor chain if origin exists
2803
+ // Include effects that either directly depend on origin or have an ancestor that does
2804
+ if (origin && allowedEffects) {
2805
+ const filteredEffects = new Set();
2806
+ for (const effect of currentEffects) {
2807
+ // Check if effect itself is allowed OR has an ancestor that is allowed
2808
+ if (allowedEffects.has(effect) || hasAncestorInSet(effect, allowedEffects)) {
2809
+ filteredEffects.add(effect);
2810
+ }
2811
+ }
2812
+ currentEffects = filteredEffects;
2813
+ }
2814
+ for (const effect of currentEffects)
2815
+ combinedEffects.add(effect);
2816
+ }
2817
+ options.touched(obj, evolution, propsArray, currentEffects);
2818
+ if (objectsWithDeepWatchers.has(obj))
2819
+ bubbleUpChange(obj);
2820
+ }
2821
+ if (combinedEffects.size)
2822
+ batch([...combinedEffects]);
2823
+ }
2824
+
2825
+ const hasReentry = [];
2826
+ const reactiveHandlers = {
2827
+ [Symbol.toStringTag]: 'MutTs Reactive',
2828
+ get(obj, prop, receiver) {
2829
+ if (prop === nonReactiveMark)
2830
+ return false;
2831
+ const unwrappedObj = unwrap(obj);
2832
+ // Check if this property is marked as unreactive
2833
+ if (unwrappedObj[unreactiveProperties]?.has(prop) || typeof prop === 'symbol')
2834
+ return ReflectGet(obj, prop, receiver);
2835
+ // Special-case: array wrappers use prototype forwarding + numeric accessors.
2836
+ // With options.instanceMembers=true, inherited reads are normally not tracked, which breaks
2837
+ // reactivity for array indices/length (they appear inherited on the proxy).
2838
+ const isArrayCase = prototypeForwarding in obj &&
2839
+ // biome-ignore lint/suspicious/useIsArray: This is the whole point here
2840
+ obj[prototypeForwarding] instanceof Array &&
2841
+ typeof prop === 'string' &&
2842
+ (prop === 'length' || !Number.isNaN(Number(prop)));
2843
+ if (isArrayCase) {
2844
+ dependant(obj, prop === 'length' ? 'length' : Number(prop));
2845
+ }
2846
+ // Check if property exists and if it's an own property (cached for later use)
2847
+ const hasProp = Reflect.has(receiver, prop);
2848
+ const isOwnProp = hasProp && Object.hasOwn(receiver, prop);
2849
+ const isInheritedAccess = hasProp && !isOwnProp;
2850
+ // For accessor properties, check the unwrapped object to see if it's an accessor
2851
+ // This ensures ignoreAccessors works correctly even after operations like Object.setPrototypeOf
2852
+ const shouldIgnoreAccessor = options.ignoreAccessors &&
2853
+ isOwnProp &&
2854
+ (isOwnAccessor(receiver, prop) || isOwnAccessor(unwrappedObj, prop));
2855
+ // Depend if...
2856
+ if (!hasProp ||
2857
+ (!(options.instanceMembers && isInheritedAccess && obj instanceof Object) &&
2858
+ !shouldIgnoreAccessor))
2859
+ dependant(obj, prop);
2860
+ // Watch the whole prototype chain when requested or for null-proto objects
2861
+ if (isInheritedAccess && (!options.instanceMembers || !(obj instanceof Object))) {
2862
+ let current = reactiveObject(Object.getPrototypeOf(obj));
2863
+ while (current && current !== Object.prototype) {
2864
+ dependant(current, prop);
2865
+ if (Object.hasOwn(current, prop))
2866
+ break;
2867
+ current = reactiveObject(Object.getPrototypeOf(current));
2868
+ }
2869
+ }
2870
+ const value = ReflectGet(obj, prop, receiver);
2871
+ if (typeof value === 'object' && value !== null) {
2872
+ const reactiveValue = reactiveObject(value);
2873
+ // Only create back-references if this object needs them
2874
+ if (needsBackReferences(obj)) {
2875
+ addBackReference(reactiveValue, obj, prop);
2876
+ }
2877
+ return reactiveValue;
2878
+ }
2879
+ return value;
2880
+ },
2881
+ set(obj, prop, value, receiver) {
2882
+ // Read old value directly from unwrapped object to avoid triggering dependency tracking
2883
+ const unwrappedObj = unwrap(obj);
2884
+ const unwrappedReceiver = unwrap(receiver);
2885
+ // Check if this property is marked as unreactive
2886
+ if (unwrappedObj[unreactiveProperties]?.has(prop) || unwrappedObj !== unwrappedReceiver)
2887
+ return ReflectSet(obj, prop, value, receiver);
2888
+ // Really specific case for when Array is forwarder, in order to let it manage the reactivity
2889
+ const isArrayCase = prototypeForwarding in obj &&
2890
+ // biome-ignore lint/suspicious/useIsArray: This is the whole point here
2891
+ obj[prototypeForwarding] instanceof Array &&
2892
+ (!Number.isNaN(Number(prop)) || prop === 'length');
2893
+ const newValue = unwrap(value);
2894
+ if (isArrayCase) {
2895
+ obj[prop] = newValue;
2896
+ return true;
2897
+ }
2898
+ // Read old value, using withEffect(undefined, ...) for getter-only accessors to avoid
2899
+ // breaking memoization dependency tracking during SET operations
2900
+ let oldVal = absent;
2901
+ if (Reflect.has(unwrappedReceiver, prop)) {
2902
+ // Check descriptor on both receiver and target to handle proxy cases
2903
+ const receiverDesc = Object.getOwnPropertyDescriptor(unwrappedReceiver, prop);
2904
+ const targetDesc = Object.getOwnPropertyDescriptor(unwrappedObj, prop);
2905
+ const desc = receiverDesc || targetDesc;
2906
+ // If it's a getter-only accessor (has getter but no setter), read without tracking
2907
+ // to avoid breaking memoization invalidation when the getter calls memoized functions
2908
+ if (desc?.get && !desc?.set) {
2909
+ oldVal = withEffect(undefined, () => Reflect.get(unwrappedObj, prop, unwrappedReceiver));
2910
+ }
2911
+ else {
2912
+ oldVal = Reflect.get(unwrappedObj, prop, unwrappedReceiver);
2913
+ }
2914
+ }
2915
+ if (objectsWithDeepWatchers.has(obj)) {
2916
+ if (typeof oldVal === 'object' && oldVal !== null) {
2917
+ removeBackReference(oldVal, obj, prop);
2918
+ }
2919
+ if (typeof newValue === 'object' && newValue !== null) {
2920
+ const reactiveValue = reactiveObject(newValue);
2921
+ addBackReference(reactiveValue, obj, prop);
2922
+ }
2923
+ }
2924
+ if (oldVal !== newValue) {
2925
+ // For getter-only accessors, Reflect.set() may fail, but we still return true
2926
+ // to avoid throwing errors. Only proceed with change notifications if set succeeded.
2927
+ if (ReflectSet(obj, prop, newValue, receiver)) {
2928
+ notifyPropertyChange(obj, prop, oldVal, newValue, oldVal !== absent);
2929
+ }
2930
+ }
2931
+ return true;
2932
+ },
2933
+ has(obj, prop) {
2934
+ if (hasReentry.includes(obj))
2935
+ throw new ReactiveError(`[reactive] Circular dependency detected in 'has' check for property '${String(prop)}'`, {
2936
+ code: ReactiveErrorCode.CycleDetected,
2937
+ cycle: [], // We don't have the full cycle here, but we know it involves obj
2938
+ });
2939
+ hasReentry.push(obj);
2940
+ dependant(obj, prop);
2941
+ const rv = Reflect.has(obj, prop);
2942
+ hasReentry.pop();
2943
+ return rv;
2944
+ },
2945
+ deleteProperty(obj, prop) {
2946
+ if (!Object.hasOwn(obj, prop))
2947
+ return false;
2948
+ const oldVal = obj[prop];
2949
+ // Remove back-references if this object has deep watchers
2950
+ if (objectsWithDeepWatchers.has(obj) && typeof oldVal === 'object' && oldVal !== null) {
2951
+ removeBackReference(oldVal, obj, prop);
2952
+ }
2953
+ delete obj[prop];
2954
+ touched1(obj, { type: 'del', prop }, prop);
2955
+ // Bubble up changes if this object has deep watchers
2956
+ if (objectsWithDeepWatchers.has(obj)) {
2957
+ bubbleUpChange(obj);
2958
+ }
2959
+ return true;
2960
+ },
2961
+ getPrototypeOf(obj) {
2962
+ if (prototypeForwarding in obj)
2963
+ return obj[prototypeForwarding];
2964
+ return Object.getPrototypeOf(obj);
2965
+ },
2966
+ setPrototypeOf(obj, proto) {
2967
+ if (prototypeForwarding in obj)
2968
+ return false;
2969
+ Object.setPrototypeOf(obj, proto);
2970
+ return true;
2971
+ },
2972
+ ownKeys(obj) {
2973
+ dependant(obj, allProps);
2974
+ return Reflect.ownKeys(obj);
2975
+ },
2976
+ };
2977
+ const reactiveClasses = new WeakSet();
2978
+ // Create the ReactiveBase mixin
2979
+ /**
2980
+ * Base mixin for reactive classes that provides proper constructor reactivity
2981
+ * Solves constructor reactivity issues in complex inheritance trees
2982
+ */
2983
+ const ReactiveBase = mixin((base) => {
2984
+ class ReactiveMixin extends base {
2985
+ constructor(...args) {
2986
+ super(...args);
2987
+ // Only apply reactive transformation if the class is marked with @reactive
2988
+ // This allows the mixin to work properly with method inheritance
2989
+ // biome-ignore lint/correctness/noConstructorReturn: This is the whole point here
2990
+ return reactiveClasses.has(new.target) ? reactive(this) : this;
2991
+ }
2992
+ }
2993
+ return ReactiveMixin;
2994
+ });
2995
+ function reactiveObject(anyTarget) {
2996
+ if (!anyTarget || typeof anyTarget !== 'object')
2997
+ return anyTarget;
2998
+ const target = anyTarget;
2999
+ // If target is already a proxy, return it
3000
+ if (isNonReactive(target))
3001
+ return target;
3002
+ const isProxy = proxyToObject.has(target);
3003
+ if (isProxy)
3004
+ return target;
3005
+ // If we already have a proxy for this object, return it (optimized: get returns undefined if not found)
3006
+ const existing = getExistingProxy(target);
3007
+ if (existing !== undefined)
3008
+ return existing;
3009
+ const proxied = nativeReactive in target && !(target instanceof target[nativeReactive])
3010
+ ? new target[nativeReactive](target)
3011
+ : target;
3012
+ if (proxied !== target)
3013
+ trackProxyObject(proxied, target);
3014
+ const proxy = new Proxy(proxied, reactiveHandlers);
3015
+ // Store the relationships
3016
+ storeProxyRelationship(target, proxy);
3017
+ return proxy;
3018
+ }
3019
+ /**
3020
+ * Main decorator for making classes reactive
3021
+ * Automatically makes class instances reactive when created
3022
+ */
3023
+ const reactive = decorator({
3024
+ class(original) {
3025
+ if (original.prototype instanceof ReactiveBase) {
3026
+ reactiveClasses.add(original);
3027
+ return original;
3028
+ }
3029
+ class Reactive extends original {
3030
+ constructor(...args) {
3031
+ super(...args);
3032
+ if (new.target !== Reactive && !reactiveClasses.has(new.target))
3033
+ options.warn(`${original.name} has been inherited by ${this.constructor.name} that is not reactive.
3034
+ @reactive decorator must be applied to the leaf class OR classes have to extend ReactiveBase.`);
3035
+ // biome-ignore lint/correctness/noConstructorReturn: This is the whole point here
3036
+ return reactive(this);
3037
+ }
3038
+ }
3039
+ Object.defineProperty(Reactive, 'name', {
3040
+ value: `Reactive<${original.name}>`,
3041
+ });
3042
+ return Reactive;
3043
+ },
3044
+ get(original) {
3045
+ return reactiveObject(original);
3046
+ },
3047
+ default: reactiveObject,
3048
+ });
3049
+
3050
+ function isObject(value) {
3051
+ return typeof value === 'object' && value !== null;
3052
+ }
3053
+ /**
3054
+ * Deep watch an object and all its nested properties
3055
+ * @param target - The object to watch deeply
3056
+ * @param callback - The callback to call when any nested property changes
3057
+ * @param options - Options for the deep watch
3058
+ * @returns A cleanup function to stop watching
3059
+ */
3060
+ /**
3061
+ * Sets up deep watching for an object, tracking all nested property changes
3062
+ * @param target - The object to watch
3063
+ * @param callback - The callback to call when changes occur
3064
+ * @param options - Options for deep watching
3065
+ * @returns A cleanup function to stop deep watching
3066
+ */
3067
+ function deepWatch(target, callback, { immediate = false } = {}) {
3068
+ if (target === null || target === undefined)
3069
+ return undefined;
3070
+ if (typeof target !== 'object')
3071
+ throw new Error('Target of deep watching must be an object');
3072
+ // Create a wrapper callback that matches ScopedCallback signature
3073
+ const wrappedCallback = markWithRoot(() => callback(target), callback);
3074
+ // Use the existing effect system to register dependencies
3075
+ return effect(() => {
3076
+ // Mark the target object as having deep watchers
3077
+ objectsWithDeepWatchers.add(target);
3078
+ // Track which objects this effect is watching for cleanup
3079
+ let effectObjects = effectToDeepWatchedObjects.get(wrappedCallback);
3080
+ if (!effectObjects) {
3081
+ effectObjects = new Set();
3082
+ effectToDeepWatchedObjects.set(wrappedCallback, effectObjects);
3083
+ }
3084
+ effectObjects.add(target);
3085
+ // Traverse the object graph and register dependencies
3086
+ // This will re-run every time the effect runs, ensuring we catch all changes
3087
+ const visited = new WeakSet();
3088
+ function traverseAndTrack(obj, depth = 0) {
3089
+ // Prevent infinite recursion and excessive depth
3090
+ if (!obj || visited.has(obj) || !isObject(obj) || depth > options.maxDeepWatchDepth)
3091
+ return;
3092
+ // Do not traverse into unreactive objects
3093
+ if (isNonReactive(obj))
3094
+ return;
3095
+ visited.add(obj);
3096
+ // Mark this object as having deep watchers
3097
+ objectsWithDeepWatchers.add(obj);
3098
+ effectObjects.add(obj);
3099
+ // Traverse all properties to register dependencies
3100
+ // unwrap to avoid kicking dependency
3101
+ for (const key in unwrap(obj)) {
3102
+ if (Object.hasOwn(obj, key)) {
3103
+ // Access the property to register dependency
3104
+ const value = obj[key];
3105
+ // Make the value reactive if it's an object
3106
+ const reactiveValue = typeof value === 'object' && value !== null ? reactive(value) : value;
3107
+ traverseAndTrack(reactiveValue, depth + 1);
3108
+ }
3109
+ }
3110
+ // Also handle array indices and length
3111
+ // biome-ignore lint/suspicious/useIsArray: Check for both native arrays and reactive arrays
3112
+ if (Array.isArray(obj) || obj instanceof Array) {
3113
+ // Access array length to register dependency on length changes
3114
+ const length = obj.length;
3115
+ // Access all current array elements to register dependencies
3116
+ for (let i = 0; i < length; i++) {
3117
+ // Access the array element to register dependency
3118
+ const value = obj[i];
3119
+ // Make the value reactive if it's an object
3120
+ const reactiveValue = typeof value === 'object' && value !== null ? reactive(value) : value;
3121
+ traverseAndTrack(reactiveValue, depth + 1);
3122
+ }
3123
+ }
3124
+ // Handle Set values (deep watch values only, not keys since Sets don't have separate keys)
3125
+ else if (obj instanceof Set) {
3126
+ // Access all Set values to register dependencies
3127
+ for (const value of obj) {
3128
+ // Make the value reactive if it's an object
3129
+ const reactiveValue = typeof value === 'object' && value !== null ? reactive(value) : value;
3130
+ traverseAndTrack(reactiveValue, depth + 1);
3131
+ }
3132
+ }
3133
+ // Handle Map values (deep watch values only, not keys)
3134
+ else if (obj instanceof Map) {
3135
+ // Access all Map values to register dependencies
3136
+ for (const [_key, value] of obj) {
3137
+ // Make the value reactive if it's an object
3138
+ const reactiveValue = typeof value === 'object' && value !== null ? reactive(value) : value;
3139
+ traverseAndTrack(reactiveValue, depth + 1);
3140
+ }
3141
+ }
3142
+ // Note: WeakSet and WeakMap cannot be iterated, so we can't deep watch their contents
3143
+ // They will only trigger when the collection itself is replaced
3144
+ }
3145
+ // Traverse the target object to register all dependencies
3146
+ // This will register dependencies on all current properties and array elements
3147
+ traverseAndTrack(target);
3148
+ // Only call the callback if immediate is true or if it's not the first run
3149
+ if (immediate)
3150
+ callback(target);
3151
+ immediate = true;
3152
+ // Return a cleanup function that properly removes deep watcher tracking
3153
+ return () => {
3154
+ // Get the objects this effect was watching
3155
+ const effectObjects = effectToDeepWatchedObjects.get(wrappedCallback);
3156
+ if (effectObjects) {
3157
+ // Remove deep watcher tracking from all objects this effect was watching
3158
+ for (const obj of effectObjects) {
3159
+ // Check if this object still has other deep watchers
3160
+ const watchers = deepWatchers.get(obj);
3161
+ if (watchers) {
3162
+ // Remove this effect's callback from the watchers
3163
+ watchers.delete(wrappedCallback);
3164
+ // If no more watchers, remove the object from deep watchers tracking
3165
+ if (watchers.size === 0) {
3166
+ deepWatchers.delete(obj);
3167
+ objectsWithDeepWatchers.delete(obj);
3168
+ }
3169
+ }
3170
+ else {
3171
+ // No watchers found, remove from deep watchers tracking
3172
+ objectsWithDeepWatchers.delete(obj);
3173
+ }
3174
+ }
3175
+ // Clean up the tracking data
3176
+ effectToDeepWatchedObjects.delete(wrappedCallback);
3177
+ }
3178
+ };
3179
+ });
3180
+ }
3181
+
3182
+ /**
3183
+ * Symbol for accessing the cleanup function on cleaned objects
3184
+ */
3185
+ const cleanup = Symbol('cleanup');
3186
+ //#region watch
3187
+ const unsetYet = Symbol('unset-yet');
3188
+ function watch(value, //object | ((dep: DependencyAccess) => object),
3189
+ changed, options = {}) {
3190
+ return typeof value === 'function'
3191
+ ? watchCallBack(value, changed, options)
3192
+ : typeof value === 'object' && value !== null
3193
+ ? watchObject(value, changed, options)
3194
+ : (() => {
3195
+ throw new Error('watch: value must be a function or an object');
3196
+ })();
3197
+ }
3198
+ function watchObject(value, changed, { immediate = false, deep = false } = {}) {
3199
+ const myParentEffect = getActiveEffect();
3200
+ if (deep)
3201
+ return deepWatch(value, changed, { immediate });
3202
+ return effect(markWithRoot(function watchObjectEffect() {
3203
+ dependant(value);
3204
+ if (immediate)
3205
+ withEffect(myParentEffect, () => changed(value));
3206
+ immediate = true;
3207
+ }, changed));
3208
+ }
3209
+ function watchCallBack(value, changed, { immediate = false, deep = false } = {}) {
3210
+ const myParentEffect = getActiveEffect();
3211
+ let oldValue = unsetYet;
3212
+ let deepCleanup;
3213
+ const cbCleanup = effect(markWithRoot(function watchCallBackEffect(access) {
3214
+ const newValue = value(access);
3215
+ if (oldValue !== newValue)
3216
+ withEffect(myParentEffect, markWithRoot(() => {
3217
+ if (oldValue === unsetYet) {
3218
+ if (immediate)
3219
+ changed(newValue);
3220
+ }
3221
+ else
3222
+ changed(newValue, oldValue);
3223
+ oldValue = newValue;
3224
+ if (deep) {
3225
+ if (deepCleanup)
3226
+ deepCleanup();
3227
+ deepCleanup = deepWatch(newValue, markWithRoot((value) => changed(value, value), changed));
3228
+ }
3229
+ }, changed));
3230
+ }, value));
3231
+ return () => {
3232
+ cbCleanup();
3233
+ if (deepCleanup)
3234
+ deepCleanup();
3235
+ };
3236
+ }
3237
+ //#endregion
3238
+ //#region nonReactive
3239
+ /**
3240
+ * Mark an object as non-reactive. This object and all its properties will never be made reactive.
3241
+ * @param obj - The object to mark as non-reactive
3242
+ */
3243
+ function deepNonReactive(obj) {
3244
+ obj = unwrap(obj);
3245
+ if (isNonReactive(obj))
3246
+ return obj;
3247
+ try {
3248
+ Object.defineProperty(obj, nonReactiveMark, {
3249
+ value: true,
3250
+ writable: false,
3251
+ enumerable: false,
3252
+ configurable: true,
3253
+ });
3254
+ }
3255
+ catch { }
3256
+ if (!(nonReactiveMark in obj))
3257
+ nonReactiveObjects.add(obj);
3258
+ //for (const key in obj) deepNonReactive(obj[key])
3259
+ return obj;
3260
+ }
3261
+ function unreactiveApplication(arg1, ...args) {
3262
+ return typeof arg1 === 'object'
3263
+ ? deepNonReactive(arg1)
3264
+ : ((original) => {
3265
+ // Copy the parent's unreactive properties if they exist
3266
+ original.prototype[unreactiveProperties] = new Set(original.prototype[unreactiveProperties] || []);
3267
+ // Add all arguments (including the first one)
3268
+ original.prototype[unreactiveProperties].add(arg1);
3269
+ for (const arg of args)
3270
+ original.prototype[unreactiveProperties].add(arg);
3271
+ return original; // Return the class
3272
+ });
3273
+ }
3274
+ /**
3275
+ * Decorator that marks classes or properties as non-reactive
3276
+ * Prevents objects from being made reactive
3277
+ */
3278
+ const unreactive = decorator({
3279
+ class(original) {
3280
+ // Called without arguments, mark entire class as non-reactive
3281
+ nonReactiveClass(original);
3282
+ },
3283
+ default: unreactiveApplication,
3284
+ });
3285
+ //#endregion
3286
+ function cleanedBy(obj, cleanupFn) {
3287
+ return Object.defineProperty(obj, cleanup, {
3288
+ value: cleanupFn,
3289
+ writable: false,
3290
+ enumerable: false,
3291
+ configurable: true,
3292
+ });
3293
+ }
3294
+ //#region greedy caching
3295
+ /**
3296
+ * Creates a derived value that automatically recomputes when dependencies change
3297
+ * @param compute - Function that computes the derived value
3298
+ * @returns Object with value and cleanup function
3299
+ */
3300
+ function derived(compute) {
3301
+ const rv = { value: undefined };
3302
+ return cleanedBy(rv, untracked(() => effect(markWithRoot(function derivedEffect(access) {
3303
+ rv.value = compute(access);
3304
+ }, compute))));
3305
+ }
3306
+
3307
+ /**
3308
+ * Converts an iterator to a generator that yields reactive values
3309
+ */
3310
+ function* makeReactiveIterator(iterator) {
3311
+ let result = iterator.next();
3312
+ while (!result.done) {
3313
+ yield reactive(result.value);
3314
+ result = iterator.next();
3315
+ }
3316
+ }
3317
+ /**
3318
+ * Converts an iterator of key-value pairs to a generator that yields reactive key-value pairs
3319
+ */
3320
+ function* makeReactiveEntriesIterator(iterator) {
3321
+ let result = iterator.next();
3322
+ while (!result.done) {
3323
+ const [key, value] = result.value;
3324
+ yield [reactive(key), reactive(value)];
3325
+ result = iterator.next();
3326
+ }
3327
+ }
3328
+
3329
+ const native$2 = Symbol('native');
3330
+ const isArray = Array.isArray;
3331
+ Array.isArray = ((value) => isArray(value) ||
3332
+ // biome-ignore lint/suspicious/useIsArray: We are defining it
3333
+ (value &&
3334
+ typeof value === 'object' &&
3335
+ prototypeForwarding in value &&
3336
+ Array.isArray(value[prototypeForwarding])));
3337
+ class ReactiveBaseArray {
3338
+ // Safe array access with negative indices
3339
+ at(index) {
3340
+ const actualIndex = index < 0 ? this[native$2].length + index : index;
3341
+ dependant(this, actualIndex);
3342
+ if (actualIndex < 0 || actualIndex >= this[native$2].length)
3343
+ return undefined;
3344
+ return reactive(this[native$2][actualIndex]);
3345
+ }
3346
+ // Immutable versions of mutator methods
3347
+ toReversed() {
3348
+ dependant(this);
3349
+ return reactive(this[native$2].toReversed());
3350
+ }
3351
+ toSorted(compareFn) {
3352
+ dependant(this);
3353
+ return reactive(this[native$2].toSorted(compareFn));
3354
+ }
3355
+ toSpliced(start, deleteCount, ...items) {
3356
+ dependant(this);
3357
+ return deleteCount === undefined
3358
+ ? this[native$2].toSpliced(start)
3359
+ : this[native$2].toSpliced(start, deleteCount, ...items);
3360
+ }
3361
+ with(index, value) {
3362
+ dependant(this);
3363
+ return reactive(this[native$2].with(index, value));
3364
+ }
3365
+ // Iterator methods with reactivity tracking
3366
+ entries() {
3367
+ dependant(this);
3368
+ return makeReactiveEntriesIterator(this[native$2].entries());
3369
+ }
3370
+ keys() {
3371
+ dependant(this, 'length');
3372
+ return this[native$2].keys();
3373
+ }
3374
+ values() {
3375
+ dependant(this);
3376
+ return makeReactiveIterator(this[native$2].values());
3377
+ }
3378
+ [Symbol.iterator]() {
3379
+ dependant(this);
3380
+ const nativeIterator = this[native$2][Symbol.iterator]();
3381
+ return {
3382
+ next() {
3383
+ const result = nativeIterator.next();
3384
+ if (result.done) {
3385
+ return result;
3386
+ }
3387
+ return { value: reactive(result.value), done: false };
3388
+ },
3389
+ };
3390
+ }
3391
+ indexOf(searchElement, fromIndex) {
3392
+ dependant(this);
3393
+ const unwrappedSearch = unwrap(searchElement);
3394
+ // Check both wrapped and unwrapped versions since array may contain either
3395
+ const index = this[native$2].indexOf(unwrappedSearch, fromIndex);
3396
+ if (index !== -1)
3397
+ return index;
3398
+ // If not found with unwrapped, try with wrapped (in case array contains wrapped version)
3399
+ return this[native$2].indexOf(searchElement, fromIndex);
3400
+ }
3401
+ lastIndexOf(searchElement, fromIndex) {
3402
+ dependant(this);
3403
+ const unwrappedSearch = unwrap(searchElement);
3404
+ // Check both wrapped and unwrapped versions since array may contain either
3405
+ const index = this[native$2].lastIndexOf(unwrappedSearch, fromIndex);
3406
+ if (index !== -1)
3407
+ return index;
3408
+ // If not found with unwrapped, try with wrapped (in case array contains wrapped version)
3409
+ return this[native$2].lastIndexOf(searchElement, fromIndex);
3410
+ }
3411
+ includes(searchElement, fromIndex) {
3412
+ dependant(this);
3413
+ const unwrappedSearch = unwrap(searchElement);
3414
+ // Check both wrapped and unwrapped versions since array may contain either
3415
+ return (this[native$2].includes(unwrappedSearch, fromIndex) ||
3416
+ this[native$2].includes(searchElement, fromIndex));
3417
+ }
3418
+ find(predicateOrElement, thisArg) {
3419
+ dependant(this);
3420
+ if (typeof predicateOrElement === 'function') {
3421
+ const predicate = predicateOrElement;
3422
+ return reactive(this[native$2].find((value, index, array) => predicate.call(thisArg, reactive(value), index, array), thisArg));
3423
+ }
3424
+ const fromIndex = typeof thisArg === 'number' ? thisArg : undefined;
3425
+ const index = this[native$2].indexOf(predicateOrElement, fromIndex);
3426
+ if (index === -1)
3427
+ return undefined;
3428
+ return reactive(this[native$2][index]);
3429
+ }
3430
+ findIndex(predicateOrElement, thisArg) {
3431
+ dependant(this);
3432
+ if (typeof predicateOrElement === 'function') {
3433
+ const predicate = predicateOrElement;
3434
+ return this[native$2].findIndex((value, index, array) => predicate.call(thisArg, reactive(value), index, array), thisArg);
3435
+ }
3436
+ const fromIndex = typeof thisArg === 'number' ? thisArg : undefined;
3437
+ return this[native$2].indexOf(predicateOrElement, fromIndex);
3438
+ }
3439
+ flat() {
3440
+ dependant(this);
3441
+ return reactive(this[native$2].flat());
3442
+ }
3443
+ flatMap(callbackfn, thisArg) {
3444
+ dependant(this);
3445
+ return reactive(this[native$2].flatMap(callbackfn, thisArg));
3446
+ }
3447
+ filter(callbackfn, thisArg) {
3448
+ dependant(this);
3449
+ return reactive(this[native$2].filter((item, index, array) => callbackfn(reactive(item), index, array), thisArg));
3450
+ }
3451
+ map(callbackfn, thisArg) {
3452
+ dependant(this);
3453
+ return reactive(this[native$2].map((item, index, array) => callbackfn(reactive(item), index, array), thisArg));
3454
+ }
3455
+ reduce(callbackfn, initialValue) {
3456
+ dependant(this);
3457
+ const result = initialValue === undefined
3458
+ ? this[native$2].reduce(callbackfn)
3459
+ : this[native$2].reduce(callbackfn, initialValue);
3460
+ return reactive(result);
3461
+ }
3462
+ reduceRight(callbackfn, initialValue) {
3463
+ dependant(this);
3464
+ const result = initialValue !== undefined
3465
+ ? this[native$2].reduceRight(callbackfn, initialValue)
3466
+ : this[native$2].reduceRight(callbackfn);
3467
+ return reactive(result);
3468
+ }
3469
+ slice(start, end) {
3470
+ for (const i of range(start || 0, end || this[native$2].length - 1))
3471
+ dependant(this, i);
3472
+ return start === undefined
3473
+ ? this[native$2].slice()
3474
+ : end === undefined
3475
+ ? this[native$2].slice(start)
3476
+ : this[native$2].slice(start, end);
3477
+ }
3478
+ concat(...items) {
3479
+ dependant(this);
3480
+ return reactive(this[native$2].concat(...items));
3481
+ }
3482
+ join(separator) {
3483
+ dependant(this);
3484
+ return this[native$2].join(separator);
3485
+ }
3486
+ forEach(callbackfn, thisArg) {
3487
+ dependant(this);
3488
+ this[native$2].forEach((value, index, array) => {
3489
+ callbackfn.call(thisArg, reactive(value), index, array);
3490
+ });
3491
+ }
3492
+ // TODO: re-implement for fun dependencies? (eg - every only check the first ones until it find some),
3493
+ // no need to make it dependant on indexes after the found one
3494
+ every(callbackfn, thisArg) {
3495
+ dependant(this);
3496
+ return this[native$2].every((value, index, array) => callbackfn.call(thisArg, reactive(value), index, array), thisArg);
3497
+ }
3498
+ some(callbackfn, thisArg) {
3499
+ dependant(this);
3500
+ return this[native$2].some((value, index, array) => callbackfn.call(thisArg, reactive(value), index, array), thisArg);
3501
+ }
3502
+ }
3503
+ function* index(i, { length = true } = {}) {
3504
+ if (length)
3505
+ yield 'length';
3506
+ yield i;
3507
+ }
3508
+ function* range(a, b, { length = false } = {}) {
3509
+ const start = Math.min(a, b);
3510
+ const end = Math.max(a, b);
3511
+ if (length)
3512
+ yield 'length';
3513
+ for (let i = start; i <= end; i++)
3514
+ yield i;
3515
+ }
3516
+ /**
3517
+ * Reactive wrapper around JavaScript's Array class with full array method support
3518
+ * Tracks length changes, individual index operations, and collection-wide operations
3519
+ */
3520
+ class ReactiveArray extends Indexable(ReactiveBaseArray, {
3521
+ get(i) {
3522
+ dependant(this, i);
3523
+ return reactive(this[native$2][i]);
3524
+ },
3525
+ set(i, value) {
3526
+ const added = i >= this[native$2].length;
3527
+ this[native$2][i] = value;
3528
+ touched(this, { type: 'set', prop: i }, index(i, { length: added }));
3529
+ },
3530
+ getLength() {
3531
+ dependant(this, 'length');
3532
+ return this[native$2].length;
3533
+ },
3534
+ setLength(value) {
3535
+ const oldLength = this[native$2].length;
3536
+ try {
3537
+ this[native$2].length = value;
3538
+ }
3539
+ finally {
3540
+ touched(this, { type: 'set', prop: 'length' }, range(oldLength, value, { length: true }));
3541
+ }
3542
+ },
3543
+ }) {
3544
+ constructor(original) {
3545
+ super();
3546
+ Object.defineProperties(this, {
3547
+ // We have to make it double, as [native] must be `unique symbol` - impossible through import
3548
+ [native$2]: { value: original },
3549
+ [prototypeForwarding]: { value: original },
3550
+ });
3551
+ }
3552
+ push(...items) {
3553
+ const oldLength = this[native$2].length;
3554
+ try {
3555
+ return this[native$2].push(...items);
3556
+ }
3557
+ finally {
3558
+ touched(this, { type: 'bunch', method: 'push' }, range(oldLength, oldLength + items.length - 1, { length: true }));
3559
+ }
3560
+ }
3561
+ pop() {
3562
+ if (this[native$2].length === 0)
3563
+ return undefined;
3564
+ try {
3565
+ return reactive(this[native$2].pop());
3566
+ }
3567
+ finally {
3568
+ touched(this, { type: 'bunch', method: 'pop' }, index(this[native$2].length));
3569
+ }
3570
+ }
3571
+ shift() {
3572
+ if (this[native$2].length === 0)
3573
+ return undefined;
3574
+ try {
3575
+ return reactive(this[native$2].shift());
3576
+ }
3577
+ finally {
3578
+ touched(this, { type: 'bunch', method: 'shift' }, range(0, this[native$2].length + 1, { length: true }));
3579
+ }
3580
+ }
3581
+ unshift(...items) {
3582
+ try {
3583
+ return this[native$2].unshift(...items);
3584
+ }
3585
+ finally {
3586
+ touched(this, { type: 'bunch', method: 'unshift' }, range(0, this[native$2].length - items.length, { length: true }));
3587
+ }
3588
+ }
3589
+ splice(start, deleteCount, ...items) {
3590
+ const oldLength = this[native$2].length;
3591
+ if (deleteCount === undefined)
3592
+ deleteCount = oldLength - start;
3593
+ try {
3594
+ if (deleteCount === undefined)
3595
+ return reactive(this[native$2].splice(start));
3596
+ return reactive(this[native$2].splice(start, deleteCount, ...items));
3597
+ }
3598
+ finally {
3599
+ touched(this, { type: 'bunch', method: 'splice' },
3600
+ // TODO: edge cases
3601
+ deleteCount === items.length
3602
+ ? range(start, start + deleteCount)
3603
+ : range(start, oldLength + Math.max(items.length - deleteCount, 0), {
3604
+ length: true,
3605
+ }));
3606
+ }
3607
+ }
3608
+ reverse() {
3609
+ try {
3610
+ return this[native$2].reverse();
3611
+ }
3612
+ finally {
3613
+ touched(this, { type: 'bunch', method: 'reverse' }, range(0, this[native$2].length - 1));
3614
+ }
3615
+ }
3616
+ sort(compareFn) {
3617
+ compareFn = compareFn || ((a, b) => a.toString().localeCompare(b.toString()));
3618
+ try {
3619
+ return this[native$2].sort((a, b) => compareFn(reactive(a), reactive(b)));
3620
+ }
3621
+ finally {
3622
+ touched(this, { type: 'bunch', method: 'sort' }, range(0, this[native$2].length - 1));
3623
+ }
3624
+ }
3625
+ fill(value, start, end) {
3626
+ try {
3627
+ if (start === undefined)
3628
+ return this[native$2].fill(value);
3629
+ if (end === undefined)
3630
+ return this[native$2].fill(value, start);
3631
+ return this[native$2].fill(value, start, end);
3632
+ }
3633
+ finally {
3634
+ touched(this, { type: 'bunch', method: 'fill' }, range(0, this[native$2].length - 1));
3635
+ }
3636
+ }
3637
+ copyWithin(target, start, end) {
3638
+ try {
3639
+ if (end === undefined)
3640
+ return this[native$2].copyWithin(target, start);
3641
+ return this[native$2].copyWithin(target, start, end);
3642
+ }
3643
+ finally {
3644
+ touched(this, { type: 'bunch', method: 'copyWithin' },
3645
+ // TODO: calculate the range properly
3646
+ range(0, this[native$2].length - 1));
3647
+ }
3648
+ // Touch all affected indices with a single allProps call
3649
+ }
3650
+ }
3651
+
3652
+ // TODO: Lazy reactivity ?
3653
+ class ReadOnlyError extends Error {
3654
+ }
3655
+ /**
3656
+ * Reactive wrapper around JavaScript's Array class with full array method support
3657
+ * Tracks length changes, individual index operations, and collection-wide operations
3658
+ */
3659
+ class ReactiveReadOnlyArrayClass extends Indexable(ReactiveBaseArray, {
3660
+ get(i) {
3661
+ dependant(this, i);
3662
+ return reactive(this[native$2][i]);
3663
+ },
3664
+ set(i, _value) {
3665
+ throw new ReadOnlyError(`Setting index ${i} on a read-only array`);
3666
+ },
3667
+ getLength() {
3668
+ dependant(this, 'length');
3669
+ return this[native$2].length;
3670
+ },
3671
+ setLength(value) {
3672
+ throw new ReadOnlyError(`Setting length to ${value} on a read-only array`);
3673
+ },
3674
+ }) {
3675
+ constructor(original) {
3676
+ super();
3677
+ Object.defineProperties(this, {
3678
+ // We have to make it double, as [native] must be `unique symbol` - impossible through import
3679
+ [native$2]: { value: original },
3680
+ [prototypeForwarding]: { value: original },
3681
+ });
3682
+ }
3683
+ push(..._items) {
3684
+ throw new ReadOnlyError(`Pushing items to a read-only array`);
3685
+ }
3686
+ pop() {
3687
+ throw new ReadOnlyError(`Popping from a read-only array`);
3688
+ }
3689
+ shift() {
3690
+ throw new ReadOnlyError(`Shifting from a read-only array`);
3691
+ }
3692
+ unshift(..._items) {
3693
+ throw new ReadOnlyError(`Unshifting items to a read-only array`);
3694
+ }
3695
+ splice(_start, _deleteCount, ..._items) {
3696
+ throw new ReadOnlyError(`Splice from a read-only array`);
3697
+ }
3698
+ reverse() {
3699
+ throw new ReadOnlyError(`Reversing a read-only array`);
3700
+ }
3701
+ sort(_compareFn) {
3702
+ throw new ReadOnlyError(`Sorting a read-only array`);
3703
+ }
3704
+ fill(_value, _start, _end) {
3705
+ throw new ReadOnlyError(`Filling a read-only array`);
3706
+ }
3707
+ copyWithin(_target, _start, _end) {
3708
+ throw new ReadOnlyError(`Copying within a read-only array`);
3709
+ }
3710
+ }
3711
+ const ReactiveReadOnlyArray = reactive(ReactiveReadOnlyArrayClass);
3712
+ function mapped(inputs, compute, resize) {
3713
+ const result = [];
3714
+ const resultReactive = new ReactiveReadOnlyArray(result);
3715
+ const cleanups = [];
3716
+ function input(index) {
3717
+ return effect(function computedIndexedMapInputEffect() {
3718
+ result[index] = compute(inputs[index], index, resultReactive);
3719
+ touched1(resultReactive, { type: 'set', prop: index }, index);
3720
+ });
3721
+ }
3722
+ const cleanupLength = effect(function computedMapLengthEffect({ ascend }) {
3723
+ const length = inputs.length;
3724
+ const resultLength = untracked(() => result.length);
3725
+ resize?.(length, resultLength);
3726
+ touched1(resultReactive, { type: 'set', prop: 'length' }, 'length');
3727
+ if (length < resultLength) {
3728
+ const toCleanup = cleanups.splice(length);
3729
+ for (const cleanup of toCleanup)
3730
+ cleanup();
3731
+ result.length = length;
3732
+ }
3733
+ else if (length > resultLength)
3734
+ // the input effects will be registered as the call's children, so they will remain not cleaned with this effect on length
3735
+ ascend(function computedMapNewElements() {
3736
+ for (let i = resultLength; i < length; i++)
3737
+ cleanups.push(input(i));
3738
+ });
3739
+ });
3740
+ return cleanedBy(resultReactive, () => {
3741
+ for (const cleanup of cleanups)
3742
+ cleanup();
3743
+ cleanups.length = 0;
3744
+ cleanupLength();
3745
+ });
3746
+ }
3747
+ function reduced(inputs, compute) {
3748
+ const result = [];
3749
+ const resultReactive = new ReactiveReadOnlyArray(result);
3750
+ const cleanupFactor = effect(function computedReducedFactorEffect() {
3751
+ const factor = {};
3752
+ result.length = 0;
3753
+ for (const input of inputs)
3754
+ result.push(...compute(input, factor));
3755
+ touched(resultReactive, { type: 'invalidate', prop: 'reduced' });
3756
+ });
3757
+ return cleanedBy(resultReactive, cleanupFactor);
3758
+ }
3759
+
3760
+ const memoizedRegistry = new WeakMap();
3761
+ function getBranch(tree, key) {
3762
+ tree.branches ?? (tree.branches = new WeakMap());
3763
+ let branch = tree.branches.get(key);
3764
+ if (!branch) {
3765
+ branch = {};
3766
+ tree.branches.set(key, branch);
3767
+ }
3768
+ return branch;
3769
+ }
3770
+ function memoizeFunction(fn) {
3771
+ const fnRoot = getRoot(fn);
3772
+ const existing = memoizedRegistry.get(fnRoot);
3773
+ if (existing)
3774
+ return existing;
3775
+ const cacheRoot = {};
3776
+ const memoized = markWithRoot((...args) => {
3777
+ const localArgs = args; //: Args = maxArgs !== undefined ? (args.slice(0, maxArgs) as Args) : args
3778
+ if (localArgs.some((arg) => !(arg && ['object', 'symbol', 'function'].includes(typeof arg))))
3779
+ throw new Error('memoize expects non-null object arguments');
3780
+ let node = cacheRoot;
3781
+ for (const arg of localArgs) {
3782
+ node = getBranch(node, arg);
3783
+ }
3784
+ dependant(node, 'memoize');
3785
+ if ('result' in node)
3786
+ return node.result;
3787
+ // Create memoize internal effect to track dependencies and invalidate cache
3788
+ // Use untracked to prevent the effect creation from being affected by parent effects
3789
+ node.cleanup = root(() => effect(markWithRoot(() => {
3790
+ // Execute the function and track its dependencies
3791
+ // The function execution will automatically track dependencies on reactive objects
3792
+ node.result = fn(...localArgs);
3793
+ return () => {
3794
+ // When dependencies change, clear the cache and notify consumers
3795
+ delete node.result;
3796
+ touched1(node, { type: 'invalidate', prop: localArgs }, 'memoize');
3797
+ };
3798
+ }, fnRoot), { opaque: true }));
3799
+ return node.result;
3800
+ }, fn);
3801
+ memoizedRegistry.set(fnRoot, memoized);
3802
+ memoizedRegistry.set(memoized, memoized);
3803
+ return memoized;
3804
+ }
3805
+ const memoize = decorator({
3806
+ getter(original, propertyKey) {
3807
+ const memoized = memoizeFunction(markWithRoot(renamed((that) => {
3808
+ return original.call(that);
3809
+ }, `${String(this.constructor.name)}.${String(propertyKey)}`), original));
3810
+ return function () {
3811
+ return memoized(this);
3812
+ };
3813
+ },
3814
+ method(original, name) {
3815
+ const memoized = memoizeFunction(markWithRoot(renamed((that, ...args) => {
3816
+ return original.call(that, ...args);
3817
+ }, `${String(this.constructor.name)}.${String(name)}`), original));
3818
+ return function (...args) {
3819
+ return memoized(this, ...args);
3820
+ };
3821
+ },
3822
+ default: memoizeFunction,
3823
+ });
3824
+
3825
+ // Helper to work around TypeScript limitation: base class expressions cannot reference class type parameters
3826
+ function getRegisterBase() {
3827
+ class RegisterBase extends Indexable(ArrayReadForward, {
3828
+ get(index) {
3829
+ return this[getAt](index);
3830
+ },
3831
+ set(index, value) {
3832
+ this[setAt](index, value);
3833
+ },
3834
+ getLength() {
3835
+ return this.length;
3836
+ },
3837
+ setLength(value) {
3838
+ this.length = value;
3839
+ },
3840
+ }) {
3841
+ toArray() {
3842
+ return Array.from(this);
3843
+ }
3844
+ }
3845
+ return RegisterBase;
3846
+ }
3847
+ let RegisterClass = (() => {
3848
+ var _RegisterClass_instances, _RegisterClass_keyFn, _RegisterClass_keys, _RegisterClass_values, _RegisterClass_usage, _RegisterClass_valueInfo, _RegisterClass_keyEffects, _RegisterClass_ascend, _RegisterClass_rekeyValue;
3849
+ let _classDecorators = [unreactive];
3850
+ let _classDescriptor;
3851
+ let _classExtraInitializers = [];
3852
+ let _classThis;
3853
+ let _classSuper = getRegisterBase();
3854
+ _classThis = class extends _classSuper {
3855
+ get [(_RegisterClass_keyFn = new WeakMap(), _RegisterClass_keys = new WeakMap(), _RegisterClass_values = new WeakMap(), _RegisterClass_usage = new WeakMap(), _RegisterClass_valueInfo = new WeakMap(), _RegisterClass_keyEffects = new WeakMap(), _RegisterClass_ascend = new WeakMap(), _RegisterClass_instances = new WeakSet(), forwardArray)]() {
3856
+ return this.toArray();
3857
+ }
3858
+ constructor(keyFn, initial) {
3859
+ super();
3860
+ _RegisterClass_instances.add(this);
3861
+ _RegisterClass_keyFn.set(this, void 0);
3862
+ _RegisterClass_keys.set(this, void 0);
3863
+ _RegisterClass_values.set(this, void 0);
3864
+ _RegisterClass_usage.set(this, new Map());
3865
+ _RegisterClass_valueInfo.set(this, new Map());
3866
+ _RegisterClass_keyEffects.set(this, new Set());
3867
+ _RegisterClass_ascend.set(this, void 0);
3868
+ /* Moved below initialization */
3869
+ let ascendGet;
3870
+ effect(({ ascend }) => {
3871
+ ascendGet = ascend;
3872
+ });
3873
+ __classPrivateFieldSet(this, _RegisterClass_ascend, ascendGet, "f");
3874
+ if (typeof keyFn !== 'function')
3875
+ throw new Error('Register requires a key function');
3876
+ __classPrivateFieldSet(this, _RegisterClass_keyFn, keyFn, "f");
3877
+ __classPrivateFieldSet(this, _RegisterClass_keys, reactive([]), "f");
3878
+ __classPrivateFieldSet(this, _RegisterClass_values, reactive(new Map()), "f");
3879
+ Object.defineProperties(this, {
3880
+ [prototypeForwarding]: { value: __classPrivateFieldGet(this, _RegisterClass_keys, "f") },
3881
+ });
3882
+ if (initial)
3883
+ this.push(...initial);
3884
+ }
3885
+ ensureKey(value) {
3886
+ let info = __classPrivateFieldGet(this, _RegisterClass_valueInfo, "f").get(value);
3887
+ if (info)
3888
+ return info.key;
3889
+ info = { key: undefined };
3890
+ __classPrivateFieldGet(this, _RegisterClass_valueInfo, "f").set(value, info);
3891
+ __classPrivateFieldGet(this, _RegisterClass_ascend, "f").call(this, () => {
3892
+ const stop = effect(({ reaction }) => {
3893
+ const nextKey = __classPrivateFieldGet(this, _RegisterClass_keyFn, "f").call(this, value);
3894
+ this.assertValidKey(nextKey);
3895
+ const previousKey = info.key;
3896
+ if (reaction && previousKey !== undefined && !Object.is(nextKey, previousKey))
3897
+ __classPrivateFieldGet(this, _RegisterClass_instances, "m", _RegisterClass_rekeyValue).call(this, value, previousKey, nextKey);
3898
+ info.key = nextKey;
3899
+ });
3900
+ info.stop = stop;
3901
+ __classPrivateFieldGet(this, _RegisterClass_keyEffects, "f").add(stop);
3902
+ });
3903
+ if (info.key === undefined)
3904
+ throw new Error('Register key function must return a property key');
3905
+ return info.key;
3906
+ }
3907
+ assertValidKey(key) {
3908
+ const type = typeof key;
3909
+ if (type !== 'string' && type !== 'number' && type !== 'symbol')
3910
+ throw new Error('Register key function must return a property key');
3911
+ }
3912
+ setKeyValue(key, value) {
3913
+ const existing = __classPrivateFieldGet(this, _RegisterClass_values, "f").get(key);
3914
+ if (existing !== undefined && existing !== value)
3915
+ this.cleanupValue(existing);
3916
+ __classPrivateFieldGet(this, _RegisterClass_values, "f").set(key, value);
3917
+ }
3918
+ cleanupValue(value) {
3919
+ const info = __classPrivateFieldGet(this, _RegisterClass_valueInfo, "f").get(value);
3920
+ if (!info)
3921
+ return;
3922
+ const stop = info.stop;
3923
+ if (stop) {
3924
+ info.stop = undefined;
3925
+ __classPrivateFieldGet(this, _RegisterClass_keyEffects, "f").delete(stop);
3926
+ stop();
3927
+ }
3928
+ __classPrivateFieldGet(this, _RegisterClass_valueInfo, "f").delete(value);
3929
+ }
3930
+ disposeKeyEffects() {
3931
+ for (const value of Array.from(__classPrivateFieldGet(this, _RegisterClass_valueInfo, "f").keys()))
3932
+ this.cleanupValue(value);
3933
+ __classPrivateFieldGet(this, _RegisterClass_keyEffects, "f").clear();
3934
+ }
3935
+ incrementUsage(key) {
3936
+ const count = __classPrivateFieldGet(this, _RegisterClass_usage, "f").get(key) ?? 0;
3937
+ __classPrivateFieldGet(this, _RegisterClass_usage, "f").set(key, count + 1);
3938
+ }
3939
+ decrementUsage(key) {
3940
+ const count = __classPrivateFieldGet(this, _RegisterClass_usage, "f").get(key);
3941
+ if (!count)
3942
+ return;
3943
+ if (count <= 1) {
3944
+ const value = __classPrivateFieldGet(this, _RegisterClass_values, "f").get(key);
3945
+ __classPrivateFieldGet(this, _RegisterClass_usage, "f").delete(key);
3946
+ __classPrivateFieldGet(this, _RegisterClass_values, "f").delete(key);
3947
+ if (value !== undefined)
3948
+ this.cleanupValue(value);
3949
+ }
3950
+ else {
3951
+ __classPrivateFieldGet(this, _RegisterClass_usage, "f").set(key, count - 1);
3952
+ }
3953
+ }
3954
+ normalizeIndex(index, allowEnd = false) {
3955
+ const length = this.length;
3956
+ let resolved = index;
3957
+ if (resolved < 0)
3958
+ resolved = Math.max(length + resolved, 0);
3959
+ if (resolved > length) {
3960
+ if (allowEnd)
3961
+ resolved = length;
3962
+ else
3963
+ throw new RangeError('Index out of bounds');
3964
+ }
3965
+ if (!allowEnd && resolved === length)
3966
+ throw new RangeError('Index out of bounds');
3967
+ return resolved;
3968
+ }
3969
+ assignAt(index, key, value) {
3970
+ const oldKey = __classPrivateFieldGet(this, _RegisterClass_keys, "f")[index];
3971
+ if (oldKey !== undefined && Object.is(oldKey, key)) {
3972
+ this.setKeyValue(key, value);
3973
+ return;
3974
+ }
3975
+ if (oldKey !== undefined)
3976
+ this.decrementUsage(oldKey);
3977
+ __classPrivateFieldGet(this, _RegisterClass_keys, "f")[index] = key;
3978
+ this.incrementUsage(key);
3979
+ this.setKeyValue(key, value);
3980
+ }
3981
+ insertKeyValue(index, key, value) {
3982
+ __classPrivateFieldGet(this, _RegisterClass_keys, "f").splice(index, 0, key);
3983
+ this.incrementUsage(key);
3984
+ this.setKeyValue(key, value);
3985
+ }
3986
+ rebuildFrom(values) {
3987
+ this.disposeKeyEffects();
3988
+ __classPrivateFieldGet(this, _RegisterClass_keys, "f").splice(0, __classPrivateFieldGet(this, _RegisterClass_keys, "f").length);
3989
+ __classPrivateFieldGet(this, _RegisterClass_usage, "f").clear();
3990
+ __classPrivateFieldGet(this, _RegisterClass_values, "f").clear();
3991
+ for (const value of values) {
3992
+ const key = this.ensureKey(value);
3993
+ __classPrivateFieldGet(this, _RegisterClass_keys, "f").push(key);
3994
+ this.incrementUsage(key);
3995
+ __classPrivateFieldGet(this, _RegisterClass_values, "f").set(key, value);
3996
+ }
3997
+ }
3998
+ get length() {
3999
+ return __classPrivateFieldGet(this, _RegisterClass_keys, "f").length;
4000
+ }
4001
+ [(_RegisterClass_rekeyValue = function _RegisterClass_rekeyValue(value, oldKey, newKey) {
4002
+ if (Object.is(oldKey, newKey))
4003
+ return;
4004
+ const existingValue = __classPrivateFieldGet(this, _RegisterClass_values, "f").get(newKey);
4005
+ if (existingValue !== undefined && existingValue !== value)
4006
+ throw new Error(`Register key collision for key ${String(newKey)}`);
4007
+ const count = __classPrivateFieldGet(this, _RegisterClass_usage, "f").get(oldKey);
4008
+ if (!count)
4009
+ return;
4010
+ const existingCount = __classPrivateFieldGet(this, _RegisterClass_usage, "f").get(newKey) ?? 0;
4011
+ this.setKeyValue(newKey, value);
4012
+ for (let i = 0; i < __classPrivateFieldGet(this, _RegisterClass_keys, "f").length; i++)
4013
+ if (Object.is(__classPrivateFieldGet(this, _RegisterClass_keys, "f")[i], oldKey))
4014
+ __classPrivateFieldGet(this, _RegisterClass_keys, "f")[i] = newKey;
4015
+ __classPrivateFieldGet(this, _RegisterClass_usage, "f").set(newKey, existingCount + count);
4016
+ __classPrivateFieldGet(this, _RegisterClass_usage, "f").delete(oldKey);
4017
+ __classPrivateFieldGet(this, _RegisterClass_values, "f").delete(oldKey);
4018
+ const updatedInfo = __classPrivateFieldGet(this, _RegisterClass_valueInfo, "f").get(value);
4019
+ if (updatedInfo)
4020
+ updatedInfo.key = newKey;
4021
+ }, getAt)](index) {
4022
+ const key = __classPrivateFieldGet(this, _RegisterClass_keys, "f")[index];
4023
+ return key === undefined ? undefined : __classPrivateFieldGet(this, _RegisterClass_values, "f").get(key);
4024
+ }
4025
+ [setAt](index, value) {
4026
+ const key = this.ensureKey(value);
4027
+ if (index === this.length) {
4028
+ this.insertKeyValue(index, key, value);
4029
+ return;
4030
+ }
4031
+ const normalized = this.normalizeIndex(index);
4032
+ this.assignAt(normalized, key, value);
4033
+ }
4034
+ push(...items) {
4035
+ for (const item of items) {
4036
+ const key = this.ensureKey(item);
4037
+ this.insertKeyValue(this.length, key, item);
4038
+ }
4039
+ return this.length;
4040
+ }
4041
+ pop() {
4042
+ if (!this.length)
4043
+ return undefined;
4044
+ return this.removeAt(this.length - 1);
4045
+ }
4046
+ shift() {
4047
+ if (!this.length)
4048
+ return undefined;
4049
+ return this.removeAt(0);
4050
+ }
4051
+ unshift(...items) {
4052
+ let index = 0;
4053
+ for (const item of items) {
4054
+ const key = this.ensureKey(item);
4055
+ this.insertKeyValue(index++, key, item);
4056
+ }
4057
+ return this.length;
4058
+ }
4059
+ splice(start, deleteCount, ...items) {
4060
+ const normalizedStart = this.normalizeIndex(start, true);
4061
+ const maxDeletions = this.length - normalizedStart;
4062
+ const actualDelete = Math.min(deleteCount === undefined ? maxDeletions : Math.max(deleteCount, 0), maxDeletions);
4063
+ const keysToInsert = [];
4064
+ for (const item of items)
4065
+ keysToInsert.push(this.ensureKey(item));
4066
+ const removedKeys = __classPrivateFieldGet(this, _RegisterClass_keys, "f").splice(normalizedStart, actualDelete, ...keysToInsert);
4067
+ const removedValues = [];
4068
+ for (const key of removedKeys) {
4069
+ if (key === undefined)
4070
+ continue;
4071
+ const value = __classPrivateFieldGet(this, _RegisterClass_values, "f").get(key);
4072
+ this.decrementUsage(key);
4073
+ removedValues.push(value);
4074
+ }
4075
+ for (let i = 0; i < keysToInsert.length; i++) {
4076
+ const key = keysToInsert[i];
4077
+ const value = items[i];
4078
+ this.incrementUsage(key);
4079
+ this.setKeyValue(key, value);
4080
+ }
4081
+ return removedValues;
4082
+ }
4083
+ clear() {
4084
+ __classPrivateFieldGet(this, _RegisterClass_keys, "f").length = 0;
4085
+ __classPrivateFieldGet(this, _RegisterClass_usage, "f").clear();
4086
+ __classPrivateFieldGet(this, _RegisterClass_values, "f").clear();
4087
+ this.disposeKeyEffects();
4088
+ }
4089
+ get(key) {
4090
+ return __classPrivateFieldGet(this, _RegisterClass_values, "f").get(key);
4091
+ }
4092
+ set(key, value) {
4093
+ if (__classPrivateFieldGet(this, _RegisterClass_values, "f").has(key))
4094
+ this.setKeyValue(key, value);
4095
+ }
4096
+ remove(key) {
4097
+ let index = this.indexOfKey(key);
4098
+ while (index !== -1) {
4099
+ this.removeAt(index);
4100
+ index = this.indexOfKey(key);
4101
+ }
4102
+ }
4103
+ removeAt(index) {
4104
+ const [key] = __classPrivateFieldGet(this, _RegisterClass_keys, "f").splice(index, 1);
4105
+ if (key === undefined)
4106
+ return undefined;
4107
+ const value = __classPrivateFieldGet(this, _RegisterClass_values, "f").get(key);
4108
+ this.decrementUsage(key);
4109
+ return value;
4110
+ }
4111
+ /**
4112
+ * Keep only the items for which the predicate returns true.
4113
+ * Items for which the predicate returns false are removed.
4114
+ *
4115
+ * The predicate is evaluated once per distinct key; duplicate keys
4116
+ * will follow the same keep/remove decision.
4117
+ */
4118
+ keep(predicate) {
4119
+ const decisions = new Map();
4120
+ for (const [index, key] of __classPrivateFieldGet(this, _RegisterClass_keys, "f").entries()) {
4121
+ if (decisions.has(key)) {
4122
+ if (!decisions.get(key))
4123
+ this.removeAt(index);
4124
+ continue;
4125
+ }
4126
+ const value = __classPrivateFieldGet(this, _RegisterClass_values, "f").get(key);
4127
+ const shouldKeep = predicate(value);
4128
+ decisions.set(key, shouldKeep);
4129
+ if (!shouldKeep)
4130
+ this.removeAt(index);
4131
+ }
4132
+ }
4133
+ hasKey(key) {
4134
+ return __classPrivateFieldGet(this, _RegisterClass_usage, "f").has(key);
4135
+ }
4136
+ indexOfKey(key) {
4137
+ return __classPrivateFieldGet(this, _RegisterClass_keys, "f").indexOf(key);
4138
+ }
4139
+ mapKeys() {
4140
+ return __classPrivateFieldGet(this, _RegisterClass_values, "f").keys();
4141
+ }
4142
+ update(...values) {
4143
+ for (const value of values) {
4144
+ const key = this.ensureKey(value);
4145
+ if (__classPrivateFieldGet(this, _RegisterClass_values, "f").has(key))
4146
+ this.setKeyValue(key, value);
4147
+ }
4148
+ }
4149
+ upsert(insert, ...values) {
4150
+ for (const value of values) {
4151
+ const key = this.ensureKey(value);
4152
+ if (__classPrivateFieldGet(this, _RegisterClass_values, "f").has(key))
4153
+ this.setKeyValue(key, value);
4154
+ else
4155
+ insert(value);
4156
+ }
4157
+ }
4158
+ entries() {
4159
+ const self = this;
4160
+ function* iterator() {
4161
+ for (let i = 0; i < __classPrivateFieldGet(self, _RegisterClass_keys, "f").length; i++) {
4162
+ const val = __classPrivateFieldGet(self, _RegisterClass_values, "f").get(__classPrivateFieldGet(self, _RegisterClass_keys, "f")[i]);
4163
+ if (val !== undefined)
4164
+ yield [i, val];
4165
+ }
4166
+ }
4167
+ return iterator();
4168
+ }
4169
+ [Symbol.iterator]() {
4170
+ const self = this;
4171
+ function* iterator() {
4172
+ for (const key of __classPrivateFieldGet(self, _RegisterClass_keys, "f")) {
4173
+ const value = __classPrivateFieldGet(self, _RegisterClass_values, "f").get(key);
4174
+ if (value !== undefined)
4175
+ yield value;
4176
+ }
4177
+ }
4178
+ return iterator();
4179
+ }
4180
+ toString() {
4181
+ return `[Register length=${this.length}]`;
4182
+ }
4183
+ at(index) {
4184
+ const resolved = index < 0 ? this.length + index : index;
4185
+ if (resolved < 0 || resolved >= this.length)
4186
+ return undefined;
4187
+ return this[getAt](resolved);
4188
+ }
4189
+ reverse() {
4190
+ __classPrivateFieldGet(this, _RegisterClass_keys, "f").reverse();
4191
+ return this;
4192
+ }
4193
+ sort(compareFn) {
4194
+ const fwdCompareFn = compareFn
4195
+ ? (a, b) => compareFn(__classPrivateFieldGet(this, _RegisterClass_values, "f").get(a), __classPrivateFieldGet(this, _RegisterClass_values, "f").get(b))
4196
+ : undefined;
4197
+ __classPrivateFieldGet(this, _RegisterClass_keys, "f").sort(fwdCompareFn);
4198
+ return this;
4199
+ }
4200
+ fill(value, start = 0, end = this.length) {
4201
+ const values = this.toArray();
4202
+ values.fill(value, start, end);
4203
+ this.rebuildFrom(values);
4204
+ return this;
4205
+ }
4206
+ copyWithin(target, start, end) {
4207
+ const values = this.toArray();
4208
+ values.copyWithin(target, start, end);
4209
+ this.rebuildFrom(values);
4210
+ return this;
4211
+ }
4212
+ };
4213
+ __setFunctionName(_classThis, "RegisterClass");
4214
+ (() => {
4215
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
4216
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
4217
+ _classThis = _classDescriptor.value;
4218
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
4219
+ __runInitializers(_classThis, _classExtraInitializers);
4220
+ })();
4221
+ return _classThis;
4222
+ })();
4223
+ const Register = RegisterClass;
4224
+ function register(keyFn, initial) {
4225
+ return new RegisterClass(keyFn, initial);
4226
+ }
4227
+
4228
+ function defineAccessValue(access) {
4229
+ Object.defineProperty(access, 'value', {
4230
+ get: access.get,
4231
+ set: access.set,
4232
+ configurable: true,
4233
+ enumerable: true,
4234
+ });
4235
+ }
4236
+ function makeCleanup(target, effectMap, onDispose) {
4237
+ return cleanedBy(target, () => {
4238
+ onDispose();
4239
+ for (const stop of effectMap.values())
4240
+ stop?.();
4241
+ effectMap.clear();
4242
+ });
4243
+ }
4244
+ function projectArray(source, apply) {
4245
+ const observedSource = reactive(source);
4246
+ const target = reactive([]);
4247
+ const indexEffects = new Map();
4248
+ function normalizeTargetLength(length) {
4249
+ ReflectSet(target, 'length', length, target);
4250
+ }
4251
+ function disposeIndex(index) {
4252
+ const stopEffect = indexEffects.get(index);
4253
+ if (stopEffect) {
4254
+ indexEffects.delete(index);
4255
+ stopEffect();
4256
+ Reflect.deleteProperty(target, index);
4257
+ }
4258
+ }
4259
+ const cleanupLength = effect(function projectArrayLengthEffect({ ascend }) {
4260
+ const length = observedSource.length;
4261
+ normalizeTargetLength(length);
4262
+ const existing = Array.from(indexEffects.keys());
4263
+ for (let i = 0; i < length; i++) {
4264
+ if (indexEffects.has(i))
4265
+ continue;
4266
+ ascend(() => {
4267
+ const index = i;
4268
+ const stop = effect(function projectArrayIndexEffect() {
4269
+ const previous = untracked(() => target[index]);
4270
+ const accessBase = {
4271
+ key: index,
4272
+ source: observedSource,
4273
+ get: () => ReflectGet(observedSource, index, observedSource),
4274
+ set: (value) => ReflectSet(observedSource, index, value, observedSource),
4275
+ old: previous,
4276
+ };
4277
+ defineAccessValue(accessBase);
4278
+ const produced = apply(accessBase, target);
4279
+ target[index] = produced;
4280
+ });
4281
+ indexEffects.set(i, stop);
4282
+ });
4283
+ }
4284
+ for (const index of existing)
4285
+ if (index >= length)
4286
+ disposeIndex(index);
4287
+ });
4288
+ return makeCleanup(target, indexEffects, () => cleanupLength());
4289
+ }
4290
+ function projectRegister(source, apply) {
4291
+ const observedSource = reactive(source);
4292
+ const rawTarget = new Map();
4293
+ const target = reactive(rawTarget);
4294
+ const keyEffects = new Map();
4295
+ function disposeKey(key) {
4296
+ const stopEffect = keyEffects.get(key);
4297
+ if (stopEffect) {
4298
+ stopEffect();
4299
+ keyEffects.delete(key);
4300
+ target.delete(key);
4301
+ }
4302
+ }
4303
+ const cleanupKeys = effect(function projectRegisterEffect({ ascend }) {
4304
+ const keys = new Set();
4305
+ for (const key of observedSource.mapKeys())
4306
+ keys.add(key);
4307
+ for (const key of keys) {
4308
+ if (keyEffects.has(key))
4309
+ continue;
4310
+ ascend(() => {
4311
+ const stop = effect(function projectRegisterKeyEffect() {
4312
+ const previous = untracked(() => target.get(key));
4313
+ const accessBase = {
4314
+ key,
4315
+ source: observedSource,
4316
+ get: () => observedSource.get(key),
4317
+ set: (value) => {
4318
+ observedSource.set(key, value);
4319
+ return true;
4320
+ },
4321
+ old: previous,
4322
+ };
4323
+ defineAccessValue(accessBase);
4324
+ const produced = apply(accessBase, target);
4325
+ target.set(key, produced);
4326
+ });
4327
+ keyEffects.set(key, stop);
4328
+ });
4329
+ }
4330
+ for (const key of Array.from(keyEffects.keys()))
4331
+ if (!keys.has(key))
4332
+ disposeKey(key);
4333
+ });
4334
+ return makeCleanup(target, keyEffects, () => cleanupKeys());
4335
+ }
4336
+ function projectRecord(source, apply) {
4337
+ const observedSource = reactive(source);
4338
+ const target = reactive({});
4339
+ const keyEffects = new Map();
4340
+ function disposeKey(key) {
4341
+ const stopEffect = keyEffects.get(key);
4342
+ if (stopEffect) {
4343
+ stopEffect();
4344
+ keyEffects.delete(key);
4345
+ Reflect.deleteProperty(target, key);
4346
+ }
4347
+ }
4348
+ const cleanupKeys = effect(function projectRecordEffect({ ascend }) {
4349
+ const keys = new Set();
4350
+ for (const key in observedSource)
4351
+ keys.add(key);
4352
+ const observed = Reflect.ownKeys(observedSource);
4353
+ for (const key of observed)
4354
+ keys.add(key);
4355
+ for (const key of keys) {
4356
+ if (keyEffects.has(key))
4357
+ continue;
4358
+ ascend(() => {
4359
+ const stop = effect(function projectRecordKeyEffect() {
4360
+ const sourceKey = key;
4361
+ const previous = untracked(() => target[key]);
4362
+ const accessBase = {
4363
+ key: sourceKey,
4364
+ source: observedSource,
4365
+ get: () => ReflectGet(observedSource, sourceKey, observedSource),
4366
+ set: (value) => ReflectSet(observedSource, sourceKey, value, observedSource),
4367
+ old: previous,
4368
+ };
4369
+ defineAccessValue(accessBase);
4370
+ const produced = apply(accessBase, target);
4371
+ target[sourceKey] = produced;
4372
+ });
4373
+ keyEffects.set(key, stop);
4374
+ });
4375
+ }
4376
+ for (const key of Array.from(keyEffects.keys()))
4377
+ if (!keys.has(key))
4378
+ disposeKey(key);
4379
+ });
4380
+ return makeCleanup(target, keyEffects, () => cleanupKeys());
4381
+ }
4382
+ function projectMap(source, apply) {
4383
+ const observedSource = reactive(source);
4384
+ const rawTarget = new Map();
4385
+ const target = reactive(rawTarget);
4386
+ const keyEffects = new Map();
4387
+ function disposeKey(key) {
4388
+ const stopEffect = keyEffects.get(key);
4389
+ if (stopEffect) {
4390
+ stopEffect();
4391
+ keyEffects.delete(key);
4392
+ target.delete(key);
4393
+ }
4394
+ }
4395
+ const cleanupKeys = effect(function projectMapEffect({ ascend }) {
4396
+ const keys = new Set();
4397
+ for (const key of observedSource.keys())
4398
+ keys.add(key);
4399
+ for (const key of keys) {
4400
+ if (keyEffects.has(key))
4401
+ continue;
4402
+ ascend(() => {
4403
+ const stop = effect(function projectMapKeyEffect() {
4404
+ const previous = untracked(() => target.get(key));
4405
+ const accessBase = {
4406
+ key,
4407
+ source: observedSource,
4408
+ get: () => observedSource.get(key),
4409
+ set: (value) => {
4410
+ observedSource.set(key, value);
4411
+ return true;
4412
+ },
4413
+ old: previous,
4414
+ };
4415
+ defineAccessValue(accessBase);
4416
+ const produced = apply(accessBase, target);
4417
+ target.set(key, produced);
4418
+ });
4419
+ keyEffects.set(key, stop);
4420
+ });
4421
+ }
4422
+ for (const key of Array.from(keyEffects.keys()))
4423
+ if (!keys.has(key))
4424
+ disposeKey(key);
4425
+ });
4426
+ return makeCleanup(target, keyEffects, () => cleanupKeys());
4427
+ }
4428
+ function projectCore(source, apply) {
4429
+ if (Array.isArray(source))
4430
+ return projectArray(source, apply);
4431
+ if (source instanceof Map)
4432
+ return projectMap(source, apply);
4433
+ if (source instanceof Register)
4434
+ return projectRegister(source, apply);
4435
+ if (source && (source.constructor === Object || source.constructor === undefined))
4436
+ return projectRecord(source, apply);
4437
+ throw new Error('Unsupported source type');
4438
+ }
4439
+ const project = Object.assign(projectCore, {
4440
+ array: projectArray,
4441
+ register: projectRegister,
4442
+ record: projectRecord,
4443
+ map: projectMap,
4444
+ });
4445
+
4446
+ /**
4447
+ * Organizes a source object's properties into a target object using a callback function.
4448
+ * This creates a reactive mapping between source properties and a target object,
4449
+ * automatically handling property additions, updates, and removals.
4450
+ *
4451
+ * @template Source - The type of the source object
4452
+ * @template Target - The type of the target object (defaults to Record<PropertyKey, any>)
4453
+ *
4454
+ * @param {Source} source - The source object to organize
4455
+ * @param {OrganizedCallback<Source, Target>} apply - Callback function that defines how each source property is mapped to the target
4456
+ * @param {Target} [baseTarget={}] - Optional base target object to use (will be made reactive if not already)
4457
+ *
4458
+ * @returns {OrganizedResult<Target>} The target object with cleanup capability
4459
+ *
4460
+ * @example
4461
+ * // Organize user permissions into role-based access
4462
+ * const user = reactive({ isAdmin: true, canEdit: false });
4463
+ * const permissions = organized(
4464
+ * user,
4465
+ * (access, target) => {
4466
+ * if (access.key === 'isAdmin') {
4467
+ * target.hasFullAccess = access.value;
4468
+ * }
4469
+ * target[`can${access.key.charAt(0).toUpperCase() + access.key.slice(1)}`] = access.value;
4470
+ * }
4471
+ * );
4472
+ *
4473
+ * @example
4474
+ * // Transform object structure with cleanup
4475
+ * const source = reactive({ firstName: 'John', lastName: 'Doe' });
4476
+ * const formatted = organized(
4477
+ * source,
4478
+ * (access, target) => {
4479
+ * if (access.key === 'firstName' || access.key === 'lastName') {
4480
+ * target.fullName = `${source.firstName} ${source.lastName}`.trim();
4481
+ * }
4482
+ * }
4483
+ * );
4484
+ *
4485
+ * @example
4486
+ * // Using with cleanup in a component
4487
+ * effect(() => {
4488
+ * const data = fetchData();
4489
+ * const organizedData = organized(data, (access, target) => {
4490
+ * // Transform data
4491
+ * });
4492
+ *
4493
+ * // The cleanup will be called automatically when the effect is disposed
4494
+ * return () => organizedData[cleanup]();
4495
+ * });
4496
+ */
4497
+ function organized(source, apply, baseTarget = {}) {
4498
+ const observedSource = reactive(source);
4499
+ const target = reactive(baseTarget);
4500
+ const keyEffects = new Map();
4501
+ function disposeKey(key) {
4502
+ const stopEffect = keyEffects.get(key);
4503
+ if (stopEffect) {
4504
+ keyEffects.delete(key);
4505
+ stopEffect();
4506
+ }
4507
+ }
4508
+ const cleanupKeys = effect(function organizedKeysEffect({ ascend }) {
4509
+ //const keys = Reflect.ownKeys(observedSource) as PropertyKey[]
4510
+ const keys = new Set();
4511
+ for (const key in observedSource)
4512
+ keys.add(key);
4513
+ for (const key of keys) {
4514
+ if (keyEffects.has(key))
4515
+ continue;
4516
+ ascend(() => {
4517
+ const stop = effect(function organizedKeyEffect() {
4518
+ const sourceKey = key;
4519
+ const accessBase = {
4520
+ key: sourceKey,
4521
+ get: () => ReflectGet(observedSource, sourceKey, observedSource),
4522
+ set: (value) => ReflectSet(observedSource, sourceKey, value, observedSource),
4523
+ };
4524
+ Object.defineProperty(accessBase, 'value', {
4525
+ get: accessBase.get,
4526
+ set: accessBase.set,
4527
+ configurable: true,
4528
+ enumerable: true,
4529
+ });
4530
+ return apply(accessBase, target);
4531
+ });
4532
+ keyEffects.set(key, stop);
4533
+ });
4534
+ }
4535
+ for (const key of Array.from(keyEffects.keys()))
4536
+ if (!keys.has(key))
4537
+ disposeKey(key);
4538
+ });
4539
+ return cleanedBy(target, () => {
4540
+ cleanupKeys();
4541
+ for (const key of Array.from(keyEffects.keys()))
4542
+ disposeKey(key);
4543
+ });
4544
+ }
4545
+ /**
4546
+ * Organizes a property on a target object
4547
+ * Shortcut for defineProperty/delete with touched signal
4548
+ * @param target - The target object
4549
+ * @param property - The property to organize
4550
+ * @param access - The access object
4551
+ * @returns The property descriptor
4552
+ */
4553
+ function organize(target, property, access) {
4554
+ Object.defineProperty(target, property, {
4555
+ get: access.get,
4556
+ set: access.set,
4557
+ configurable: true,
4558
+ enumerable: true,
4559
+ });
4560
+ touched1(target, { type: 'set', prop: property }, property);
4561
+ return () => delete target[property];
4562
+ }
4563
+
4564
+ const native$1 = Symbol('native');
4565
+ /**
4566
+ * Reactive wrapper around JavaScript's WeakMap class
4567
+ * Only tracks individual key operations, no size tracking (WeakMap limitation)
4568
+ */
4569
+ class ReactiveWeakMap {
4570
+ constructor(original) {
4571
+ Object.defineProperties(this, {
4572
+ [native$1]: { value: original },
4573
+ [prototypeForwarding]: { value: original },
4574
+ content: { value: Symbol('content') },
4575
+ [Symbol.toStringTag]: { value: 'ReactiveWeakMap' },
4576
+ });
4577
+ }
4578
+ // Implement WeakMap interface methods with reactivity
4579
+ delete(key) {
4580
+ const hadKey = this[native$1].has(key);
4581
+ const result = this[native$1].delete(key);
4582
+ if (hadKey)
4583
+ touched1(this.content, { type: 'del', prop: key }, key);
4584
+ return result;
4585
+ }
4586
+ get(key) {
4587
+ dependant(this.content, key);
4588
+ return reactive(this[native$1].get(key));
4589
+ }
4590
+ has(key) {
4591
+ dependant(this.content, key);
4592
+ return this[native$1].has(key);
4593
+ }
4594
+ set(key, value) {
4595
+ const hadKey = this[native$1].has(key);
4596
+ const oldValue = this[native$1].get(key);
4597
+ const reactiveValue = reactive(value);
4598
+ this[native$1].set(key, reactiveValue);
4599
+ if (!hadKey || oldValue !== reactiveValue) {
4600
+ notifyPropertyChange(this.content, key, oldValue, reactiveValue, hadKey);
4601
+ }
4602
+ return this;
4603
+ }
4604
+ }
4605
+ /**
4606
+ * Reactive wrapper around JavaScript's Map class
4607
+ * Tracks size changes, individual key operations, and collection-wide operations
4608
+ */
4609
+ class ReactiveMap {
4610
+ constructor(original) {
4611
+ Object.defineProperties(this, {
4612
+ [native$1]: { value: original },
4613
+ [prototypeForwarding]: { value: original },
4614
+ content: { value: Symbol('content') },
4615
+ [Symbol.toStringTag]: { value: 'ReactiveMap' },
4616
+ });
4617
+ }
4618
+ // Implement Map interface methods with reactivity
4619
+ get size() {
4620
+ dependant(this, 'size'); // The ReactiveMap instance still goes through proxy
4621
+ return this[native$1].size;
4622
+ }
4623
+ clear() {
4624
+ const hadEntries = this[native$1].size > 0;
4625
+ this[native$1].clear();
4626
+ if (hadEntries) {
4627
+ const evolution = { type: 'bunch', method: 'clear' };
4628
+ // Clear triggers all effects since all keys are affected
4629
+ touched1(this, evolution, 'size');
4630
+ touched(this.content, evolution);
4631
+ }
4632
+ }
4633
+ entries() {
4634
+ dependant(this.content);
4635
+ return makeReactiveEntriesIterator(this[native$1].entries());
4636
+ }
4637
+ forEach(callbackfn, thisArg) {
4638
+ dependant(this.content);
4639
+ this[native$1].forEach(callbackfn, thisArg);
4640
+ }
4641
+ keys() {
4642
+ dependant(this.content);
4643
+ return this[native$1].keys();
4644
+ }
4645
+ values() {
4646
+ dependant(this.content);
4647
+ return makeReactiveIterator(this[native$1].values());
4648
+ }
4649
+ [Symbol.iterator]() {
4650
+ dependant(this.content);
4651
+ const nativeIterator = this[native$1][Symbol.iterator]();
4652
+ return {
4653
+ next() {
4654
+ const result = nativeIterator.next();
4655
+ if (result.done) {
4656
+ return result;
4657
+ }
4658
+ return {
4659
+ value: [result.value[0], reactive(result.value[1])],
4660
+ done: false,
4661
+ };
4662
+ },
4663
+ };
4664
+ }
4665
+ // Implement Map methods with reactivity
4666
+ delete(key) {
4667
+ const hadKey = this[native$1].has(key);
4668
+ const result = this[native$1].delete(key);
4669
+ if (hadKey) {
4670
+ const evolution = { type: 'del', prop: key };
4671
+ touched1(this.content, evolution, key);
4672
+ touched1(this, evolution, 'size');
4673
+ }
4674
+ return result;
4675
+ }
4676
+ get(key) {
4677
+ dependant(this.content, key);
4678
+ return reactive(this[native$1].get(key));
4679
+ }
4680
+ has(key) {
4681
+ dependant(this.content, key);
4682
+ return this[native$1].has(key);
4683
+ }
4684
+ set(key, value) {
4685
+ const hadKey = this[native$1].has(key);
4686
+ const oldValue = this[native$1].get(key);
4687
+ const reactiveValue = reactive(value);
4688
+ this[native$1].set(key, reactiveValue);
4689
+ if (!hadKey || oldValue !== reactiveValue) {
4690
+ notifyPropertyChange(this.content, key, oldValue, reactiveValue, hadKey);
4691
+ // Also notify size change for Map (WeakMap doesn't track size)
4692
+ const evolution = { type: hadKey ? 'set' : 'add', prop: key };
4693
+ touched1(this, evolution, 'size');
4694
+ }
4695
+ return this;
4696
+ }
4697
+ }
4698
+
4699
+ const native = Symbol('native');
4700
+ /**
4701
+ * Reactive wrapper around JavaScript's WeakSet class
4702
+ * Only tracks individual value operations, no size tracking (WeakSet limitation)
4703
+ */
4704
+ class ReactiveWeakSet {
4705
+ constructor(original) {
4706
+ Object.defineProperties(this, {
4707
+ [native]: { value: original },
4708
+ [prototypeForwarding]: { value: original },
4709
+ content: { value: Symbol('content') },
4710
+ [Symbol.toStringTag]: { value: 'ReactiveWeakSet' },
4711
+ });
4712
+ }
4713
+ add(value) {
4714
+ const had = this[native].has(value);
4715
+ this[native].add(value);
4716
+ if (!had) {
4717
+ // touch the specific value and the collection view
4718
+ touched1(this.content, { type: 'add', prop: value }, value);
4719
+ // no size/allProps for WeakSet
4720
+ }
4721
+ return this;
4722
+ }
4723
+ delete(value) {
4724
+ const had = this[native].has(value);
4725
+ const res = this[native].delete(value);
4726
+ if (had)
4727
+ touched1(this.content, { type: 'del', prop: value }, value);
4728
+ return res;
4729
+ }
4730
+ has(value) {
4731
+ dependant(this.content, value);
4732
+ return this[native].has(value);
4733
+ }
4734
+ }
4735
+ /**
4736
+ * Reactive wrapper around JavaScript's Set class
4737
+ * Tracks size changes, individual value operations, and collection-wide operations
4738
+ */
4739
+ class ReactiveSet {
4740
+ constructor(original) {
4741
+ Object.defineProperties(this, {
4742
+ [native]: { value: original },
4743
+ [prototypeForwarding]: { value: original },
4744
+ content: { value: Symbol('content') },
4745
+ [Symbol.toStringTag]: { value: 'ReactiveSet' },
4746
+ });
4747
+ }
4748
+ get size() {
4749
+ // size depends on the wrapper instance, like Map counterpart
4750
+ dependant(this, 'size');
4751
+ return this[native].size;
4752
+ }
4753
+ add(value) {
4754
+ const had = this[native].has(value);
4755
+ const reactiveValue = reactive(value);
4756
+ this[native].add(reactiveValue);
4757
+ if (!had) {
4758
+ const evolution = { type: 'add', prop: reactiveValue };
4759
+ // touch for value-specific and aggregate dependencies
4760
+ touched1(this.content, evolution, reactiveValue);
4761
+ touched1(this, evolution, 'size');
4762
+ }
4763
+ return this;
4764
+ }
4765
+ clear() {
4766
+ const hadEntries = this[native].size > 0;
4767
+ this[native].clear();
4768
+ if (hadEntries) {
4769
+ const evolution = { type: 'bunch', method: 'clear' };
4770
+ touched1(this, evolution, 'size');
4771
+ touched(this.content, evolution);
4772
+ }
4773
+ }
4774
+ delete(value) {
4775
+ const had = this[native].has(value);
4776
+ const res = this[native].delete(value);
4777
+ if (had) {
4778
+ const evolution = { type: 'del', prop: value };
4779
+ touched1(this.content, evolution, value);
4780
+ touched1(this, evolution, 'size');
4781
+ }
4782
+ return res;
4783
+ }
4784
+ has(value) {
4785
+ dependant(this.content, value);
4786
+ return this[native].has(value);
4787
+ }
4788
+ entries() {
4789
+ dependant(this.content);
4790
+ return makeReactiveEntriesIterator(this[native].entries());
4791
+ }
4792
+ forEach(callbackfn, thisArg) {
4793
+ dependant(this.content);
4794
+ this[native].forEach(callbackfn, thisArg);
4795
+ }
4796
+ keys() {
4797
+ dependant(this.content);
4798
+ return makeReactiveIterator(this[native].keys());
4799
+ }
4800
+ values() {
4801
+ dependant(this.content);
4802
+ return makeReactiveIterator(this[native].values());
4803
+ }
4804
+ [Symbol.iterator]() {
4805
+ dependant(this.content);
4806
+ const nativeIterator = this[native][Symbol.iterator]();
4807
+ return {
4808
+ next() {
4809
+ const result = nativeIterator.next();
4810
+ if (result.done) {
4811
+ return result;
4812
+ }
4813
+ return { value: reactive(result.value), done: false };
4814
+ },
4815
+ };
4816
+ }
4817
+ }
4818
+
4819
+ // Register native collection types to use specialized reactive wrappers
4820
+ registerNativeReactivity(WeakMap, ReactiveWeakMap);
4821
+ registerNativeReactivity(Map, ReactiveMap);
4822
+ registerNativeReactivity(WeakSet, ReactiveWeakSet);
4823
+ registerNativeReactivity(Set, ReactiveSet);
4824
+ registerNativeReactivity(Array, ReactiveArray);
4825
+ /**
4826
+ * Object containing internal reactive system state for debugging and profiling
4827
+ */
4828
+ const profileInfo = {
4829
+ objectToProxy,
4830
+ proxyToObject,
4831
+ effectToReactiveObjects,
4832
+ watchers,
4833
+ objectParents,
4834
+ objectsWithDeepWatchers,
4835
+ deepWatchers,
4836
+ effectToDeepWatchedObjects,
4837
+ nonReactiveObjects,
4838
+ };
4839
+
4840
+ export { unreactive as A, watch as B, mapped as C, reduced as D, memoize as E, immutables as F, isNonReactive as G, registerNativeReactivity as H, IterableWeakMap as I, project as J, isReactive as K, ReactiveBase as L, reactive as M, unwrap as N, organize as O, organized as P, Register as Q, ReadOnlyError as R, register as S, options as T, ReactiveError as U, isZoneEnabled as V, setZoneEnabled as W, IterableWeakSet as a, touched1 as b, buildReactivityGraph as c, registerObjectForDebug as d, enableDevTools as e, setObjectName as f, getState as g, deepWatch as h, isDevtoolsEnabled as i, addBatchCleanup as j, atomic as k, biDi as l, mixin as m, defer as n, effect as o, profileInfo as p, getActiveEffect as q, registerEffectForDebug as r, setEffectName as s, touched as t, root as u, trackEffect as v, untracked as w, cleanedBy as x, cleanup as y, derived as z };
4841
+ //# sourceMappingURL=index-DzUDtFc7.esm.js.map