mutts 1.0.0

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 (82) hide show
  1. package/README.md +150 -0
  2. package/dist/chunks/decorator-BXsign4Z.js +176 -0
  3. package/dist/chunks/decorator-BXsign4Z.js.map +1 -0
  4. package/dist/chunks/decorator-CPbZNnsX.esm.js +168 -0
  5. package/dist/chunks/decorator-CPbZNnsX.esm.js.map +1 -0
  6. package/dist/decorator.d.ts +50 -0
  7. package/dist/decorator.esm.js +2 -0
  8. package/dist/decorator.esm.js.map +1 -0
  9. package/dist/decorator.js +11 -0
  10. package/dist/decorator.js.map +1 -0
  11. package/dist/destroyable.d.ts +48 -0
  12. package/dist/destroyable.esm.js +91 -0
  13. package/dist/destroyable.esm.js.map +1 -0
  14. package/dist/destroyable.js +98 -0
  15. package/dist/destroyable.js.map +1 -0
  16. package/dist/eventful.d.ts +11 -0
  17. package/dist/eventful.esm.js +88 -0
  18. package/dist/eventful.esm.js.map +1 -0
  19. package/dist/eventful.js +90 -0
  20. package/dist/eventful.js.map +1 -0
  21. package/dist/index.d.ts +15 -0
  22. package/dist/index.esm.js +7 -0
  23. package/dist/index.esm.js.map +1 -0
  24. package/dist/index.js +52 -0
  25. package/dist/index.js.map +1 -0
  26. package/dist/indexable.d.ts +31 -0
  27. package/dist/indexable.esm.js +85 -0
  28. package/dist/indexable.esm.js.map +1 -0
  29. package/dist/indexable.js +89 -0
  30. package/dist/indexable.js.map +1 -0
  31. package/dist/mutts.umd.js +2 -0
  32. package/dist/mutts.umd.js.map +1 -0
  33. package/dist/mutts.umd.min.js +2 -0
  34. package/dist/mutts.umd.min.js.map +1 -0
  35. package/dist/promiseChain.d.ts +11 -0
  36. package/dist/promiseChain.esm.js +72 -0
  37. package/dist/promiseChain.esm.js.map +1 -0
  38. package/dist/promiseChain.js +74 -0
  39. package/dist/promiseChain.js.map +1 -0
  40. package/dist/reactive.d.ts +114 -0
  41. package/dist/reactive.esm.js +1455 -0
  42. package/dist/reactive.esm.js.map +1 -0
  43. package/dist/reactive.js +1472 -0
  44. package/dist/reactive.js.map +1 -0
  45. package/dist/std-decorators.d.ts +17 -0
  46. package/dist/std-decorators.esm.js +161 -0
  47. package/dist/std-decorators.esm.js.map +1 -0
  48. package/dist/std-decorators.js +169 -0
  49. package/dist/std-decorators.js.map +1 -0
  50. package/docs/decorator.md +300 -0
  51. package/docs/destroyable.md +294 -0
  52. package/docs/events.md +225 -0
  53. package/docs/indexable.md +561 -0
  54. package/docs/promiseChain.md +218 -0
  55. package/docs/reactive.md +2072 -0
  56. package/docs/std-decorators.md +558 -0
  57. package/package.json +132 -0
  58. package/src/decorator.test.ts +495 -0
  59. package/src/decorator.ts +205 -0
  60. package/src/destroyable.test.ts +155 -0
  61. package/src/destroyable.ts +158 -0
  62. package/src/eventful.test.ts +380 -0
  63. package/src/eventful.ts +69 -0
  64. package/src/index.ts +7 -0
  65. package/src/indexable.test.ts +388 -0
  66. package/src/indexable.ts +124 -0
  67. package/src/promiseChain.test.ts +201 -0
  68. package/src/promiseChain.ts +99 -0
  69. package/src/reactive/array.test.ts +923 -0
  70. package/src/reactive/array.ts +352 -0
  71. package/src/reactive/core.test.ts +1663 -0
  72. package/src/reactive/core.ts +866 -0
  73. package/src/reactive/index.ts +28 -0
  74. package/src/reactive/interface.test.ts +1477 -0
  75. package/src/reactive/interface.ts +231 -0
  76. package/src/reactive/map.test.ts +866 -0
  77. package/src/reactive/map.ts +162 -0
  78. package/src/reactive/set.test.ts +289 -0
  79. package/src/reactive/set.ts +142 -0
  80. package/src/std-decorators.test.ts +679 -0
  81. package/src/std-decorators.ts +182 -0
  82. package/src/utils.ts +52 -0
