mutts 1.0.1 → 1.0.2

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