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