@@ -0,0 +1,1472 @@
1
+ 'use strict';
2
+
3
+ var decorator = require('./chunks/decorator-BXsign4Z.js');
4
+ var indexable = require('./indexable.js');
5
+
6
+ // biome-ignore-all lint/suspicious/noConfusingVoidType: Type 'void' is not assignable to type 'ScopedCallback | undefined'.
7
+ // Argument of type '() => void' is not assignable to parameter of type '(dep: DependencyFunction) => ScopedCallback | undefined'.
8
+ // Track which effects are watching which reactive objects for cleanup
9
+ const effectToReactiveObjects = new WeakMap();
10
+ // Track object -> proxy and proxy -> object relationships
11
+ const objectToProxy = new WeakMap();
12
+ const proxyToObject = new WeakMap();
13
+ // Deep watching data structures
14
+ // Track which objects contain which other objects (back-references)
15
+ const objectParents = new WeakMap();
16
+ // Track which objects have deep watchers
17
+ const objectsWithDeepWatchers = new WeakSet();
18
+ // Track deep watchers per object
19
+ const deepWatchers = new WeakMap();
20
+ // Track which effects are doing deep watching
21
+ const effectToDeepWatchedObjects = new WeakMap();
22
+ // Track objects that should never be reactive and cannot be modified
23
+ const nonReactiveObjects = new WeakSet();
24
+ /**
25
+ * Converts an iterator to a generator that yields reactive values
26
+ */
27
+ function* makeReactiveIterator(iterator) {
28
+ let result = iterator.next();
29
+ while (!result.done) {
30
+ yield reactive(result.value);
31
+ result = iterator.next();
32
+ }
33
+ }
34
+ /**
35
+ * Converts an iterator of key-value pairs to a generator that yields reactive key-value pairs
36
+ */
37
+ function* makeReactiveEntriesIterator(iterator) {
38
+ let result = iterator.next();
39
+ while (!result.done) {
40
+ const [key, value] = result.value;
41
+ yield [reactive(key), reactive(value)];
42
+ result = iterator.next();
43
+ }
44
+ }
45
+ // Track effects per reactive object and property
46
+ const watchers = new WeakMap();
47
+ const profileInfo = {
48
+ objectToProxy,
49
+ proxyToObject,
50
+ effectToReactiveObjects,
51
+ watchers,
52
+ objectParents,
53
+ objectsWithDeepWatchers,
54
+ deepWatchers,
55
+ effectToDeepWatchedObjects,
56
+ nonReactiveObjects,
57
+ };
58
+ // Track native reactivity
59
+ const nativeReactive = Symbol('native-reactive');
60
+ // Symbol to mark individual objects as non-reactive
61
+ const nonReactiveMark = Symbol('non-reactive');
62
+ // Symbol to mark class properties as non-reactive
63
+ const unreactiveProperties = Symbol('unreactive-properties');
64
+ const prototypeForwarding = Symbol('prototype-forwarding');
65
+ const allProps = Symbol('all-props');
66
+ // Symbol to mark functions with their root function
67
+ const rootFunction = Symbol('root-function');
68
+ /**
69
+ * Mark a function with its root function. If the function already has a root,
70
+ * the root becomes the root of the new root (transitive root tracking).
71
+ * @param fn - The function to mark
72
+ * @param root - The root function to associate with fn
73
+ */
74
+ function markWithRoot(fn, root) {
75
+ // Mark fn with the new root
76
+ return Object.defineProperty(fn, rootFunction, {
77
+ value: getRoot(root),
78
+ writable: false,
79
+ });
80
+ }
81
+ /**
82
+ * Retrieve the root function from a callback. Returns the function itself if it has no root.
83
+ * @param fn - The function to get the root from
84
+ * @returns The root function, or the function itself if no root exists
85
+ */
86
+ function getRoot(fn) {
87
+ return fn?.[rootFunction] || fn;
88
+ }
89
+ class ReactiveError extends Error {
90
+ constructor(message) {
91
+ super(message);
92
+ this.name = 'ReactiveError';
93
+ }
94
+ }
95
+ // biome-ignore-start lint/correctness/noUnusedFunctionParameters: Interface declaration with empty defaults
96
+ /**
97
+ * Options for the reactive system, can be configured at runtime
98
+ */
99
+ const options = {
100
+ /**
101
+ * Debug purpose: called when an effect is entered
102
+ * @param effect - The effect that is entered
103
+ */
104
+ enter: (effect) => { },
105
+ /**
106
+ * Debug purpose: called when an effect is left
107
+ * @param effect - The effect that is left
108
+ */
109
+ leave: (effect) => { },
110
+ /**
111
+ * Debug purpose: called when an effect is chained
112
+ * @param target - The effect that is being triggered
113
+ * @param caller - The effect that is calling the target
114
+ */
115
+ chain: (target, caller) => { },
116
+ /**
117
+ * Debug purpose: maximum effect chain (like call stack max depth)
118
+ * Used to prevent infinite loops
119
+ * @default 100
120
+ */
121
+ maxEffectChain: 100,
122
+ /**
123
+ * Maximum depth for deep watching traversal
124
+ * Used to prevent infinite recursion in circular references
125
+ * @default 100
126
+ */
127
+ maxDeepWatchDepth: 100,
128
+ /**
129
+ * Only react on instance members modification (not inherited properties)
130
+ * For instance, do not track class methods
131
+ * @default true
132
+ */
133
+ instanceMembers: true,
134
+ // biome-ignore lint/suspicious/noConsole: This is the whole point here
135
+ warn: (...args) => console.warn(...args),
136
+ };
137
+ // biome-ignore-end lint/correctness/noUnusedFunctionParameters: Interface declaration with empty defaults
138
+ //#region evolution
139
+ function raiseDeps(objectWatchers, ...keyChains) {
140
+ for (const keys of keyChains)
141
+ for (const key of keys) {
142
+ const deps = objectWatchers.get(key);
143
+ if (deps)
144
+ for (const effect of Array.from(deps))
145
+ hasEffect(effect);
146
+ }
147
+ }
148
+ function touched1(obj, evolution, prop) {
149
+ touched(obj, evolution, [prop]);
150
+ }
151
+ function touched(obj, evolution, props) {
152
+ obj = unwrap(obj);
153
+ addState(obj, evolution);
154
+ const objectWatchers = watchers.get(obj);
155
+ if (objectWatchers) {
156
+ if (props)
157
+ raiseDeps(objectWatchers, [allProps], props);
158
+ else
159
+ raiseDeps(objectWatchers, objectWatchers.keys());
160
+ }
161
+ // Bubble up changes if this object has deep watchers
162
+ if (objectsWithDeepWatchers.has(obj)) {
163
+ bubbleUpChange(obj);
164
+ }
165
+ }
166
+ const states = new WeakMap();
167
+ function addState(obj, evolution) {
168
+ obj = unwrap(obj);
169
+ const next = {};
170
+ const state = getState(obj);
171
+ if (state)
172
+ Object.assign(state, { evolution, next });
173
+ states.set(obj, next);
174
+ }
175
+ function getState(obj) {
176
+ obj = unwrap(obj);
177
+ let state = states.get(obj);
178
+ if (!state) {
179
+ state = {};
180
+ states.set(obj, state);
181
+ }
182
+ return state;
183
+ }
184
+ function dependant(obj, prop = allProps) {
185
+ // TODO: avoid depending on property get?
186
+ obj = unwrap(obj);
187
+ if (activeEffect && (typeof prop !== 'symbol' || prop === allProps)) {
188
+ let objectWatchers = watchers.get(obj);
189
+ if (!objectWatchers) {
190
+ objectWatchers = new Map();
191
+ watchers.set(obj, objectWatchers);
192
+ }
193
+ let deps = objectWatchers.get(prop);
194
+ if (!deps) {
195
+ deps = new Set();
196
+ objectWatchers.set(prop, deps);
197
+ }
198
+ deps.add(activeEffect);
199
+ // Track which reactive objects this effect is watching
200
+ let effectObjects = effectToReactiveObjects.get(activeEffect);
201
+ if (!effectObjects) {
202
+ effectObjects = new Set();
203
+ effectToReactiveObjects.set(activeEffect, effectObjects);
204
+ }
205
+ effectObjects.add(obj);
206
+ }
207
+ }
208
+ // Stack of active effects to handle nested effects
209
+ let activeEffect;
210
+ // Parent effect used for lifecycle/cleanup relationships (can diverge later)
211
+ let parentEffect;
212
+ // Track currently executing effects to prevent re-execution
213
+ // These are all the effects triggered under `activeEffect`
214
+ let batchedEffects;
215
+ // Track which sub-effects have been executed to prevent infinite loops
216
+ // These are all the effects triggered under `activeEffect` and all their sub-effects
217
+ function hasEffect(effect) {
218
+ const root = getRoot(effect);
219
+ options?.chain(getRoot(effect), getRoot(activeEffect));
220
+ if (batchedEffects)
221
+ batchedEffects.set(root, effect);
222
+ else {
223
+ const runEffects = [];
224
+ batchedEffects = new Map([[root, effect]]);
225
+ try {
226
+ while (batchedEffects.size) {
227
+ if (runEffects.length > options.maxEffectChain)
228
+ throw new ReactiveError('[reactive] Max effect chain reached');
229
+ const [root, effect] = batchedEffects.entries().next().value;
230
+ runEffects.push(root);
231
+ effect();
232
+ batchedEffects.delete(root);
233
+ }
234
+ }
235
+ finally {
236
+ batchedEffects = undefined;
237
+ }
238
+ }
239
+ }
240
+ function withEffect(effect, fn, keepParent) {
241
+ if (getRoot(effect) === getRoot(activeEffect))
242
+ return fn();
243
+ const oldActiveEffect = activeEffect;
244
+ const oldParentEffect = parentEffect;
245
+ activeEffect = effect;
246
+ if (!keepParent)
247
+ parentEffect = effect;
248
+ try {
249
+ return fn();
250
+ }
251
+ finally {
252
+ activeEffect = oldActiveEffect;
253
+ parentEffect = oldParentEffect;
254
+ }
255
+ }
256
+ //#endregion
257
+ //#region deep watching
258
+ /**
259
+ * Add a back-reference from child to parent
260
+ */
261
+ function addBackReference(child, parent, prop) {
262
+ let parents = objectParents.get(child);
263
+ if (!parents) {
264
+ parents = new Set();
265
+ objectParents.set(child, parents);
266
+ }
267
+ parents.add({ parent, prop });
268
+ }
269
+ /**
270
+ * Remove a back-reference from child to parent
271
+ */
272
+ function removeBackReference(child, parent, prop) {
273
+ const parents = objectParents.get(child);
274
+ if (parents) {
275
+ parents.delete({ parent, prop });
276
+ if (parents.size === 0) {
277
+ objectParents.delete(child);
278
+ }
279
+ }
280
+ }
281
+ /**
282
+ * Check if an object needs back-references (has deep watchers or parents with deep watchers)
283
+ */
284
+ function needsBackReferences(obj) {
285
+ return objectsWithDeepWatchers.has(obj) || hasParentWithDeepWatchers(obj);
286
+ }
287
+ /**
288
+ * Check if an object has any parent with deep watchers
289
+ */
290
+ function hasParentWithDeepWatchers(obj) {
291
+ const parents = objectParents.get(obj);
292
+ if (!parents)
293
+ return false;
294
+ for (const { parent } of parents) {
295
+ if (objectsWithDeepWatchers.has(parent))
296
+ return true;
297
+ if (hasParentWithDeepWatchers(parent))
298
+ return true;
299
+ }
300
+ return false;
301
+ }
302
+ /**
303
+ * Bubble up changes through the back-reference chain
304
+ */
305
+ function bubbleUpChange(changedObject, evolution) {
306
+ const parents = objectParents.get(changedObject);
307
+ if (!parents)
308
+ return;
309
+ for (const { parent } of parents) {
310
+ // Trigger deep watchers on parent
311
+ const parentDeepWatchers = deepWatchers.get(parent);
312
+ if (parentDeepWatchers)
313
+ for (const watcher of parentDeepWatchers)
314
+ hasEffect(watcher);
315
+ // Continue bubbling up
316
+ bubbleUpChange(parent);
317
+ }
318
+ }
319
+ function track1(obj, prop, oldVal, newValue) {
320
+ // Manage back-references if this object has deep watchers
321
+ if (objectsWithDeepWatchers.has(obj)) {
322
+ // Remove old back-references
323
+ if (typeof oldVal === 'object' && oldVal !== null) {
324
+ removeBackReference(oldVal, obj, prop);
325
+ }
326
+ // Add new back-references
327
+ if (typeof newValue === 'object' && newValue !== null) {
328
+ const reactiveValue = reactive(newValue);
329
+ addBackReference(reactiveValue, obj, prop);
330
+ }
331
+ }
332
+ return newValue;
333
+ }
334
+ //#endregion
335
+ const reactiveHandlers = {
336
+ [Symbol.toStringTag]: 'MutTs Reactive',
337
+ get(obj, prop, receiver) {
338
+ if (prop === nonReactiveMark)
339
+ return false;
340
+ // Check if this property is marked as unreactive
341
+ if (obj[unreactiveProperties]?.has(prop) || typeof prop === 'symbol')
342
+ return Reflect.get(obj, prop, receiver);
343
+ const absent = !(prop in obj);
344
+ // Depend if...
345
+ if (!options.instanceMembers || Object.hasOwn(receiver, prop) || absent)
346
+ dependant(obj, prop);
347
+ const value = Reflect.get(obj, prop, receiver);
348
+ if (typeof value === 'object' && value !== null) {
349
+ const reactiveValue = reactive(value);
350
+ // Only create back-references if this object needs them
351
+ if (needsBackReferences(obj)) {
352
+ addBackReference(reactiveValue, obj, prop);
353
+ }
354
+ return reactiveValue;
355
+ }
356
+ return value;
357
+ },
358
+ set(obj, prop, value, receiver) {
359
+ // Check if this property is marked as unreactive
360
+ if (obj[unreactiveProperties]?.has(prop))
361
+ return Reflect.set(obj, prop, value, receiver);
362
+ // Really specific case for when Array is forwarder, in order to let it manage the reactivity
363
+ const isArrayCase = prototypeForwarding in obj &&
364
+ // biome-ignore lint/suspicious/useIsArray: This is the whole point here
365
+ obj[prototypeForwarding] instanceof Array &&
366
+ (!Number.isNaN(Number(prop)) || prop === 'length');
367
+ const newValue = unwrap(value);
368
+ if (isArrayCase) {
369
+ obj[prop] = newValue;
370
+ return true;
371
+ }
372
+ const oldVal = obj[prop];
373
+ const oldPresent = prop in obj;
374
+ track1(obj, prop, oldVal, newValue);
375
+ if (oldVal !== newValue) {
376
+ Reflect.set(obj, prop, newValue, receiver);
377
+ // try to find a "generic" way to express that
378
+ touched1(obj, { type: oldPresent ? 'set' : 'add', prop }, prop);
379
+ }
380
+ return true;
381
+ },
382
+ deleteProperty(obj, prop) {
383
+ if (!Object.hasOwn(obj, prop))
384
+ return false;
385
+ const oldVal = obj[prop];
386
+ // Remove back-references if this object has deep watchers
387
+ if (objectsWithDeepWatchers.has(obj) && typeof oldVal === 'object' && oldVal !== null) {
388
+ removeBackReference(oldVal, obj, prop);
389
+ }
390
+ delete obj[prop];
391
+ touched1(obj, { type: 'del', prop }, prop);
392
+ // Bubble up changes if this object has deep watchers
393
+ if (objectsWithDeepWatchers.has(obj)) {
394
+ bubbleUpChange(obj);
395
+ }
396
+ return true;
397
+ },
398
+ getPrototypeOf(obj) {
399
+ if (prototypeForwarding in obj)
400
+ return obj[prototypeForwarding];
401
+ return Object.getPrototypeOf(obj);
402
+ },
403
+ setPrototypeOf(obj, proto) {
404
+ if (prototypeForwarding in obj)
405
+ return false;
406
+ Object.setPrototypeOf(obj, proto);
407
+ return true;
408
+ },
409
+ ownKeys(obj) {
410
+ dependant(obj, allProps);
411
+ return Reflect.ownKeys(obj);
412
+ },
413
+ };
414
+ const reactiveClasses = new WeakSet();
415
+ class ReactiveBase {
416
+ constructor() {
417
+ // biome-ignore lint/correctness/noConstructorReturn: This is the whole point here
418
+ return reactiveClasses.has(new.target) ? reactive(this) : this;
419
+ }
420
+ }
421
+ function reactiveObject(anyTarget) {
422
+ if (!anyTarget || typeof anyTarget !== 'object')
423
+ return anyTarget;
424
+ const target = anyTarget;
425
+ // If target is already a proxy, return it
426
+ if (proxyToObject.has(target) || isNonReactive(target))
427
+ return target;
428
+ // If we already have a proxy for this object, return it
429
+ if (objectToProxy.has(target))
430
+ return objectToProxy.get(target);
431
+ const proxied = nativeReactive in target && !(target instanceof target[nativeReactive])
432
+ ? new target[nativeReactive](target)
433
+ : target;
434
+ if (proxied !== target)
435
+ proxyToObject.set(proxied, target);
436
+ const proxy = new Proxy(proxied, reactiveHandlers);
437
+ // Store the relationships
438
+ objectToProxy.set(target, proxy);
439
+ proxyToObject.set(proxy, target);
440
+ return proxy;
441
+ }
442
+ const reactive = decorator.decorator({
443
+ class(original) {
444
+ if (original.prototype instanceof ReactiveBase) {
445
+ reactiveClasses.add(original);
446
+ return original;
447
+ }
448
+ class Reactive extends original {
449
+ constructor(...args) {
450
+ super(...args);
451
+ if (new.target !== Reactive && !reactiveClasses.has(new.target))
452
+ options.warn(`${original.name} has been inherited by ${this.constructor.name} that is not reactive.
453
+ @reactive decorator must be applied to the leaf class OR classes have to extend ReactiveBase.`);
454
+ // biome-ignore lint/correctness/noConstructorReturn: This is the whole point here
455
+ return reactive(this);
456
+ }
457
+ }
458
+ Object.defineProperty(Reactive, 'name', {
459
+ value: `Reactive<${original.name}>`,
460
+ });
461
+ return Reactive;
462
+ },
463
+ get(original) {
464
+ return reactiveObject(original);
465
+ },
466
+ default: reactiveObject,
467
+ });
468
+ function unwrap(proxy) {
469
+ // Return the original object
470
+ return proxyToObject.get(proxy) ?? proxy;
471
+ }
472
+ function isReactive(obj) {
473
+ return proxyToObject.has(obj);
474
+ }
475
+ function untracked(fn) {
476
+ withEffect(undefined, fn, true);
477
+ }
478
+ // runEffect -> set<cleanup>
479
+ const effectChildren = new WeakMap();
480
+ const fr = new FinalizationRegistry((f) => f());
481
+ /**
482
+ * @param fn - The effect function to run - provides the cleaner
483
+ * @returns The cleanup function
484
+ */
485
+ function effect(fn, ...args) {
486
+ let cleanup = null;
487
+ const dep = markWithRoot((cb) => withEffect(runEffect, cb), fn);
488
+ let effectStopped = false;
489
+ function runEffect() {
490
+ // Clear previous dependencies
491
+ cleanup?.();
492
+ options.enter(fn);
493
+ const reactionCleanup = withEffect(effectStopped ? undefined : runEffect, () => fn(dep, ...args));
494
+ options.leave(fn);
495
+ // Create cleanup function for next run
496
+ cleanup = () => {
497
+ cleanup = null;
498
+ reactionCleanup?.();
499
+ // Remove this effect from all reactive objects it's watching
500
+ const effectObjects = effectToReactiveObjects.get(runEffect);
501
+ if (effectObjects) {
502
+ for (const reactiveObj of effectObjects) {
503
+ const objectWatchers = watchers.get(reactiveObj);
504
+ if (objectWatchers) {
505
+ for (const [prop, deps] of objectWatchers.entries()) {
506
+ deps.delete(runEffect);
507
+ if (deps.size === 0) {
508
+ objectWatchers.delete(prop);
509
+ }
510
+ }
511
+ if (objectWatchers.size === 0) {
512
+ watchers.delete(reactiveObj);
513
+ }
514
+ }
515
+ }
516
+ effectToReactiveObjects.delete(runEffect);
517
+ }
518
+ };
519
+ }
520
+ // Mark the runEffect callback with the original function as its root
521
+ markWithRoot(runEffect, fn);
522
+ // Run the effect immediately
523
+ if (!batchedEffects) {
524
+ hasEffect(runEffect);
525
+ }
526
+ else {
527
+ const oldBatchedEffects = batchedEffects;
528
+ try {
529
+ // Simulate a hasEffect who batches, but do not execute the batch, give it back to the parent batch,
530
+ // Only the immediate effect has to be executed, the sub-effects will be executed by the parent batch
531
+ batchedEffects = new Map([[fn, runEffect]]);
532
+ runEffect();
533
+ batchedEffects.delete(fn);
534
+ for (const [root, effect] of batchedEffects)
535
+ oldBatchedEffects.set(root, effect);
536
+ }
537
+ finally {
538
+ batchedEffects = oldBatchedEffects;
539
+ }
540
+ }
541
+ const mainCleanup = () => {
542
+ if (effectStopped)
543
+ return;
544
+ effectStopped = true;
545
+ cleanup?.();
546
+ // Invoke all child cleanups (recursive via subEffectCleanup calling its own mainCleanup)
547
+ const children = effectChildren.get(runEffect);
548
+ if (children) {
549
+ for (const childCleanup of children)
550
+ childCleanup();
551
+ effectChildren.delete(runEffect);
552
+ }
553
+ fr.unregister(mainCleanup);
554
+ };
555
+ // Only ROOT effects are registered for GC cleanup
556
+ if (!parentEffect) {
557
+ const callIfCollected = () => mainCleanup();
558
+ fr.register(callIfCollected, mainCleanup, callIfCollected);
559
+ return callIfCollected;
560
+ }
561
+ // TODO: parentEffect = last non-undefined activeEffect
562
+ // Register this effect to be cleaned up with the parent effect
563
+ let children = effectChildren.get(parentEffect);
564
+ if (!children) {
565
+ children = new Set();
566
+ effectChildren.set(parentEffect, children);
567
+ }
568
+ const parent = parentEffect;
569
+ const subEffectCleanup = () => {
570
+ children.delete(subEffectCleanup);
571
+ if (children.size === 0) {
572
+ effectChildren.delete(parent);
573
+ }
574
+ // Execute this child effect cleanup (which triggers its own mainCleanup)
575
+ mainCleanup();
576
+ };
577
+ children.add(subEffectCleanup);
578
+ return subEffectCleanup;
579
+ }
580
+ /**
581
+ * Mark an object as non-reactive. This object and all its properties will never be made reactive.
582
+ * @param obj - The object to mark as non-reactive
583
+ */
584
+ function nonReactive(...obj) {
585
+ for (const o of obj) {
586
+ try {
587
+ Object.defineProperty(o, nonReactiveMark, {
588
+ value: true,
589
+ writable: false,
590
+ enumerable: false,
591
+ configurable: false,
592
+ });
593
+ }
594
+ catch { }
595
+ if (!(nonReactiveMark in o))
596
+ nonReactiveObjects.add(o);
597
+ }
598
+ return obj[0];
599
+ }
600
+ /**
601
+ * Set of functions to test if an object is immutable
602
+ */
603
+ const immutables = new Set();
604
+ /**
605
+ * Check if an object is marked as non-reactive (for testing purposes)
606
+ * @param obj - The object to check
607
+ * @returns true if the object is marked as non-reactive
608
+ */
609
+ function isNonReactive(obj) {
610
+ // Don't make primitives reactive
611
+ if (obj === null || typeof obj !== 'object')
612
+ return true;
613
+ // Check if the object itself is marked as non-reactive
614
+ if (nonReactiveObjects.has(obj))
615
+ return true;
616
+ // Check if the object has the non-reactive symbol
617
+ if (obj[nonReactiveMark])
618
+ return true;
619
+ // Check if the object is immutable
620
+ if (Array.from(immutables).some((fn) => fn(obj)))
621
+ return true;
622
+ return false;
623
+ }
624
+ /**
625
+ * Mark a class as non-reactive. All instances of this class will automatically be non-reactive.
626
+ * @param cls - The class constructor to mark as non-reactive
627
+ */
628
+ function nonReactiveClass(...cls) {
629
+ for (const c of cls)
630
+ if (c)
631
+ c.prototype[nonReactiveMark] = true;
632
+ return cls[0];
633
+ }
634
+ nonReactiveClass(Date, RegExp, Error, Promise, Function);
635
+ if (typeof window !== 'undefined')
636
+ nonReactive(window, document);
637
+ if (typeof Element !== 'undefined')
638
+ nonReactiveClass(Element, Node);
639
+ function registerNativeReactivity(originalClass, reactiveClass) {
640
+ originalClass.prototype[nativeReactive] = reactiveClass;
641
+ nonReactiveClass(reactiveClass);
642
+ }
643
+ /**
644
+ * Deep watch an object and all its nested properties
645
+ * @param target - The object to watch deeply
646
+ * @param callback - The callback to call when any nested property changes
647
+ * @param options - Options for the deep watch
648
+ * @returns A cleanup function to stop watching
649
+ */
650
+ function deepWatch(target, callback, { immediate = false } = {}) {
651
+ if (target === null || target === undefined)
652
+ return undefined;
653
+ if (typeof target !== 'object')
654
+ throw new Error('Target of deep watching must be an object');
655
+ // Create a wrapper callback that matches ScopedCallback signature
656
+ const wrappedCallback = markWithRoot(() => callback(target), callback);
657
+ // Use the existing effect system to register dependencies
658
+ return effect(() => {
659
+ // Mark the target object as having deep watchers
660
+ objectsWithDeepWatchers.add(target);
661
+ // Track which objects this effect is watching for cleanup
662
+ let effectObjects = effectToDeepWatchedObjects.get(wrappedCallback);
663
+ if (!effectObjects) {
664
+ effectObjects = new Set();
665
+ effectToDeepWatchedObjects.set(wrappedCallback, effectObjects);
666
+ }
667
+ effectObjects.add(target);
668
+ // Traverse the object graph and register dependencies
669
+ // This will re-run every time the effect runs, ensuring we catch all changes
670
+ const visited = new WeakSet();
671
+ function traverseAndTrack(obj, depth = 0) {
672
+ // Prevent infinite recursion and excessive depth
673
+ if (visited.has(obj) || !isObject(obj) || depth > options.maxDeepWatchDepth)
674
+ return;
675
+ // Do not traverse into unreactive objects
676
+ if (isNonReactive(obj))
677
+ return;
678
+ visited.add(obj);
679
+ // Mark this object as having deep watchers
680
+ objectsWithDeepWatchers.add(obj);
681
+ effectObjects.add(obj);
682
+ // Traverse all properties to register dependencies
683
+ // unwrap to avoid kicking dependency
684
+ for (const key in unwrap(obj)) {
685
+ if (Object.hasOwn(obj, key)) {
686
+ // Access the property to register dependency
687
+ const value = obj[key];
688
+ // Make the value reactive if it's an object
689
+ const reactiveValue = typeof value === 'object' && value !== null ? reactive(value) : value;
690
+ traverseAndTrack(reactiveValue, depth + 1);
691
+ }
692
+ }
693
+ // Also handle array indices and length
694
+ // biome-ignore lint/suspicious/useIsArray: Check for both native arrays and reactive arrays
695
+ if (Array.isArray(obj) || obj instanceof Array) {
696
+ // Access array length to register dependency on length changes
697
+ const length = obj.length;
698
+ // Access all current array elements to register dependencies
699
+ for (let i = 0; i < length; i++) {
700
+ // Access the array element to register dependency
701
+ const value = obj[i];
702
+ // Make the value reactive if it's an object
703
+ const reactiveValue = typeof value === 'object' && value !== null ? reactive(value) : value;
704
+ traverseAndTrack(reactiveValue, depth + 1);
705
+ }
706
+ }
707
+ // Handle Set values (deep watch values only, not keys since Sets don't have separate keys)
708
+ else if (obj instanceof Set) {
709
+ // Access all Set values to register dependencies
710
+ for (const value of obj) {
711
+ // Make the value reactive if it's an object
712
+ const reactiveValue = typeof value === 'object' && value !== null ? reactive(value) : value;
713
+ traverseAndTrack(reactiveValue, depth + 1);
714
+ }
715
+ }
716
+ // Handle Map values (deep watch values only, not keys)
717
+ else if (obj instanceof Map) {
718
+ // Access all Map values to register dependencies
719
+ for (const [_key, value] of obj) {
720
+ // Make the value reactive if it's an object
721
+ const reactiveValue = typeof value === 'object' && value !== null ? reactive(value) : value;
722
+ traverseAndTrack(reactiveValue, depth + 1);
723
+ }
724
+ }
725
+ // Note: WeakSet and WeakMap cannot be iterated, so we can't deep watch their contents
726
+ // They will only trigger when the collection itself is replaced
727
+ }
728
+ // Traverse the target object to register all dependencies
729
+ // This will register dependencies on all current properties and array elements
730
+ traverseAndTrack(target);
731
+ // Only call the callback if immediate is true or if it's not the first run
732
+ if (immediate)
733
+ callback(target);
734
+ immediate = true;
735
+ // Return a cleanup function that properly removes deep watcher tracking
736
+ return () => {
737
+ // Get the objects this effect was watching
738
+ const effectObjects = effectToDeepWatchedObjects.get(wrappedCallback);
739
+ if (effectObjects) {
740
+ // Remove deep watcher tracking from all objects this effect was watching
741
+ for (const obj of effectObjects) {
742
+ // Check if this object still has other deep watchers
743
+ const watchers = deepWatchers.get(obj);
744
+ if (watchers) {
745
+ // Remove this effect's callback from the watchers
746
+ watchers.delete(wrappedCallback);
747
+ // If no more watchers, remove the object from deep watchers tracking
748
+ if (watchers.size === 0) {
749
+ deepWatchers.delete(obj);
750
+ objectsWithDeepWatchers.delete(obj);
751
+ }
752
+ }
753
+ else {
754
+ // No watchers found, remove from deep watchers tracking
755
+ objectsWithDeepWatchers.delete(obj);
756
+ }
757
+ }
758
+ // Clean up the tracking data
759
+ effectToDeepWatchedObjects.delete(wrappedCallback);
760
+ }
761
+ };
762
+ });
763
+ }
764
+ /**
765
+ * Check if an object is an object (not null, not primitive)
766
+ */
767
+ function isObject(obj) {
768
+ return obj !== null && typeof obj === 'object';
769
+ }
770
+
771
+ //#region computed
772
+ let computedInvalidations;
773
+ /**
774
+ * When used in a computed property computation, it will register the callback to be called when the computed property is invalidated
775
+ * @param cb - The callback to register
776
+ * @param warn - Whether to warn if used outside of a computed property
777
+ */
778
+ function invalidateComputed(cb, warn = true) {
779
+ if (computedInvalidations)
780
+ computedInvalidations.push(cb);
781
+ else if (warn)
782
+ options.warn('Using `invalidateComputed` outside of a computed property');
783
+ }
784
+ const computedCache = new WeakMap();
785
+ function computedFunction(getter) {
786
+ const key = getRoot(getter);
787
+ let invalidations = [];
788
+ dependant(computedCache, key);
789
+ if (computedCache.has(key))
790
+ return computedCache.get(key);
791
+ withEffect(undefined, () => {
792
+ const stop = effect(markWithRoot((dep) => {
793
+ const oldCI = computedInvalidations;
794
+ if (computedCache.has(key)) {
795
+ // This should *not* be called in the cleanup chain, as its effects would be lost and cleaned-up
796
+ for (const cb of invalidations)
797
+ cb();
798
+ invalidations = [];
799
+ computedCache.delete(key);
800
+ touched1(computedCache, { type: 'set', prop: key }, key);
801
+ stop();
802
+ }
803
+ else
804
+ try {
805
+ computedInvalidations = invalidations;
806
+ computedCache.set(key, getter(dep));
807
+ }
808
+ finally {
809
+ computedInvalidations = oldCI;
810
+ }
811
+ }, getter));
812
+ });
813
+ return computedCache.get(key);
814
+ }
815
+ /**
816
+ * Get the cached value of a computed function - cache is invalidated when the dependencies change
817
+ */
818
+ const computed = decorator.decorator({
819
+ getter(original, propertyKey) {
820
+ const computers = new WeakMap();
821
+ return function () {
822
+ if (!computers.has(this)) {
823
+ computers.set(this, decorator.renamed(() => original.call(this), `${String(this.constructor.name)}.${String(propertyKey)}`));
824
+ }
825
+ return computedFunction(computers.get(this));
826
+ };
827
+ },
828
+ default(getter) {
829
+ return computedFunction(getter);
830
+ },
831
+ });
832
+ //#endregion
833
+ //#region watch
834
+ const unsetYet = Symbol('unset-yet');
835
+ function watch(value, //object | ((dep: DependencyFunction) => object),
836
+ changed, options = {}) {
837
+ return typeof value === 'function'
838
+ ? watchCallBack(value, changed, options)
839
+ : typeof value === 'object'
840
+ ? watchObject(value, changed, options)
841
+ : (() => {
842
+ throw new Error('watch: value must be a function or an object');
843
+ })();
844
+ }
845
+ function watchObject(value, changed, { immediate = false, deep = false } = {}) {
846
+ if (deep)
847
+ return deepWatch(value, changed, { immediate });
848
+ return effect(markWithRoot(() => {
849
+ dependant(value);
850
+ if (immediate)
851
+ untracked(() => changed(value));
852
+ immediate = true;
853
+ }, changed));
854
+ }
855
+ function watchCallBack(value, changed, { immediate = false, deep = false } = {}) {
856
+ let oldValue = unsetYet;
857
+ let deepCleanup;
858
+ const cbCleanup = effect(markWithRoot((dep) => {
859
+ const newValue = value(dep);
860
+ if (oldValue !== newValue)
861
+ untracked(markWithRoot(() => {
862
+ if (oldValue === unsetYet) {
863
+ if (immediate)
864
+ changed(newValue);
865
+ }
866
+ else
867
+ changed(newValue, oldValue);
868
+ oldValue = newValue;
869
+ if (deep) {
870
+ if (deepCleanup)
871
+ deepCleanup();
872
+ deepCleanup = deepWatch(newValue, markWithRoot((value) => changed(value, value), changed));
873
+ }
874
+ }, changed));
875
+ }, value));
876
+ return () => {
877
+ cbCleanup();
878
+ if (deepCleanup)
879
+ deepCleanup();
880
+ };
881
+ }
882
+ //#endregion
883
+ //#region nonReactive
884
+ /**
885
+ * Mark an object as non-reactive. This object and all its properties will never be made reactive.
886
+ * @param obj - The object to mark as non-reactive
887
+ */
888
+ function deepNonReactive(obj) {
889
+ obj = unwrap(obj);
890
+ if (isNonReactive(obj))
891
+ return obj;
892
+ try {
893
+ Object.defineProperty(obj, nonReactiveMark, {
894
+ value: true,
895
+ writable: false,
896
+ enumerable: false,
897
+ configurable: true,
898
+ });
899
+ }
900
+ catch { }
901
+ if (!(nonReactiveMark in obj))
902
+ nonReactiveObjects.add(obj);
903
+ for (const key in obj)
904
+ deepNonReactive(obj[key]);
905
+ return obj;
906
+ }
907
+ function unreactiveApplication(arg1, ...args) {
908
+ return typeof arg1 === 'object'
909
+ ? deepNonReactive(arg1)
910
+ : ((original) => {
911
+ // Copy the parent's unreactive properties if they exist
912
+ original.prototype[unreactiveProperties] = new Set(original.prototype[unreactiveProperties] || []);
913
+ // Add all arguments (including the first one)
914
+ original.prototype[unreactiveProperties].add(arg1);
915
+ for (const arg of args)
916
+ original.prototype[unreactiveProperties].add(arg);
917
+ return original; // Return the class
918
+ });
919
+ }
920
+ const unreactive = decorator.decorator({
921
+ class(original) {
922
+ // Called without arguments, mark entire class as non-reactive
923
+ nonReactiveClass(original);
924
+ },
925
+ default: unreactiveApplication,
926
+ });
927
+ Object.assign(profileInfo, { computedCache });
928
+
929
+ const native$2 = Symbol('native');
930
+ const isArray = Array.isArray;
931
+ Array.isArray = ((value) =>
932
+ // biome-ignore lint/suspicious/useIsArray: We are defining it
933
+ isArray(value) || (value instanceof Array && native$2 in value));
934
+ class ReactiveBaseArray {
935
+ }
936
+ function* index(i, { length = true } = {}) {
937
+ yield i;
938
+ if (length)
939
+ yield 'length';
940
+ }
941
+ function* range(a, b, { length = false } = {}) {
942
+ const start = Math.min(a, b);
943
+ const end = Math.max(a, b);
944
+ for (let i = start; i <= end; i++)
945
+ yield i;
946
+ if (length)
947
+ yield 'length';
948
+ }
949
+ class ReactiveArray extends indexable.Indexable(ReactiveBaseArray, {
950
+ get(i) {
951
+ dependant(this[native$2], i);
952
+ return reactive(this[native$2][i]);
953
+ },
954
+ set(i, value) {
955
+ const added = i >= this[native$2].length;
956
+ this[native$2][i] = value;
957
+ touched(this[native$2], { type: 'bunch', method: 'set' }, index(i, { length: added }));
958
+ },
959
+ getLength() {
960
+ dependant(this[native$2], 'length');
961
+ return this[native$2].length;
962
+ },
963
+ setLength(value) {
964
+ const oldLength = this[native$2].length;
965
+ try {
966
+ this[native$2].length = value;
967
+ }
968
+ finally {
969
+ touched(this[native$2], { type: 'set', prop: 'length' }, range(oldLength, value, { length: true }));
970
+ }
971
+ },
972
+ }) {
973
+ constructor(original) {
974
+ super();
975
+ Object.defineProperties(this, {
976
+ // We have to make it double, as [native] must be `unique symbol` - impossible through import
977
+ [native$2]: { value: original },
978
+ [prototypeForwarding]: { value: original },
979
+ });
980
+ }
981
+ // Safe array access with negative indices
982
+ at(index) {
983
+ const actualIndex = index < 0 ? this[native$2].length + index : index;
984
+ dependant(this, actualIndex);
985
+ if (actualIndex < 0 || actualIndex >= this[native$2].length)
986
+ return undefined;
987
+ return reactive(this[native$2][actualIndex]);
988
+ }
989
+ push(...items) {
990
+ const oldLength = this[native$2].length;
991
+ try {
992
+ return this[native$2].push(...items);
993
+ }
994
+ finally {
995
+ touched(this, { type: 'bunch', method: 'push' }, range(oldLength, oldLength + items.length - 1, { length: true }));
996
+ }
997
+ }
998
+ pop() {
999
+ if (this[native$2].length === 0)
1000
+ return undefined;
1001
+ try {
1002
+ return reactive(this[native$2].pop());
1003
+ }
1004
+ finally {
1005
+ touched(this, { type: 'bunch', method: 'pop' }, index(this[native$2].length));
1006
+ }
1007
+ }
1008
+ shift() {
1009
+ if (this[native$2].length === 0)
1010
+ return undefined;
1011
+ try {
1012
+ return reactive(this[native$2].shift());
1013
+ }
1014
+ finally {
1015
+ touched(this, { type: 'bunch', method: 'shift' }, range(0, this[native$2].length + 1, { length: true }));
1016
+ }
1017
+ }
1018
+ unshift(...items) {
1019
+ try {
1020
+ return this[native$2].unshift(...items);
1021
+ }
1022
+ finally {
1023
+ touched(this, { type: 'bunch', method: 'unshift' }, range(0, this[native$2].length - items.length, { length: true }));
1024
+ }
1025
+ }
1026
+ splice(start, deleteCount, ...items) {
1027
+ const oldLength = this[native$2].length;
1028
+ if (deleteCount === undefined)
1029
+ deleteCount = oldLength - start;
1030
+ try {
1031
+ if (deleteCount === undefined)
1032
+ return reactive(this[native$2].splice(start));
1033
+ return reactive(this[native$2].splice(start, deleteCount, ...items));
1034
+ }
1035
+ finally {
1036
+ touched(this, { type: 'bunch', method: 'splice' },
1037
+ // TODO: edge cases
1038
+ deleteCount === items.length
1039
+ ? range(start, start + deleteCount)
1040
+ : range(start, oldLength + Math.max(items.length - deleteCount, 0), {
1041
+ length: true,
1042
+ }));
1043
+ }
1044
+ }
1045
+ reverse() {
1046
+ try {
1047
+ return this[native$2].reverse();
1048
+ }
1049
+ finally {
1050
+ touched(this, { type: 'bunch', method: 'reverse' }, range(0, this[native$2].length - 1));
1051
+ }
1052
+ }
1053
+ sort(compareFn) {
1054
+ try {
1055
+ return this[native$2].sort(compareFn);
1056
+ }
1057
+ finally {
1058
+ touched(this, { type: 'bunch', method: 'sort' }, range(0, this[native$2].length - 1));
1059
+ }
1060
+ }
1061
+ fill(value, start, end) {
1062
+ try {
1063
+ if (start === undefined)
1064
+ return this[native$2].fill(value);
1065
+ if (end === undefined)
1066
+ return this[native$2].fill(value, start);
1067
+ return this[native$2].fill(value, start, end);
1068
+ }
1069
+ finally {
1070
+ touched(this, { type: 'bunch', method: 'fill' }, range(0, this[native$2].length - 1));
1071
+ }
1072
+ }
1073
+ copyWithin(target, start, end) {
1074
+ try {
1075
+ if (end === undefined)
1076
+ return this[native$2].copyWithin(target, start);
1077
+ return this[native$2].copyWithin(target, start, end);
1078
+ }
1079
+ finally {
1080
+ touched(this, { type: 'bunch', method: 'copyWithin' },
1081
+ // TODO: calculate the range properly
1082
+ range(0, this[native$2].length - 1));
1083
+ }
1084
+ // Touch all affected indices with a single allProps call
1085
+ }
1086
+ // Immutable versions of mutator methods
1087
+ toReversed() {
1088
+ dependant(this);
1089
+ return reactive(this[native$2].toReversed());
1090
+ }
1091
+ toSorted(compareFn) {
1092
+ dependant(this);
1093
+ return reactive(this[native$2].toSorted(compareFn));
1094
+ }
1095
+ toSpliced(start, deleteCount, ...items) {
1096
+ dependant(this);
1097
+ return deleteCount === undefined
1098
+ ? this[native$2].toSpliced(start)
1099
+ : this[native$2].toSpliced(start, deleteCount, ...items);
1100
+ }
1101
+ with(index, value) {
1102
+ dependant(this);
1103
+ return reactive(this[native$2].with(index, value));
1104
+ }
1105
+ // Iterator methods with reactivity tracking
1106
+ entries() {
1107
+ dependant(this);
1108
+ return makeReactiveEntriesIterator(this[native$2].entries());
1109
+ }
1110
+ keys() {
1111
+ dependant(this);
1112
+ return this[native$2].keys();
1113
+ }
1114
+ values() {
1115
+ dependant(this);
1116
+ return makeReactiveIterator(this[native$2].values());
1117
+ }
1118
+ [Symbol.iterator]() {
1119
+ dependant(this);
1120
+ const nativeIterator = this[native$2][Symbol.iterator]();
1121
+ return {
1122
+ next() {
1123
+ const result = nativeIterator.next();
1124
+ if (result.done) {
1125
+ return result;
1126
+ }
1127
+ return { value: reactive(result.value), done: false };
1128
+ },
1129
+ };
1130
+ }
1131
+ indexOf(searchElement, fromIndex) {
1132
+ dependant(this);
1133
+ return this[native$2].indexOf(searchElement, fromIndex);
1134
+ }
1135
+ lastIndexOf(searchElement, fromIndex) {
1136
+ dependant(this);
1137
+ return this[native$2].lastIndexOf(searchElement, fromIndex);
1138
+ }
1139
+ includes(searchElement, fromIndex) {
1140
+ dependant(this);
1141
+ return this[native$2].includes(searchElement, fromIndex);
1142
+ }
1143
+ find(predicate, thisArg) {
1144
+ dependant(this);
1145
+ return reactive(this[native$2].find(predicate, thisArg));
1146
+ }
1147
+ findIndex(predicate, thisArg) {
1148
+ dependant(this);
1149
+ return this[native$2].findIndex(predicate, thisArg);
1150
+ }
1151
+ flat() {
1152
+ dependant(this);
1153
+ return reactive(this[native$2].flat());
1154
+ }
1155
+ flatMap(callbackfn, thisArg) {
1156
+ dependant(this);
1157
+ return reactive(this[native$2].flatMap(callbackfn, thisArg));
1158
+ }
1159
+ filter(callbackfn, thisArg) {
1160
+ dependant(this);
1161
+ return reactive(this[native$2].filter(callbackfn, thisArg));
1162
+ }
1163
+ map(callbackfn, thisArg) {
1164
+ dependant(this);
1165
+ return reactive(this[native$2].map(callbackfn, thisArg));
1166
+ }
1167
+ reduce(callbackfn, initialValue) {
1168
+ dependant(this);
1169
+ const result = initialValue === undefined
1170
+ ? this[native$2].reduce(callbackfn)
1171
+ : this[native$2].reduce(callbackfn, initialValue);
1172
+ return reactive(result);
1173
+ }
1174
+ reduceRight(callbackfn, initialValue) {
1175
+ dependant(this);
1176
+ const result = initialValue !== undefined
1177
+ ? this[native$2].reduceRight(callbackfn, initialValue)
1178
+ : this[native$2].reduceRight(callbackfn);
1179
+ return reactive(result);
1180
+ }
1181
+ slice(start, end) {
1182
+ for (const i of range(start || 0, end || this[native$2].length - 1))
1183
+ dependant(this, i);
1184
+ return start === undefined
1185
+ ? this[native$2].slice()
1186
+ : end === undefined
1187
+ ? this[native$2].slice(start)
1188
+ : this[native$2].slice(start, end);
1189
+ }
1190
+ concat(...items) {
1191
+ dependant(this);
1192
+ return reactive(this[native$2].concat(...items));
1193
+ }
1194
+ join(separator) {
1195
+ dependant(this);
1196
+ return this[native$2].join(separator);
1197
+ }
1198
+ forEach(callbackfn, thisArg) {
1199
+ dependant(this);
1200
+ this[native$2].forEach(callbackfn, thisArg);
1201
+ }
1202
+ // TODO: re-implement for fun dependencies? (eg - every only check the first ones until it find some)
1203
+ every(callbackfn, thisArg) {
1204
+ dependant(this);
1205
+ return this[native$2].every(callbackfn, thisArg);
1206
+ }
1207
+ some(callbackfn, thisArg) {
1208
+ dependant(this);
1209
+ return this[native$2].some(callbackfn, thisArg);
1210
+ }
1211
+ }
1212
+
1213
+ const native$1 = Symbol('native');
1214
+ // TODO: [prototypeForwarding]
1215
+ class ReactiveWeakMap {
1216
+ constructor(original) {
1217
+ Object.defineProperties(this, {
1218
+ [native$1]: { value: original },
1219
+ [prototypeForwarding]: { value: original },
1220
+ content: { value: Symbol('content') },
1221
+ [Symbol.toStringTag]: { value: 'ReactiveWeakMap' },
1222
+ });
1223
+ }
1224
+ // Implement WeakMap interface methods with reactivity
1225
+ delete(key) {
1226
+ const hadKey = this[native$1].has(key);
1227
+ const result = this[native$1].delete(key);
1228
+ if (hadKey)
1229
+ touched1(this.content, { type: 'del', prop: key }, key);
1230
+ return result;
1231
+ }
1232
+ get(key) {
1233
+ dependant(this.content, key);
1234
+ return reactive(this[native$1].get(key));
1235
+ }
1236
+ has(key) {
1237
+ dependant(this.content, key);
1238
+ return this[native$1].has(key);
1239
+ }
1240
+ set(key, value) {
1241
+ // Trigger effects for the specific key
1242
+ touched1(this.content, { type: this[native$1].has(key) ? 'set' : 'add', prop: key }, key);
1243
+ this[native$1].set(key, value);
1244
+ return this;
1245
+ }
1246
+ }
1247
+ class ReactiveMap {
1248
+ constructor(original) {
1249
+ Object.defineProperties(this, {
1250
+ [native$1]: { value: original },
1251
+ [prototypeForwarding]: { value: original },
1252
+ content: { value: Symbol('content') },
1253
+ [Symbol.toStringTag]: { value: 'ReactiveMap' },
1254
+ });
1255
+ }
1256
+ // Implement Map interface methods with reactivity
1257
+ get size() {
1258
+ dependant(this, 'size'); // The ReactiveMap instance still goes through proxy
1259
+ return this[native$1].size;
1260
+ }
1261
+ clear() {
1262
+ const hadEntries = this[native$1].size > 0;
1263
+ this[native$1].clear();
1264
+ if (hadEntries) {
1265
+ const evolution = { type: 'bunch', method: 'clear' };
1266
+ // Clear triggers all effects since all keys are affected
1267
+ touched1(this, evolution, 'size');
1268
+ touched(this.content, evolution);
1269
+ }
1270
+ }
1271
+ entries() {
1272
+ dependant(this.content);
1273
+ return makeReactiveEntriesIterator(this[native$1].entries());
1274
+ }
1275
+ forEach(callbackfn, thisArg) {
1276
+ dependant(this.content);
1277
+ this[native$1].forEach(callbackfn, thisArg);
1278
+ }
1279
+ keys() {
1280
+ dependant(this.content);
1281
+ return this[native$1].keys();
1282
+ }
1283
+ values() {
1284
+ dependant(this.content);
1285
+ return makeReactiveIterator(this[native$1].values());
1286
+ }
1287
+ [Symbol.iterator]() {
1288
+ dependant(this.content);
1289
+ const nativeIterator = this[native$1][Symbol.iterator]();
1290
+ return {
1291
+ next() {
1292
+ const result = nativeIterator.next();
1293
+ if (result.done) {
1294
+ return result;
1295
+ }
1296
+ return {
1297
+ value: [result.value[0], reactive(result.value[1])],
1298
+ done: false,
1299
+ };
1300
+ },
1301
+ };
1302
+ }
1303
+ // Implement Map methods with reactivity
1304
+ delete(key) {
1305
+ const hadKey = this[native$1].has(key);
1306
+ const result = this[native$1].delete(key);
1307
+ if (hadKey) {
1308
+ const evolution = { type: 'del', prop: key };
1309
+ touched1(this.content, evolution, key);
1310
+ touched1(this, evolution, 'size');
1311
+ }
1312
+ return result;
1313
+ }
1314
+ get(key) {
1315
+ dependant(this.content, key);
1316
+ return reactive(this[native$1].get(key));
1317
+ }
1318
+ has(key) {
1319
+ dependant(this.content, key);
1320
+ return this[native$1].has(key);
1321
+ }
1322
+ set(key, value) {
1323
+ const hadKey = this[native$1].has(key);
1324
+ const oldValue = this[native$1].get(key);
1325
+ const reactiveValue = reactive(value);
1326
+ this[native$1].set(key, reactiveValue);
1327
+ if (!hadKey || oldValue !== reactiveValue) {
1328
+ const evolution = { type: hadKey ? 'set' : 'add', prop: key };
1329
+ touched1(this.content, evolution, key);
1330
+ touched1(this, evolution, 'size');
1331
+ }
1332
+ return this;
1333
+ }
1334
+ }
1335
+
1336
+ const native = Symbol('native');
1337
+ // TODO: [prototypeForwarding]
1338
+ class ReactiveWeakSet {
1339
+ constructor(original) {
1340
+ Object.defineProperties(this, {
1341
+ [native]: { value: original },
1342
+ [prototypeForwarding]: { value: original },
1343
+ content: { value: Symbol('content') },
1344
+ [Symbol.toStringTag]: { value: 'ReactiveWeakSet' },
1345
+ });
1346
+ }
1347
+ add(value) {
1348
+ const had = this[native].has(value);
1349
+ this[native].add(value);
1350
+ if (!had) {
1351
+ // touch the specific value and the collection view
1352
+ touched1(this.content, { type: 'add', prop: value }, value);
1353
+ // no size/allProps for WeakSet
1354
+ }
1355
+ return this;
1356
+ }
1357
+ delete(value) {
1358
+ const had = this[native].has(value);
1359
+ const res = this[native].delete(value);
1360
+ if (had)
1361
+ touched1(this.content, { type: 'del', prop: value }, value);
1362
+ return res;
1363
+ }
1364
+ has(value) {
1365
+ dependant(this.content, value);
1366
+ return this[native].has(value);
1367
+ }
1368
+ }
1369
+ class ReactiveSet {
1370
+ constructor(original) {
1371
+ Object.defineProperties(this, {
1372
+ [native]: { value: original },
1373
+ [prototypeForwarding]: { value: original },
1374
+ content: { value: Symbol('content') },
1375
+ [Symbol.toStringTag]: { value: 'ReactiveSet' },
1376
+ });
1377
+ }
1378
+ get size() {
1379
+ // size depends on the wrapper instance, like Map counterpart
1380
+ dependant(this, 'size');
1381
+ return this[native].size;
1382
+ }
1383
+ add(value) {
1384
+ const had = this[native].has(value);
1385
+ const reactiveValue = reactive(value);
1386
+ this[native].add(reactiveValue);
1387
+ if (!had) {
1388
+ const evolution = { type: 'add', prop: reactiveValue };
1389
+ // touch for value-specific and aggregate dependencies
1390
+ touched1(this.content, evolution, reactiveValue);
1391
+ touched1(this, evolution, 'size');
1392
+ }
1393
+ return this;
1394
+ }
1395
+ clear() {
1396
+ const hadEntries = this[native].size > 0;
1397
+ this[native].clear();
1398
+ if (hadEntries) {
1399
+ const evolution = { type: 'bunch', method: 'clear' };
1400
+ touched1(this, evolution, 'size');
1401
+ touched(this.content, evolution);
1402
+ }
1403
+ }
1404
+ delete(value) {
1405
+ const had = this[native].has(value);
1406
+ const res = this[native].delete(value);
1407
+ if (had) {
1408
+ const evolution = { type: 'del', prop: value };
1409
+ touched1(this.content, evolution, value);
1410
+ touched1(this, evolution, 'size');
1411
+ }
1412
+ return res;
1413
+ }
1414
+ has(value) {
1415
+ dependant(this.content, value);
1416
+ return this[native].has(value);
1417
+ }
1418
+ entries() {
1419
+ dependant(this.content);
1420
+ return makeReactiveEntriesIterator(this[native].entries());
1421
+ }
1422
+ forEach(callbackfn, thisArg) {
1423
+ dependant(this.content);
1424
+ this[native].forEach(callbackfn, thisArg);
1425
+ }
1426
+ keys() {
1427
+ dependant(this.content);
1428
+ return makeReactiveIterator(this[native].keys());
1429
+ }
1430
+ values() {
1431
+ dependant(this.content);
1432
+ return makeReactiveIterator(this[native].values());
1433
+ }
1434
+ [Symbol.iterator]() {
1435
+ dependant(this.content);
1436
+ const nativeIterator = this[native][Symbol.iterator]();
1437
+ return {
1438
+ next() {
1439
+ const result = nativeIterator.next();
1440
+ if (result.done) {
1441
+ return result;
1442
+ }
1443
+ return { value: reactive(result.value), done: false };
1444
+ },
1445
+ };
1446
+ }
1447
+ }
1448
+
1449
+ // Register native collection types to use specialized reactive wrappers
1450
+ registerNativeReactivity(WeakMap, ReactiveWeakMap);
1451
+ registerNativeReactivity(Map, ReactiveMap);
1452
+ registerNativeReactivity(WeakSet, ReactiveWeakSet);
1453
+ registerNativeReactivity(Set, ReactiveSet);
1454
+ registerNativeReactivity(Array, ReactiveArray);
1455
+
1456
+ exports.ReactiveBase = ReactiveBase;
1457
+ exports.ReactiveError = ReactiveError;
1458
+ exports.computed = computed;
1459
+ exports.effect = effect;
1460
+ exports.getState = getState;
1461
+ exports.immutables = immutables;
1462
+ exports.invalidateComputed = invalidateComputed;
1463
+ exports.isNonReactive = isNonReactive;
1464
+ exports.isReactive = isReactive;
1465
+ exports.profileInfo = profileInfo;
1466
+ exports.reactive = reactive;
1467
+ exports.reactiveOptions = options;
1468
+ exports.unreactive = unreactive;
1469
+ exports.untracked = untracked;
1470
+ exports.unwrap = unwrap;
1471
+ exports.watch = watch;
1472
+ //# sourceMappingURL=reactive.js.map