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