@the-w1zard/singularity 1.0.12 → 1.0.14
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.html +450 -0
- package/README.md +327 -2
- package/dist/bus_db_rpc/RULE_OF_2_same_node.d.ts +139 -0
- package/dist/bus_db_rpc/RULE_OF_2_same_node.d.ts.map +1 -0
- package/dist/bus_db_rpc/RULE_OF_2_same_node.js +502 -0
- package/dist/class.d.ts +161 -0
- package/dist/class.d.ts.map +1 -0
- package/dist/class.js +804 -0
- package/dist/comps/DISRUPTOR_CRYPTO.d.ts +47 -0
- package/dist/comps/DISRUPTOR_CRYPTO.d.ts.map +1 -0
- package/dist/comps/RULE_OF_2_same_app.d.ts +175 -0
- package/dist/comps/RULE_OF_2_same_app.d.ts.map +1 -0
- package/dist/comps/RULE_OF_2_same_app.js +482 -0
- package/dist/comps/binary.d.ts +71 -0
- package/dist/comps/binary.d.ts.map +1 -0
- package/dist/comps/byte_id.d.ts +12 -0
- package/dist/comps/byte_id.d.ts.map +1 -0
- package/dist/comps/cell-worker.d.ts +9 -0
- package/dist/comps/cell-worker.d.ts.map +1 -0
- package/dist/comps/cell.d.ts +349 -0
- package/dist/comps/cell.d.ts.map +1 -0
- package/dist/comps/create_live_self.d.ts +22 -0
- package/dist/comps/create_live_self.d.ts.map +1 -0
- package/dist/comps/queue.d.ts +103 -0
- package/dist/comps/queue.d.ts.map +1 -0
- package/dist/comps/serializer.d.ts +30 -0
- package/dist/comps/serializer.d.ts.map +1 -0
- package/dist/comps/serializer.js +131 -0
- package/dist/comps/vm-types.d.ts +20 -0
- package/dist/comps/vm-types.d.ts.map +1 -0
- package/dist/db/binary.d.ts +76 -0
- package/dist/db/binary.d.ts.map +1 -0
- package/dist/db/binary.js +441 -0
- package/dist/db/client.d.ts +28 -0
- package/dist/db/client.d.ts.map +1 -0
- package/dist/db/client.js +82 -0
- package/dist/db/db.d.ts +141 -0
- package/dist/db/db.d.ts.map +1 -0
- package/dist/db/db.js +898 -0
- package/dist/db/op.d.ts +13 -0
- package/dist/db/op.d.ts.map +1 -0
- package/dist/db/op.js +11 -0
- package/dist/db/storage-worker.d.ts +2 -0
- package/dist/db/storage-worker.d.ts.map +1 -0
- package/dist/db/storage-worker.js +152 -0
- package/dist/db/types.d.ts +134 -0
- package/dist/db/types.d.ts.map +1 -0
- package/dist/db/types.js +14 -0
- package/dist/db/wal.d.ts +30 -0
- package/dist/db/wal.d.ts.map +1 -0
- package/dist/db/wal.js +217 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/signals.js +10740 -5498
- package/dist/slab.d.ts +95 -0
- package/dist/slab.d.ts.map +1 -0
- package/dist/slab.js +575 -0
- package/dist/slab.types.d.ts +5 -0
- package/dist/slab.types.d.ts.map +1 -0
- package/dist/slab.types.js +1 -0
- package/dist/test.d.ts +152 -0
- package/dist/test.d.ts.map +1 -0
- package/dist/test.js +1183 -0
- package/dist/testt.d.ts +61 -0
- package/dist/testt.d.ts.map +1 -0
- package/dist/testt.js +143 -0
- package/dist/y.d.ts +152 -0
- package/dist/y.d.ts.map +1 -0
- package/dist/y.js +36 -65
- package/package.json +22 -19
package/dist/class.js
ADDED
|
@@ -0,0 +1,804 @@
|
|
|
1
|
+
// [FLAGS]
|
|
2
|
+
// #region ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
|
|
3
|
+
const NONE = 0;
|
|
4
|
+
const MUTABLE = 1;
|
|
5
|
+
const WATCHING = 2;
|
|
6
|
+
const DIRTY = 4;
|
|
7
|
+
const PENDING = 8;
|
|
8
|
+
const QUEUED = 16;
|
|
9
|
+
const DISPOSED = 32;
|
|
10
|
+
const RUNNING = 64;
|
|
11
|
+
const DIRECT = 128;
|
|
12
|
+
const COMPUTED = 256;
|
|
13
|
+
// #endregion
|
|
14
|
+
// [ENGINE STATE]
|
|
15
|
+
// #region ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
|
|
16
|
+
let activeTarget;
|
|
17
|
+
let batchDepth = 0;
|
|
18
|
+
let cycle = 0;
|
|
19
|
+
let isFlushing = false;
|
|
20
|
+
let effectHead;
|
|
21
|
+
let effectTail;
|
|
22
|
+
function sameValue(a, b) {
|
|
23
|
+
return a === b || (a !== a && b !== b) || Object.is(a, b);
|
|
24
|
+
}
|
|
25
|
+
let _signals_root = {
|
|
26
|
+
id: "THE_WIZARD_SIGNALS_ROOT",
|
|
27
|
+
hooks: Object.create(null),
|
|
28
|
+
unmount: new Set(),
|
|
29
|
+
contextProviders: Object.create(null),
|
|
30
|
+
};
|
|
31
|
+
let _rootSeq = 0;
|
|
32
|
+
const rx = Object.create(null);
|
|
33
|
+
const str = (v) => (v == null ? '' : String(v));
|
|
34
|
+
// #endregion
|
|
35
|
+
// [RX]
|
|
36
|
+
// #region ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
|
|
37
|
+
export class SignalRx {
|
|
38
|
+
rxid() {
|
|
39
|
+
return `${_signals_root.id}:rx${++_rootSeq}`;
|
|
40
|
+
}
|
|
41
|
+
constructor() { }
|
|
42
|
+
get(id) { return rx[id].signal.get(); }
|
|
43
|
+
set(id, v) {
|
|
44
|
+
const b = rx[id];
|
|
45
|
+
if (!b)
|
|
46
|
+
return;
|
|
47
|
+
b.signal.set(v);
|
|
48
|
+
}
|
|
49
|
+
makeRx(id, b) {
|
|
50
|
+
const fn = (...args) => {
|
|
51
|
+
if (args.length === 0)
|
|
52
|
+
return this.get(id);
|
|
53
|
+
this.set(id, args[0]);
|
|
54
|
+
};
|
|
55
|
+
const r = fn;
|
|
56
|
+
Object.defineProperties(r, { id: { value: id }, node: { value: b.node }, el: { value: b.el } });
|
|
57
|
+
rx[id] = b;
|
|
58
|
+
b.disposeEffect = effect(() => { b.write(b.signal.get()); });
|
|
59
|
+
r.attach = (parent) => { for (const n of b.owned)
|
|
60
|
+
parent.appendChild(n); return r; };
|
|
61
|
+
r.dispose = () => {
|
|
62
|
+
b.disposeEffect?.();
|
|
63
|
+
delete rx[id];
|
|
64
|
+
_signals_root.unmount.delete(r.dispose);
|
|
65
|
+
for (const n of b.owned)
|
|
66
|
+
n.parentNode?.removeChild(n);
|
|
67
|
+
};
|
|
68
|
+
_signals_root.unmount.add(r.dispose);
|
|
69
|
+
return r;
|
|
70
|
+
}
|
|
71
|
+
ele(initialValue, injectDom = true) {
|
|
72
|
+
const id = this.rxid();
|
|
73
|
+
const el = injectDom ? document.createElement('span') : null;
|
|
74
|
+
el?.setAttribute('data-sid', id);
|
|
75
|
+
return this.makeRx(id, {
|
|
76
|
+
signal: signal(initialValue),
|
|
77
|
+
node: el, el, owned: el ? [el] : [],
|
|
78
|
+
write(v) { if (el)
|
|
79
|
+
el.textContent = str(v); },
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
txt(initialValue, node, injectDom = true) {
|
|
83
|
+
const id = this.rxid();
|
|
84
|
+
let text = node ?? null;
|
|
85
|
+
const owned = [];
|
|
86
|
+
if (!text && injectDom)
|
|
87
|
+
owned.push(document.createComment(id), (text = document.createTextNode('')));
|
|
88
|
+
return this.makeRx(id, {
|
|
89
|
+
signal: signal(initialValue),
|
|
90
|
+
node: text, el: text?.parentElement ?? null, owned,
|
|
91
|
+
write(v) { if (text)
|
|
92
|
+
text.data = str(v); },
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
atr(initialValue, node, attributeName = 'value', injectDom = true) {
|
|
96
|
+
const id = this.rxid();
|
|
97
|
+
if (node && injectDom && !node.hasAttribute('data-sid'))
|
|
98
|
+
node.setAttribute('data-sid', id);
|
|
99
|
+
const live = node instanceof HTMLInputElement && attributeName === 'value' ? node : null;
|
|
100
|
+
return this.makeRx(id, {
|
|
101
|
+
signal: signal(initialValue),
|
|
102
|
+
node, el: node ?? null, owned: [],
|
|
103
|
+
write(v) {
|
|
104
|
+
if (!node)
|
|
105
|
+
return;
|
|
106
|
+
if (v == null || v === false)
|
|
107
|
+
node.removeAttribute(attributeName);
|
|
108
|
+
else
|
|
109
|
+
node.setAttribute(attributeName, v === true ? '' : str(v));
|
|
110
|
+
if (live && live.value !== str(v) && document.activeElement !== live)
|
|
111
|
+
live.value = str(v);
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
scanRoot(root = document, componentId = _signals_root.id) {
|
|
116
|
+
const out = Object.create(null);
|
|
117
|
+
for (const el of root.querySelectorAll('[data-sid]')) {
|
|
118
|
+
out[el.getAttribute('data-sid')] = el.getAttribute('value') ?? el.textContent;
|
|
119
|
+
}
|
|
120
|
+
const w = document.createTreeWalker(root, NodeFilter.SHOW_COMMENT);
|
|
121
|
+
while (w.nextNode()) {
|
|
122
|
+
const c = w.currentNode;
|
|
123
|
+
if (c.data.startsWith('rx'))
|
|
124
|
+
out[c.data] = c.nextSibling?.textContent ?? null;
|
|
125
|
+
}
|
|
126
|
+
return out;
|
|
127
|
+
}
|
|
128
|
+
unmountAll() {
|
|
129
|
+
for (const dispose of [..._signals_root.unmount])
|
|
130
|
+
dispose();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
// #endregion
|
|
134
|
+
// [SIGNAL]
|
|
135
|
+
// #region ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
|
|
136
|
+
export class Signal {
|
|
137
|
+
flags = MUTABLE;
|
|
138
|
+
value;
|
|
139
|
+
version = 0;
|
|
140
|
+
targets;
|
|
141
|
+
targetsTail;
|
|
142
|
+
constructor(value) { this.value = value; }
|
|
143
|
+
get() {
|
|
144
|
+
if (activeTarget !== undefined) {
|
|
145
|
+
track(this, activeTarget);
|
|
146
|
+
}
|
|
147
|
+
return this.value;
|
|
148
|
+
}
|
|
149
|
+
peek() { return this.value; }
|
|
150
|
+
set(value) {
|
|
151
|
+
if (Object.is(this.value, value)) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
this.value = value;
|
|
155
|
+
this.version = ++cycle;
|
|
156
|
+
const target = this.targets;
|
|
157
|
+
if (target === undefined) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const subscriber = target.target;
|
|
161
|
+
if (target.nextTarget === undefined && batchDepth === 0 && !isFlushing) {
|
|
162
|
+
const subFlags = subscriber.flags;
|
|
163
|
+
if ((subFlags & WATCHING) !== NONE) {
|
|
164
|
+
const effect = subscriber;
|
|
165
|
+
effect.flags |= DIRTY;
|
|
166
|
+
effect.runEffect();
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if ((subFlags & COMPUTED) !== NONE && (subFlags & (DIRTY | PENDING | DISPOSED)) === NONE && subscriber.targets === undefined) {
|
|
170
|
+
subscriber.flags = subFlags | PENDING;
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
propagate(target);
|
|
175
|
+
if (batchDepth === 0 && !isFlushing && effectHead !== undefined) {
|
|
176
|
+
flushEffects();
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
dispose() {
|
|
180
|
+
clearSources(this);
|
|
181
|
+
clearTargets(this);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
// #endregion
|
|
185
|
+
// [COMPUTED]
|
|
186
|
+
// #region ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
|
|
187
|
+
export class Computed {
|
|
188
|
+
flags = DIRTY | COMPUTED;
|
|
189
|
+
value;
|
|
190
|
+
version = 0;
|
|
191
|
+
compute;
|
|
192
|
+
sources;
|
|
193
|
+
sourcesTail;
|
|
194
|
+
targets;
|
|
195
|
+
targetsTail;
|
|
196
|
+
constructor(compute) {
|
|
197
|
+
this.value = undefined;
|
|
198
|
+
this.compute = compute;
|
|
199
|
+
}
|
|
200
|
+
get1() {
|
|
201
|
+
const flags = this.flags;
|
|
202
|
+
if (flags === COMPUTED) {
|
|
203
|
+
if (activeTarget !== undefined)
|
|
204
|
+
track(this, activeTarget);
|
|
205
|
+
return this.value;
|
|
206
|
+
}
|
|
207
|
+
if ((flags & DIRTY) !== NONE) {
|
|
208
|
+
this.recompute();
|
|
209
|
+
}
|
|
210
|
+
else if ((flags & PENDING) !== NONE) {
|
|
211
|
+
if (checkDirty(this))
|
|
212
|
+
this.recompute();
|
|
213
|
+
else
|
|
214
|
+
this.flags &= ~PENDING;
|
|
215
|
+
}
|
|
216
|
+
if (activeTarget !== undefined)
|
|
217
|
+
track(this, activeTarget);
|
|
218
|
+
return this.value;
|
|
219
|
+
}
|
|
220
|
+
get() {
|
|
221
|
+
if (this.flags !== COMPUTED) {
|
|
222
|
+
this.refresh();
|
|
223
|
+
}
|
|
224
|
+
if (activeTarget !== undefined) {
|
|
225
|
+
track(this, activeTarget);
|
|
226
|
+
}
|
|
227
|
+
return this.value;
|
|
228
|
+
}
|
|
229
|
+
refresh() {
|
|
230
|
+
const flags = this.flags;
|
|
231
|
+
if ((flags & DIRTY) !== NONE) {
|
|
232
|
+
this.recompute();
|
|
233
|
+
}
|
|
234
|
+
else if ((flags & PENDING) !== NONE) {
|
|
235
|
+
if (checkDirty(this)) {
|
|
236
|
+
this.recompute();
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
this.flags &= ~PENDING;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
recompute() {
|
|
244
|
+
const previousValue = this.value;
|
|
245
|
+
this.flags = (this.flags & ~(PENDING | DIRTY)) | RUNNING;
|
|
246
|
+
const previousTarget = activeTarget;
|
|
247
|
+
activeTarget = this;
|
|
248
|
+
const previousTail = this.sourcesTail;
|
|
249
|
+
this.sourcesTail = undefined;
|
|
250
|
+
try {
|
|
251
|
+
this.value = this.compute(previousValue);
|
|
252
|
+
}
|
|
253
|
+
catch (error) {
|
|
254
|
+
this.flags |= DIRTY;
|
|
255
|
+
throw error;
|
|
256
|
+
}
|
|
257
|
+
finally {
|
|
258
|
+
activeTarget = previousTarget;
|
|
259
|
+
if (this.sourcesTail !== previousTail)
|
|
260
|
+
removeStaleSources(this);
|
|
261
|
+
this.flags &= ~RUNNING;
|
|
262
|
+
}
|
|
263
|
+
if (!Object.is(previousValue, this.value)) {
|
|
264
|
+
this.version = ++cycle;
|
|
265
|
+
shallowPropagate(this.targets);
|
|
266
|
+
}
|
|
267
|
+
return this.value;
|
|
268
|
+
}
|
|
269
|
+
dispose() {
|
|
270
|
+
if ((this.flags & DISPOSED) !== NONE) {
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
this.flags |= DISPOSED;
|
|
274
|
+
clearSources(this);
|
|
275
|
+
clearTargets(this);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
// #endregion
|
|
279
|
+
// [EFFECT]
|
|
280
|
+
// #region ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
|
|
281
|
+
export class Effect {
|
|
282
|
+
flags = WATCHING;
|
|
283
|
+
run;
|
|
284
|
+
cleanup;
|
|
285
|
+
sources;
|
|
286
|
+
sourcesTail;
|
|
287
|
+
nextQueuedEffect;
|
|
288
|
+
constructor(run) { this.run = run; }
|
|
289
|
+
runEffect() {
|
|
290
|
+
if ((this.flags & DIRTY) !== NONE) {
|
|
291
|
+
this.execute();
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
if ((this.flags & PENDING) !== NONE && (this.flags & DIRECT) === NONE && checkDirty(this)) {
|
|
295
|
+
this.execute();
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
this.flags &= ~PENDING;
|
|
299
|
+
}
|
|
300
|
+
execute() {
|
|
301
|
+
if (this.cleanup !== undefined) {
|
|
302
|
+
const cleanup = this.cleanup;
|
|
303
|
+
this.cleanup = undefined;
|
|
304
|
+
const previousTarget = activeTarget;
|
|
305
|
+
activeTarget = undefined;
|
|
306
|
+
try {
|
|
307
|
+
cleanup();
|
|
308
|
+
}
|
|
309
|
+
finally {
|
|
310
|
+
activeTarget = previousTarget;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
this.flags &= ~(PENDING | DIRTY);
|
|
314
|
+
const previousTarget = activeTarget;
|
|
315
|
+
activeTarget = this;
|
|
316
|
+
const previousTail = this.sourcesTail;
|
|
317
|
+
this.sourcesTail = undefined;
|
|
318
|
+
try {
|
|
319
|
+
const result = this.run();
|
|
320
|
+
this.cleanup = typeof result === 'function' ? result : undefined;
|
|
321
|
+
}
|
|
322
|
+
finally {
|
|
323
|
+
activeTarget = previousTarget;
|
|
324
|
+
if (this.sourcesTail !== previousTail) {
|
|
325
|
+
removeStaleSources(this);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
dispose() {
|
|
330
|
+
if ((this.flags & DISPOSED) !== NONE) {
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
this.flags |= DISPOSED;
|
|
334
|
+
if (this.cleanup !== undefined) {
|
|
335
|
+
const cleanup = this.cleanup;
|
|
336
|
+
this.cleanup = undefined;
|
|
337
|
+
const previousTarget = activeTarget;
|
|
338
|
+
activeTarget = undefined;
|
|
339
|
+
cleanup();
|
|
340
|
+
activeTarget = previousTarget;
|
|
341
|
+
}
|
|
342
|
+
clearSources(this);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
// #endregion
|
|
346
|
+
// [TRACKING]
|
|
347
|
+
// #region ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
|
|
348
|
+
function track(source, target) {
|
|
349
|
+
const tail = target.sourcesTail;
|
|
350
|
+
if (tail !== undefined && tail.source === source) {
|
|
351
|
+
if (tail.version !== source.version) {
|
|
352
|
+
tail.version = source.version;
|
|
353
|
+
}
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
let link = tail !== undefined ? tail.nextSource : target.sources;
|
|
357
|
+
while (link !== undefined) {
|
|
358
|
+
if (link.source === source) {
|
|
359
|
+
if (link.version !== source.version) {
|
|
360
|
+
link.version = source.version;
|
|
361
|
+
}
|
|
362
|
+
target.sourcesTail = link;
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
link = link.nextSource;
|
|
366
|
+
}
|
|
367
|
+
link = target.sources;
|
|
368
|
+
while (link !== tail) {
|
|
369
|
+
if (link.source === source) {
|
|
370
|
+
unlink(link);
|
|
371
|
+
const previousTargetLink = source.targetsTail;
|
|
372
|
+
link.prevSource = tail;
|
|
373
|
+
link.nextSource = tail !== undefined ? tail.nextSource : target.sources;
|
|
374
|
+
link.prevTarget = previousTargetLink;
|
|
375
|
+
link.nextTarget = undefined;
|
|
376
|
+
target.sourcesTail = link;
|
|
377
|
+
if (tail !== undefined) {
|
|
378
|
+
tail.nextSource = link;
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
target.sources = link;
|
|
382
|
+
}
|
|
383
|
+
if (link.nextSource !== undefined) {
|
|
384
|
+
link.nextSource.prevSource = link;
|
|
385
|
+
}
|
|
386
|
+
source.targetsTail = link;
|
|
387
|
+
if (previousTargetLink !== undefined) {
|
|
388
|
+
previousTargetLink.nextTarget = link;
|
|
389
|
+
}
|
|
390
|
+
else {
|
|
391
|
+
source.targets = link;
|
|
392
|
+
}
|
|
393
|
+
if (link.version !== source.version) {
|
|
394
|
+
link.version = source.version;
|
|
395
|
+
}
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
link = link.nextSource;
|
|
399
|
+
}
|
|
400
|
+
const previousTargetLink = source.targetsTail;
|
|
401
|
+
link = {
|
|
402
|
+
source,
|
|
403
|
+
target,
|
|
404
|
+
version: source.version,
|
|
405
|
+
prevSource: tail,
|
|
406
|
+
nextSource: tail !== undefined ? tail.nextSource : target.sources,
|
|
407
|
+
prevTarget: previousTargetLink,
|
|
408
|
+
nextTarget: undefined,
|
|
409
|
+
};
|
|
410
|
+
target.sourcesTail = link;
|
|
411
|
+
if (tail !== undefined) {
|
|
412
|
+
tail.nextSource = link;
|
|
413
|
+
}
|
|
414
|
+
else {
|
|
415
|
+
target.sources = link;
|
|
416
|
+
}
|
|
417
|
+
if (link.nextSource !== undefined) {
|
|
418
|
+
link.nextSource.prevSource = link;
|
|
419
|
+
}
|
|
420
|
+
source.targetsTail = link;
|
|
421
|
+
if (previousTargetLink !== undefined) {
|
|
422
|
+
previousTargetLink.nextTarget = link;
|
|
423
|
+
}
|
|
424
|
+
else {
|
|
425
|
+
source.targets = link;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
function removeStaleSources(target) {
|
|
429
|
+
const tail = target.sourcesTail;
|
|
430
|
+
let link = tail !== undefined ? tail.nextSource : target.sources;
|
|
431
|
+
while (link !== undefined) {
|
|
432
|
+
const next = link.nextSource;
|
|
433
|
+
unlink(link);
|
|
434
|
+
link = next;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
function clearSources(target) {
|
|
438
|
+
let link = target.sources;
|
|
439
|
+
while (link !== undefined) {
|
|
440
|
+
const next = link.nextSource;
|
|
441
|
+
unlink(link);
|
|
442
|
+
link = next;
|
|
443
|
+
}
|
|
444
|
+
target.sources = undefined;
|
|
445
|
+
target.sourcesTail = undefined;
|
|
446
|
+
}
|
|
447
|
+
function clearTargets(target) {
|
|
448
|
+
let link = target.targets;
|
|
449
|
+
while (link !== undefined) {
|
|
450
|
+
const next = link.nextTarget;
|
|
451
|
+
unlink(link);
|
|
452
|
+
link = next;
|
|
453
|
+
}
|
|
454
|
+
target.targets = undefined;
|
|
455
|
+
target.targetsTail = undefined;
|
|
456
|
+
}
|
|
457
|
+
function unlink(link) {
|
|
458
|
+
const source = link.source;
|
|
459
|
+
const target = link.target;
|
|
460
|
+
const previousSource = link.prevSource;
|
|
461
|
+
const nextSource = link.nextSource;
|
|
462
|
+
const previousTarget = link.prevTarget;
|
|
463
|
+
const nextTarget = link.nextTarget;
|
|
464
|
+
if (nextSource !== undefined) {
|
|
465
|
+
nextSource.prevSource = previousSource;
|
|
466
|
+
}
|
|
467
|
+
else {
|
|
468
|
+
target.sourcesTail = previousSource;
|
|
469
|
+
}
|
|
470
|
+
if (previousSource !== undefined) {
|
|
471
|
+
previousSource.nextSource = nextSource;
|
|
472
|
+
}
|
|
473
|
+
else {
|
|
474
|
+
target.sources = nextSource;
|
|
475
|
+
}
|
|
476
|
+
if (nextTarget !== undefined) {
|
|
477
|
+
nextTarget.prevTarget = previousTarget;
|
|
478
|
+
}
|
|
479
|
+
else {
|
|
480
|
+
source.targetsTail = previousTarget;
|
|
481
|
+
}
|
|
482
|
+
if (previousTarget !== undefined) {
|
|
483
|
+
previousTarget.nextTarget = nextTarget;
|
|
484
|
+
}
|
|
485
|
+
else {
|
|
486
|
+
source.targets = nextTarget;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
// #endregion
|
|
490
|
+
// [PROPAGATION]
|
|
491
|
+
// #region ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
|
|
492
|
+
const propagateStack = [];
|
|
493
|
+
let propagateStackLength = 0;
|
|
494
|
+
function propagate(startLink) {
|
|
495
|
+
let link = startLink;
|
|
496
|
+
propagateStackLength = 0;
|
|
497
|
+
while (true) {
|
|
498
|
+
while (link !== undefined) {
|
|
499
|
+
const target = link.target;
|
|
500
|
+
const flags = target.flags;
|
|
501
|
+
if ((flags & (DIRTY | PENDING | DISPOSED)) !== NONE) {
|
|
502
|
+
link = link.nextTarget;
|
|
503
|
+
continue;
|
|
504
|
+
}
|
|
505
|
+
target.flags = flags | PENDING;
|
|
506
|
+
if ((flags & WATCHING) !== NONE) {
|
|
507
|
+
enqueueEffect(target);
|
|
508
|
+
}
|
|
509
|
+
else if ((flags & COMPUTED) !== NONE) {
|
|
510
|
+
const computedTarget = target;
|
|
511
|
+
if (computedTarget.targets !== undefined) {
|
|
512
|
+
const next = link.nextTarget;
|
|
513
|
+
if (next !== undefined) {
|
|
514
|
+
propagateStack[propagateStackLength++] = next;
|
|
515
|
+
}
|
|
516
|
+
link = computedTarget.targets;
|
|
517
|
+
continue;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
link = link.nextTarget;
|
|
521
|
+
}
|
|
522
|
+
if (propagateStackLength === 0) {
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
link = propagateStack[--propagateStackLength];
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
function shallowPropagate(startLink) {
|
|
529
|
+
let link = startLink;
|
|
530
|
+
while (link !== undefined) {
|
|
531
|
+
const target = link.target;
|
|
532
|
+
const flags = target.flags;
|
|
533
|
+
if ((flags & RUNNING) === NONE && (flags & (DIRTY | PENDING | DISPOSED)) === PENDING) {
|
|
534
|
+
target.flags = flags | DIRTY;
|
|
535
|
+
if ((flags & WATCHING) !== NONE) {
|
|
536
|
+
enqueueEffect(target);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
link = link.nextTarget;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
const checkDirtyStack = [];
|
|
543
|
+
let checkDirtyStackLength = 0;
|
|
544
|
+
function checkDirty(target) {
|
|
545
|
+
let link = target.sources;
|
|
546
|
+
checkDirtyStackLength = 0;
|
|
547
|
+
while (true) {
|
|
548
|
+
while (link !== undefined) {
|
|
549
|
+
const source = link.source;
|
|
550
|
+
const sourceFlags = source.flags;
|
|
551
|
+
if ((sourceFlags & DIRTY) !== NONE) {
|
|
552
|
+
return true;
|
|
553
|
+
}
|
|
554
|
+
if (link.version !== source.version) {
|
|
555
|
+
return true;
|
|
556
|
+
}
|
|
557
|
+
if ((sourceFlags & MUTABLE) === NONE && (sourceFlags & PENDING) !== NONE) {
|
|
558
|
+
const next = link.nextSource;
|
|
559
|
+
if (next !== undefined) {
|
|
560
|
+
checkDirtyStack[checkDirtyStackLength++] = next;
|
|
561
|
+
}
|
|
562
|
+
link = source.sources;
|
|
563
|
+
continue;
|
|
564
|
+
}
|
|
565
|
+
link = link.nextSource;
|
|
566
|
+
}
|
|
567
|
+
if (checkDirtyStackLength === 0) {
|
|
568
|
+
return false;
|
|
569
|
+
}
|
|
570
|
+
link = checkDirtyStack[--checkDirtyStackLength];
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
// #endregion
|
|
574
|
+
// [EFFECT QUEUE]
|
|
575
|
+
// #region ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
|
|
576
|
+
function enqueueEffect(effect) {
|
|
577
|
+
if ((effect.flags & QUEUED) !== NONE || (effect.flags & DISPOSED) !== NONE) {
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
effect.flags |= QUEUED;
|
|
581
|
+
if (effectTail === undefined) {
|
|
582
|
+
effectHead = effect;
|
|
583
|
+
effectTail = effect;
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
effectTail.nextQueuedEffect = effect;
|
|
587
|
+
effectTail = effect;
|
|
588
|
+
}
|
|
589
|
+
function flushEffects() {
|
|
590
|
+
if (effectHead === undefined)
|
|
591
|
+
return;
|
|
592
|
+
if (isFlushing)
|
|
593
|
+
return;
|
|
594
|
+
isFlushing = true;
|
|
595
|
+
let firstError;
|
|
596
|
+
try {
|
|
597
|
+
while (effectHead !== undefined) {
|
|
598
|
+
const head = effectHead;
|
|
599
|
+
effectHead = undefined;
|
|
600
|
+
effectTail = undefined;
|
|
601
|
+
let effect = head;
|
|
602
|
+
while (effect !== undefined) {
|
|
603
|
+
const next = effect.nextQueuedEffect;
|
|
604
|
+
effect.nextQueuedEffect = undefined;
|
|
605
|
+
effect.flags &= ~QUEUED;
|
|
606
|
+
try {
|
|
607
|
+
effect.runEffect();
|
|
608
|
+
}
|
|
609
|
+
catch (error) {
|
|
610
|
+
firstError ??= error;
|
|
611
|
+
}
|
|
612
|
+
effect = next;
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
finally {
|
|
617
|
+
isFlushing = false;
|
|
618
|
+
}
|
|
619
|
+
// if (firstError !== undefined) { throw firstError; }
|
|
620
|
+
}
|
|
621
|
+
// #endregion
|
|
622
|
+
// [PUBLIC API]
|
|
623
|
+
// #region ═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
|
|
624
|
+
export function signal(initialValue) { return new Signal(initialValue); }
|
|
625
|
+
export function computed(compute) { return new Computed(compute); }
|
|
626
|
+
export function effect(fn) {
|
|
627
|
+
const node = new Effect(fn);
|
|
628
|
+
node.flags |= DIRTY;
|
|
629
|
+
node.runEffect();
|
|
630
|
+
return () => node.dispose();
|
|
631
|
+
}
|
|
632
|
+
export function batch(fn) {
|
|
633
|
+
batchDepth++;
|
|
634
|
+
try {
|
|
635
|
+
return fn();
|
|
636
|
+
}
|
|
637
|
+
finally {
|
|
638
|
+
if (--batchDepth === 0) {
|
|
639
|
+
flushEffects();
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
export function startBatch() { batchDepth++; }
|
|
644
|
+
export function endBatch() { if (--batchDepth === 0) {
|
|
645
|
+
flushEffects();
|
|
646
|
+
} }
|
|
647
|
+
export function untracked(fn) {
|
|
648
|
+
const previousTarget = activeTarget;
|
|
649
|
+
activeTarget = undefined;
|
|
650
|
+
try {
|
|
651
|
+
return fn();
|
|
652
|
+
}
|
|
653
|
+
finally {
|
|
654
|
+
activeTarget = previousTarget;
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
// #endregion
|
|
658
|
+
function track1(source, target) {
|
|
659
|
+
const tail = target.sourcesTail;
|
|
660
|
+
// Fast path: same source as the immediately preceding read.
|
|
661
|
+
if (tail !== undefined && tail.source === source) {
|
|
662
|
+
if (tail.version !== source.version) {
|
|
663
|
+
tail.version = source.version;
|
|
664
|
+
}
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
// Fast path: the next expected source in a repeated read order.
|
|
668
|
+
let link = tail !== undefined ? tail.nextSource : target.sources;
|
|
669
|
+
while (link !== undefined) {
|
|
670
|
+
if (link.source === source) {
|
|
671
|
+
if (link.version !== source.version) {
|
|
672
|
+
link.version = source.version;
|
|
673
|
+
}
|
|
674
|
+
target.sourcesTail = link;
|
|
675
|
+
return;
|
|
676
|
+
}
|
|
677
|
+
link = link.nextSource;
|
|
678
|
+
}
|
|
679
|
+
// The source is already tracked, but appears before the current tail.
|
|
680
|
+
// This happens when the same dependencies are read in the same order
|
|
681
|
+
// on every evaluation, such as C -> B -> A.
|
|
682
|
+
link = target.sources;
|
|
683
|
+
while (link !== tail) {
|
|
684
|
+
if (link.source === source) {
|
|
685
|
+
// Move the existing link to the tail (most-recently-used position).
|
|
686
|
+
unlink(link);
|
|
687
|
+
const previousTargetLink = source.targetsTail;
|
|
688
|
+
link.prevSource = tail;
|
|
689
|
+
link.nextSource =
|
|
690
|
+
tail !== undefined ? tail.nextSource : target.sources;
|
|
691
|
+
link.prevTarget = previousTargetLink;
|
|
692
|
+
link.nextTarget = undefined;
|
|
693
|
+
target.sourcesTail = link;
|
|
694
|
+
if (tail !== undefined) {
|
|
695
|
+
tail.nextSource = link;
|
|
696
|
+
}
|
|
697
|
+
else {
|
|
698
|
+
target.sources = link;
|
|
699
|
+
}
|
|
700
|
+
if (link.nextSource !== undefined) {
|
|
701
|
+
link.nextSource.prevSource = link;
|
|
702
|
+
}
|
|
703
|
+
source.targetsTail = link;
|
|
704
|
+
if (previousTargetLink !== undefined) {
|
|
705
|
+
previousTargetLink.nextTarget = link;
|
|
706
|
+
}
|
|
707
|
+
else {
|
|
708
|
+
source.targets = link;
|
|
709
|
+
}
|
|
710
|
+
if (link.version !== source.version) {
|
|
711
|
+
link.version = source.version;
|
|
712
|
+
}
|
|
713
|
+
return;
|
|
714
|
+
}
|
|
715
|
+
link = link.nextSource;
|
|
716
|
+
}
|
|
717
|
+
// Create a new link.
|
|
718
|
+
const previousTargetLink = source.targetsTail;
|
|
719
|
+
link = {
|
|
720
|
+
source,
|
|
721
|
+
target,
|
|
722
|
+
version: source.version,
|
|
723
|
+
prevSource: tail,
|
|
724
|
+
nextSource: tail !== undefined ? tail.nextSource : target.sources,
|
|
725
|
+
prevTarget: previousTargetLink,
|
|
726
|
+
nextTarget: undefined,
|
|
727
|
+
};
|
|
728
|
+
target.sourcesTail = link;
|
|
729
|
+
if (tail !== undefined) {
|
|
730
|
+
tail.nextSource = link;
|
|
731
|
+
}
|
|
732
|
+
else {
|
|
733
|
+
target.sources = link;
|
|
734
|
+
}
|
|
735
|
+
if (link.nextSource !== undefined) {
|
|
736
|
+
link.nextSource.prevSource = link;
|
|
737
|
+
}
|
|
738
|
+
source.targetsTail = link;
|
|
739
|
+
if (previousTargetLink !== undefined) {
|
|
740
|
+
previousTargetLink.nextTarget = link;
|
|
741
|
+
}
|
|
742
|
+
else {
|
|
743
|
+
source.targets = link;
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
/**
|
|
747
|
+
*
|
|
748
|
+
*
|
|
749
|
+
* get1() {
|
|
750
|
+
const flags = this.flags;
|
|
751
|
+
if ((flags & (DISPOSED | RUNNING)) !== NONE) {
|
|
752
|
+
if ((flags & DISPOSED) !== NONE) throw new Error('Cannot read a disposed computed.');
|
|
753
|
+
throw new Error('Circular computed dependency.');
|
|
754
|
+
}
|
|
755
|
+
if ((flags & DIRTY) !== NONE) {
|
|
756
|
+
this.recompute();
|
|
757
|
+
} else if ((flags & PENDING) !== NONE) {
|
|
758
|
+
if (checkDirty(this)) this.recompute();
|
|
759
|
+
else this.flags &= ~PENDING;
|
|
760
|
+
}
|
|
761
|
+
if (activeTarget !== undefined) track(this, activeTarget);
|
|
762
|
+
return this.value;
|
|
763
|
+
}
|
|
764
|
+
* set1(value: T): void {
|
|
765
|
+
if (Object.is(this.value, value)) {
|
|
766
|
+
return;
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
this.value = value;
|
|
770
|
+
this.version = ++cycle;
|
|
771
|
+
|
|
772
|
+
const target = this.targets;
|
|
773
|
+
|
|
774
|
+
if (target === undefined) {
|
|
775
|
+
return;
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
const subscriber = target.target;
|
|
779
|
+
|
|
780
|
+
if (
|
|
781
|
+
target.nextTarget === undefined &&
|
|
782
|
+
batchDepth === 0 &&
|
|
783
|
+
!isFlushing &&
|
|
784
|
+
(subscriber.flags & WATCHING) !== NONE
|
|
785
|
+
) {
|
|
786
|
+
const effect = subscriber as Effect;
|
|
787
|
+
|
|
788
|
+
effect.flags |= DIRTY;
|
|
789
|
+
effect.runEffect();
|
|
790
|
+
|
|
791
|
+
return;
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
propagate(target);
|
|
795
|
+
|
|
796
|
+
if (batchDepth === 0 && !isFlushing && effectHead !== undefined) {
|
|
797
|
+
flushEffects();
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
if ((flags & (DISPOSED | RUNNING)) !== NONE) {
|
|
801
|
+
if ((flags & DISPOSED) !== NONE) throw new Error('Cannot read a disposed computed.');
|
|
802
|
+
throw new Error('Circular computed dependency.');
|
|
803
|
+
}
|
|
804
|
+
*/
|