mutts 1.0.2 → 1.0.4

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