mutts 1.0.7 → 1.0.9
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 +72 -17
- package/dist/browser.cjs +3111 -0
- package/dist/browser.cjs.map +1 -0
- package/dist/browser.d.ts +1391 -1
- package/dist/browser.esm.js +264 -35
- package/dist/browser.esm.js.map +1 -1
- package/dist/chunks/async-browser-lvzLOCgk.cjs +297 -0
- package/dist/chunks/async-browser-lvzLOCgk.cjs.map +1 -0
- package/dist/chunks/async-core-CRLKP3l-.cjs +29 -0
- package/dist/chunks/async-core-CRLKP3l-.cjs.map +1 -0
- package/dist/chunks/async-node-C3DeIb0y.cjs +96 -0
- package/dist/chunks/async-node-C3DeIb0y.cjs.map +1 -0
- package/dist/chunks/index-2vea86wD.esm.js +3011 -0
- package/dist/chunks/index-2vea86wD.esm.js.map +1 -0
- package/dist/chunks/index-CtA2AWl3.esm.js +4376 -0
- package/dist/chunks/index-CtA2AWl3.esm.js.map +1 -0
- package/dist/chunks/index-VTO-b2vR.cjs +4461 -0
- package/dist/chunks/index-VTO-b2vR.cjs.map +1 -0
- package/dist/debug/debug.d.ts +122 -0
- package/dist/debug/debug.d.ts.map +1 -0
- package/dist/debug/index.d.ts +4 -0
- package/dist/debug/index.d.ts.map +1 -0
- package/dist/debug/lineage-panel.d.ts +5 -0
- package/dist/debug/lineage-panel.d.ts.map +1 -0
- package/dist/debug/lineage.d.ts +79 -0
- package/dist/debug/lineage.d.ts.map +1 -0
- package/dist/debug.cjs +35 -0
- package/dist/debug.cjs.map +1 -0
- package/dist/debug.d.ts +209 -0
- package/dist/debug.esm.js +2 -0
- package/dist/debug.esm.js.map +1 -0
- package/dist/devtools/devtool/devtools.d.ts +1 -0
- package/dist/devtools/devtool/devtools.d.ts.map +1 -0
- package/dist/devtools/devtool/panel.d.ts +2 -0
- package/dist/devtools/devtool/panel.d.ts.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/mutts.umd.js +1 -1
- package/dist/mutts.umd.js.map +1 -1
- package/dist/mutts.umd.min.js +1 -1
- package/dist/mutts.umd.min.js.map +1 -1
- package/dist/node.cjs +107 -0
- package/dist/node.cjs.map +1 -0
- package/dist/node.d.ts +2 -2
- package/dist/node.esm.js +86 -33
- package/dist/node.esm.js.map +1 -1
- package/dist/src/async/browser.d.ts +2 -0
- package/dist/src/async/browser.d.ts.map +1 -0
- package/dist/src/async/index.d.ts +19 -0
- package/dist/src/async/index.d.ts.map +1 -0
- package/dist/src/async/node.d.ts +2 -0
- package/dist/src/async/node.d.ts.map +1 -0
- package/dist/src/decorator.d.ts +106 -0
- package/dist/src/decorator.d.ts.map +1 -0
- package/dist/src/destroyable.d.ts +87 -0
- package/dist/src/destroyable.d.ts.map +1 -0
- package/dist/src/entry-browser.d.ts +3 -0
- package/dist/src/entry-browser.d.ts.map +1 -0
- package/dist/src/entry-node.d.ts +3 -0
- package/dist/src/entry-node.d.ts.map +1 -0
- package/dist/src/eventful.d.ts +20 -0
- package/dist/src/eventful.d.ts.map +1 -0
- package/dist/src/flavored.d.ts +33 -0
- package/dist/src/flavored.d.ts.map +1 -0
- package/dist/src/index.d.ts +14 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/indexable.d.ts +243 -0
- package/dist/src/indexable.d.ts.map +1 -0
- package/dist/src/introspection.d.ts +27 -0
- package/dist/src/introspection.d.ts.map +1 -0
- package/dist/src/iterableWeak.d.ts +53 -0
- package/dist/src/iterableWeak.d.ts.map +1 -0
- package/dist/src/mixins.d.ts +25 -0
- package/dist/src/mixins.d.ts.map +1 -0
- package/dist/src/promiseChain.d.ts +20 -0
- package/dist/src/promiseChain.d.ts.map +1 -0
- package/dist/src/reactive/array.d.ts +48 -0
- package/dist/src/reactive/array.d.ts.map +1 -0
- package/dist/src/reactive/buffer.d.ts +120 -0
- package/dist/src/reactive/buffer.d.ts.map +1 -0
- package/dist/src/reactive/change.d.ts +29 -0
- package/dist/src/reactive/change.d.ts.map +1 -0
- package/dist/src/reactive/deep-touch.d.ts +28 -0
- package/dist/src/reactive/deep-touch.d.ts.map +1 -0
- package/dist/src/reactive/deep-watch-state.d.ts +25 -0
- package/dist/src/reactive/deep-watch-state.d.ts.map +1 -0
- package/dist/src/reactive/deep-watch.d.ts +20 -0
- package/dist/src/reactive/deep-watch.d.ts.map +1 -0
- package/dist/src/reactive/describe.d.ts +12 -0
- package/dist/src/reactive/describe.d.ts.map +1 -0
- package/dist/src/reactive/effect-context.d.ts +34 -0
- package/dist/src/reactive/effect-context.d.ts.map +1 -0
- package/dist/src/reactive/effects.d.ts +164 -0
- package/dist/src/reactive/effects.d.ts.map +1 -0
- package/dist/src/reactive/index.d.ts +19 -0
- package/dist/src/reactive/index.d.ts.map +1 -0
- package/dist/src/reactive/map.d.ts +28 -0
- package/dist/src/reactive/map.d.ts.map +1 -0
- package/dist/src/reactive/memoize.d.ts +28 -0
- package/dist/src/reactive/memoize.d.ts.map +1 -0
- package/dist/src/reactive/non-reactive-state.d.ts +9 -0
- package/dist/src/reactive/non-reactive-state.d.ts.map +1 -0
- package/dist/src/reactive/non-reactive.d.ts +11 -0
- package/dist/src/reactive/non-reactive.d.ts.map +1 -0
- package/dist/src/reactive/project.d.ts +40 -0
- package/dist/src/reactive/project.d.ts.map +1 -0
- package/dist/src/reactive/proxy-state.d.ts +8 -0
- package/dist/src/reactive/proxy-state.d.ts.map +1 -0
- package/dist/src/reactive/proxy.d.ts +23 -0
- package/dist/src/reactive/proxy.d.ts.map +1 -0
- package/dist/src/reactive/record.d.ts +115 -0
- package/dist/src/reactive/record.d.ts.map +1 -0
- package/dist/src/reactive/register.d.ts +125 -0
- package/dist/src/reactive/register.d.ts.map +1 -0
- package/dist/src/reactive/registry.d.ts +21 -0
- package/dist/src/reactive/registry.d.ts.map +1 -0
- package/dist/src/reactive/set.d.ts +26 -0
- package/dist/src/reactive/set.d.ts.map +1 -0
- package/dist/src/reactive/tracking.d.ts +7 -0
- package/dist/src/reactive/tracking.d.ts.map +1 -0
- package/dist/src/reactive/types.d.ts +424 -0
- package/dist/src/reactive/types.d.ts.map +1 -0
- package/dist/src/reactive/watch.d.ts +48 -0
- package/dist/src/reactive/watch.d.ts.map +1 -0
- package/dist/src/std-decorators.d.ts +45 -0
- package/dist/src/std-decorators.d.ts.map +1 -0
- package/dist/src/utils.d.ts +49 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/zone.d.ts +40 -0
- package/dist/src/zone.d.ts.map +1 -0
- package/dist/types-DaHFfhlN.d.ts +414 -0
- package/docs/ai/api-reference.md +1 -1
- package/docs/ai/manual.md +443 -17
- package/docs/events.md +57 -2
- package/docs/flavored.md +210 -0
- package/docs/reactive/advanced.md +2 -3
- package/docs/reactive/attend.md +143 -0
- package/docs/reactive/collections.md +55 -0
- package/docs/reactive/core.md +120 -4
- package/docs/reactive/debugging.md +8 -10
- package/docs/reactive/describe.md +85 -0
- package/docs/reactive/error-handling.md +121 -0
- package/docs/reactive/project.md +1 -1
- package/docs/reactive/scan.md +215 -0
- package/docs/reactive.md +2 -0
- package/docs/std-decorators.md +171 -52
- package/docs/zone.md +9 -6
- package/package.json +44 -27
- package/src/async/browser.ts +277 -41
- package/src/async/index.ts +23 -4
- package/src/async/node.ts +78 -32
- package/src/entry-browser.ts +5 -0
- package/src/entry-node.ts +5 -0
- package/src/eventful.ts +82 -49
- package/src/flavored.ts +106 -0
- package/src/index.d.ts +12 -9
- package/src/index.ts +2 -1
- package/src/introspection.ts +1 -1
- package/src/reactive/array.ts +160 -80
- package/src/reactive/buffer.ts +187 -27
- package/src/reactive/change.ts +16 -23
- package/src/reactive/deep-touch.ts +19 -14
- package/src/reactive/deep-watch-state.ts +3 -3
- package/src/reactive/deep-watch.ts +7 -4
- package/src/reactive/describe.ts +39 -0
- package/src/reactive/effect-context.ts +71 -6
- package/src/reactive/effects.ts +445 -393
- package/src/reactive/index.ts +12 -14
- package/src/reactive/map.ts +8 -24
- package/src/reactive/memoize.ts +26 -3
- package/src/reactive/project.md +107 -0
- package/src/reactive/project.ts +48 -71
- package/src/reactive/proxy.ts +21 -3
- package/src/reactive/record.ts +30 -45
- package/src/reactive/register.ts +138 -19
- package/src/reactive/registry.ts +22 -9
- package/src/reactive/set.ts +7 -20
- package/src/reactive/tracking.ts +9 -12
- package/src/reactive/types.ts +104 -24
- package/src/reactive/{interface.ts → watch.ts} +29 -59
- package/src/std-decorators.ts +56 -17
- package/src/utils.ts +3 -4
- package/src/zone.ts +43 -28
- package/dist/browser.js +0 -161
- package/dist/browser.js.map +0 -1
- package/dist/chunks/index-BFYK02LG.js +0 -6378
- package/dist/chunks/index-BFYK02LG.js.map +0 -1
- package/dist/chunks/index-CNR6QRUl.esm.js +0 -6291
- package/dist/chunks/index-CNR6QRUl.esm.js.map +0 -1
- package/dist/node.js +0 -136
- package/dist/node.js.map +0 -1
- package/src/reactive/debug.ts +0 -517
|
@@ -0,0 +1,3011 @@
|
|
|
1
|
+
import { r as decorator, a3 as markWithRoot, a4 as rootFunction, V as renamed, a5 as getRoot, a6 as dependant, T as options, a0 as untracked, s as deepCompare, a7 as optionCall, X as root, v as effect, $ as touched1, S as reactive, n as cleanedBy, a8 as __runInitializers, a9 as __esDecorate, F as FoolProof, aa as objectsWithDeepWatchers, ab as effectToDeepWatchedObjects, ac as deepWatchers, H as isNonReactive, a1 as unwrap, ad as stopped, ae as nonReactiveClass, af as unreactiveProperties, ag as nonReactiveMark, ah as nonReactiveObjects, ai as __setFunctionName, aj as __classPrivateFieldGet, ak as __classPrivateFieldSet, al as setEffectName, O as named, B as getActiveEffect, am as projectionInfo, _ as touched, p as contentRef, an as notifyPropertyChange, ao as metaProtos, ap as objectParents, aq as watchers, ar as effectToReactiveObjects, as as proxyToObject, at as objectToProxy, y as flavored } from './index-CtA2AWl3.esm.js';
|
|
2
|
+
|
|
3
|
+
// Integrated with `using` statement via Symbol.dispose
|
|
4
|
+
const fr = new FinalizationRegistry((f) => f());
|
|
5
|
+
/**
|
|
6
|
+
* Symbol for marking destructor methods
|
|
7
|
+
*/
|
|
8
|
+
const destructor = Symbol('destructor');
|
|
9
|
+
/**
|
|
10
|
+
* Symbol for accessing allocated values in destroyable objects
|
|
11
|
+
*/
|
|
12
|
+
const allocatedValues = Symbol('allocated');
|
|
13
|
+
/**
|
|
14
|
+
* Error thrown when attempting to access a destroyed object
|
|
15
|
+
*/
|
|
16
|
+
class DestructionError extends Error {
|
|
17
|
+
static throw(msg) {
|
|
18
|
+
return () => {
|
|
19
|
+
throw new DestructionError(msg);
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
constructor(msg) {
|
|
23
|
+
super(`Object is destroyed. ${msg}`);
|
|
24
|
+
this.name = 'DestroyedAccessError';
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const destroyedHandler = {
|
|
28
|
+
[Symbol.toStringTag]: 'MutTs Destroyable',
|
|
29
|
+
get: DestructionError.throw('Cannot access destroyed object'),
|
|
30
|
+
set: DestructionError.throw('Cannot access destroyed object'),
|
|
31
|
+
};
|
|
32
|
+
function Destroyable(base, destructorObj) {
|
|
33
|
+
var _a;
|
|
34
|
+
if (base && typeof base !== 'function') {
|
|
35
|
+
destructorObj = base;
|
|
36
|
+
base = undefined;
|
|
37
|
+
}
|
|
38
|
+
if (!base) {
|
|
39
|
+
base = class {
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
return _a = class Destroyable extends base {
|
|
43
|
+
static destroy(obj) {
|
|
44
|
+
const destructor = _a.destructors.get(obj);
|
|
45
|
+
if (!destructor)
|
|
46
|
+
return false;
|
|
47
|
+
fr.unregister(obj[allocatedValues]);
|
|
48
|
+
_a.destructors.delete(obj);
|
|
49
|
+
Object.setPrototypeOf(obj, new Proxy({}, destroyedHandler));
|
|
50
|
+
// Clear all own properties
|
|
51
|
+
for (const key of Object.getOwnPropertyNames(obj)) {
|
|
52
|
+
delete obj[key];
|
|
53
|
+
}
|
|
54
|
+
destructor();
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
static isDestroyable(obj) {
|
|
58
|
+
return _a.destructors.has(obj);
|
|
59
|
+
}
|
|
60
|
+
constructor(...args) {
|
|
61
|
+
super(...args);
|
|
62
|
+
const allocated = {};
|
|
63
|
+
this[allocatedValues] = allocated;
|
|
64
|
+
// @ts-expect-error `this` is an AbstractDestroyable
|
|
65
|
+
const myDestructor = destructorObj?.destructor ?? this[destructor];
|
|
66
|
+
if (!myDestructor) {
|
|
67
|
+
throw new DestructionError('Destructor is not defined');
|
|
68
|
+
}
|
|
69
|
+
function destruction() {
|
|
70
|
+
myDestructor(allocated);
|
|
71
|
+
}
|
|
72
|
+
_a.destructors.set(this, destruction);
|
|
73
|
+
fr.register(this, destruction, allocated);
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
_a.destructors = new WeakMap(),
|
|
77
|
+
_a;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Decorator that marks properties to be stored in the allocated object and passed to the destructor
|
|
81
|
+
* Use with accessor properties or explicit get/set pairs
|
|
82
|
+
*/
|
|
83
|
+
const allocated = decorator({
|
|
84
|
+
setter(original, _target, propertyKey) {
|
|
85
|
+
return function (value) {
|
|
86
|
+
this[allocatedValues][propertyKey] = value;
|
|
87
|
+
return original.call(this, value);
|
|
88
|
+
};
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
/**
|
|
92
|
+
* Registers a callback to be called when an object is garbage collected
|
|
93
|
+
* @param cb - The callback function to execute on garbage collection
|
|
94
|
+
* @returns The object whose reference can be collected
|
|
95
|
+
*/
|
|
96
|
+
function callOnGC(cb) {
|
|
97
|
+
let called = false;
|
|
98
|
+
const forward = () => {
|
|
99
|
+
if (called)
|
|
100
|
+
return;
|
|
101
|
+
called = true;
|
|
102
|
+
cb();
|
|
103
|
+
};
|
|
104
|
+
fr.register(forward, cb, cb);
|
|
105
|
+
return forward;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
var _a, _b;
|
|
109
|
+
const events = Symbol('events');
|
|
110
|
+
const hooks = Symbol('hooks');
|
|
111
|
+
const eventBehavior = {
|
|
112
|
+
on(eventOrEvents, cb) {
|
|
113
|
+
if (typeof eventOrEvents === 'object') {
|
|
114
|
+
for (const e of Object.keys(eventOrEvents)) {
|
|
115
|
+
this.on(e, eventOrEvents[e]);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
else if (cb !== undefined) {
|
|
119
|
+
const callbacks = this[events].get(eventOrEvents) ?? new Set();
|
|
120
|
+
if (!callbacks.has(cb))
|
|
121
|
+
callbacks.add(cb);
|
|
122
|
+
this[events].set(eventOrEvents, callbacks);
|
|
123
|
+
}
|
|
124
|
+
return () => this.off(eventOrEvents, cb);
|
|
125
|
+
},
|
|
126
|
+
off(eventOrEvents, cb) {
|
|
127
|
+
if (typeof eventOrEvents === 'object') {
|
|
128
|
+
for (const e of Object.keys(eventOrEvents)) {
|
|
129
|
+
this.off(e, eventOrEvents[e]);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
else if (cb !== null && cb !== undefined) {
|
|
133
|
+
const callbacks = this[events].get(eventOrEvents);
|
|
134
|
+
if (callbacks) {
|
|
135
|
+
callbacks.delete(cb);
|
|
136
|
+
if (!callbacks.size)
|
|
137
|
+
this[events].delete(eventOrEvents);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
// Remove all listeners for this event
|
|
142
|
+
this[events].delete(eventOrEvents);
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
emit(event, ...args) {
|
|
146
|
+
const callbacks = this[events].get(event);
|
|
147
|
+
if (callbacks)
|
|
148
|
+
for (const cb of callbacks)
|
|
149
|
+
cb.apply(this, args);
|
|
150
|
+
for (const cb of this[hooks])
|
|
151
|
+
cb.call(this, event, ...args);
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
function perEvent(eventful, fct, use) {
|
|
155
|
+
const cache = new Map();
|
|
156
|
+
return new Proxy(fct, {
|
|
157
|
+
get(target, prop) {
|
|
158
|
+
if (typeof prop !== 'string')
|
|
159
|
+
return target[prop];
|
|
160
|
+
if (use && !eventful[events].has(prop) && !eventful[hooks].size)
|
|
161
|
+
return () => { };
|
|
162
|
+
// Return cached function or create and cache
|
|
163
|
+
let cached = cache.get(prop);
|
|
164
|
+
if (!cached) {
|
|
165
|
+
cached = (...args) => fct.apply(eventful, [prop, ...args]);
|
|
166
|
+
cache.set(prop, cached);
|
|
167
|
+
}
|
|
168
|
+
return cached;
|
|
169
|
+
},
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* A type-safe event system that provides a clean API for event handling
|
|
174
|
+
* @template Events - The event map defining event names and their handler signatures
|
|
175
|
+
*/
|
|
176
|
+
class Eventful {
|
|
177
|
+
constructor() {
|
|
178
|
+
this[_a] = new Map();
|
|
179
|
+
this[_b] = new Set();
|
|
180
|
+
this.on = perEvent(this, eventBehavior.on);
|
|
181
|
+
this.off = perEvent(this, eventBehavior.off);
|
|
182
|
+
this.emit = perEvent(this, eventBehavior.emit, 'use');
|
|
183
|
+
}
|
|
184
|
+
hook(cb) {
|
|
185
|
+
this[hooks].add(cb);
|
|
186
|
+
return () => {
|
|
187
|
+
this[hooks].delete(cb);
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
_a = events, _b = hooks;
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Symbol for defining custom getter logic for numeric index access
|
|
195
|
+
*/
|
|
196
|
+
const getAt = Symbol('getAt');
|
|
197
|
+
/**
|
|
198
|
+
* Symbol for defining custom setter logic for numeric index access
|
|
199
|
+
*/
|
|
200
|
+
const setAt = Symbol('setAt');
|
|
201
|
+
function Indexable(base, accessor) {
|
|
202
|
+
if (base && typeof base !== 'function') {
|
|
203
|
+
accessor = base;
|
|
204
|
+
base = undefined;
|
|
205
|
+
}
|
|
206
|
+
if (!base) {
|
|
207
|
+
//@ts-expect-error
|
|
208
|
+
base = class {
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
if (!accessor) {
|
|
212
|
+
accessor = {
|
|
213
|
+
get(index) {
|
|
214
|
+
if (typeof this[getAt] !== 'function') {
|
|
215
|
+
throw new Error('Indexable class must have an [getAt] method');
|
|
216
|
+
}
|
|
217
|
+
return this[getAt](index);
|
|
218
|
+
},
|
|
219
|
+
set(index, value) {
|
|
220
|
+
if (typeof this[setAt] !== 'function') {
|
|
221
|
+
throw new Error('Indexable class has read-only numeric index access');
|
|
222
|
+
}
|
|
223
|
+
this[setAt](index, value);
|
|
224
|
+
},
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
class Indexable extends base {
|
|
228
|
+
}
|
|
229
|
+
Object.setPrototypeOf(Indexable.prototype, new Proxy(base.prototype, {
|
|
230
|
+
//@ts-expect-error
|
|
231
|
+
[Symbol.toStringTag]: 'MutTs Indexable',
|
|
232
|
+
get(target, prop, receiver) {
|
|
233
|
+
if (prop in target) {
|
|
234
|
+
const getter = Object.getOwnPropertyDescriptor(target, prop)?.get;
|
|
235
|
+
return getter ? getter.call(receiver) : target[prop];
|
|
236
|
+
}
|
|
237
|
+
if (typeof prop === 'string') {
|
|
238
|
+
if (prop === 'length' && accessor.getLength)
|
|
239
|
+
return accessor.getLength.call(receiver);
|
|
240
|
+
const numProp = Number(prop);
|
|
241
|
+
if (!Number.isNaN(numProp)) {
|
|
242
|
+
return accessor.get.call(receiver, numProp);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return undefined;
|
|
246
|
+
},
|
|
247
|
+
set(target, prop, value, receiver) {
|
|
248
|
+
if (prop in target) {
|
|
249
|
+
const setter = Object.getOwnPropertyDescriptor(target, prop)?.set;
|
|
250
|
+
if (setter)
|
|
251
|
+
setter.call(receiver, value);
|
|
252
|
+
else
|
|
253
|
+
target[prop] = value;
|
|
254
|
+
return true;
|
|
255
|
+
}
|
|
256
|
+
if (typeof prop === 'string') {
|
|
257
|
+
if (prop === 'length' && accessor.setLength) {
|
|
258
|
+
accessor.setLength.call(receiver, value);
|
|
259
|
+
return true;
|
|
260
|
+
}
|
|
261
|
+
const numProp = Number(prop);
|
|
262
|
+
if (!Number.isNaN(numProp)) {
|
|
263
|
+
if (!accessor.set)
|
|
264
|
+
throw new Error('Indexable class has read-only numeric index access');
|
|
265
|
+
accessor.set.call(receiver, numProp, value);
|
|
266
|
+
return true;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
Object.defineProperty(receiver, prop, {
|
|
270
|
+
value,
|
|
271
|
+
writable: true,
|
|
272
|
+
enumerable: true,
|
|
273
|
+
configurable: true,
|
|
274
|
+
});
|
|
275
|
+
return true;
|
|
276
|
+
},
|
|
277
|
+
has(target, prop) {
|
|
278
|
+
if (prop in target)
|
|
279
|
+
return true;
|
|
280
|
+
if (typeof prop === 'string') {
|
|
281
|
+
if (prop === 'length' && accessor.getLength)
|
|
282
|
+
return true;
|
|
283
|
+
const numProp = Number(prop);
|
|
284
|
+
if (!Number.isNaN(numProp))
|
|
285
|
+
return true;
|
|
286
|
+
}
|
|
287
|
+
return false;
|
|
288
|
+
},
|
|
289
|
+
ownKeys(target) {
|
|
290
|
+
const keys = Reflect.ownKeys(target);
|
|
291
|
+
if (accessor.getLength) {
|
|
292
|
+
keys.push('length');
|
|
293
|
+
const len = accessor.getLength.call(this);
|
|
294
|
+
for (let i = 0; i < len; i++)
|
|
295
|
+
keys.push(String(i));
|
|
296
|
+
}
|
|
297
|
+
return keys;
|
|
298
|
+
},
|
|
299
|
+
getOwnPropertyDescriptor(target, prop) {
|
|
300
|
+
if (prop in target)
|
|
301
|
+
return Object.getOwnPropertyDescriptor(target, prop);
|
|
302
|
+
if (typeof prop === 'string') {
|
|
303
|
+
if (prop === 'length' && accessor.getLength) {
|
|
304
|
+
return {
|
|
305
|
+
enumerable: false,
|
|
306
|
+
configurable: true,
|
|
307
|
+
get: () => accessor.getLength.call(this),
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
const numProp = Number(prop);
|
|
311
|
+
if (!Number.isNaN(numProp)) {
|
|
312
|
+
return {
|
|
313
|
+
enumerable: true,
|
|
314
|
+
configurable: true,
|
|
315
|
+
get: () => accessor.get.call(this, numProp),
|
|
316
|
+
set: accessor.set
|
|
317
|
+
? (v) => accessor.set.call(this, numProp, v)
|
|
318
|
+
: undefined,
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
return undefined;
|
|
323
|
+
},
|
|
324
|
+
}));
|
|
325
|
+
return Indexable;
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Symbol for accessing the forwarded array in ArrayReadForward
|
|
329
|
+
*/
|
|
330
|
+
const forwardArray = Symbol('forwardArray');
|
|
331
|
+
/**
|
|
332
|
+
* A read-only array forwarder that implements all reading/iterating methods of Array
|
|
333
|
+
* but does not implement modification methods.
|
|
334
|
+
*
|
|
335
|
+
* The constructor takes a callback that returns an array, and all methods forward
|
|
336
|
+
* their behavior to the result of that callback.
|
|
337
|
+
*/
|
|
338
|
+
class ArrayReadForward {
|
|
339
|
+
get [forwardArray]() {
|
|
340
|
+
throw new Error('ArrayReadForward is not implemented');
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Get the length of the array
|
|
344
|
+
*/
|
|
345
|
+
get length() {
|
|
346
|
+
return this[forwardArray].length;
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Iterator protocol support
|
|
350
|
+
*/
|
|
351
|
+
[Symbol.iterator]() {
|
|
352
|
+
return this[forwardArray][Symbol.iterator]();
|
|
353
|
+
}
|
|
354
|
+
// Reading/Iterating methods
|
|
355
|
+
/**
|
|
356
|
+
* Creates a new array with the results of calling a provided function on every element
|
|
357
|
+
*/
|
|
358
|
+
map(callbackfn, thisArg) {
|
|
359
|
+
return this[forwardArray].map(callbackfn, thisArg);
|
|
360
|
+
}
|
|
361
|
+
filter(predicate, thisArg) {
|
|
362
|
+
return this[forwardArray].filter(predicate, thisArg);
|
|
363
|
+
}
|
|
364
|
+
reduce(callbackfn, initialValue) {
|
|
365
|
+
return initialValue !== undefined
|
|
366
|
+
? this[forwardArray].reduce(callbackfn, initialValue)
|
|
367
|
+
: this[forwardArray].reduce(callbackfn);
|
|
368
|
+
}
|
|
369
|
+
reduceRight(callbackfn, initialValue) {
|
|
370
|
+
return initialValue !== undefined
|
|
371
|
+
? this[forwardArray].reduceRight(callbackfn, initialValue)
|
|
372
|
+
: this[forwardArray].reduceRight(callbackfn);
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* Executes a provided function once for each array element
|
|
376
|
+
*/
|
|
377
|
+
forEach(callbackfn, thisArg) {
|
|
378
|
+
this[forwardArray].forEach(callbackfn, thisArg);
|
|
379
|
+
}
|
|
380
|
+
find(predicate, thisArg) {
|
|
381
|
+
return this[forwardArray].find(predicate, thisArg);
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Returns the index of the first element in the array that satisfies the provided testing function
|
|
385
|
+
*/
|
|
386
|
+
findIndex(predicate, thisArg) {
|
|
387
|
+
return this[forwardArray].findIndex(predicate, thisArg);
|
|
388
|
+
}
|
|
389
|
+
findLast(predicate, thisArg) {
|
|
390
|
+
return this[forwardArray].findLast(predicate, thisArg);
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* Returns the index of the last element in the array that satisfies the provided testing function
|
|
394
|
+
*/
|
|
395
|
+
findLastIndex(predicate, thisArg) {
|
|
396
|
+
return this[forwardArray].findLastIndex(predicate, thisArg);
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Determines whether an array includes a certain value among its entries
|
|
400
|
+
*/
|
|
401
|
+
includes(searchElement, fromIndex) {
|
|
402
|
+
return this[forwardArray].includes(searchElement, fromIndex);
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Returns the first index at which a given element can be found in the array
|
|
406
|
+
*/
|
|
407
|
+
indexOf(searchElement, fromIndex) {
|
|
408
|
+
return this[forwardArray].indexOf(searchElement, fromIndex);
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Returns the last index at which a given element can be found in the array
|
|
412
|
+
*/
|
|
413
|
+
lastIndexOf(searchElement, fromIndex) {
|
|
414
|
+
return this[forwardArray].lastIndexOf(searchElement, fromIndex);
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* Returns a shallow copy of a portion of an array into a new array object
|
|
418
|
+
*/
|
|
419
|
+
slice(start, end) {
|
|
420
|
+
return this[forwardArray].slice(start, end);
|
|
421
|
+
}
|
|
422
|
+
concat(...items) {
|
|
423
|
+
return this[forwardArray].concat(...items);
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* Tests whether all elements in the array pass the test implemented by the provided function
|
|
427
|
+
*/
|
|
428
|
+
every(predicate, thisArg) {
|
|
429
|
+
return this[forwardArray].every(predicate, thisArg);
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* Tests whether at least one element in the array passes the test implemented by the provided function
|
|
433
|
+
*/
|
|
434
|
+
some(predicate, thisArg) {
|
|
435
|
+
return this[forwardArray].some(predicate, thisArg);
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* Joins all elements of an array into a string
|
|
439
|
+
*/
|
|
440
|
+
join(separator) {
|
|
441
|
+
return this[forwardArray].join(separator);
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Returns a new array iterator that contains the keys for each index in the array
|
|
445
|
+
*/
|
|
446
|
+
keys() {
|
|
447
|
+
return this[forwardArray].keys();
|
|
448
|
+
}
|
|
449
|
+
/**
|
|
450
|
+
* Returns a new array iterator that contains the values for each index in the array
|
|
451
|
+
*/
|
|
452
|
+
values() {
|
|
453
|
+
return this[forwardArray].values();
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Returns a new array iterator that contains the key/value pairs for each index in the array
|
|
457
|
+
*/
|
|
458
|
+
entries() {
|
|
459
|
+
return this[forwardArray].entries();
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Returns a string representation of the array
|
|
463
|
+
*/
|
|
464
|
+
toString() {
|
|
465
|
+
return this[forwardArray].toString();
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* Returns a localized string representing the array
|
|
469
|
+
*/
|
|
470
|
+
toLocaleString(locales, options) {
|
|
471
|
+
return this[forwardArray].toLocaleString(locales, options);
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Returns the element at the specified index, or undefined if the index is out of bounds
|
|
475
|
+
*/
|
|
476
|
+
at(index) {
|
|
477
|
+
return this[forwardArray].at(index);
|
|
478
|
+
}
|
|
479
|
+
/**
|
|
480
|
+
* Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth
|
|
481
|
+
*/
|
|
482
|
+
flat(depth) {
|
|
483
|
+
return this[forwardArray].flat(depth);
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* Returns a new array formed by applying a given callback function to each element of the array,
|
|
487
|
+
* and then flattening the result by one level
|
|
488
|
+
*/
|
|
489
|
+
flatMap(callback, thisArg) {
|
|
490
|
+
return this[forwardArray].flatMap(callback, thisArg);
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* Returns a new array with elements in reversed order (ES2023)
|
|
494
|
+
*/
|
|
495
|
+
toReversed() {
|
|
496
|
+
return this[forwardArray].toReversed?.() ?? [...this[forwardArray]].reverse();
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* Returns a new array with elements sorted (ES2023)
|
|
500
|
+
*/
|
|
501
|
+
toSorted(compareFn) {
|
|
502
|
+
return this[forwardArray].toSorted?.(compareFn) ?? [...this[forwardArray]].sort(compareFn);
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* Returns a new array with some elements removed and/or replaced at a given index (ES2023)
|
|
506
|
+
*/
|
|
507
|
+
toSpliced(start, deleteCount, ...items) {
|
|
508
|
+
if (deleteCount === undefined)
|
|
509
|
+
return this[forwardArray].toSpliced(start);
|
|
510
|
+
return this[forwardArray].toSpliced(start, deleteCount, ...items);
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Returns a new array with the element at the given index replaced with the given value (ES2023)
|
|
514
|
+
*/
|
|
515
|
+
with(index, value) {
|
|
516
|
+
return this[forwardArray].with(index, value);
|
|
517
|
+
}
|
|
518
|
+
get [Symbol.unscopables]() {
|
|
519
|
+
return this[forwardArray][Symbol.unscopables];
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
const forward = (name, target) => (...args) => {
|
|
524
|
+
return target[name](...args);
|
|
525
|
+
};
|
|
526
|
+
const alreadyChained = new WeakMap();
|
|
527
|
+
const originals = new WeakMap();
|
|
528
|
+
function cache$1(target, rv) {
|
|
529
|
+
originals.set(rv, target);
|
|
530
|
+
alreadyChained.set(target, rv);
|
|
531
|
+
}
|
|
532
|
+
const promiseProxyHandler = {
|
|
533
|
+
//@ts-expect-error
|
|
534
|
+
[Symbol.toStringTag]: 'MutTs PromiseChain function',
|
|
535
|
+
get(target, prop) {
|
|
536
|
+
if (prop === Symbol.toStringTag)
|
|
537
|
+
return 'PromiseProxy';
|
|
538
|
+
if (typeof prop === 'string' && ['then', 'catch', 'finally'].includes(prop))
|
|
539
|
+
return target[prop];
|
|
540
|
+
return chainPromise(target.then((r) => r[prop]));
|
|
541
|
+
},
|
|
542
|
+
};
|
|
543
|
+
const promiseForward = (target) => ({
|
|
544
|
+
// biome-ignore lint/suspicious/noThenProperty: This one is the whole point
|
|
545
|
+
then: forward('then', target),
|
|
546
|
+
catch: forward('catch', target),
|
|
547
|
+
finally: forward('finally', target),
|
|
548
|
+
});
|
|
549
|
+
const objectProxyHandler = {
|
|
550
|
+
//@ts-expect-error
|
|
551
|
+
[Symbol.toStringTag]: 'MutTs PromiseChain object',
|
|
552
|
+
get(target, prop, receiver) {
|
|
553
|
+
const getter = Object.getOwnPropertyDescriptor(target, prop)?.get;
|
|
554
|
+
const rv = getter ? getter.call(receiver) : target[prop];
|
|
555
|
+
// Allows fct.call or fct.apply to bypass the chain system
|
|
556
|
+
if (typeof target === 'function')
|
|
557
|
+
return rv;
|
|
558
|
+
return chainPromise(rv);
|
|
559
|
+
},
|
|
560
|
+
apply(target, thisArg, args) {
|
|
561
|
+
return chainPromise(target.apply(thisArg, args));
|
|
562
|
+
},
|
|
563
|
+
};
|
|
564
|
+
function chainObject(given) {
|
|
565
|
+
const rv = new Proxy(given, objectProxyHandler);
|
|
566
|
+
cache$1(given, rv);
|
|
567
|
+
return rv;
|
|
568
|
+
}
|
|
569
|
+
function chainable(x) {
|
|
570
|
+
return x && ['function', 'object'].includes(typeof x);
|
|
571
|
+
}
|
|
572
|
+
/**
|
|
573
|
+
* Transforms a promise or value into a chainable object
|
|
574
|
+
* Allows calling methods directly on promise results without awaiting them first
|
|
575
|
+
* @param given - The promise or value to make chainable
|
|
576
|
+
* @returns A chainable version of the input
|
|
577
|
+
*/
|
|
578
|
+
function chainPromise(given) {
|
|
579
|
+
if (!chainable(given))
|
|
580
|
+
return given;
|
|
581
|
+
if (alreadyChained.has(given))
|
|
582
|
+
return alreadyChained.get(given);
|
|
583
|
+
if (!(given instanceof Promise))
|
|
584
|
+
return chainObject(given);
|
|
585
|
+
// @ts-expect-error It's ok as we check if it's an object above
|
|
586
|
+
given = given.then((r) => (chainable(r) ? chainObject(r) : r));
|
|
587
|
+
const target = Object.assign(function (...args) {
|
|
588
|
+
return chainPromise(given.then((r) => {
|
|
589
|
+
return this?.then
|
|
590
|
+
? this.then((t) => r.apply(t, args))
|
|
591
|
+
: r.apply(this, args);
|
|
592
|
+
}));
|
|
593
|
+
}, promiseForward(given));
|
|
594
|
+
const chained = new Proxy(target, promiseProxyHandler);
|
|
595
|
+
cache$1(given, chained);
|
|
596
|
+
return chained;
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
const memoizedRegistry = new WeakMap();
|
|
600
|
+
const wrapperRegistry = new WeakMap();
|
|
601
|
+
function getBranch(tree, key) {
|
|
602
|
+
tree.branches ?? (tree.branches = new WeakMap());
|
|
603
|
+
let branch = tree.branches.get(key);
|
|
604
|
+
if (!branch) {
|
|
605
|
+
branch = {};
|
|
606
|
+
tree.branches.set(key, branch);
|
|
607
|
+
}
|
|
608
|
+
return branch;
|
|
609
|
+
}
|
|
610
|
+
function memoizeFunction(fn) {
|
|
611
|
+
const fnRoot = getRoot(fn);
|
|
612
|
+
const existing = memoizedRegistry.get(fnRoot);
|
|
613
|
+
if (existing)
|
|
614
|
+
return existing;
|
|
615
|
+
const cacheRoot = {};
|
|
616
|
+
const memoized = markWithRoot((...args) => {
|
|
617
|
+
const localArgs = args; //: Args = maxArgs !== undefined ? (args.slice(0, maxArgs) as Args) : args
|
|
618
|
+
if (localArgs.some((arg) => !(arg && ['object', 'symbol', 'function'].includes(typeof arg))))
|
|
619
|
+
throw new Error('memoize expects non-null object arguments');
|
|
620
|
+
let node = cacheRoot;
|
|
621
|
+
// Note: decorators add `this` as first argument
|
|
622
|
+
for (const arg of localArgs) {
|
|
623
|
+
node = getBranch(node, arg);
|
|
624
|
+
}
|
|
625
|
+
dependant(node, 'memoize');
|
|
626
|
+
if ('result' in node) {
|
|
627
|
+
if (options.onMemoizationDiscrepancy) {
|
|
628
|
+
const wasVerification = options.isVerificationRun;
|
|
629
|
+
options.isVerificationRun = true;
|
|
630
|
+
try {
|
|
631
|
+
const fresh = untracked(() => fn(...localArgs));
|
|
632
|
+
if (!deepCompare(node.result, fresh)) {
|
|
633
|
+
optionCall('onMemoizationDiscrepancy', node.result, fresh, fn, localArgs, 'calculation');
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
finally {
|
|
637
|
+
options.isVerificationRun = wasVerification;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
return node.result;
|
|
641
|
+
}
|
|
642
|
+
// Create memoize internal effect to track dependencies and invalidate cache
|
|
643
|
+
// Use untracked to prevent the effect creation from being affected by parent effects
|
|
644
|
+
node.cleanup = root(() => effect(() => {
|
|
645
|
+
// Execute the function and track its dependencies
|
|
646
|
+
// The function execution will automatically track dependencies on reactive objects
|
|
647
|
+
node.result = fn(...localArgs);
|
|
648
|
+
return () => {
|
|
649
|
+
// When dependencies change, clear the cache and notify consumers
|
|
650
|
+
delete node.result;
|
|
651
|
+
touched1(node, { type: 'invalidate', prop: localArgs }, 'memoize');
|
|
652
|
+
// Lazy memoization: stop the effect so it doesn't re-run immediately.
|
|
653
|
+
// It will be re-created on next access.
|
|
654
|
+
if (node.cleanup) {
|
|
655
|
+
node.cleanup();
|
|
656
|
+
node.cleanup = undefined;
|
|
657
|
+
}
|
|
658
|
+
};
|
|
659
|
+
}, { opaque: true }));
|
|
660
|
+
if (options.onMemoizationDiscrepancy) {
|
|
661
|
+
const wasVerification = options.isVerificationRun;
|
|
662
|
+
options.isVerificationRun = true;
|
|
663
|
+
try {
|
|
664
|
+
const fresh = untracked(() => fn(...localArgs));
|
|
665
|
+
if (!deepCompare(node.result, fresh)) {
|
|
666
|
+
optionCall('onMemoizationDiscrepancy', node.result, fresh, fn, localArgs, 'comparison');
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
finally {
|
|
670
|
+
options.isVerificationRun = wasVerification;
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
return node.result;
|
|
674
|
+
}, fn);
|
|
675
|
+
memoizedRegistry.set(fnRoot, memoized);
|
|
676
|
+
memoizedRegistry.set(memoized, memoized);
|
|
677
|
+
return memoized;
|
|
678
|
+
}
|
|
679
|
+
/**
|
|
680
|
+
* Decorator and function wrapper for memoizing computed values based on reactive dependencies.
|
|
681
|
+
*
|
|
682
|
+
* When used as a decorator on getters or methods, it caches the result and automatically
|
|
683
|
+
* invalidates the cache when reactive dependencies change.
|
|
684
|
+
*
|
|
685
|
+
* When used as a function wrapper, it memoizes based on object arguments (WeakMap-based cache).
|
|
686
|
+
*
|
|
687
|
+
* @example
|
|
688
|
+
* ```typescript
|
|
689
|
+
* class User {
|
|
690
|
+
* @memoize
|
|
691
|
+
* get fullName() {
|
|
692
|
+
* return `${this.firstName} ${this.lastName}`
|
|
693
|
+
* }
|
|
694
|
+
* }
|
|
695
|
+
*
|
|
696
|
+
* // Or as a function wrapper
|
|
697
|
+
* const expensive = memoize((obj: SomeObject) => {
|
|
698
|
+
* return heavyComputation(obj)
|
|
699
|
+
* })
|
|
700
|
+
* ```
|
|
701
|
+
*/
|
|
702
|
+
const memoize = decorator({
|
|
703
|
+
getter(original, target, propertyKey) {
|
|
704
|
+
return function () {
|
|
705
|
+
let wrapper = wrapperRegistry.get(original);
|
|
706
|
+
if (!wrapper) {
|
|
707
|
+
wrapper = markWithRoot(renamed((that) => {
|
|
708
|
+
return original.call(that);
|
|
709
|
+
}, `${String(target?.constructor?.name ?? target?.name ?? 'Object')}.${String(propertyKey)}`), {
|
|
710
|
+
method: original,
|
|
711
|
+
propertyKey,
|
|
712
|
+
...(original[rootFunction]
|
|
713
|
+
? { [rootFunction]: original[rootFunction] }
|
|
714
|
+
: {}),
|
|
715
|
+
});
|
|
716
|
+
wrapperRegistry.set(original, wrapper);
|
|
717
|
+
}
|
|
718
|
+
const memoized = memoizeFunction(wrapper);
|
|
719
|
+
return memoized(this);
|
|
720
|
+
};
|
|
721
|
+
},
|
|
722
|
+
method(original, target, name) {
|
|
723
|
+
return function (...args) {
|
|
724
|
+
let wrapper = wrapperRegistry.get(original);
|
|
725
|
+
if (!wrapper) {
|
|
726
|
+
wrapper = markWithRoot(renamed((that, ...args) => {
|
|
727
|
+
return original.call(that, ...args);
|
|
728
|
+
}, `${String(target?.constructor?.name ?? target?.name ?? 'Object')}.${String(name)}`), {
|
|
729
|
+
method: original,
|
|
730
|
+
propertyKey: name,
|
|
731
|
+
...(original[rootFunction]
|
|
732
|
+
? { [rootFunction]: original[rootFunction] }
|
|
733
|
+
: {}),
|
|
734
|
+
});
|
|
735
|
+
wrapperRegistry.set(original, wrapper);
|
|
736
|
+
}
|
|
737
|
+
const memoized = memoizeFunction(wrapper);
|
|
738
|
+
return memoized(this, ...args);
|
|
739
|
+
};
|
|
740
|
+
},
|
|
741
|
+
default: memoizeFunction,
|
|
742
|
+
});
|
|
743
|
+
|
|
744
|
+
function attend(source, callback) {
|
|
745
|
+
const enumerate = typeof source === 'function'
|
|
746
|
+
? source
|
|
747
|
+
: Array.isArray(source)
|
|
748
|
+
? () => Array.from({ length: source.length }, (_, i) => i)
|
|
749
|
+
: source instanceof Map
|
|
750
|
+
? () => source.keys()
|
|
751
|
+
: source instanceof Set
|
|
752
|
+
? () => source.values()
|
|
753
|
+
: () => Object.keys(source);
|
|
754
|
+
const keyEffects = new Map();
|
|
755
|
+
const outer = effect(({ ascend }) => {
|
|
756
|
+
const keys = new Set();
|
|
757
|
+
for (const key of enumerate())
|
|
758
|
+
keys.add(key);
|
|
759
|
+
for (const key of keys) {
|
|
760
|
+
if (keyEffects.has(key))
|
|
761
|
+
continue;
|
|
762
|
+
keyEffects.set(key, ascend(() => effect(() => callback(key))));
|
|
763
|
+
}
|
|
764
|
+
for (const key of Array.from(keyEffects.keys())) {
|
|
765
|
+
if (!keys.has(key)) {
|
|
766
|
+
keyEffects.get(key)();
|
|
767
|
+
keyEffects.delete(key);
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
});
|
|
771
|
+
return () => {
|
|
772
|
+
outer();
|
|
773
|
+
for (const stop of keyEffects.values())
|
|
774
|
+
stop();
|
|
775
|
+
keyEffects.clear();
|
|
776
|
+
};
|
|
777
|
+
}
|
|
778
|
+
/**
|
|
779
|
+
* Perform a reactive scan over an array of items.
|
|
780
|
+
*
|
|
781
|
+
* This implementation is highly optimized for performance and fine-grained reactivity:
|
|
782
|
+
* - **Incremental Updates**: Changes to an item only trigger re-computation from that
|
|
783
|
+
* point onwards in the result chain.
|
|
784
|
+
* - **Move Optimization**: If items are moved within the array, their accumulated
|
|
785
|
+
* values are reused as long as their predecessor remains the same.
|
|
786
|
+
* - **Duplicate Support**: Correctly handles multiple occurrences of the same object
|
|
787
|
+
* instance using an internal occurrence tracking mechanism.
|
|
788
|
+
* - **Memory Efficient**: Uses `WeakMap` for caching intermediates, which are
|
|
789
|
+
* automatically cleared when source items are garbage collected.
|
|
790
|
+
*
|
|
791
|
+
* @example
|
|
792
|
+
* ```typescript
|
|
793
|
+
* const source = reactive([{ val: 1 }, { val: 2 }, { val: 3 }])
|
|
794
|
+
* const sum = scan(source, (acc, item) => acc + item.val, 0)
|
|
795
|
+
*
|
|
796
|
+
* expect([...sum]).toEqual([1, 3, 6])
|
|
797
|
+
*
|
|
798
|
+
* // Modifying an item only re-computes subsequent sums
|
|
799
|
+
* source[1].val = 10
|
|
800
|
+
* expect([...sum]).toEqual([1, 11, 14])
|
|
801
|
+
* ```
|
|
802
|
+
*
|
|
803
|
+
* @param source The source array of objects (will be made reactive)
|
|
804
|
+
* @param callback The accumulator function called with (accumulator, currentItem)
|
|
805
|
+
* @param initialValue The starting value for the accumulation
|
|
806
|
+
* @returns A reactive array of accumulated values, with a [cleanup] property to stop the tracking
|
|
807
|
+
*/
|
|
808
|
+
function scan(source, callback, initialValue) {
|
|
809
|
+
const observedSource = reactive(source);
|
|
810
|
+
const result = reactive([]);
|
|
811
|
+
// Track effects for each index to dispose them when the array shrinks
|
|
812
|
+
const indexEffects = new Map();
|
|
813
|
+
// Mapping from index to its current intermediate object
|
|
814
|
+
const indexToIntermediate = reactive([]);
|
|
815
|
+
const intermediaries = new WeakMap();
|
|
816
|
+
let Intermediate = (() => {
|
|
817
|
+
var _a;
|
|
818
|
+
let _instanceExtraInitializers = [];
|
|
819
|
+
let _get_acc_decorators;
|
|
820
|
+
return _a = class Intermediate {
|
|
821
|
+
constructor(val, prev) {
|
|
822
|
+
this.val = (__runInitializers(this, _instanceExtraInitializers), val);
|
|
823
|
+
this.prev = prev;
|
|
824
|
+
}
|
|
825
|
+
get acc() {
|
|
826
|
+
const prevAcc = this.prev ? this.prev.acc : initialValue;
|
|
827
|
+
return callback(prevAcc, this.val);
|
|
828
|
+
}
|
|
829
|
+
},
|
|
830
|
+
(() => {
|
|
831
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
832
|
+
_get_acc_decorators = [memoize];
|
|
833
|
+
__esDecorate(_a, null, _get_acc_decorators, { kind: "getter", name: "acc", static: false, private: false, access: { has: obj => "acc" in obj, get: obj => obj.acc }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
834
|
+
if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
835
|
+
})(),
|
|
836
|
+
_a;
|
|
837
|
+
})();
|
|
838
|
+
function disposeIndex(index) {
|
|
839
|
+
const stop = indexEffects.get(index);
|
|
840
|
+
if (stop) {
|
|
841
|
+
stop();
|
|
842
|
+
indexEffects.delete(index);
|
|
843
|
+
untracked(() => {
|
|
844
|
+
Reflect.deleteProperty(indexToIntermediate, index);
|
|
845
|
+
Reflect.deleteProperty(result, index);
|
|
846
|
+
});
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
const mainEffect = effect(function scanMainEffect({ ascend }) {
|
|
850
|
+
const length = observedSource.length;
|
|
851
|
+
const occurrenceCount = new Map();
|
|
852
|
+
let prev;
|
|
853
|
+
for (let i = 0; i < length; i++) {
|
|
854
|
+
const val = FoolProof.get(observedSource, i, observedSource);
|
|
855
|
+
if (!(val && (typeof val === 'object' || typeof val === 'function' || typeof val === 'symbol'))) {
|
|
856
|
+
throw new Error('scan: items must be objects (WeakKey) for intermediate caching');
|
|
857
|
+
}
|
|
858
|
+
const count = occurrenceCount.get(val) ?? 0;
|
|
859
|
+
occurrenceCount.set(val, count + 1);
|
|
860
|
+
let list = intermediaries.get(val);
|
|
861
|
+
if (!list) {
|
|
862
|
+
list = [];
|
|
863
|
+
intermediaries.set(val, list);
|
|
864
|
+
}
|
|
865
|
+
let intermediate = list[count];
|
|
866
|
+
if (!intermediate) {
|
|
867
|
+
intermediate = reactive(new Intermediate(val, prev));
|
|
868
|
+
list[count] = intermediate;
|
|
869
|
+
}
|
|
870
|
+
else {
|
|
871
|
+
// Update the link.
|
|
872
|
+
if (untracked(() => intermediate.prev) !== prev) {
|
|
873
|
+
intermediate.prev = prev;
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
// Update the reactive mapping for this index
|
|
877
|
+
if (indexToIntermediate[i] !== intermediate) {
|
|
878
|
+
indexToIntermediate[i] = intermediate;
|
|
879
|
+
}
|
|
880
|
+
// If we don't have an effect for this index yet, create one
|
|
881
|
+
if (!indexEffects.has(i)) {
|
|
882
|
+
ascend(() => {
|
|
883
|
+
const index = i;
|
|
884
|
+
const stop = effect(function scanIndexSyncEffect() {
|
|
885
|
+
const inter = indexToIntermediate[index];
|
|
886
|
+
if (inter) {
|
|
887
|
+
const accValue = inter.acc;
|
|
888
|
+
untracked(() => {
|
|
889
|
+
result[index] = accValue;
|
|
890
|
+
});
|
|
891
|
+
}
|
|
892
|
+
});
|
|
893
|
+
indexEffects.set(index, stop);
|
|
894
|
+
});
|
|
895
|
+
}
|
|
896
|
+
prev = intermediate;
|
|
897
|
+
}
|
|
898
|
+
// Cleanup trailing indices
|
|
899
|
+
for (const index of Array.from(indexEffects.keys())) {
|
|
900
|
+
if (index >= length)
|
|
901
|
+
disposeIndex(index);
|
|
902
|
+
}
|
|
903
|
+
// Ensure result length matches source length
|
|
904
|
+
untracked(() => {
|
|
905
|
+
if (result.length !== length) {
|
|
906
|
+
FoolProof.set(result, 'length', length, result);
|
|
907
|
+
}
|
|
908
|
+
});
|
|
909
|
+
});
|
|
910
|
+
return cleanedBy(result, (() => {
|
|
911
|
+
mainEffect();
|
|
912
|
+
for (const stop of indexEffects.values())
|
|
913
|
+
stop();
|
|
914
|
+
indexEffects.clear();
|
|
915
|
+
}));
|
|
916
|
+
}
|
|
917
|
+
function lift(cb) {
|
|
918
|
+
let result;
|
|
919
|
+
const liftCleanup = effect((access) => {
|
|
920
|
+
const source = cb(access);
|
|
921
|
+
if (!source || typeof source !== 'object')
|
|
922
|
+
throw new Error('lift callback must return an array or object');
|
|
923
|
+
if (!result) {
|
|
924
|
+
if (Array.isArray(source)) {
|
|
925
|
+
result = reactive([]);
|
|
926
|
+
}
|
|
927
|
+
else {
|
|
928
|
+
result = reactive({});
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
if (Array.isArray(source) && Array.isArray(result)) {
|
|
932
|
+
if (result.length !== source.length)
|
|
933
|
+
result.length = source.length;
|
|
934
|
+
for (let i = 0; i < source.length; i++)
|
|
935
|
+
if (result[i] !== source[i])
|
|
936
|
+
result[i] = source[i];
|
|
937
|
+
}
|
|
938
|
+
else if (!Array.isArray(source) && !Array.isArray(result)) {
|
|
939
|
+
Object.assign(result, source);
|
|
940
|
+
for (const key of Object.keys(result))
|
|
941
|
+
if (!(key in source))
|
|
942
|
+
delete result[key];
|
|
943
|
+
}
|
|
944
|
+
else {
|
|
945
|
+
throw new Error('lift callback must return the same type as the previous result');
|
|
946
|
+
}
|
|
947
|
+
});
|
|
948
|
+
return cleanedBy(result, liftCleanup);
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
function isObject(value) {
|
|
952
|
+
return typeof value === 'object' && value !== null;
|
|
953
|
+
}
|
|
954
|
+
/**
|
|
955
|
+
* Deep watch an object and all its nested properties
|
|
956
|
+
* @param target - The object to watch deeply
|
|
957
|
+
* @param callback - The callback to call when any nested property changes
|
|
958
|
+
* @param options - Options for the deep watch
|
|
959
|
+
* @returns A cleanup function to stop watching
|
|
960
|
+
*/
|
|
961
|
+
/**
|
|
962
|
+
* Sets up deep watching for an object, tracking all nested property changes
|
|
963
|
+
* @param target - The object to watch
|
|
964
|
+
* @param callback - The callback to call when changes occur
|
|
965
|
+
* @param options - Options for deep watching
|
|
966
|
+
* @returns A cleanup function to stop deep watching
|
|
967
|
+
*/
|
|
968
|
+
function deepWatch(target, callback, { immediate = false } = {}) {
|
|
969
|
+
if (target === null || target === undefined)
|
|
970
|
+
return undefined;
|
|
971
|
+
if (typeof target !== 'object')
|
|
972
|
+
throw new Error('Target of deep watching must be an object');
|
|
973
|
+
// Create a wrapper callback that matches EffectTrigger signature
|
|
974
|
+
const wrappedCallback = markWithRoot((() => callback(target)), callback);
|
|
975
|
+
// Use the existing effect system to register dependencies
|
|
976
|
+
return effect(() => {
|
|
977
|
+
// Mark the target object as having deep watchers
|
|
978
|
+
objectsWithDeepWatchers.add(target);
|
|
979
|
+
// Track which objects this effect is watching for cleanup
|
|
980
|
+
let effectObjects = effectToDeepWatchedObjects.get(wrappedCallback);
|
|
981
|
+
if (!effectObjects) {
|
|
982
|
+
effectObjects = new Set();
|
|
983
|
+
effectToDeepWatchedObjects.set(wrappedCallback, effectObjects);
|
|
984
|
+
}
|
|
985
|
+
effectObjects.add(target);
|
|
986
|
+
// Traverse the object graph and register dependencies
|
|
987
|
+
// This will re-run every time the effect runs, ensuring we catch all changes
|
|
988
|
+
const visited = new WeakSet();
|
|
989
|
+
function traverseAndTrack(obj, depth = 0) {
|
|
990
|
+
// Prevent infinite recursion and excessive depth
|
|
991
|
+
if (!obj || visited.has(obj) || !isObject(obj) || depth > options.maxDeepWatchDepth)
|
|
992
|
+
return;
|
|
993
|
+
// Do not traverse into unreactive objects
|
|
994
|
+
if (isNonReactive(obj))
|
|
995
|
+
return;
|
|
996
|
+
visited.add(obj);
|
|
997
|
+
// Mark this object as having deep watchers
|
|
998
|
+
objectsWithDeepWatchers.add(obj);
|
|
999
|
+
effectObjects.add(obj);
|
|
1000
|
+
// Traverse all properties to register dependencies
|
|
1001
|
+
// unwrap to avoid kicking dependency
|
|
1002
|
+
for (const key in unwrap(obj)) {
|
|
1003
|
+
if (Object.hasOwn(obj, key)) {
|
|
1004
|
+
// Access the property to register dependency
|
|
1005
|
+
const value = obj[key];
|
|
1006
|
+
// Make the value reactive if it's an object
|
|
1007
|
+
const reactiveValue = typeof value === 'object' && value !== null ? reactive(value) : value;
|
|
1008
|
+
traverseAndTrack(reactiveValue, depth + 1);
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
// Also handle array indices and length
|
|
1012
|
+
// biome-ignore lint/suspicious/useIsArray: Check for both native arrays and reactive arrays
|
|
1013
|
+
if (Array.isArray(obj) || obj instanceof Array) {
|
|
1014
|
+
// Access array length to register dependency on length changes
|
|
1015
|
+
const length = obj.length;
|
|
1016
|
+
// Access all current array elements to register dependencies
|
|
1017
|
+
for (let i = 0; i < length; i++) {
|
|
1018
|
+
// Access the array element to register dependency
|
|
1019
|
+
const value = obj[i];
|
|
1020
|
+
// Make the value reactive if it's an object
|
|
1021
|
+
const reactiveValue = typeof value === 'object' && value !== null ? reactive(value) : value;
|
|
1022
|
+
traverseAndTrack(reactiveValue, depth + 1);
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
// Handle Set values (deep watch values only, not keys since Sets don't have separate keys)
|
|
1026
|
+
else if (obj instanceof Set) {
|
|
1027
|
+
// Access all Set values to register dependencies
|
|
1028
|
+
for (const value of obj) {
|
|
1029
|
+
// Make the value reactive if it's an object
|
|
1030
|
+
const reactiveValue = typeof value === 'object' && value !== null ? reactive(value) : value;
|
|
1031
|
+
traverseAndTrack(reactiveValue, depth + 1);
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
// Handle Map values (deep watch values only, not keys)
|
|
1035
|
+
else if (obj instanceof Map) {
|
|
1036
|
+
// Access all Map values to register dependencies
|
|
1037
|
+
for (const [_key, value] of obj) {
|
|
1038
|
+
// Make the value reactive if it's an object
|
|
1039
|
+
const reactiveValue = typeof value === 'object' && value !== null ? reactive(value) : value;
|
|
1040
|
+
traverseAndTrack(reactiveValue, depth + 1);
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
// Note: WeakSet and WeakMap cannot be iterated, so we can't deep watch their contents
|
|
1044
|
+
// They will only trigger when the collection itself is replaced
|
|
1045
|
+
}
|
|
1046
|
+
// Traverse the target object to register all dependencies
|
|
1047
|
+
// This will register dependencies on all current properties and array elements
|
|
1048
|
+
traverseAndTrack(target);
|
|
1049
|
+
// Only call the callback if immediate is true or if it's not the first run
|
|
1050
|
+
if (immediate)
|
|
1051
|
+
callback(target);
|
|
1052
|
+
immediate = true;
|
|
1053
|
+
// Return a cleanup function that properly removes deep watcher tracking
|
|
1054
|
+
return () => {
|
|
1055
|
+
// Get the objects this effect was watching
|
|
1056
|
+
const effectObjects = effectToDeepWatchedObjects.get(wrappedCallback);
|
|
1057
|
+
if (effectObjects) {
|
|
1058
|
+
// Remove deep watcher tracking from all objects this effect was watching
|
|
1059
|
+
for (const obj of effectObjects) {
|
|
1060
|
+
// Check if this object still has other deep watchers
|
|
1061
|
+
const watchers = deepWatchers.get(obj);
|
|
1062
|
+
if (watchers) {
|
|
1063
|
+
// Remove this effect's callback from the watchers
|
|
1064
|
+
watchers.delete(wrappedCallback);
|
|
1065
|
+
// If no more watchers, remove the object from deep watchers tracking
|
|
1066
|
+
if (watchers.size === 0) {
|
|
1067
|
+
deepWatchers.delete(obj);
|
|
1068
|
+
objectsWithDeepWatchers.delete(obj);
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
else {
|
|
1072
|
+
// No watchers found, remove from deep watchers tracking
|
|
1073
|
+
objectsWithDeepWatchers.delete(obj);
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
// Clean up the tracking data
|
|
1077
|
+
effectToDeepWatchedObjects.delete(wrappedCallback);
|
|
1078
|
+
}
|
|
1079
|
+
};
|
|
1080
|
+
});
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
/**
|
|
1084
|
+
* Converts an iterator to a generator that yields reactive values
|
|
1085
|
+
*/
|
|
1086
|
+
function* makeReactiveIterator(iterator) {
|
|
1087
|
+
let result = iterator.next();
|
|
1088
|
+
while (!result.done) {
|
|
1089
|
+
yield reactive(result.value);
|
|
1090
|
+
result = iterator.next();
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
/**
|
|
1094
|
+
* Converts an iterator of key-value pairs to a generator that yields reactive key-value pairs
|
|
1095
|
+
*/
|
|
1096
|
+
function* makeReactiveEntriesIterator(iterator) {
|
|
1097
|
+
let result = iterator.next();
|
|
1098
|
+
while (!result.done) {
|
|
1099
|
+
const [key, value] = result.value;
|
|
1100
|
+
yield [reactive(key), reactive(value)];
|
|
1101
|
+
result = iterator.next();
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
//#region watch
|
|
1106
|
+
const unsetYet = Symbol('unset-yet');
|
|
1107
|
+
function watch(value, //object | ((dep: DependencyAccess) => object),
|
|
1108
|
+
changed, options = {}) {
|
|
1109
|
+
return typeof value === 'function'
|
|
1110
|
+
? watchCallBack(value, changed, options)
|
|
1111
|
+
: typeof value === 'object' && value !== null
|
|
1112
|
+
? watchObject(value, changed, options)
|
|
1113
|
+
: (() => {
|
|
1114
|
+
throw new Error('watch: value must be a function or an object');
|
|
1115
|
+
})();
|
|
1116
|
+
}
|
|
1117
|
+
function watchObject(value, changed, { immediate = false, deep = false } = {}) {
|
|
1118
|
+
if (deep)
|
|
1119
|
+
return deepWatch(value, changed, { immediate });
|
|
1120
|
+
return effect(function watchObjectEffect() {
|
|
1121
|
+
dependant(value);
|
|
1122
|
+
if (immediate)
|
|
1123
|
+
changed(value);
|
|
1124
|
+
immediate = true;
|
|
1125
|
+
});
|
|
1126
|
+
}
|
|
1127
|
+
function watchCallBack(value, changed, { immediate = false, deep = false } = {}) {
|
|
1128
|
+
let oldValue = unsetYet;
|
|
1129
|
+
let deepCleanup;
|
|
1130
|
+
const cbCleanup = effect(markWithRoot(function watchCallBackEffect(access) {
|
|
1131
|
+
const newValue = value(access);
|
|
1132
|
+
if (oldValue !== newValue)
|
|
1133
|
+
if (oldValue === unsetYet) {
|
|
1134
|
+
if (immediate)
|
|
1135
|
+
changed(newValue);
|
|
1136
|
+
}
|
|
1137
|
+
else
|
|
1138
|
+
changed(newValue, oldValue);
|
|
1139
|
+
oldValue = newValue;
|
|
1140
|
+
if (deep) {
|
|
1141
|
+
if (deepCleanup)
|
|
1142
|
+
deepCleanup();
|
|
1143
|
+
deepCleanup = deepWatch(newValue, (value) => changed(value, value));
|
|
1144
|
+
}
|
|
1145
|
+
}, value));
|
|
1146
|
+
return Object.defineProperties(() => {
|
|
1147
|
+
cbCleanup();
|
|
1148
|
+
if (deepCleanup)
|
|
1149
|
+
deepCleanup();
|
|
1150
|
+
}, {
|
|
1151
|
+
[stopped]: { get: () => cbCleanup[stopped] },
|
|
1152
|
+
});
|
|
1153
|
+
}
|
|
1154
|
+
//#endregion
|
|
1155
|
+
//#region nonReactive
|
|
1156
|
+
/**
|
|
1157
|
+
* Mark an object as non-reactive. This object and all its properties will never be made reactive.
|
|
1158
|
+
* @param obj - The object to mark as non-reactive
|
|
1159
|
+
*/
|
|
1160
|
+
function shallowNonReactive(obj) {
|
|
1161
|
+
obj = unwrap(obj);
|
|
1162
|
+
if (isNonReactive(obj))
|
|
1163
|
+
return obj;
|
|
1164
|
+
try {
|
|
1165
|
+
Object.defineProperty(obj, nonReactiveMark, {
|
|
1166
|
+
value: true,
|
|
1167
|
+
writable: false,
|
|
1168
|
+
enumerable: false,
|
|
1169
|
+
configurable: true,
|
|
1170
|
+
});
|
|
1171
|
+
}
|
|
1172
|
+
catch { }
|
|
1173
|
+
if (!(nonReactiveMark in obj))
|
|
1174
|
+
nonReactiveObjects.add(obj);
|
|
1175
|
+
// Finally, not deep
|
|
1176
|
+
//for (const key in obj) deepNonReactive(obj[key])
|
|
1177
|
+
return obj;
|
|
1178
|
+
}
|
|
1179
|
+
function unreactiveApplication(arg1, ...args) {
|
|
1180
|
+
return typeof arg1 === 'object'
|
|
1181
|
+
? shallowNonReactive(arg1)
|
|
1182
|
+
: ((original) => {
|
|
1183
|
+
// Copy the parent's unreactive properties if they exist
|
|
1184
|
+
original.prototype[unreactiveProperties] = new Set(original.prototype[unreactiveProperties] || []);
|
|
1185
|
+
// Add all arguments (including the first one)
|
|
1186
|
+
original.prototype[unreactiveProperties].add(arg1);
|
|
1187
|
+
for (const arg of args)
|
|
1188
|
+
original.prototype[unreactiveProperties].add(arg);
|
|
1189
|
+
return original; // Return the class
|
|
1190
|
+
});
|
|
1191
|
+
}
|
|
1192
|
+
/**
|
|
1193
|
+
* Decorator that marks classes or properties as non-reactive
|
|
1194
|
+
* Prevents objects from being made reactive
|
|
1195
|
+
*/
|
|
1196
|
+
const unreactive = decorator({
|
|
1197
|
+
class(original) {
|
|
1198
|
+
// Called without arguments, mark entire class as non-reactive
|
|
1199
|
+
nonReactiveClass(original);
|
|
1200
|
+
},
|
|
1201
|
+
default: unreactiveApplication,
|
|
1202
|
+
});
|
|
1203
|
+
//#endregion
|
|
1204
|
+
|
|
1205
|
+
// Helper to work around TypeScript limitation: base class expressions cannot reference class type parameters
|
|
1206
|
+
function getRegisterBase() {
|
|
1207
|
+
class RegisterBase extends Indexable(ArrayReadForward, {
|
|
1208
|
+
get(index) {
|
|
1209
|
+
return this[getAt](index);
|
|
1210
|
+
},
|
|
1211
|
+
set(index, value) {
|
|
1212
|
+
this[setAt](index, value);
|
|
1213
|
+
},
|
|
1214
|
+
getLength() {
|
|
1215
|
+
return this.length;
|
|
1216
|
+
},
|
|
1217
|
+
setLength(value) {
|
|
1218
|
+
this.length = value;
|
|
1219
|
+
},
|
|
1220
|
+
}) {
|
|
1221
|
+
toArray() {
|
|
1222
|
+
return Array.from(this);
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
return RegisterBase;
|
|
1226
|
+
}
|
|
1227
|
+
let RegisterClass = (() => {
|
|
1228
|
+
var _RegisterClass_instances, _RegisterClass_keyFn, _RegisterClass_keys, _RegisterClass_values, _RegisterClass_usage, _RegisterClass_valueInfo, _RegisterClass_keyEffects, _RegisterClass_ascend, _RegisterClass_events, _RegisterClass_rekeyValue;
|
|
1229
|
+
let _classDecorators = [unreactive];
|
|
1230
|
+
let _classDescriptor;
|
|
1231
|
+
let _classExtraInitializers = [];
|
|
1232
|
+
let _classThis;
|
|
1233
|
+
let _classSuper = getRegisterBase();
|
|
1234
|
+
_classThis = class extends _classSuper {
|
|
1235
|
+
get [(_RegisterClass_keyFn = new WeakMap(), _RegisterClass_keys = new WeakMap(), _RegisterClass_values = new WeakMap(), _RegisterClass_usage = new WeakMap(), _RegisterClass_valueInfo = new WeakMap(), _RegisterClass_keyEffects = new WeakMap(), _RegisterClass_ascend = new WeakMap(), _RegisterClass_events = new WeakMap(), _RegisterClass_instances = new WeakSet(), forwardArray)]() {
|
|
1236
|
+
return this.toArray();
|
|
1237
|
+
}
|
|
1238
|
+
on(eventOrEvents, cb) {
|
|
1239
|
+
// @ts-expect-error Delegate to Eventful
|
|
1240
|
+
return __classPrivateFieldGet(this, _RegisterClass_events, "f").on(eventOrEvents, cb);
|
|
1241
|
+
}
|
|
1242
|
+
off(eventOrEvents, cb) {
|
|
1243
|
+
// @ts-expect-error Delegate to Eventful
|
|
1244
|
+
__classPrivateFieldGet(this, _RegisterClass_events, "f").off(eventOrEvents, cb);
|
|
1245
|
+
}
|
|
1246
|
+
/**
|
|
1247
|
+
* Register a global hook that receives all events
|
|
1248
|
+
*/
|
|
1249
|
+
hook(cb) {
|
|
1250
|
+
return __classPrivateFieldGet(this, _RegisterClass_events, "f").hook(cb);
|
|
1251
|
+
}
|
|
1252
|
+
constructor(keyFn, initial) {
|
|
1253
|
+
super();
|
|
1254
|
+
_RegisterClass_instances.add(this);
|
|
1255
|
+
_RegisterClass_keyFn.set(this, void 0);
|
|
1256
|
+
_RegisterClass_keys.set(this, void 0);
|
|
1257
|
+
_RegisterClass_values.set(this, void 0);
|
|
1258
|
+
_RegisterClass_usage.set(this, new Map());
|
|
1259
|
+
_RegisterClass_valueInfo.set(this, new Map());
|
|
1260
|
+
_RegisterClass_keyEffects.set(this, new Set());
|
|
1261
|
+
_RegisterClass_ascend.set(this, void 0);
|
|
1262
|
+
_RegisterClass_events.set(this, new Eventful()
|
|
1263
|
+
/**
|
|
1264
|
+
* Register event listeners for CRUD operations
|
|
1265
|
+
*/
|
|
1266
|
+
);
|
|
1267
|
+
/* Moved below initialization */
|
|
1268
|
+
let ascendGet;
|
|
1269
|
+
effect(({ ascend }) => {
|
|
1270
|
+
ascendGet = ascend;
|
|
1271
|
+
});
|
|
1272
|
+
__classPrivateFieldSet(this, _RegisterClass_ascend, ascendGet, "f");
|
|
1273
|
+
if (typeof keyFn !== 'function')
|
|
1274
|
+
throw new Error('Register requires a key function');
|
|
1275
|
+
__classPrivateFieldSet(this, _RegisterClass_keyFn, keyFn, "f");
|
|
1276
|
+
__classPrivateFieldSet(this, _RegisterClass_keys, reactive([]), "f");
|
|
1277
|
+
__classPrivateFieldSet(this, _RegisterClass_values, reactive(new Map()), "f");
|
|
1278
|
+
if (initial)
|
|
1279
|
+
this.push(...initial);
|
|
1280
|
+
}
|
|
1281
|
+
ensureKey(value) {
|
|
1282
|
+
let info = __classPrivateFieldGet(this, _RegisterClass_valueInfo, "f").get(value);
|
|
1283
|
+
if (info)
|
|
1284
|
+
return info.key;
|
|
1285
|
+
info = { key: undefined };
|
|
1286
|
+
__classPrivateFieldGet(this, _RegisterClass_valueInfo, "f").set(value, info);
|
|
1287
|
+
__classPrivateFieldGet(this, _RegisterClass_ascend, "f").call(this, () => {
|
|
1288
|
+
const stop = effect(({ reaction }) => {
|
|
1289
|
+
const nextKey = __classPrivateFieldGet(this, _RegisterClass_keyFn, "f").call(this, value);
|
|
1290
|
+
this.assertValidKey(nextKey);
|
|
1291
|
+
const previousKey = info.key;
|
|
1292
|
+
if (reaction && previousKey !== undefined && !Object.is(nextKey, previousKey))
|
|
1293
|
+
__classPrivateFieldGet(this, _RegisterClass_instances, "m", _RegisterClass_rekeyValue).call(this, value, previousKey, nextKey);
|
|
1294
|
+
info.key = nextKey;
|
|
1295
|
+
});
|
|
1296
|
+
info.stop = stop;
|
|
1297
|
+
__classPrivateFieldGet(this, _RegisterClass_keyEffects, "f").add(stop);
|
|
1298
|
+
});
|
|
1299
|
+
if (info.key === undefined)
|
|
1300
|
+
throw new Error('Register key function must return a property key');
|
|
1301
|
+
return info.key;
|
|
1302
|
+
}
|
|
1303
|
+
assertValidKey(key) {
|
|
1304
|
+
const type = typeof key;
|
|
1305
|
+
if (type !== 'string' && type !== 'number' && type !== 'symbol')
|
|
1306
|
+
throw new Error('Register key function must return a property key');
|
|
1307
|
+
}
|
|
1308
|
+
setKeyValue(key, value, index) {
|
|
1309
|
+
const existing = __classPrivateFieldGet(this, _RegisterClass_values, "f").get(key);
|
|
1310
|
+
if (existing !== undefined && existing !== value) {
|
|
1311
|
+
this.cleanupValue(existing);
|
|
1312
|
+
__classPrivateFieldGet(this, _RegisterClass_values, "f").set(key, value);
|
|
1313
|
+
if (index !== undefined) {
|
|
1314
|
+
__classPrivateFieldGet(this, _RegisterClass_events, "f").emit('update', existing, value, key, index);
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
else {
|
|
1318
|
+
__classPrivateFieldGet(this, _RegisterClass_values, "f").set(key, value);
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
cleanupValue(value) {
|
|
1322
|
+
const info = __classPrivateFieldGet(this, _RegisterClass_valueInfo, "f").get(value);
|
|
1323
|
+
if (!info)
|
|
1324
|
+
return;
|
|
1325
|
+
const stop = info.stop;
|
|
1326
|
+
if (stop) {
|
|
1327
|
+
info.stop = undefined;
|
|
1328
|
+
__classPrivateFieldGet(this, _RegisterClass_keyEffects, "f").delete(stop);
|
|
1329
|
+
stop();
|
|
1330
|
+
}
|
|
1331
|
+
__classPrivateFieldGet(this, _RegisterClass_valueInfo, "f").delete(value);
|
|
1332
|
+
}
|
|
1333
|
+
disposeKeyEffects() {
|
|
1334
|
+
for (const value of Array.from(__classPrivateFieldGet(this, _RegisterClass_valueInfo, "f").keys()))
|
|
1335
|
+
this.cleanupValue(value);
|
|
1336
|
+
__classPrivateFieldGet(this, _RegisterClass_keyEffects, "f").clear();
|
|
1337
|
+
}
|
|
1338
|
+
incrementUsage(key) {
|
|
1339
|
+
const count = __classPrivateFieldGet(this, _RegisterClass_usage, "f").get(key) ?? 0;
|
|
1340
|
+
__classPrivateFieldGet(this, _RegisterClass_usage, "f").set(key, count + 1);
|
|
1341
|
+
}
|
|
1342
|
+
decrementUsage(key, index) {
|
|
1343
|
+
const count = __classPrivateFieldGet(this, _RegisterClass_usage, "f").get(key);
|
|
1344
|
+
if (!count)
|
|
1345
|
+
return;
|
|
1346
|
+
if (count <= 1) {
|
|
1347
|
+
const value = __classPrivateFieldGet(this, _RegisterClass_values, "f").get(key);
|
|
1348
|
+
__classPrivateFieldGet(this, _RegisterClass_usage, "f").delete(key);
|
|
1349
|
+
__classPrivateFieldGet(this, _RegisterClass_values, "f").delete(key);
|
|
1350
|
+
if (value !== undefined) {
|
|
1351
|
+
this.cleanupValue(value);
|
|
1352
|
+
const idx = index ?? __classPrivateFieldGet(this, _RegisterClass_keys, "f").indexOf(key);
|
|
1353
|
+
__classPrivateFieldGet(this, _RegisterClass_events, "f").emit('delete', value, key, idx);
|
|
1354
|
+
}
|
|
1355
|
+
}
|
|
1356
|
+
else {
|
|
1357
|
+
__classPrivateFieldGet(this, _RegisterClass_usage, "f").set(key, count - 1);
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
normalizeIndex(index, allowEnd = false) {
|
|
1361
|
+
const length = this.length;
|
|
1362
|
+
let resolved = index;
|
|
1363
|
+
if (resolved < 0)
|
|
1364
|
+
resolved = Math.max(length + resolved, 0);
|
|
1365
|
+
if (resolved > length) {
|
|
1366
|
+
if (allowEnd)
|
|
1367
|
+
resolved = length;
|
|
1368
|
+
else
|
|
1369
|
+
throw new RangeError('Index out of bounds');
|
|
1370
|
+
}
|
|
1371
|
+
if (!allowEnd && resolved === length)
|
|
1372
|
+
throw new RangeError('Index out of bounds');
|
|
1373
|
+
return resolved;
|
|
1374
|
+
}
|
|
1375
|
+
assignAt(index, key, value) {
|
|
1376
|
+
const oldKey = __classPrivateFieldGet(this, _RegisterClass_keys, "f")[index];
|
|
1377
|
+
if (oldKey !== undefined && Object.is(oldKey, key)) {
|
|
1378
|
+
const oldValue = __classPrivateFieldGet(this, _RegisterClass_values, "f").get(key);
|
|
1379
|
+
this.setKeyValue(key, value);
|
|
1380
|
+
if (oldValue !== undefined && oldValue !== value) {
|
|
1381
|
+
__classPrivateFieldGet(this, _RegisterClass_events, "f").emit('update', oldValue, value, key, index);
|
|
1382
|
+
}
|
|
1383
|
+
return;
|
|
1384
|
+
}
|
|
1385
|
+
if (oldKey !== undefined)
|
|
1386
|
+
this.decrementUsage(oldKey);
|
|
1387
|
+
__classPrivateFieldGet(this, _RegisterClass_keys, "f")[index] = key;
|
|
1388
|
+
this.incrementUsage(key);
|
|
1389
|
+
this.setKeyValue(key, value);
|
|
1390
|
+
__classPrivateFieldGet(this, _RegisterClass_events, "f").emit('add', value, key, index);
|
|
1391
|
+
}
|
|
1392
|
+
insertKeyValue(index, key, value) {
|
|
1393
|
+
__classPrivateFieldGet(this, _RegisterClass_keys, "f").splice(index, 0, key);
|
|
1394
|
+
this.incrementUsage(key);
|
|
1395
|
+
this.setKeyValue(key, value);
|
|
1396
|
+
__classPrivateFieldGet(this, _RegisterClass_events, "f").emit('add', value, key, index);
|
|
1397
|
+
}
|
|
1398
|
+
rebuildFrom(values) {
|
|
1399
|
+
this.disposeKeyEffects();
|
|
1400
|
+
__classPrivateFieldGet(this, _RegisterClass_keys, "f").splice(0, __classPrivateFieldGet(this, _RegisterClass_keys, "f").length);
|
|
1401
|
+
__classPrivateFieldGet(this, _RegisterClass_usage, "f").clear();
|
|
1402
|
+
__classPrivateFieldGet(this, _RegisterClass_values, "f").clear();
|
|
1403
|
+
for (const value of values) {
|
|
1404
|
+
const key = this.ensureKey(value);
|
|
1405
|
+
__classPrivateFieldGet(this, _RegisterClass_keys, "f").push(key);
|
|
1406
|
+
this.incrementUsage(key);
|
|
1407
|
+
__classPrivateFieldGet(this, _RegisterClass_values, "f").set(key, value);
|
|
1408
|
+
}
|
|
1409
|
+
}
|
|
1410
|
+
get length() {
|
|
1411
|
+
return __classPrivateFieldGet(this, _RegisterClass_keys, "f").length;
|
|
1412
|
+
}
|
|
1413
|
+
[(_RegisterClass_rekeyValue = function _RegisterClass_rekeyValue(value, oldKey, newKey) {
|
|
1414
|
+
if (Object.is(oldKey, newKey))
|
|
1415
|
+
return;
|
|
1416
|
+
const existingValue = __classPrivateFieldGet(this, _RegisterClass_values, "f").get(newKey);
|
|
1417
|
+
if (existingValue !== undefined && existingValue !== value)
|
|
1418
|
+
throw new Error(`Register key collision for key ${String(newKey)}`);
|
|
1419
|
+
const count = __classPrivateFieldGet(this, _RegisterClass_usage, "f").get(oldKey);
|
|
1420
|
+
if (!count)
|
|
1421
|
+
return;
|
|
1422
|
+
const existingCount = __classPrivateFieldGet(this, _RegisterClass_usage, "f").get(newKey) ?? 0;
|
|
1423
|
+
this.setKeyValue(newKey, value);
|
|
1424
|
+
let index = -1;
|
|
1425
|
+
for (let i = 0; i < __classPrivateFieldGet(this, _RegisterClass_keys, "f").length; i++)
|
|
1426
|
+
if (Object.is(__classPrivateFieldGet(this, _RegisterClass_keys, "f")[i], oldKey)) {
|
|
1427
|
+
__classPrivateFieldGet(this, _RegisterClass_keys, "f")[i] = newKey;
|
|
1428
|
+
if (index === -1)
|
|
1429
|
+
index = i;
|
|
1430
|
+
}
|
|
1431
|
+
__classPrivateFieldGet(this, _RegisterClass_usage, "f").set(newKey, existingCount + count);
|
|
1432
|
+
__classPrivateFieldGet(this, _RegisterClass_usage, "f").delete(oldKey);
|
|
1433
|
+
__classPrivateFieldGet(this, _RegisterClass_values, "f").delete(oldKey);
|
|
1434
|
+
const updatedInfo = __classPrivateFieldGet(this, _RegisterClass_valueInfo, "f").get(value);
|
|
1435
|
+
if (updatedInfo)
|
|
1436
|
+
updatedInfo.key = newKey;
|
|
1437
|
+
if (index !== -1) {
|
|
1438
|
+
__classPrivateFieldGet(this, _RegisterClass_events, "f").emit('rekey', value, oldKey, newKey, index);
|
|
1439
|
+
}
|
|
1440
|
+
}, getAt)](index) {
|
|
1441
|
+
const key = __classPrivateFieldGet(this, _RegisterClass_keys, "f")[index];
|
|
1442
|
+
return key === undefined ? undefined : __classPrivateFieldGet(this, _RegisterClass_values, "f").get(key);
|
|
1443
|
+
}
|
|
1444
|
+
[setAt](index, value) {
|
|
1445
|
+
const key = this.ensureKey(value);
|
|
1446
|
+
if (index === this.length) {
|
|
1447
|
+
this.insertKeyValue(index, key, value);
|
|
1448
|
+
return;
|
|
1449
|
+
}
|
|
1450
|
+
const normalized = this.normalizeIndex(index);
|
|
1451
|
+
this.assignAt(normalized, key, value);
|
|
1452
|
+
}
|
|
1453
|
+
push(...items) {
|
|
1454
|
+
for (const item of items) {
|
|
1455
|
+
const key = this.ensureKey(item);
|
|
1456
|
+
this.insertKeyValue(this.length, key, item);
|
|
1457
|
+
}
|
|
1458
|
+
return this.length;
|
|
1459
|
+
}
|
|
1460
|
+
pop() {
|
|
1461
|
+
if (!this.length)
|
|
1462
|
+
return undefined;
|
|
1463
|
+
return this.removeAt(this.length - 1);
|
|
1464
|
+
}
|
|
1465
|
+
shift() {
|
|
1466
|
+
if (!this.length)
|
|
1467
|
+
return undefined;
|
|
1468
|
+
return this.removeAt(0);
|
|
1469
|
+
}
|
|
1470
|
+
unshift(...items) {
|
|
1471
|
+
let index = 0;
|
|
1472
|
+
for (const item of items) {
|
|
1473
|
+
const key = this.ensureKey(item);
|
|
1474
|
+
this.insertKeyValue(index++, key, item);
|
|
1475
|
+
}
|
|
1476
|
+
return this.length;
|
|
1477
|
+
}
|
|
1478
|
+
splice(start, deleteCount, ...items) {
|
|
1479
|
+
const normalizedStart = this.normalizeIndex(start, true);
|
|
1480
|
+
const maxDeletions = this.length - normalizedStart;
|
|
1481
|
+
const actualDelete = Math.min(deleteCount === undefined ? maxDeletions : Math.max(deleteCount, 0), maxDeletions);
|
|
1482
|
+
const keysToInsert = [];
|
|
1483
|
+
for (const item of items)
|
|
1484
|
+
keysToInsert.push(this.ensureKey(item));
|
|
1485
|
+
const removedKeys = __classPrivateFieldGet(this, _RegisterClass_keys, "f").splice(normalizedStart, actualDelete, ...keysToInsert);
|
|
1486
|
+
const removedValues = [];
|
|
1487
|
+
for (let i = 0; i < removedKeys.length; i++) {
|
|
1488
|
+
const key = removedKeys[i];
|
|
1489
|
+
if (key === undefined)
|
|
1490
|
+
continue;
|
|
1491
|
+
const value = __classPrivateFieldGet(this, _RegisterClass_values, "f").get(key);
|
|
1492
|
+
this.decrementUsage(key, normalizedStart + i);
|
|
1493
|
+
removedValues.push(value);
|
|
1494
|
+
}
|
|
1495
|
+
for (let i = 0; i < keysToInsert.length; i++) {
|
|
1496
|
+
const key = keysToInsert[i];
|
|
1497
|
+
const value = items[i];
|
|
1498
|
+
this.incrementUsage(key);
|
|
1499
|
+
this.setKeyValue(key, value);
|
|
1500
|
+
}
|
|
1501
|
+
return removedValues;
|
|
1502
|
+
}
|
|
1503
|
+
clear() {
|
|
1504
|
+
__classPrivateFieldGet(this, _RegisterClass_keys, "f").length = 0;
|
|
1505
|
+
__classPrivateFieldGet(this, _RegisterClass_usage, "f").clear();
|
|
1506
|
+
__classPrivateFieldGet(this, _RegisterClass_values, "f").clear();
|
|
1507
|
+
this.disposeKeyEffects();
|
|
1508
|
+
}
|
|
1509
|
+
get(key) {
|
|
1510
|
+
return __classPrivateFieldGet(this, _RegisterClass_values, "f").get(key);
|
|
1511
|
+
}
|
|
1512
|
+
set(key, value) {
|
|
1513
|
+
if (__classPrivateFieldGet(this, _RegisterClass_values, "f").has(key))
|
|
1514
|
+
this.setKeyValue(key, value);
|
|
1515
|
+
}
|
|
1516
|
+
remove(key) {
|
|
1517
|
+
let index = this.indexOfKey(key);
|
|
1518
|
+
while (index !== -1) {
|
|
1519
|
+
this.removeAt(index);
|
|
1520
|
+
index = this.indexOfKey(key);
|
|
1521
|
+
}
|
|
1522
|
+
}
|
|
1523
|
+
removeAt(index) {
|
|
1524
|
+
const [key] = __classPrivateFieldGet(this, _RegisterClass_keys, "f").splice(index, 1);
|
|
1525
|
+
if (key === undefined)
|
|
1526
|
+
return undefined;
|
|
1527
|
+
const value = __classPrivateFieldGet(this, _RegisterClass_values, "f").get(key);
|
|
1528
|
+
this.decrementUsage(key, index);
|
|
1529
|
+
return value;
|
|
1530
|
+
}
|
|
1531
|
+
/**
|
|
1532
|
+
* Keep only the items for which the predicate returns true.
|
|
1533
|
+
* Items for which the predicate returns false are removed.
|
|
1534
|
+
*
|
|
1535
|
+
* The predicate is evaluated once per distinct key; duplicate keys
|
|
1536
|
+
* will follow the same keep/remove decision.
|
|
1537
|
+
*/
|
|
1538
|
+
keep(predicate) {
|
|
1539
|
+
const decisions = new Map();
|
|
1540
|
+
for (const [index, key] of __classPrivateFieldGet(this, _RegisterClass_keys, "f").entries()) {
|
|
1541
|
+
if (decisions.has(key)) {
|
|
1542
|
+
if (!decisions.get(key))
|
|
1543
|
+
this.removeAt(index);
|
|
1544
|
+
continue;
|
|
1545
|
+
}
|
|
1546
|
+
const value = __classPrivateFieldGet(this, _RegisterClass_values, "f").get(key);
|
|
1547
|
+
const shouldKeep = predicate(value);
|
|
1548
|
+
decisions.set(key, shouldKeep);
|
|
1549
|
+
if (!shouldKeep)
|
|
1550
|
+
this.removeAt(index);
|
|
1551
|
+
}
|
|
1552
|
+
}
|
|
1553
|
+
hasKey(key) {
|
|
1554
|
+
return __classPrivateFieldGet(this, _RegisterClass_usage, "f").has(key);
|
|
1555
|
+
}
|
|
1556
|
+
indexOfKey(key) {
|
|
1557
|
+
return __classPrivateFieldGet(this, _RegisterClass_keys, "f").indexOf(key);
|
|
1558
|
+
}
|
|
1559
|
+
mapKeys() {
|
|
1560
|
+
return __classPrivateFieldGet(this, _RegisterClass_values, "f").keys();
|
|
1561
|
+
}
|
|
1562
|
+
update(...values) {
|
|
1563
|
+
for (const value of values) {
|
|
1564
|
+
const key = this.ensureKey(value);
|
|
1565
|
+
if (__classPrivateFieldGet(this, _RegisterClass_values, "f").has(key)) {
|
|
1566
|
+
const index = __classPrivateFieldGet(this, _RegisterClass_keys, "f").indexOf(key);
|
|
1567
|
+
this.setKeyValue(key, value, index);
|
|
1568
|
+
}
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
upsert(insert, ...values) {
|
|
1572
|
+
for (const value of values) {
|
|
1573
|
+
const key = this.ensureKey(value);
|
|
1574
|
+
if (__classPrivateFieldGet(this, _RegisterClass_values, "f").has(key))
|
|
1575
|
+
this.setKeyValue(key, value);
|
|
1576
|
+
else
|
|
1577
|
+
insert(value);
|
|
1578
|
+
}
|
|
1579
|
+
}
|
|
1580
|
+
entries() {
|
|
1581
|
+
const self = this;
|
|
1582
|
+
function* iterator() {
|
|
1583
|
+
for (let i = 0; i < __classPrivateFieldGet(self, _RegisterClass_keys, "f").length; i++) {
|
|
1584
|
+
const val = __classPrivateFieldGet(self, _RegisterClass_values, "f").get(__classPrivateFieldGet(self, _RegisterClass_keys, "f")[i]);
|
|
1585
|
+
if (val !== undefined)
|
|
1586
|
+
yield [i, val];
|
|
1587
|
+
}
|
|
1588
|
+
}
|
|
1589
|
+
return iterator();
|
|
1590
|
+
}
|
|
1591
|
+
[Symbol.iterator]() {
|
|
1592
|
+
const self = this;
|
|
1593
|
+
function* iterator() {
|
|
1594
|
+
for (const key of __classPrivateFieldGet(self, _RegisterClass_keys, "f")) {
|
|
1595
|
+
const value = __classPrivateFieldGet(self, _RegisterClass_values, "f").get(key);
|
|
1596
|
+
if (value !== undefined)
|
|
1597
|
+
yield value;
|
|
1598
|
+
}
|
|
1599
|
+
}
|
|
1600
|
+
return iterator();
|
|
1601
|
+
}
|
|
1602
|
+
toString() {
|
|
1603
|
+
return `[Register length=${this.length}]`;
|
|
1604
|
+
}
|
|
1605
|
+
at(index) {
|
|
1606
|
+
const resolved = index < 0 ? this.length + index : index;
|
|
1607
|
+
if (resolved < 0 || resolved >= this.length)
|
|
1608
|
+
return undefined;
|
|
1609
|
+
return this[getAt](resolved);
|
|
1610
|
+
}
|
|
1611
|
+
reverse() {
|
|
1612
|
+
__classPrivateFieldGet(this, _RegisterClass_keys, "f").reverse();
|
|
1613
|
+
return this;
|
|
1614
|
+
}
|
|
1615
|
+
sort(compareFn) {
|
|
1616
|
+
const fwdCompareFn = compareFn
|
|
1617
|
+
? (a, b) => compareFn(__classPrivateFieldGet(this, _RegisterClass_values, "f").get(a), __classPrivateFieldGet(this, _RegisterClass_values, "f").get(b))
|
|
1618
|
+
: undefined;
|
|
1619
|
+
__classPrivateFieldGet(this, _RegisterClass_keys, "f").sort(fwdCompareFn);
|
|
1620
|
+
return this;
|
|
1621
|
+
}
|
|
1622
|
+
fill(value, start = 0, end = this.length) {
|
|
1623
|
+
const values = this.toArray();
|
|
1624
|
+
values.fill(value, start, end);
|
|
1625
|
+
this.rebuildFrom(values);
|
|
1626
|
+
return this;
|
|
1627
|
+
}
|
|
1628
|
+
copyWithin(target, start, end) {
|
|
1629
|
+
const values = this.toArray();
|
|
1630
|
+
values.copyWithin(target, start, end);
|
|
1631
|
+
this.rebuildFrom(values);
|
|
1632
|
+
return this;
|
|
1633
|
+
}
|
|
1634
|
+
};
|
|
1635
|
+
__setFunctionName(_classThis, "RegisterClass");
|
|
1636
|
+
(() => {
|
|
1637
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
|
|
1638
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
1639
|
+
_classThis = _classDescriptor.value;
|
|
1640
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
1641
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
1642
|
+
})();
|
|
1643
|
+
return _classThis;
|
|
1644
|
+
})();
|
|
1645
|
+
const Register = RegisterClass;
|
|
1646
|
+
/**
|
|
1647
|
+
* Creates a reactive Register - an ordered, array-like collection with stable key-based identity.
|
|
1648
|
+
*
|
|
1649
|
+
* Register combines array semantics (indexable access, ordering, iteration) with Map-like
|
|
1650
|
+
* key-based lookups. Items with the same key share the same value instance, making it ideal
|
|
1651
|
+
* for UI lists keyed by IDs or when you need to preserve identity across reorders.
|
|
1652
|
+
*
|
|
1653
|
+
* @param keyFn - Function that extracts the key from each item
|
|
1654
|
+
* @param initial - Optional initial items to populate the register
|
|
1655
|
+
* @returns A reactive Register instance
|
|
1656
|
+
*
|
|
1657
|
+
* @example
|
|
1658
|
+
* ```typescript
|
|
1659
|
+
* const users = register(
|
|
1660
|
+
* (user: User) => user.id,
|
|
1661
|
+
* [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]
|
|
1662
|
+
* )
|
|
1663
|
+
*
|
|
1664
|
+
* users.push({ id: 3, name: 'Charlie' })
|
|
1665
|
+
* const bob = users.get(2) // Get by key
|
|
1666
|
+
* ```
|
|
1667
|
+
*/
|
|
1668
|
+
function register(keyFn, initial) {
|
|
1669
|
+
return new RegisterClass(keyFn, initial);
|
|
1670
|
+
}
|
|
1671
|
+
|
|
1672
|
+
/**
|
|
1673
|
+
* Maps projection effects (item effects) to their projection context
|
|
1674
|
+
*/
|
|
1675
|
+
const effectProjectionMetadata = new WeakMap();
|
|
1676
|
+
/**
|
|
1677
|
+
* Returns the projection context of the currently running effect, if any.
|
|
1678
|
+
*/
|
|
1679
|
+
function getActiveProjection() {
|
|
1680
|
+
const active = getActiveEffect();
|
|
1681
|
+
return active ? effectProjectionMetadata.get(active) : undefined;
|
|
1682
|
+
}
|
|
1683
|
+
function setActiveProjection(projection) {
|
|
1684
|
+
const active = getActiveEffect();
|
|
1685
|
+
if (active)
|
|
1686
|
+
effectProjectionMetadata.set(active, projection);
|
|
1687
|
+
}
|
|
1688
|
+
function defineAccessValue(access) {
|
|
1689
|
+
Object.defineProperty(access, 'value', {
|
|
1690
|
+
get: access.get,
|
|
1691
|
+
set: access.set,
|
|
1692
|
+
configurable: true,
|
|
1693
|
+
enumerable: true,
|
|
1694
|
+
});
|
|
1695
|
+
}
|
|
1696
|
+
function makeCleanup(target, effectMap, onDispose, metadata) {
|
|
1697
|
+
if (metadata) {
|
|
1698
|
+
Object.defineProperty(target, projectionInfo, {
|
|
1699
|
+
value: metadata,
|
|
1700
|
+
writable: false,
|
|
1701
|
+
enumerable: false,
|
|
1702
|
+
configurable: true,
|
|
1703
|
+
});
|
|
1704
|
+
}
|
|
1705
|
+
return cleanedBy(target, () => {
|
|
1706
|
+
onDispose();
|
|
1707
|
+
for (const stop of effectMap.values())
|
|
1708
|
+
stop?.();
|
|
1709
|
+
effectMap.clear();
|
|
1710
|
+
});
|
|
1711
|
+
}
|
|
1712
|
+
function projectArray(source, apply) {
|
|
1713
|
+
source = reactive(source);
|
|
1714
|
+
const target = reactive([]);
|
|
1715
|
+
const indexEffects = new Map();
|
|
1716
|
+
function normalizeTargetLength(length) {
|
|
1717
|
+
FoolProof.set(target, 'length', length, target);
|
|
1718
|
+
}
|
|
1719
|
+
function disposeIndex(index) {
|
|
1720
|
+
const stopEffect = indexEffects.get(index);
|
|
1721
|
+
if (stopEffect) {
|
|
1722
|
+
indexEffects.delete(index);
|
|
1723
|
+
stopEffect();
|
|
1724
|
+
Reflect.deleteProperty(target, index);
|
|
1725
|
+
}
|
|
1726
|
+
}
|
|
1727
|
+
const parent = getActiveProjection();
|
|
1728
|
+
const depth = parent ? parent.depth + 1 : 0;
|
|
1729
|
+
const cleanupLength = effect(named(`project:${source[Symbol.toStringTag]}.length`, ({ ascend }) => {
|
|
1730
|
+
const length = source.length;
|
|
1731
|
+
normalizeTargetLength(length);
|
|
1732
|
+
const existing = Array.from(indexEffects.keys());
|
|
1733
|
+
for (let i = 0; i < length; i++) {
|
|
1734
|
+
if (indexEffects.has(i))
|
|
1735
|
+
continue;
|
|
1736
|
+
ascend(function projectArrayIndexAscend() {
|
|
1737
|
+
const index = i;
|
|
1738
|
+
const stop = effect(named(`project:${source[Symbol.toStringTag]}[${index}]`, ({ reaction }) => {
|
|
1739
|
+
if (!reaction)
|
|
1740
|
+
setActiveProjection({ source, key: index, target, depth, parent });
|
|
1741
|
+
const previous = untracked(() => target[index]);
|
|
1742
|
+
const accessBase = {
|
|
1743
|
+
key: index,
|
|
1744
|
+
source,
|
|
1745
|
+
get: () => FoolProof.get(source, index, source),
|
|
1746
|
+
set: (value) => FoolProof.set(source, index, value, source),
|
|
1747
|
+
old: previous,
|
|
1748
|
+
};
|
|
1749
|
+
defineAccessValue(accessBase);
|
|
1750
|
+
target[index] = apply(accessBase, target);
|
|
1751
|
+
}));
|
|
1752
|
+
setEffectName(stop, `project[${depth}]:${index}`);
|
|
1753
|
+
indexEffects.set(i, stop);
|
|
1754
|
+
});
|
|
1755
|
+
}
|
|
1756
|
+
for (const index of existing)
|
|
1757
|
+
if (index >= length)
|
|
1758
|
+
disposeIndex(index);
|
|
1759
|
+
}));
|
|
1760
|
+
return makeCleanup(target, indexEffects, () => cleanupLength(), {
|
|
1761
|
+
source,
|
|
1762
|
+
target,
|
|
1763
|
+
apply,
|
|
1764
|
+
depth,
|
|
1765
|
+
parent,
|
|
1766
|
+
});
|
|
1767
|
+
}
|
|
1768
|
+
function projectRegister(source, apply) {
|
|
1769
|
+
source = reactive(source);
|
|
1770
|
+
const rawTarget = new Map();
|
|
1771
|
+
const target = reactive(rawTarget);
|
|
1772
|
+
const keyEffects = new Map();
|
|
1773
|
+
function disposeKey(key) {
|
|
1774
|
+
const stopEffect = keyEffects.get(key);
|
|
1775
|
+
if (stopEffect) {
|
|
1776
|
+
stopEffect();
|
|
1777
|
+
keyEffects.delete(key);
|
|
1778
|
+
target.delete(key);
|
|
1779
|
+
}
|
|
1780
|
+
}
|
|
1781
|
+
const parent = getActiveProjection();
|
|
1782
|
+
const depth = parent ? parent.depth + 1 : 0;
|
|
1783
|
+
const cleanupKeys = effect(function projectRegisterEffect({ ascend }) {
|
|
1784
|
+
const keys = new Set();
|
|
1785
|
+
for (const key of source.mapKeys())
|
|
1786
|
+
keys.add(key);
|
|
1787
|
+
for (const key of keys) {
|
|
1788
|
+
if (keyEffects.has(key))
|
|
1789
|
+
continue;
|
|
1790
|
+
ascend(() => {
|
|
1791
|
+
const stop = effect(function projectRegisterKeyEffect({ reaction }) {
|
|
1792
|
+
if (!reaction)
|
|
1793
|
+
setActiveProjection({ source, key, target, depth, parent });
|
|
1794
|
+
const previous = untracked(() => target.get(key));
|
|
1795
|
+
const accessBase = {
|
|
1796
|
+
key,
|
|
1797
|
+
source: source,
|
|
1798
|
+
get: () => source.get(key),
|
|
1799
|
+
set: (value) => {
|
|
1800
|
+
source.set(key, value);
|
|
1801
|
+
return true;
|
|
1802
|
+
},
|
|
1803
|
+
old: previous,
|
|
1804
|
+
};
|
|
1805
|
+
defineAccessValue(accessBase);
|
|
1806
|
+
const produced = apply(accessBase, target);
|
|
1807
|
+
target.set(key, produced);
|
|
1808
|
+
});
|
|
1809
|
+
setEffectName(stop, `project[${depth}]:${String(key)}`);
|
|
1810
|
+
keyEffects.set(key, stop);
|
|
1811
|
+
});
|
|
1812
|
+
}
|
|
1813
|
+
for (const key of Array.from(keyEffects.keys()))
|
|
1814
|
+
if (!keys.has(key))
|
|
1815
|
+
disposeKey(key);
|
|
1816
|
+
});
|
|
1817
|
+
return makeCleanup(target, keyEffects, () => cleanupKeys(), {
|
|
1818
|
+
source: source,
|
|
1819
|
+
target,
|
|
1820
|
+
apply,
|
|
1821
|
+
depth,
|
|
1822
|
+
parent,
|
|
1823
|
+
});
|
|
1824
|
+
}
|
|
1825
|
+
function projectRecord(source, apply) {
|
|
1826
|
+
source = reactive(source);
|
|
1827
|
+
const target = reactive({});
|
|
1828
|
+
const keyEffects = new Map();
|
|
1829
|
+
function disposeKey(key) {
|
|
1830
|
+
const stopEffect = keyEffects.get(key);
|
|
1831
|
+
if (stopEffect) {
|
|
1832
|
+
stopEffect();
|
|
1833
|
+
keyEffects.delete(key);
|
|
1834
|
+
Reflect.deleteProperty(target, key);
|
|
1835
|
+
}
|
|
1836
|
+
}
|
|
1837
|
+
const parent = getActiveProjection();
|
|
1838
|
+
const depth = parent ? parent.depth + 1 : 0;
|
|
1839
|
+
const cleanupKeys = effect(function projectRecordEffect({ ascend }) {
|
|
1840
|
+
const keys = new Set();
|
|
1841
|
+
for (const key in source)
|
|
1842
|
+
keys.add(key);
|
|
1843
|
+
const observed = Reflect.ownKeys(source);
|
|
1844
|
+
for (const key of observed)
|
|
1845
|
+
keys.add(key);
|
|
1846
|
+
for (const key of keys) {
|
|
1847
|
+
if (keyEffects.has(key))
|
|
1848
|
+
continue;
|
|
1849
|
+
ascend(() => {
|
|
1850
|
+
const stop = effect(function projectRecordKeyEffect({ reaction }) {
|
|
1851
|
+
if (!reaction)
|
|
1852
|
+
setActiveProjection({ source, key, target, depth, parent });
|
|
1853
|
+
const sourceKey = key;
|
|
1854
|
+
const previous = untracked(() => target[key]);
|
|
1855
|
+
const accessBase = {
|
|
1856
|
+
key: sourceKey,
|
|
1857
|
+
source: source,
|
|
1858
|
+
get: () => FoolProof.get(source, sourceKey, source),
|
|
1859
|
+
set: (value) => FoolProof.set(source, sourceKey, value, source),
|
|
1860
|
+
old: previous,
|
|
1861
|
+
};
|
|
1862
|
+
defineAccessValue(accessBase);
|
|
1863
|
+
const produced = apply(accessBase, target);
|
|
1864
|
+
target[sourceKey] = produced;
|
|
1865
|
+
});
|
|
1866
|
+
setEffectName(stop, `project[${depth}]:${String(key)}`);
|
|
1867
|
+
keyEffects.set(key, stop);
|
|
1868
|
+
});
|
|
1869
|
+
}
|
|
1870
|
+
for (const key of Array.from(keyEffects.keys()))
|
|
1871
|
+
if (!keys.has(key))
|
|
1872
|
+
disposeKey(key);
|
|
1873
|
+
});
|
|
1874
|
+
return makeCleanup(target, keyEffects, () => cleanupKeys(), {
|
|
1875
|
+
source: source,
|
|
1876
|
+
target,
|
|
1877
|
+
apply,
|
|
1878
|
+
depth,
|
|
1879
|
+
parent,
|
|
1880
|
+
});
|
|
1881
|
+
}
|
|
1882
|
+
function projectMap(source, apply) {
|
|
1883
|
+
source = reactive(source);
|
|
1884
|
+
const rawTarget = new Map();
|
|
1885
|
+
const target = reactive(rawTarget);
|
|
1886
|
+
const keyEffects = new Map();
|
|
1887
|
+
function disposeKey(key) {
|
|
1888
|
+
const stopEffect = keyEffects.get(key);
|
|
1889
|
+
if (stopEffect) {
|
|
1890
|
+
stopEffect();
|
|
1891
|
+
keyEffects.delete(key);
|
|
1892
|
+
target.delete(key);
|
|
1893
|
+
}
|
|
1894
|
+
}
|
|
1895
|
+
const parent = getActiveProjection();
|
|
1896
|
+
const depth = parent ? parent.depth + 1 : 0;
|
|
1897
|
+
const cleanupKeys = effect(function projectMapEffect({ ascend }) {
|
|
1898
|
+
const keys = new Set();
|
|
1899
|
+
for (const key of source.keys())
|
|
1900
|
+
keys.add(key);
|
|
1901
|
+
for (const key of keys) {
|
|
1902
|
+
if (keyEffects.has(key))
|
|
1903
|
+
continue;
|
|
1904
|
+
ascend(() => {
|
|
1905
|
+
const stop = effect(function projectMapKeyEffect({ reaction }) {
|
|
1906
|
+
if (!reaction)
|
|
1907
|
+
setActiveProjection({ source, key, target, depth, parent });
|
|
1908
|
+
const previous = untracked(() => target.get(key));
|
|
1909
|
+
const accessBase = {
|
|
1910
|
+
key,
|
|
1911
|
+
source: source,
|
|
1912
|
+
get: () => source.get(key),
|
|
1913
|
+
set: (value) => {
|
|
1914
|
+
source.set(key, value);
|
|
1915
|
+
return true;
|
|
1916
|
+
},
|
|
1917
|
+
old: previous,
|
|
1918
|
+
};
|
|
1919
|
+
defineAccessValue(accessBase);
|
|
1920
|
+
const produced = apply(accessBase, target);
|
|
1921
|
+
target.set(key, produced);
|
|
1922
|
+
});
|
|
1923
|
+
setEffectName(stop, `project[${depth}]:${String(key)}`);
|
|
1924
|
+
keyEffects.set(key, stop);
|
|
1925
|
+
});
|
|
1926
|
+
}
|
|
1927
|
+
for (const key of Array.from(keyEffects.keys()))
|
|
1928
|
+
if (!keys.has(key))
|
|
1929
|
+
disposeKey(key);
|
|
1930
|
+
});
|
|
1931
|
+
return makeCleanup(target, keyEffects, () => cleanupKeys(), {
|
|
1932
|
+
source: source,
|
|
1933
|
+
target,
|
|
1934
|
+
apply,
|
|
1935
|
+
depth,
|
|
1936
|
+
parent,
|
|
1937
|
+
});
|
|
1938
|
+
}
|
|
1939
|
+
function projectCore(source, apply) {
|
|
1940
|
+
if (Array.isArray(source))
|
|
1941
|
+
return projectArray(source, apply);
|
|
1942
|
+
if (source instanceof Map)
|
|
1943
|
+
return projectMap(source, apply);
|
|
1944
|
+
if (source instanceof Register)
|
|
1945
|
+
return projectRegister(source, apply);
|
|
1946
|
+
if (source && (source.constructor === Object || source.constructor === undefined))
|
|
1947
|
+
return projectRecord(source, apply);
|
|
1948
|
+
throw new Error('Unsupported source type');
|
|
1949
|
+
}
|
|
1950
|
+
const project = Object.assign(projectCore, {
|
|
1951
|
+
array: projectArray,
|
|
1952
|
+
register: projectRegister,
|
|
1953
|
+
record: projectRecord,
|
|
1954
|
+
map: projectMap,
|
|
1955
|
+
});
|
|
1956
|
+
|
|
1957
|
+
/**
|
|
1958
|
+
* Organizes a source object's properties into a target object using a callback function.
|
|
1959
|
+
* This creates a reactive mapping between source properties and a target object,
|
|
1960
|
+
* automatically handling property additions, updates, and removals.
|
|
1961
|
+
*
|
|
1962
|
+
* @template Source - The type of the source object
|
|
1963
|
+
* @template Target - The type of the target object (defaults to Record<PropertyKey, any>)
|
|
1964
|
+
*
|
|
1965
|
+
* @param {Source} source - The source object to organize
|
|
1966
|
+
* @param {OrganizedCallback<Source, Target>} apply - Callback function that defines how each source property is mapped to the target
|
|
1967
|
+
* @param {Target} [baseTarget={}] - Optional base target object to use (will be made reactive if not already)
|
|
1968
|
+
*
|
|
1969
|
+
* @returns {OrganizedResult<Target>} The target object with cleanup capability
|
|
1970
|
+
*
|
|
1971
|
+
* @example
|
|
1972
|
+
* // Organize user permissions into role-based access
|
|
1973
|
+
* const user = reactive({ isAdmin: true, canEdit: false });
|
|
1974
|
+
* const permissions = organized(
|
|
1975
|
+
* user,
|
|
1976
|
+
* (access, target) => {
|
|
1977
|
+
* if (access.key === 'isAdmin') {
|
|
1978
|
+
* target.hasFullAccess = access.value;
|
|
1979
|
+
* }
|
|
1980
|
+
* target[`can${access.key.charAt(0).toUpperCase() + access.key.slice(1)}`] = access.value;
|
|
1981
|
+
* }
|
|
1982
|
+
* );
|
|
1983
|
+
*
|
|
1984
|
+
* @example
|
|
1985
|
+
* // Transform object structure with cleanup
|
|
1986
|
+
* const source = reactive({ firstName: 'John', lastName: 'Doe' });
|
|
1987
|
+
* const formatted = organized(
|
|
1988
|
+
* source,
|
|
1989
|
+
* (access, target) => {
|
|
1990
|
+
* if (access.key === 'firstName' || access.key === 'lastName') {
|
|
1991
|
+
* target.fullName = `${source.firstName} ${source.lastName}`.trim();
|
|
1992
|
+
* }
|
|
1993
|
+
* }
|
|
1994
|
+
* );
|
|
1995
|
+
*
|
|
1996
|
+
* @example
|
|
1997
|
+
* // Using with cleanup in a component
|
|
1998
|
+
* effect(() => {
|
|
1999
|
+
* const data = fetchData();
|
|
2000
|
+
* const organizedData = organized(data, (access, target) => {
|
|
2001
|
+
* // Transform data
|
|
2002
|
+
* });
|
|
2003
|
+
*
|
|
2004
|
+
* // The cleanup will be called automatically when the effect is disposed
|
|
2005
|
+
* return () => organizedData[cleanup]();
|
|
2006
|
+
* });
|
|
2007
|
+
*/
|
|
2008
|
+
function organized(source, apply, baseTarget = {}) {
|
|
2009
|
+
const observedSource = reactive(source);
|
|
2010
|
+
const target = reactive(baseTarget);
|
|
2011
|
+
const stop = attend(() => {
|
|
2012
|
+
const keys = [];
|
|
2013
|
+
for (const key in observedSource)
|
|
2014
|
+
keys.push(key);
|
|
2015
|
+
return keys;
|
|
2016
|
+
}, (key) => {
|
|
2017
|
+
const sourceKey = key;
|
|
2018
|
+
const accessBase = {
|
|
2019
|
+
key: sourceKey,
|
|
2020
|
+
get: () => FoolProof.get(observedSource, sourceKey, observedSource),
|
|
2021
|
+
set: (value) => FoolProof.set(observedSource, sourceKey, value, observedSource),
|
|
2022
|
+
};
|
|
2023
|
+
Object.defineProperty(accessBase, 'value', {
|
|
2024
|
+
get: accessBase.get,
|
|
2025
|
+
set: accessBase.set,
|
|
2026
|
+
configurable: true,
|
|
2027
|
+
enumerable: true,
|
|
2028
|
+
});
|
|
2029
|
+
return apply(accessBase, target);
|
|
2030
|
+
});
|
|
2031
|
+
return cleanedBy(target, stop);
|
|
2032
|
+
}
|
|
2033
|
+
/**
|
|
2034
|
+
* Organizes a property on a target object
|
|
2035
|
+
* Shortcut for defineProperty/delete with touched signal
|
|
2036
|
+
* @param target - The target object
|
|
2037
|
+
* @param property - The property to organize
|
|
2038
|
+
* @param access - The access object
|
|
2039
|
+
* @returns The property descriptor
|
|
2040
|
+
*/
|
|
2041
|
+
function organize(target, property, access) {
|
|
2042
|
+
Object.defineProperty(target, property, {
|
|
2043
|
+
get: access.get,
|
|
2044
|
+
set: access.set,
|
|
2045
|
+
configurable: true,
|
|
2046
|
+
enumerable: true,
|
|
2047
|
+
});
|
|
2048
|
+
touched1(target, { type: 'set', prop: property }, property);
|
|
2049
|
+
return () => delete target[property];
|
|
2050
|
+
}
|
|
2051
|
+
|
|
2052
|
+
/**
|
|
2053
|
+
* Reactively defines properties on a target object based on a descriptors record.
|
|
2054
|
+
*
|
|
2055
|
+
* It tracks the keys of the descriptors record and defines/updates/removes
|
|
2056
|
+
* properties on the target object as they change.
|
|
2057
|
+
*
|
|
2058
|
+
* @param descriptors - A reactive record of property descriptors.
|
|
2059
|
+
* @param target - The object to define properties on (defaults to a new object).
|
|
2060
|
+
* @returns The target object with reactive property definitions.
|
|
2061
|
+
*/
|
|
2062
|
+
function describe(descriptors, target = Object.create(null)) {
|
|
2063
|
+
descriptors = reactive(descriptors);
|
|
2064
|
+
const stop = attend(() => Reflect.ownKeys(descriptors), (key) => {
|
|
2065
|
+
const desc = descriptors[key];
|
|
2066
|
+
if (desc) {
|
|
2067
|
+
Object.defineProperty(target, key, {
|
|
2068
|
+
enumerable: true,
|
|
2069
|
+
...desc,
|
|
2070
|
+
configurable: true,
|
|
2071
|
+
});
|
|
2072
|
+
touched1(target, { type: 'set', prop: key }, key);
|
|
2073
|
+
}
|
|
2074
|
+
return () => Reflect.deleteProperty(target, key);
|
|
2075
|
+
});
|
|
2076
|
+
return cleanedBy(target, stop);
|
|
2077
|
+
}
|
|
2078
|
+
|
|
2079
|
+
function* index(i, { length = true } = {}) {
|
|
2080
|
+
if (length)
|
|
2081
|
+
yield 'length';
|
|
2082
|
+
yield i;
|
|
2083
|
+
}
|
|
2084
|
+
function* range(a, b, { length = false } = {}) {
|
|
2085
|
+
const start = Math.min(a, b);
|
|
2086
|
+
const end = Math.max(a, b);
|
|
2087
|
+
if (length)
|
|
2088
|
+
yield 'length';
|
|
2089
|
+
for (let i = start; i <= end; i++)
|
|
2090
|
+
yield i;
|
|
2091
|
+
}
|
|
2092
|
+
class Indexer extends Array {
|
|
2093
|
+
get(i) {
|
|
2094
|
+
dependant(this, i);
|
|
2095
|
+
return reactive(this[i]);
|
|
2096
|
+
}
|
|
2097
|
+
// Returns undefined intentionally: signals the proxy handler that notifications
|
|
2098
|
+
// were already dispatched via touched(), preventing double notification
|
|
2099
|
+
set(i, value) {
|
|
2100
|
+
const added = i >= this.length;
|
|
2101
|
+
this[i] = value;
|
|
2102
|
+
touched(this, { type: 'set', prop: i }, index(i, { length: added }));
|
|
2103
|
+
}
|
|
2104
|
+
}
|
|
2105
|
+
const indexLess = { get: FoolProof.get, set: FoolProof.set };
|
|
2106
|
+
Object.assign(FoolProof, {
|
|
2107
|
+
get(obj, prop, receiver) {
|
|
2108
|
+
if (obj instanceof Array && typeof prop === 'string') {
|
|
2109
|
+
const index = parseInt(prop);
|
|
2110
|
+
if (!Number.isNaN(index))
|
|
2111
|
+
return Indexer.prototype.get.call(obj, index);
|
|
2112
|
+
}
|
|
2113
|
+
return indexLess.get(obj, prop, receiver);
|
|
2114
|
+
},
|
|
2115
|
+
set(obj, prop, value, receiver) {
|
|
2116
|
+
if (obj instanceof Array && typeof prop === 'string') {
|
|
2117
|
+
const index = parseInt(prop);
|
|
2118
|
+
if (!Number.isNaN(index))
|
|
2119
|
+
return Indexer.prototype.set.call(obj, index, value);
|
|
2120
|
+
}
|
|
2121
|
+
return indexLess.set(obj, prop, value, receiver);
|
|
2122
|
+
},
|
|
2123
|
+
});
|
|
2124
|
+
class ReactiveArray extends Array {
|
|
2125
|
+
toJSON() {
|
|
2126
|
+
return this;
|
|
2127
|
+
}
|
|
2128
|
+
get length() {
|
|
2129
|
+
dependant(this, 'length');
|
|
2130
|
+
return this.length;
|
|
2131
|
+
}
|
|
2132
|
+
set length(value) {
|
|
2133
|
+
const oldLength = this.length;
|
|
2134
|
+
if (oldLength === value)
|
|
2135
|
+
return;
|
|
2136
|
+
try {
|
|
2137
|
+
this.length = value;
|
|
2138
|
+
}
|
|
2139
|
+
finally {
|
|
2140
|
+
touched(this, { type: 'set', prop: 'length' }, range(oldLength, value, { length: true }));
|
|
2141
|
+
}
|
|
2142
|
+
}
|
|
2143
|
+
// Safe array access with negative indices
|
|
2144
|
+
at(index) {
|
|
2145
|
+
const actualIndex = index < 0 ? this.length + index : index;
|
|
2146
|
+
dependant(this, actualIndex);
|
|
2147
|
+
if (index < 0)
|
|
2148
|
+
dependant(this, 'length');
|
|
2149
|
+
if (actualIndex < 0 || actualIndex >= this.length)
|
|
2150
|
+
return undefined;
|
|
2151
|
+
return reactive(this[actualIndex]);
|
|
2152
|
+
}
|
|
2153
|
+
// Immutable versions of mutator methods
|
|
2154
|
+
toReversed() {
|
|
2155
|
+
dependant(this);
|
|
2156
|
+
return reactive(this.toReversed());
|
|
2157
|
+
}
|
|
2158
|
+
toSorted(compareFn) {
|
|
2159
|
+
dependant(this);
|
|
2160
|
+
return reactive(this.toSorted(compareFn));
|
|
2161
|
+
}
|
|
2162
|
+
toSpliced(start, deleteCount, ...items) {
|
|
2163
|
+
dependant(this);
|
|
2164
|
+
return deleteCount === undefined
|
|
2165
|
+
? this.toSpliced(start)
|
|
2166
|
+
: this.toSpliced(start, deleteCount, ...items);
|
|
2167
|
+
}
|
|
2168
|
+
with(index, value) {
|
|
2169
|
+
dependant(this);
|
|
2170
|
+
return reactive(this.with(index, value));
|
|
2171
|
+
}
|
|
2172
|
+
// Iterator methods with reactivity tracking
|
|
2173
|
+
entries() {
|
|
2174
|
+
dependant(this);
|
|
2175
|
+
return makeReactiveEntriesIterator(this.entries());
|
|
2176
|
+
}
|
|
2177
|
+
keys() {
|
|
2178
|
+
dependant(this, 'length');
|
|
2179
|
+
return this.keys();
|
|
2180
|
+
}
|
|
2181
|
+
values() {
|
|
2182
|
+
dependant(this);
|
|
2183
|
+
return makeReactiveIterator(this.values());
|
|
2184
|
+
}
|
|
2185
|
+
[Symbol.iterator]() {
|
|
2186
|
+
dependant(this);
|
|
2187
|
+
const nativeIterator = this[Symbol.iterator]();
|
|
2188
|
+
return {
|
|
2189
|
+
next() {
|
|
2190
|
+
const result = nativeIterator.next();
|
|
2191
|
+
if (result.done) {
|
|
2192
|
+
return result;
|
|
2193
|
+
}
|
|
2194
|
+
return { value: reactive(result.value), done: false };
|
|
2195
|
+
},
|
|
2196
|
+
[Symbol.iterator]() {
|
|
2197
|
+
return this;
|
|
2198
|
+
},
|
|
2199
|
+
[Symbol.dispose]() { },
|
|
2200
|
+
};
|
|
2201
|
+
}
|
|
2202
|
+
indexOf(searchElement, fromIndex) {
|
|
2203
|
+
const length = this.length;
|
|
2204
|
+
let i = fromIndex === undefined ? 0 : fromIndex;
|
|
2205
|
+
if (i < 0)
|
|
2206
|
+
i = Math.max(length + i, 0);
|
|
2207
|
+
const unwrappedSearch = unwrap(searchElement);
|
|
2208
|
+
for (; i < length; i++) {
|
|
2209
|
+
dependant(this, i);
|
|
2210
|
+
const item = this[i];
|
|
2211
|
+
if (item === searchElement || item === unwrappedSearch || unwrap(item) === unwrappedSearch) {
|
|
2212
|
+
return i;
|
|
2213
|
+
}
|
|
2214
|
+
}
|
|
2215
|
+
dependant(this, 'length');
|
|
2216
|
+
return -1;
|
|
2217
|
+
}
|
|
2218
|
+
lastIndexOf(searchElement, fromIndex) {
|
|
2219
|
+
const length = this.length;
|
|
2220
|
+
let i = fromIndex === undefined ? length - 1 : fromIndex;
|
|
2221
|
+
if (i >= length)
|
|
2222
|
+
i = length - 1;
|
|
2223
|
+
if (i < 0)
|
|
2224
|
+
i = Math.max(length + i, -1); // -1 ensures loop condition i >= 0 works correctly
|
|
2225
|
+
const unwrappedSearch = unwrap(searchElement);
|
|
2226
|
+
for (; i >= 0; i--) {
|
|
2227
|
+
dependant(this, i);
|
|
2228
|
+
const item = this[i];
|
|
2229
|
+
if (item === searchElement || item === unwrappedSearch || unwrap(item) === unwrappedSearch) {
|
|
2230
|
+
return i;
|
|
2231
|
+
}
|
|
2232
|
+
}
|
|
2233
|
+
// If we scanned the whole relevant part and didn't find it, we depend on length
|
|
2234
|
+
// (because adding elements might shift indices or add the element)
|
|
2235
|
+
// Actually for lastIndexOf, if we start from end, length dependency is implicit in the start index calculation?
|
|
2236
|
+
// But if we return -1, it means we didn't find it.
|
|
2237
|
+
// If we push an element, should lastIndexOf update?
|
|
2238
|
+
// Yes, if the new element is the one we are looking for.
|
|
2239
|
+
dependant(this, 'length');
|
|
2240
|
+
return -1;
|
|
2241
|
+
}
|
|
2242
|
+
includes(searchElement, fromIndex) {
|
|
2243
|
+
return this.indexOf(searchElement, fromIndex) !== -1;
|
|
2244
|
+
}
|
|
2245
|
+
find(predicateOrElement, thisArg) {
|
|
2246
|
+
if (typeof predicateOrElement === 'function') {
|
|
2247
|
+
const predicate = predicateOrElement;
|
|
2248
|
+
const length = this.length;
|
|
2249
|
+
for (let i = 0; i < length; i++) {
|
|
2250
|
+
dependant(this, i);
|
|
2251
|
+
const val = reactive(this[i]);
|
|
2252
|
+
if (predicate.call(thisArg, val, i, this)) {
|
|
2253
|
+
return val;
|
|
2254
|
+
}
|
|
2255
|
+
}
|
|
2256
|
+
dependant(this, 'length');
|
|
2257
|
+
return undefined;
|
|
2258
|
+
}
|
|
2259
|
+
const fromIndex = typeof thisArg === 'number' ? thisArg : undefined;
|
|
2260
|
+
const index = this.indexOf(predicateOrElement, fromIndex);
|
|
2261
|
+
if (index === -1)
|
|
2262
|
+
return undefined;
|
|
2263
|
+
return reactive(this[index]);
|
|
2264
|
+
}
|
|
2265
|
+
findIndex(predicateOrElement, thisArg) {
|
|
2266
|
+
if (typeof predicateOrElement === 'function') {
|
|
2267
|
+
const predicate = predicateOrElement;
|
|
2268
|
+
const length = this.length;
|
|
2269
|
+
for (let i = 0; i < length; i++) {
|
|
2270
|
+
dependant(this, i);
|
|
2271
|
+
const val = reactive(this[i]);
|
|
2272
|
+
if (predicate.call(thisArg, val, i, this)) {
|
|
2273
|
+
return i;
|
|
2274
|
+
}
|
|
2275
|
+
}
|
|
2276
|
+
dependant(this, 'length');
|
|
2277
|
+
return -1;
|
|
2278
|
+
}
|
|
2279
|
+
const fromIndex = typeof thisArg === 'number' ? thisArg : undefined;
|
|
2280
|
+
return this.indexOf(predicateOrElement, fromIndex);
|
|
2281
|
+
}
|
|
2282
|
+
flat(depth) {
|
|
2283
|
+
dependant(this);
|
|
2284
|
+
return reactive(depth === undefined ? this.flat() : this.flat(depth));
|
|
2285
|
+
}
|
|
2286
|
+
flatMap(callbackfn, thisArg) {
|
|
2287
|
+
dependant(this);
|
|
2288
|
+
return reactive(this.flatMap((item, index, array) => callbackfn.call(thisArg, reactive(item), index, array), thisArg));
|
|
2289
|
+
}
|
|
2290
|
+
filter(callbackfn, thisArg) {
|
|
2291
|
+
dependant(this);
|
|
2292
|
+
return reactive(this.filter((item, index, array) => callbackfn(reactive(item), index, array), thisArg));
|
|
2293
|
+
}
|
|
2294
|
+
map(callbackfn, thisArg) {
|
|
2295
|
+
dependant(this);
|
|
2296
|
+
return reactive(this.map((item, index, array) => callbackfn(reactive(item), index, array), thisArg));
|
|
2297
|
+
}
|
|
2298
|
+
reduce(callbackfn, initialValue) {
|
|
2299
|
+
dependant(this);
|
|
2300
|
+
const result = initialValue === undefined
|
|
2301
|
+
? this.reduce(callbackfn)
|
|
2302
|
+
: this.reduce(callbackfn, initialValue);
|
|
2303
|
+
return reactive(result);
|
|
2304
|
+
}
|
|
2305
|
+
reduceRight(callbackfn, initialValue) {
|
|
2306
|
+
dependant(this);
|
|
2307
|
+
const result = initialValue !== undefined
|
|
2308
|
+
? this.reduceRight(callbackfn, initialValue)
|
|
2309
|
+
: this.reduceRight(callbackfn);
|
|
2310
|
+
return reactive(result);
|
|
2311
|
+
}
|
|
2312
|
+
slice(start, end) {
|
|
2313
|
+
for (const i of range(start || 0, end || this.length - 1))
|
|
2314
|
+
dependant(this, i);
|
|
2315
|
+
return start === undefined
|
|
2316
|
+
? this.slice()
|
|
2317
|
+
: end === undefined
|
|
2318
|
+
? this.slice(start)
|
|
2319
|
+
: this.slice(start, end);
|
|
2320
|
+
}
|
|
2321
|
+
concat(...items) {
|
|
2322
|
+
dependant(this);
|
|
2323
|
+
return reactive(this.concat(...items));
|
|
2324
|
+
}
|
|
2325
|
+
join(separator) {
|
|
2326
|
+
dependant(this);
|
|
2327
|
+
return this.join(separator);
|
|
2328
|
+
}
|
|
2329
|
+
forEach(callbackfn, thisArg) {
|
|
2330
|
+
dependant(this);
|
|
2331
|
+
this.forEach((value, index, array) => {
|
|
2332
|
+
callbackfn.call(thisArg, reactive(value), index, array);
|
|
2333
|
+
});
|
|
2334
|
+
}
|
|
2335
|
+
every(callbackfn, thisArg) {
|
|
2336
|
+
const length = this.length;
|
|
2337
|
+
for (let i = 0; i < length; i++) {
|
|
2338
|
+
dependant(this, i);
|
|
2339
|
+
if (!callbackfn.call(thisArg, reactive(this[i]), i, this)) {
|
|
2340
|
+
return false;
|
|
2341
|
+
}
|
|
2342
|
+
}
|
|
2343
|
+
dependant(this, 'length');
|
|
2344
|
+
return true;
|
|
2345
|
+
}
|
|
2346
|
+
some(callbackfn, thisArg) {
|
|
2347
|
+
const length = this.length;
|
|
2348
|
+
for (let i = 0; i < length; i++) {
|
|
2349
|
+
dependant(this, i);
|
|
2350
|
+
if (callbackfn.call(thisArg, reactive(this[i]), i, this)) {
|
|
2351
|
+
return true;
|
|
2352
|
+
}
|
|
2353
|
+
}
|
|
2354
|
+
dependant(this, 'length');
|
|
2355
|
+
return false;
|
|
2356
|
+
}
|
|
2357
|
+
// Side-effectful
|
|
2358
|
+
push(...items) {
|
|
2359
|
+
const oldLength = this.length;
|
|
2360
|
+
try {
|
|
2361
|
+
return this.push(...items);
|
|
2362
|
+
}
|
|
2363
|
+
finally {
|
|
2364
|
+
touched(this, { type: 'bunch', method: 'push' }, range(oldLength, oldLength + items.length - 1, { length: true }));
|
|
2365
|
+
}
|
|
2366
|
+
}
|
|
2367
|
+
pop() {
|
|
2368
|
+
if (this.length === 0)
|
|
2369
|
+
return undefined;
|
|
2370
|
+
try {
|
|
2371
|
+
return reactive(this.pop());
|
|
2372
|
+
}
|
|
2373
|
+
finally {
|
|
2374
|
+
touched(this, { type: 'bunch', method: 'pop' }, index(this.length));
|
|
2375
|
+
}
|
|
2376
|
+
}
|
|
2377
|
+
shift() {
|
|
2378
|
+
if (this.length === 0)
|
|
2379
|
+
return undefined;
|
|
2380
|
+
try {
|
|
2381
|
+
return reactive(this.shift());
|
|
2382
|
+
}
|
|
2383
|
+
finally {
|
|
2384
|
+
touched(this, { type: 'bunch', method: 'shift' }, range(0, this.length + 1, { length: true }));
|
|
2385
|
+
}
|
|
2386
|
+
}
|
|
2387
|
+
unshift(...items) {
|
|
2388
|
+
try {
|
|
2389
|
+
return this.unshift(...items);
|
|
2390
|
+
}
|
|
2391
|
+
finally {
|
|
2392
|
+
touched(this, { type: 'bunch', method: 'unshift' }, range(0, this.length - items.length, { length: true }));
|
|
2393
|
+
}
|
|
2394
|
+
}
|
|
2395
|
+
splice(start, deleteCount, ...items) {
|
|
2396
|
+
const oldLength = this.length;
|
|
2397
|
+
// Normalize start index
|
|
2398
|
+
let actualStart = start;
|
|
2399
|
+
if (actualStart < 0)
|
|
2400
|
+
actualStart = Math.max(oldLength + actualStart, 0);
|
|
2401
|
+
else
|
|
2402
|
+
actualStart = Math.min(actualStart, oldLength);
|
|
2403
|
+
// Normalize deleteCount
|
|
2404
|
+
let actualDeleteCount = deleteCount;
|
|
2405
|
+
if (actualDeleteCount === undefined) {
|
|
2406
|
+
actualDeleteCount = oldLength - actualStart;
|
|
2407
|
+
}
|
|
2408
|
+
else {
|
|
2409
|
+
actualDeleteCount = Math.max(0, Math.min(actualDeleteCount, oldLength - actualStart));
|
|
2410
|
+
}
|
|
2411
|
+
try {
|
|
2412
|
+
if (deleteCount === undefined)
|
|
2413
|
+
return reactive(this.splice(start));
|
|
2414
|
+
return reactive(this.splice(start, deleteCount, ...items));
|
|
2415
|
+
}
|
|
2416
|
+
finally {
|
|
2417
|
+
touched(this, { type: 'bunch', method: 'splice' }, actualDeleteCount === items.length
|
|
2418
|
+
? range(actualStart, actualStart + actualDeleteCount - 1)
|
|
2419
|
+
: range(actualStart, oldLength + Math.max(items.length - actualDeleteCount, 0), {
|
|
2420
|
+
length: true,
|
|
2421
|
+
}));
|
|
2422
|
+
}
|
|
2423
|
+
}
|
|
2424
|
+
reverse() {
|
|
2425
|
+
try {
|
|
2426
|
+
return this.reverse();
|
|
2427
|
+
}
|
|
2428
|
+
finally {
|
|
2429
|
+
touched(this, { type: 'bunch', method: 'reverse' }, range(0, this.length - 1));
|
|
2430
|
+
}
|
|
2431
|
+
}
|
|
2432
|
+
sort(compareFn) {
|
|
2433
|
+
compareFn = compareFn || ((a, b) => a.toString().localeCompare(b.toString()));
|
|
2434
|
+
try {
|
|
2435
|
+
return this.sort((a, b) => compareFn(reactive(a), reactive(b)));
|
|
2436
|
+
}
|
|
2437
|
+
finally {
|
|
2438
|
+
touched(this, { type: 'bunch', method: 'sort' }, range(0, this.length - 1));
|
|
2439
|
+
}
|
|
2440
|
+
}
|
|
2441
|
+
fill(value, start, end) {
|
|
2442
|
+
const len = this.length;
|
|
2443
|
+
let k = start === undefined ? 0 : start;
|
|
2444
|
+
if (k < 0)
|
|
2445
|
+
k = Math.max(len + k, 0);
|
|
2446
|
+
else
|
|
2447
|
+
k = Math.min(k, len);
|
|
2448
|
+
let final = end === undefined ? len : end;
|
|
2449
|
+
if (final < 0)
|
|
2450
|
+
final = Math.max(len + final, 0);
|
|
2451
|
+
else
|
|
2452
|
+
final = Math.min(final, len);
|
|
2453
|
+
try {
|
|
2454
|
+
if (start === undefined)
|
|
2455
|
+
return this.fill(value);
|
|
2456
|
+
if (end === undefined)
|
|
2457
|
+
return this.fill(value, start);
|
|
2458
|
+
return this.fill(value, start, end);
|
|
2459
|
+
}
|
|
2460
|
+
finally {
|
|
2461
|
+
if (final > k) {
|
|
2462
|
+
touched(this, { type: 'bunch', method: 'fill' }, range(k, final - 1));
|
|
2463
|
+
}
|
|
2464
|
+
}
|
|
2465
|
+
}
|
|
2466
|
+
copyWithin(target, start, end) {
|
|
2467
|
+
try {
|
|
2468
|
+
if (end === undefined)
|
|
2469
|
+
return this.copyWithin(target, start);
|
|
2470
|
+
return this.copyWithin(target, start, end);
|
|
2471
|
+
}
|
|
2472
|
+
finally {
|
|
2473
|
+
const len = this.length;
|
|
2474
|
+
let to = target;
|
|
2475
|
+
if (to < 0)
|
|
2476
|
+
to = Math.max(len + to, 0);
|
|
2477
|
+
else if (to >= len)
|
|
2478
|
+
to = len;
|
|
2479
|
+
let from = start;
|
|
2480
|
+
if (from < 0)
|
|
2481
|
+
from = Math.max(len + from, 0);
|
|
2482
|
+
else if (from >= len)
|
|
2483
|
+
from = len;
|
|
2484
|
+
let final = end === undefined ? len : end;
|
|
2485
|
+
if (final < 0)
|
|
2486
|
+
final = Math.max(len + final, 0);
|
|
2487
|
+
else if (final >= len)
|
|
2488
|
+
final = len;
|
|
2489
|
+
const count = Math.min(final - from, len - to);
|
|
2490
|
+
if (count > 0) {
|
|
2491
|
+
touched(this, { type: 'bunch', method: 'copyWithin' }, range(to, to + count - 1));
|
|
2492
|
+
}
|
|
2493
|
+
}
|
|
2494
|
+
}
|
|
2495
|
+
}
|
|
2496
|
+
|
|
2497
|
+
/**
|
|
2498
|
+
* Reactive wrapper around JavaScript's WeakMap class
|
|
2499
|
+
* Only tracks individual key operations, no size tracking (WeakMap limitation)
|
|
2500
|
+
*/
|
|
2501
|
+
class ReactiveWeakMap extends WeakMap {
|
|
2502
|
+
// Implement WeakMap interface methods with reactivity
|
|
2503
|
+
delete(key) {
|
|
2504
|
+
const hadKey = this.has(key);
|
|
2505
|
+
const result = this.delete(key);
|
|
2506
|
+
if (hadKey)
|
|
2507
|
+
touched1(contentRef(this), { type: 'del', prop: key }, key);
|
|
2508
|
+
return result;
|
|
2509
|
+
}
|
|
2510
|
+
get(key) {
|
|
2511
|
+
dependant(contentRef(this), key);
|
|
2512
|
+
return reactive(this.get(key));
|
|
2513
|
+
}
|
|
2514
|
+
has(key) {
|
|
2515
|
+
dependant(contentRef(this), key);
|
|
2516
|
+
return this.has(key);
|
|
2517
|
+
}
|
|
2518
|
+
set(key, value) {
|
|
2519
|
+
const hadKey = this.has(key);
|
|
2520
|
+
const oldValue = this.get(key);
|
|
2521
|
+
const reactiveValue = reactive(value);
|
|
2522
|
+
this.set(key, reactiveValue);
|
|
2523
|
+
if (!hadKey || oldValue !== reactiveValue) {
|
|
2524
|
+
notifyPropertyChange(contentRef(this), key, oldValue, reactiveValue, hadKey);
|
|
2525
|
+
}
|
|
2526
|
+
return this;
|
|
2527
|
+
}
|
|
2528
|
+
}
|
|
2529
|
+
/**
|
|
2530
|
+
* Reactive wrapper around JavaScript's Map class
|
|
2531
|
+
* Tracks size changes, individual key operations, and collection-wide operations
|
|
2532
|
+
*/
|
|
2533
|
+
class ReactiveMap extends Map {
|
|
2534
|
+
// Implement Map interface methods with reactivity
|
|
2535
|
+
get size() {
|
|
2536
|
+
dependant(this, 'size'); // The ReactiveMap instance still goes through proxy
|
|
2537
|
+
return this.size;
|
|
2538
|
+
}
|
|
2539
|
+
clear() {
|
|
2540
|
+
const hadEntries = this.size > 0;
|
|
2541
|
+
this.clear();
|
|
2542
|
+
if (hadEntries) {
|
|
2543
|
+
const evolution = { type: 'bunch', method: 'clear' };
|
|
2544
|
+
// Clear triggers all effects since all keys are affected
|
|
2545
|
+
touched1(this, evolution, 'size');
|
|
2546
|
+
touched(contentRef(this), evolution);
|
|
2547
|
+
}
|
|
2548
|
+
}
|
|
2549
|
+
entries() {
|
|
2550
|
+
dependant(contentRef(this));
|
|
2551
|
+
return makeReactiveEntriesIterator(this.entries());
|
|
2552
|
+
}
|
|
2553
|
+
forEach(callbackfn, thisArg) {
|
|
2554
|
+
dependant(contentRef(this));
|
|
2555
|
+
this.forEach(callbackfn, thisArg);
|
|
2556
|
+
}
|
|
2557
|
+
keys() {
|
|
2558
|
+
dependant(contentRef(this));
|
|
2559
|
+
return this.keys();
|
|
2560
|
+
}
|
|
2561
|
+
values() {
|
|
2562
|
+
dependant(contentRef(this));
|
|
2563
|
+
return makeReactiveIterator(this.values());
|
|
2564
|
+
}
|
|
2565
|
+
[Symbol.iterator]() {
|
|
2566
|
+
dependant(contentRef(this));
|
|
2567
|
+
const it = Map.prototype[Symbol.iterator].call(this);
|
|
2568
|
+
const nativeNext = it.next.bind(it);
|
|
2569
|
+
it.next = () => {
|
|
2570
|
+
const result = nativeNext();
|
|
2571
|
+
if (result.done)
|
|
2572
|
+
return result;
|
|
2573
|
+
const [key, value] = result.value;
|
|
2574
|
+
return { value: [reactive(key), reactive(value)], done: false };
|
|
2575
|
+
};
|
|
2576
|
+
return it;
|
|
2577
|
+
}
|
|
2578
|
+
// Implement Map methods with reactivity
|
|
2579
|
+
delete(key) {
|
|
2580
|
+
const hadKey = this.has(key);
|
|
2581
|
+
const result = this.delete(key);
|
|
2582
|
+
if (hadKey) {
|
|
2583
|
+
const evolution = { type: 'del', prop: key };
|
|
2584
|
+
touched1(contentRef(this), evolution, key);
|
|
2585
|
+
touched1(this, evolution, 'size');
|
|
2586
|
+
}
|
|
2587
|
+
return result;
|
|
2588
|
+
}
|
|
2589
|
+
get(key) {
|
|
2590
|
+
dependant(contentRef(this), key);
|
|
2591
|
+
return reactive(this.get(key));
|
|
2592
|
+
}
|
|
2593
|
+
has(key) {
|
|
2594
|
+
dependant(contentRef(this), key);
|
|
2595
|
+
return this.has(key);
|
|
2596
|
+
}
|
|
2597
|
+
set(key, value) {
|
|
2598
|
+
const hadKey = this.has(key);
|
|
2599
|
+
const oldValue = this.get(key);
|
|
2600
|
+
const reactiveValue = reactive(value);
|
|
2601
|
+
this.set(key, reactiveValue);
|
|
2602
|
+
if (!hadKey || oldValue !== reactiveValue) {
|
|
2603
|
+
notifyPropertyChange(contentRef(this), key, oldValue, reactiveValue, hadKey);
|
|
2604
|
+
// Also notify size change for Map (WeakMap doesn't track size)
|
|
2605
|
+
const evolution = { type: hadKey ? 'set' : 'add', prop: key };
|
|
2606
|
+
touched1(this, evolution, 'size');
|
|
2607
|
+
}
|
|
2608
|
+
return this;
|
|
2609
|
+
}
|
|
2610
|
+
}
|
|
2611
|
+
|
|
2612
|
+
/**
|
|
2613
|
+
* Reactive wrapper around JavaScript's WeakSet class
|
|
2614
|
+
* Only tracks individual value operations, no size tracking (WeakSet limitation)
|
|
2615
|
+
*/
|
|
2616
|
+
class ReactiveWeakSet extends WeakSet {
|
|
2617
|
+
add(value) {
|
|
2618
|
+
const had = this.has(value);
|
|
2619
|
+
this.add(value);
|
|
2620
|
+
if (!had) {
|
|
2621
|
+
// touch the specific value and the collection view
|
|
2622
|
+
touched1(contentRef(this), { type: 'add', prop: value }, value);
|
|
2623
|
+
// no size/allProps for WeakSet
|
|
2624
|
+
}
|
|
2625
|
+
return this;
|
|
2626
|
+
}
|
|
2627
|
+
delete(value) {
|
|
2628
|
+
const had = this.has(value);
|
|
2629
|
+
const res = this.delete(value);
|
|
2630
|
+
if (had)
|
|
2631
|
+
touched1(contentRef(this), { type: 'del', prop: value }, value);
|
|
2632
|
+
return res;
|
|
2633
|
+
}
|
|
2634
|
+
has(value) {
|
|
2635
|
+
dependant(contentRef(this), value);
|
|
2636
|
+
return this.has(value);
|
|
2637
|
+
}
|
|
2638
|
+
}
|
|
2639
|
+
/**
|
|
2640
|
+
* Reactive wrapper around JavaScript's Set class
|
|
2641
|
+
* Tracks size changes, individual value operations, and collection-wide operations
|
|
2642
|
+
*/
|
|
2643
|
+
class ReactiveSet extends Set {
|
|
2644
|
+
get size() {
|
|
2645
|
+
// size depends on the wrapper instance, like Map counterpart
|
|
2646
|
+
dependant(this, 'size');
|
|
2647
|
+
return this.size;
|
|
2648
|
+
}
|
|
2649
|
+
add(value) {
|
|
2650
|
+
const had = this.has(value);
|
|
2651
|
+
const reactiveValue = reactive(value);
|
|
2652
|
+
this.add(reactiveValue);
|
|
2653
|
+
if (!had) {
|
|
2654
|
+
const evolution = { type: 'add', prop: reactiveValue };
|
|
2655
|
+
// touch for value-specific and aggregate dependencies
|
|
2656
|
+
touched1(contentRef(this), evolution, reactiveValue);
|
|
2657
|
+
touched1(this, evolution, 'size');
|
|
2658
|
+
}
|
|
2659
|
+
return this;
|
|
2660
|
+
}
|
|
2661
|
+
clear() {
|
|
2662
|
+
const hadEntries = this.size > 0;
|
|
2663
|
+
this.clear();
|
|
2664
|
+
if (hadEntries) {
|
|
2665
|
+
const evolution = { type: 'bunch', method: 'clear' };
|
|
2666
|
+
touched1(this, evolution, 'size');
|
|
2667
|
+
touched(contentRef(this), evolution);
|
|
2668
|
+
}
|
|
2669
|
+
}
|
|
2670
|
+
delete(value) {
|
|
2671
|
+
const had = this.has(value);
|
|
2672
|
+
const res = this.delete(value);
|
|
2673
|
+
if (had) {
|
|
2674
|
+
const evolution = { type: 'del', prop: value };
|
|
2675
|
+
touched1(contentRef(this), evolution, value);
|
|
2676
|
+
touched1(this, evolution, 'size');
|
|
2677
|
+
}
|
|
2678
|
+
return res;
|
|
2679
|
+
}
|
|
2680
|
+
has(value) {
|
|
2681
|
+
dependant(contentRef(this), value);
|
|
2682
|
+
return this.has(value);
|
|
2683
|
+
}
|
|
2684
|
+
entries() {
|
|
2685
|
+
dependant(contentRef(this));
|
|
2686
|
+
return makeReactiveEntriesIterator(this.entries());
|
|
2687
|
+
}
|
|
2688
|
+
forEach(callbackfn, thisArg) {
|
|
2689
|
+
dependant(contentRef(this));
|
|
2690
|
+
this.forEach(callbackfn, thisArg);
|
|
2691
|
+
}
|
|
2692
|
+
keys() {
|
|
2693
|
+
dependant(contentRef(this));
|
|
2694
|
+
return makeReactiveIterator(this.keys());
|
|
2695
|
+
}
|
|
2696
|
+
values() {
|
|
2697
|
+
dependant(contentRef(this));
|
|
2698
|
+
return makeReactiveIterator(this.values());
|
|
2699
|
+
}
|
|
2700
|
+
[Symbol.iterator]() {
|
|
2701
|
+
dependant(contentRef(this));
|
|
2702
|
+
const it = Set.prototype[Symbol.iterator].call(this);
|
|
2703
|
+
const nativeNext = it.next.bind(it);
|
|
2704
|
+
it.next = () => {
|
|
2705
|
+
const result = nativeNext();
|
|
2706
|
+
if (result.done)
|
|
2707
|
+
return result;
|
|
2708
|
+
return { value: reactive(result.value), done: false };
|
|
2709
|
+
};
|
|
2710
|
+
return it;
|
|
2711
|
+
}
|
|
2712
|
+
}
|
|
2713
|
+
|
|
2714
|
+
// Register native collection types to use specialized reactive wrappers
|
|
2715
|
+
metaProtos.set(Array, ReactiveArray.prototype);
|
|
2716
|
+
metaProtos.set(Set, ReactiveSet.prototype);
|
|
2717
|
+
metaProtos.set(WeakSet, ReactiveWeakSet.prototype);
|
|
2718
|
+
metaProtos.set(Map, ReactiveMap.prototype);
|
|
2719
|
+
metaProtos.set(WeakMap, ReactiveWeakMap.prototype);
|
|
2720
|
+
/**
|
|
2721
|
+
* Object containing internal reactive system state for debugging and profiling
|
|
2722
|
+
*/
|
|
2723
|
+
const profileInfo = {
|
|
2724
|
+
objectToProxy,
|
|
2725
|
+
proxyToObject,
|
|
2726
|
+
effectToReactiveObjects,
|
|
2727
|
+
watchers,
|
|
2728
|
+
objectParents,
|
|
2729
|
+
objectsWithDeepWatchers,
|
|
2730
|
+
deepWatchers,
|
|
2731
|
+
effectToDeepWatchedObjects,
|
|
2732
|
+
nonReactiveObjects,
|
|
2733
|
+
};
|
|
2734
|
+
|
|
2735
|
+
// In order to avoid async re-entrance, we could use zone.js or something like that.
|
|
2736
|
+
const syncCalculating = [];
|
|
2737
|
+
/**
|
|
2738
|
+
* Decorator that caches the result of a getter method and only recomputes when dependencies change
|
|
2739
|
+
* Prevents circular dependencies and provides automatic cache invalidation
|
|
2740
|
+
*/
|
|
2741
|
+
const cached = decorator({
|
|
2742
|
+
getter(original, _target, propertyKey) {
|
|
2743
|
+
return function () {
|
|
2744
|
+
const alreadyCalculating = syncCalculating.findIndex((c) => c.object === this && c.prop === propertyKey);
|
|
2745
|
+
if (alreadyCalculating > -1)
|
|
2746
|
+
throw new Error(`Circular dependency detected: ${syncCalculating
|
|
2747
|
+
.slice(alreadyCalculating)
|
|
2748
|
+
.map((c) => `${c.object.constructor.name}.${String(c.prop)}`)
|
|
2749
|
+
.join(' -> ')} -> again`);
|
|
2750
|
+
syncCalculating.push({ object: this, prop: propertyKey });
|
|
2751
|
+
try {
|
|
2752
|
+
const rv = original.call(this);
|
|
2753
|
+
cache(this, propertyKey, rv);
|
|
2754
|
+
return rv;
|
|
2755
|
+
}
|
|
2756
|
+
finally {
|
|
2757
|
+
syncCalculating.pop();
|
|
2758
|
+
}
|
|
2759
|
+
};
|
|
2760
|
+
},
|
|
2761
|
+
});
|
|
2762
|
+
/**
|
|
2763
|
+
* Checks if a property is cached (has a cached value)
|
|
2764
|
+
* @param object - The object to check
|
|
2765
|
+
* @param propertyKey - The property key to check
|
|
2766
|
+
* @returns True if the property has a cached value
|
|
2767
|
+
*/
|
|
2768
|
+
function isCached(object, propertyKey) {
|
|
2769
|
+
return !!Object.getOwnPropertyDescriptor(object, propertyKey);
|
|
2770
|
+
}
|
|
2771
|
+
/**
|
|
2772
|
+
* Caches a value for a property on an object
|
|
2773
|
+
* @param object - The object to cache the value on
|
|
2774
|
+
* @param propertyKey - The property key to cache
|
|
2775
|
+
* @param value - The value to cache
|
|
2776
|
+
*/
|
|
2777
|
+
function cache(object, propertyKey, value) {
|
|
2778
|
+
Object.defineProperty(object, propertyKey, { value });
|
|
2779
|
+
}
|
|
2780
|
+
/**
|
|
2781
|
+
* Creates a decorator that modifies property descriptors for specified properties
|
|
2782
|
+
* @param descriptor - The descriptor properties to apply
|
|
2783
|
+
* @returns A class decorator that applies the descriptor to specified properties
|
|
2784
|
+
*/
|
|
2785
|
+
const descriptor = flavored(function descriptor(descriptor) {
|
|
2786
|
+
return (...properties) => (Base) => {
|
|
2787
|
+
return class extends Base {
|
|
2788
|
+
constructor(...args) {
|
|
2789
|
+
super(...args);
|
|
2790
|
+
for (const key of properties) {
|
|
2791
|
+
const existing = Object.getOwnPropertyDescriptor(this, key);
|
|
2792
|
+
Object.defineProperty(this, key, Object.assign(existing || {}, descriptor));
|
|
2793
|
+
}
|
|
2794
|
+
}
|
|
2795
|
+
};
|
|
2796
|
+
};
|
|
2797
|
+
}, {
|
|
2798
|
+
/**
|
|
2799
|
+
* enumerable: true
|
|
2800
|
+
*/
|
|
2801
|
+
get enumerable() {
|
|
2802
|
+
return descriptor({ enumerable: true });
|
|
2803
|
+
},
|
|
2804
|
+
/**
|
|
2805
|
+
* enumerable: false
|
|
2806
|
+
*/
|
|
2807
|
+
get hidden() {
|
|
2808
|
+
return descriptor({ enumerable: false });
|
|
2809
|
+
},
|
|
2810
|
+
/**
|
|
2811
|
+
* configurable: true
|
|
2812
|
+
*/
|
|
2813
|
+
get configurable() {
|
|
2814
|
+
return descriptor({ configurable: true });
|
|
2815
|
+
},
|
|
2816
|
+
/**
|
|
2817
|
+
* configurable: false
|
|
2818
|
+
*/
|
|
2819
|
+
get frozen() {
|
|
2820
|
+
return descriptor({ configurable: false });
|
|
2821
|
+
},
|
|
2822
|
+
/**
|
|
2823
|
+
* writable: true
|
|
2824
|
+
*/
|
|
2825
|
+
get writable() {
|
|
2826
|
+
return descriptor({ writable: true });
|
|
2827
|
+
},
|
|
2828
|
+
/**
|
|
2829
|
+
* writable: false
|
|
2830
|
+
*/
|
|
2831
|
+
get readonly() {
|
|
2832
|
+
return descriptor({ writable: false });
|
|
2833
|
+
},
|
|
2834
|
+
});
|
|
2835
|
+
/**
|
|
2836
|
+
* Decorator that marks methods, properties, or classes as deprecated
|
|
2837
|
+
* Provides warning messages when deprecated items are used
|
|
2838
|
+
*/
|
|
2839
|
+
const deprecated = Object.assign(decorator({
|
|
2840
|
+
method(original, _target, propertyKey) {
|
|
2841
|
+
return function (...args) {
|
|
2842
|
+
deprecated.warn(this, propertyKey);
|
|
2843
|
+
return original.apply(this, args);
|
|
2844
|
+
};
|
|
2845
|
+
},
|
|
2846
|
+
getter(original, _target, propertyKey) {
|
|
2847
|
+
return function () {
|
|
2848
|
+
deprecated.warn(this, propertyKey);
|
|
2849
|
+
return original.call(this);
|
|
2850
|
+
};
|
|
2851
|
+
},
|
|
2852
|
+
setter(original, _target, propertyKey) {
|
|
2853
|
+
return function (value) {
|
|
2854
|
+
deprecated.warn(this, propertyKey);
|
|
2855
|
+
return original.call(this, value);
|
|
2856
|
+
};
|
|
2857
|
+
},
|
|
2858
|
+
class(original) {
|
|
2859
|
+
return class extends original {
|
|
2860
|
+
constructor(...args) {
|
|
2861
|
+
super(...args);
|
|
2862
|
+
deprecated.warn(this, 'constructor');
|
|
2863
|
+
}
|
|
2864
|
+
};
|
|
2865
|
+
},
|
|
2866
|
+
default(message) {
|
|
2867
|
+
return decorator({
|
|
2868
|
+
method(original, _target, propertyKey) {
|
|
2869
|
+
return function (...args) {
|
|
2870
|
+
deprecated.warn(this, propertyKey, message);
|
|
2871
|
+
return original.apply(this, args);
|
|
2872
|
+
};
|
|
2873
|
+
},
|
|
2874
|
+
getter(original, _target, propertyKey) {
|
|
2875
|
+
return function () {
|
|
2876
|
+
deprecated.warn(this, propertyKey, message);
|
|
2877
|
+
return original.call(this);
|
|
2878
|
+
};
|
|
2879
|
+
},
|
|
2880
|
+
setter(original, _target, propertyKey) {
|
|
2881
|
+
return function (value) {
|
|
2882
|
+
deprecated.warn(this, propertyKey, message);
|
|
2883
|
+
return original.call(this, value);
|
|
2884
|
+
};
|
|
2885
|
+
},
|
|
2886
|
+
class(original) {
|
|
2887
|
+
return class extends original {
|
|
2888
|
+
constructor(...args) {
|
|
2889
|
+
super(...args);
|
|
2890
|
+
deprecated.warn(this, 'constructor', message);
|
|
2891
|
+
}
|
|
2892
|
+
};
|
|
2893
|
+
},
|
|
2894
|
+
});
|
|
2895
|
+
},
|
|
2896
|
+
}), {
|
|
2897
|
+
warn: (target, propertyKey, message) => {
|
|
2898
|
+
// biome-ignore lint/suspicious/noConsole: To be overridden
|
|
2899
|
+
console.warn(`${target.constructor.name}.${String(propertyKey)} is deprecated${message ? `: ${message}` : ''}`);
|
|
2900
|
+
},
|
|
2901
|
+
});
|
|
2902
|
+
/**
|
|
2903
|
+
* Creates a debounced method decorator that delays execution until after the delay period has passed
|
|
2904
|
+
* @param delay - The delay in milliseconds
|
|
2905
|
+
* @returns A method decorator that debounces method calls
|
|
2906
|
+
*/
|
|
2907
|
+
function debounce(delay) {
|
|
2908
|
+
return decorator({
|
|
2909
|
+
method(original, _target, _propertyKey) {
|
|
2910
|
+
let timeoutId = null;
|
|
2911
|
+
return function (...args) {
|
|
2912
|
+
// Clear existing timeout
|
|
2913
|
+
if (timeoutId) {
|
|
2914
|
+
clearTimeout(timeoutId);
|
|
2915
|
+
}
|
|
2916
|
+
// Set new timeout
|
|
2917
|
+
timeoutId = setTimeout(() => {
|
|
2918
|
+
original.apply(this, args);
|
|
2919
|
+
timeoutId = null;
|
|
2920
|
+
}, delay);
|
|
2921
|
+
};
|
|
2922
|
+
},
|
|
2923
|
+
});
|
|
2924
|
+
}
|
|
2925
|
+
/**
|
|
2926
|
+
* Creates a throttled method decorator that limits execution to once per delay period
|
|
2927
|
+
* @param delay - The delay in milliseconds
|
|
2928
|
+
* @returns A method decorator that throttles method calls
|
|
2929
|
+
*/
|
|
2930
|
+
function throttle(delay) {
|
|
2931
|
+
return decorator({
|
|
2932
|
+
method(original, _target, _propertyKey) {
|
|
2933
|
+
let lastCallTime = 0;
|
|
2934
|
+
let timeoutId = null;
|
|
2935
|
+
return function (...args) {
|
|
2936
|
+
const now = Date.now();
|
|
2937
|
+
// If enough time has passed since last call, execute immediately
|
|
2938
|
+
if (now - lastCallTime >= delay) {
|
|
2939
|
+
// Clear any pending timeout since we're executing now
|
|
2940
|
+
if (timeoutId) {
|
|
2941
|
+
clearTimeout(timeoutId);
|
|
2942
|
+
timeoutId = null;
|
|
2943
|
+
}
|
|
2944
|
+
lastCallTime = now;
|
|
2945
|
+
return original.apply(this, args);
|
|
2946
|
+
}
|
|
2947
|
+
// Otherwise, schedule execution for when the delay period ends
|
|
2948
|
+
if (!timeoutId) {
|
|
2949
|
+
const remainingTime = delay - (now - lastCallTime);
|
|
2950
|
+
const scheduledArgs = [...args]; // Capture args at scheduling time
|
|
2951
|
+
timeoutId = setTimeout(() => {
|
|
2952
|
+
lastCallTime = Date.now();
|
|
2953
|
+
original.apply(this, scheduledArgs);
|
|
2954
|
+
timeoutId = null;
|
|
2955
|
+
}, remainingTime);
|
|
2956
|
+
}
|
|
2957
|
+
};
|
|
2958
|
+
},
|
|
2959
|
+
});
|
|
2960
|
+
}
|
|
2961
|
+
|
|
2962
|
+
var version$1 = "1.0.9";
|
|
2963
|
+
var pkg = {
|
|
2964
|
+
version: version$1};
|
|
2965
|
+
|
|
2966
|
+
const { version } = pkg;
|
|
2967
|
+
// Singleton verification
|
|
2968
|
+
const GLOBAL_MUTTS_KEY = '__MUTTS_INSTANCE__';
|
|
2969
|
+
const globalScope = (typeof globalThis !== 'undefined'
|
|
2970
|
+
? globalThis
|
|
2971
|
+
: typeof window !== 'undefined'
|
|
2972
|
+
? window
|
|
2973
|
+
: typeof global !== 'undefined'
|
|
2974
|
+
? global
|
|
2975
|
+
: false);
|
|
2976
|
+
if (globalScope) {
|
|
2977
|
+
// Detect the source of this instance safely across different environments
|
|
2978
|
+
let source = 'mutts/index';
|
|
2979
|
+
const viteEval = eval;
|
|
2980
|
+
try {
|
|
2981
|
+
if (typeof __filename !== 'undefined')
|
|
2982
|
+
source = __filename;
|
|
2983
|
+
else {
|
|
2984
|
+
// Using eval to avoid SyntaxError in CJS environments where import.meta is not allowed
|
|
2985
|
+
const meta = viteEval('import.meta');
|
|
2986
|
+
if (meta && meta.url)
|
|
2987
|
+
source = meta.url;
|
|
2988
|
+
}
|
|
2989
|
+
}
|
|
2990
|
+
catch (e) {
|
|
2991
|
+
// Fallback for environments where neither is available or accessible
|
|
2992
|
+
}
|
|
2993
|
+
const currentSourceInfo = {
|
|
2994
|
+
version,
|
|
2995
|
+
source,
|
|
2996
|
+
timestamp: Date.now(),
|
|
2997
|
+
};
|
|
2998
|
+
if (globalScope[GLOBAL_MUTTS_KEY]) {
|
|
2999
|
+
const existing = globalScope[GLOBAL_MUTTS_KEY];
|
|
3000
|
+
throw new Error(`[Mutts] Multiple instances detected!\n` +
|
|
3001
|
+
`Existing instance: ${JSON.stringify(existing, null, 2)}\n` +
|
|
3002
|
+
`New instance: ${JSON.stringify(currentSourceInfo, null, 2)}\n` +
|
|
3003
|
+
`This usually happens when 'mutts' is both installed as a dependency and bundled, ` +
|
|
3004
|
+
`or when different versions are loaded. ` +
|
|
3005
|
+
`Please check your build configuration (aliases, externals) to ensure a single source of truth.`);
|
|
3006
|
+
}
|
|
3007
|
+
globalScope[GLOBAL_MUTTS_KEY] = currentSourceInfo;
|
|
3008
|
+
}
|
|
3009
|
+
|
|
3010
|
+
export { ArrayReadForward as A, setAt as B, throttle as C, Destroyable as D, Eventful as E, unreactive as F, watch as G, Indexable as I, Register as R, DestructionError as a, allocated as b, allocatedValues as c, attend as d, cache as e, cached as f, callOnGC as g, chainPromise as h, debounce as i, deepWatch as j, deprecated as k, describe as l, descriptor as m, destructor as n, forwardArray as o, getActiveProjection as p, getAt as q, isCached as r, lift as s, memoize as t, organize as u, organized as v, profileInfo as w, project as x, register as y, scan as z };
|
|
3011
|
+
//# sourceMappingURL=index-2vea86wD.esm.js.map
|