@solidjs/signals 0.0.8 → 0.0.10
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/dist/dev.js +224 -175
- package/dist/node.cjs +424 -378
- package/dist/prod.js +422 -375
- package/dist/types/core/constants.d.ts +1 -2
- package/dist/types/core/core.d.ts +8 -11
- package/dist/types/core/effect.d.ts +9 -6
- package/dist/types/core/error.d.ts +2 -1
- package/dist/types/core/flags.d.ts +3 -0
- package/dist/types/core/index.d.ts +2 -2
- package/dist/types/core/owner.d.ts +2 -2
- package/dist/types/core/scheduler.d.ts +3 -2
- package/dist/types/index.d.ts +1 -1
- package/package.json +1 -1
package/dist/dev.js
CHANGED
|
@@ -27,13 +27,112 @@ var EffectError = class extends Error {
|
|
|
27
27
|
var STATE_CLEAN = 0;
|
|
28
28
|
var STATE_CHECK = 1;
|
|
29
29
|
var STATE_DIRTY = 2;
|
|
30
|
-
var
|
|
31
|
-
var STATE_DISPOSED = 4;
|
|
30
|
+
var STATE_DISPOSED = 3;
|
|
32
31
|
var EFFECT_PURE = 0;
|
|
33
32
|
var EFFECT_RENDER = 1;
|
|
34
33
|
var EFFECT_USER = 2;
|
|
35
34
|
var SUPPORTS_PROXY = typeof Proxy === "function";
|
|
36
35
|
|
|
36
|
+
// src/core/scheduler.ts
|
|
37
|
+
var clock = 0;
|
|
38
|
+
function getClock() {
|
|
39
|
+
return clock;
|
|
40
|
+
}
|
|
41
|
+
function incrementClock() {
|
|
42
|
+
clock++;
|
|
43
|
+
}
|
|
44
|
+
var scheduled = false;
|
|
45
|
+
function schedule() {
|
|
46
|
+
if (scheduled)
|
|
47
|
+
return;
|
|
48
|
+
scheduled = true;
|
|
49
|
+
if (!globalQueue._running)
|
|
50
|
+
queueMicrotask(flushSync);
|
|
51
|
+
}
|
|
52
|
+
var Queue = class {
|
|
53
|
+
_running = false;
|
|
54
|
+
_queues = [[], [], []];
|
|
55
|
+
_children = [];
|
|
56
|
+
enqueue(type, node) {
|
|
57
|
+
this._queues[0].push(node);
|
|
58
|
+
if (type)
|
|
59
|
+
this._queues[type].push(node);
|
|
60
|
+
schedule();
|
|
61
|
+
}
|
|
62
|
+
run(type) {
|
|
63
|
+
if (this._queues[type].length) {
|
|
64
|
+
if (type === EFFECT_PURE) {
|
|
65
|
+
runPureQueue(this._queues[type]);
|
|
66
|
+
this._queues[type] = [];
|
|
67
|
+
} else {
|
|
68
|
+
const effects = this._queues[type];
|
|
69
|
+
this._queues[type] = [];
|
|
70
|
+
runEffectQueue(effects);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
let rerun = false;
|
|
74
|
+
for (let i = 0; i < this._children.length; i++) {
|
|
75
|
+
rerun = this._children[i].run(type) || rerun;
|
|
76
|
+
}
|
|
77
|
+
if (type === EFFECT_PURE && this._queues[type].length)
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
flush() {
|
|
81
|
+
if (this._running)
|
|
82
|
+
return;
|
|
83
|
+
this._running = true;
|
|
84
|
+
try {
|
|
85
|
+
while (this.run(EFFECT_PURE)) {
|
|
86
|
+
}
|
|
87
|
+
incrementClock();
|
|
88
|
+
scheduled = false;
|
|
89
|
+
this.run(EFFECT_RENDER);
|
|
90
|
+
this.run(EFFECT_USER);
|
|
91
|
+
} finally {
|
|
92
|
+
this._running = false;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
addChild(child) {
|
|
96
|
+
this._children.push(child);
|
|
97
|
+
}
|
|
98
|
+
removeChild(child) {
|
|
99
|
+
const index = this._children.indexOf(child);
|
|
100
|
+
if (index >= 0)
|
|
101
|
+
this._children.splice(index, 1);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
var globalQueue = new Queue();
|
|
105
|
+
function flushSync() {
|
|
106
|
+
let count = 0;
|
|
107
|
+
while (scheduled) {
|
|
108
|
+
if (++count === 1e5)
|
|
109
|
+
throw new Error("Potential Infinite Loop Detected.");
|
|
110
|
+
globalQueue.flush();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function runTop(node) {
|
|
114
|
+
const ancestors = [];
|
|
115
|
+
for (let current = node; current !== null; current = current._parent) {
|
|
116
|
+
if (current._state !== STATE_CLEAN) {
|
|
117
|
+
ancestors.push(current);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
for (let i = ancestors.length - 1; i >= 0; i--) {
|
|
121
|
+
if (ancestors[i]._state !== STATE_DISPOSED)
|
|
122
|
+
ancestors[i]._updateIfNecessary();
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
function runPureQueue(queue) {
|
|
126
|
+
for (let i = 0; i < queue.length; i++) {
|
|
127
|
+
if (queue[i]._state !== STATE_CLEAN)
|
|
128
|
+
runTop(queue[i]);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function runEffectQueue(queue) {
|
|
132
|
+
for (let i = 0; i < queue.length; i++)
|
|
133
|
+
queue[i]._runEffect();
|
|
134
|
+
}
|
|
135
|
+
|
|
37
136
|
// src/core/utils.ts
|
|
38
137
|
function isUndefined(value) {
|
|
39
138
|
return typeof value === "undefined";
|
|
@@ -115,7 +214,7 @@ var Owner = class {
|
|
|
115
214
|
_disposal = null;
|
|
116
215
|
_context = defaultContext;
|
|
117
216
|
_handlers = null;
|
|
118
|
-
_queue =
|
|
217
|
+
_queue = globalQueue;
|
|
119
218
|
constructor(signal = false) {
|
|
120
219
|
if (currentOwner && !signal)
|
|
121
220
|
currentOwner.append(this);
|
|
@@ -183,7 +282,7 @@ var Owner = class {
|
|
|
183
282
|
let i = 0, len = this._handlers.length;
|
|
184
283
|
for (i = 0; i < len; i++) {
|
|
185
284
|
try {
|
|
186
|
-
this._handlers[i](error);
|
|
285
|
+
this._handlers[i](error, this);
|
|
187
286
|
break;
|
|
188
287
|
} catch (e) {
|
|
189
288
|
error = e;
|
|
@@ -237,6 +336,8 @@ var ERROR_OFFSET = 0;
|
|
|
237
336
|
var ERROR_BIT = 1 << ERROR_OFFSET;
|
|
238
337
|
var LOADING_OFFSET = 1;
|
|
239
338
|
var LOADING_BIT = 1 << LOADING_OFFSET;
|
|
339
|
+
var UNINITIALIZED_OFFSET = 2;
|
|
340
|
+
var UNINITIALIZED_BIT = 1 << UNINITIALIZED_OFFSET;
|
|
240
341
|
var DEFAULT_FLAGS = ERROR_BIT;
|
|
241
342
|
|
|
242
343
|
// src/core/core.ts
|
|
@@ -245,23 +346,18 @@ var currentMask = DEFAULT_FLAGS;
|
|
|
245
346
|
var newSources = null;
|
|
246
347
|
var newSourcesIndex = 0;
|
|
247
348
|
var newFlags = 0;
|
|
248
|
-
var
|
|
249
|
-
var syncResolve = false;
|
|
349
|
+
var notStale = false;
|
|
250
350
|
var updateCheck = null;
|
|
351
|
+
var staleCheck = null;
|
|
251
352
|
function getObserver() {
|
|
252
353
|
return currentObserver;
|
|
253
354
|
}
|
|
254
|
-
function getClock() {
|
|
255
|
-
return clock;
|
|
256
|
-
}
|
|
257
|
-
function incrementClock() {
|
|
258
|
-
clock++;
|
|
259
|
-
}
|
|
260
355
|
var UNCHANGED = Symbol("unchanged" );
|
|
261
356
|
var Computation = class extends Owner {
|
|
262
357
|
_sources = null;
|
|
263
358
|
_observers = null;
|
|
264
359
|
_value;
|
|
360
|
+
_error;
|
|
265
361
|
_compute;
|
|
266
362
|
// Used in __DEV__ mode, hopefully removed in production
|
|
267
363
|
_name;
|
|
@@ -279,7 +375,8 @@ var Computation = class extends Owner {
|
|
|
279
375
|
constructor(initialValue, compute2, options) {
|
|
280
376
|
super(compute2 === null);
|
|
281
377
|
this._compute = compute2;
|
|
282
|
-
this._state = compute2 ?
|
|
378
|
+
this._state = compute2 ? STATE_DIRTY : STATE_CLEAN;
|
|
379
|
+
this._stateFlags = compute2 && initialValue === void 0 ? UNINITIALIZED_BIT : 0;
|
|
283
380
|
this._value = initialValue;
|
|
284
381
|
this._name = options?.name ?? (this._compute ? "computed" : "signal");
|
|
285
382
|
if (options?.equals !== void 0)
|
|
@@ -288,13 +385,17 @@ var Computation = class extends Owner {
|
|
|
288
385
|
this._unobserved = options?.unobserved;
|
|
289
386
|
}
|
|
290
387
|
_read() {
|
|
291
|
-
if (this._compute)
|
|
292
|
-
this.
|
|
388
|
+
if (this._compute) {
|
|
389
|
+
if (this._stateFlags & ERROR_BIT && this._time <= getClock())
|
|
390
|
+
update(this);
|
|
391
|
+
else
|
|
392
|
+
this._updateIfNecessary();
|
|
393
|
+
}
|
|
293
394
|
if (!this._compute || this._sources?.length)
|
|
294
395
|
track(this);
|
|
295
396
|
newFlags |= this._stateFlags & ~currentMask;
|
|
296
397
|
if (this._stateFlags & ERROR_BIT) {
|
|
297
|
-
throw this.
|
|
398
|
+
throw this._error;
|
|
298
399
|
} else {
|
|
299
400
|
return this._value;
|
|
300
401
|
}
|
|
@@ -314,12 +415,14 @@ var Computation = class extends Owner {
|
|
|
314
415
|
* before continuing
|
|
315
416
|
*/
|
|
316
417
|
wait() {
|
|
317
|
-
if (this._compute && this._stateFlags & ERROR_BIT && this._time <=
|
|
418
|
+
if (this._compute && this._stateFlags & ERROR_BIT && this._time <= getClock()) {
|
|
318
419
|
update(this);
|
|
319
420
|
}
|
|
320
|
-
if (
|
|
421
|
+
if ((notStale || this._stateFlags & UNINITIALIZED_BIT) && this.loading()) {
|
|
321
422
|
throw new NotReadyError();
|
|
322
423
|
}
|
|
424
|
+
if (staleCheck && this._stateFlags & LOADING_BIT)
|
|
425
|
+
staleCheck._value = true;
|
|
323
426
|
return this._read();
|
|
324
427
|
}
|
|
325
428
|
/**
|
|
@@ -338,12 +441,14 @@ var Computation = class extends Owner {
|
|
|
338
441
|
/** Update the computation with a new value. */
|
|
339
442
|
write(value, flags = 0, raw = false) {
|
|
340
443
|
const newValue = !raw && typeof value === "function" ? value(this._value) : value;
|
|
341
|
-
const valueChanged = newValue !== UNCHANGED && (!!(
|
|
342
|
-
if (valueChanged)
|
|
444
|
+
const valueChanged = newValue !== UNCHANGED && (!!(this._stateFlags & UNINITIALIZED_BIT) || this._equals === false || !this._equals(this._value, newValue));
|
|
445
|
+
if (valueChanged) {
|
|
343
446
|
this._value = newValue;
|
|
447
|
+
this._error = void 0;
|
|
448
|
+
}
|
|
344
449
|
const changedFlagsMask = this._stateFlags ^ flags, changedFlags = changedFlagsMask & flags;
|
|
345
450
|
this._stateFlags = flags;
|
|
346
|
-
this._time =
|
|
451
|
+
this._time = getClock() + 1;
|
|
347
452
|
if (this._observers) {
|
|
348
453
|
for (let i = 0; i < this._observers.length; i++) {
|
|
349
454
|
if (valueChanged) {
|
|
@@ -398,7 +503,8 @@ var Computation = class extends Owner {
|
|
|
398
503
|
}
|
|
399
504
|
}
|
|
400
505
|
_setError(error) {
|
|
401
|
-
this.
|
|
506
|
+
this._error = error;
|
|
507
|
+
this.write(UNCHANGED, this._stateFlags & ~LOADING_BIT | ERROR_BIT | UNINITIALIZED_BIT);
|
|
402
508
|
}
|
|
403
509
|
/**
|
|
404
510
|
* This is the core part of the reactivity system, which makes sure that the values are updated
|
|
@@ -424,7 +530,7 @@ var Computation = class extends Owner {
|
|
|
424
530
|
}
|
|
425
531
|
}
|
|
426
532
|
}
|
|
427
|
-
if (this._state === STATE_DIRTY
|
|
533
|
+
if (this._state === STATE_DIRTY) {
|
|
428
534
|
update(this);
|
|
429
535
|
} else {
|
|
430
536
|
this.write(UNCHANGED, observerFlags);
|
|
@@ -484,7 +590,7 @@ function update(node) {
|
|
|
484
590
|
node.write(result, newFlags, true);
|
|
485
591
|
} catch (error) {
|
|
486
592
|
if (error instanceof NotReadyError) {
|
|
487
|
-
node.write(UNCHANGED, newFlags | LOADING_BIT);
|
|
593
|
+
node.write(UNCHANGED, newFlags | LOADING_BIT | node._stateFlags & UNINITIALIZED_BIT);
|
|
488
594
|
} else {
|
|
489
595
|
node._setError(error);
|
|
490
596
|
}
|
|
@@ -515,7 +621,7 @@ function update(node) {
|
|
|
515
621
|
newSources = prevSources;
|
|
516
622
|
newSourcesIndex = prevSourcesIndex;
|
|
517
623
|
newFlags = prevFlags;
|
|
518
|
-
node._time =
|
|
624
|
+
node._time = getClock() + 1;
|
|
519
625
|
node._state = STATE_CLEAN;
|
|
520
626
|
}
|
|
521
627
|
}
|
|
@@ -551,23 +657,27 @@ function hasUpdated(fn) {
|
|
|
551
657
|
updateCheck = current;
|
|
552
658
|
}
|
|
553
659
|
}
|
|
554
|
-
function
|
|
660
|
+
function isStale(fn) {
|
|
661
|
+
const current = staleCheck;
|
|
662
|
+
staleCheck = { _value: false };
|
|
555
663
|
try {
|
|
556
|
-
fn
|
|
557
|
-
return
|
|
558
|
-
} catch
|
|
559
|
-
|
|
664
|
+
latest(fn);
|
|
665
|
+
return staleCheck._value;
|
|
666
|
+
} catch {
|
|
667
|
+
} finally {
|
|
668
|
+
staleCheck = current;
|
|
560
669
|
}
|
|
670
|
+
return false;
|
|
561
671
|
}
|
|
562
|
-
function
|
|
672
|
+
function latest(fn) {
|
|
563
673
|
const prevFlags = newFlags;
|
|
564
|
-
|
|
674
|
+
const prevNotStale = notStale;
|
|
675
|
+
notStale = false;
|
|
565
676
|
try {
|
|
566
677
|
return fn();
|
|
567
|
-
} catch {
|
|
568
678
|
} finally {
|
|
569
679
|
newFlags = prevFlags;
|
|
570
|
-
|
|
680
|
+
notStale = prevNotStale;
|
|
571
681
|
}
|
|
572
682
|
}
|
|
573
683
|
function catchError(fn) {
|
|
@@ -579,135 +689,45 @@ function catchError(fn) {
|
|
|
579
689
|
return e;
|
|
580
690
|
}
|
|
581
691
|
}
|
|
582
|
-
function compute(owner,
|
|
583
|
-
const prevOwner = setOwner(owner), prevObserver = currentObserver, prevMask = currentMask;
|
|
692
|
+
function compute(owner, fn, observer) {
|
|
693
|
+
const prevOwner = setOwner(owner), prevObserver = currentObserver, prevMask = currentMask, prevNotStale = notStale;
|
|
584
694
|
currentObserver = observer;
|
|
585
695
|
currentMask = observer?._handlerMask ?? DEFAULT_FLAGS;
|
|
696
|
+
notStale = true;
|
|
586
697
|
try {
|
|
587
|
-
return
|
|
698
|
+
return fn(observer ? observer._value : void 0);
|
|
588
699
|
} finally {
|
|
589
700
|
setOwner(prevOwner);
|
|
590
701
|
currentObserver = prevObserver;
|
|
591
702
|
currentMask = prevMask;
|
|
592
|
-
|
|
593
|
-
}
|
|
594
|
-
|
|
595
|
-
// src/core/scheduler.ts
|
|
596
|
-
var scheduled = false;
|
|
597
|
-
function schedule() {
|
|
598
|
-
if (scheduled)
|
|
599
|
-
return;
|
|
600
|
-
scheduled = true;
|
|
601
|
-
if (!globalQueue._running)
|
|
602
|
-
queueMicrotask(flushSync);
|
|
603
|
-
}
|
|
604
|
-
var Queue = class {
|
|
605
|
-
_running = false;
|
|
606
|
-
_queues = [[], [], []];
|
|
607
|
-
_children = [];
|
|
608
|
-
enqueue(type, node) {
|
|
609
|
-
this._queues[0].push(node);
|
|
610
|
-
if (type)
|
|
611
|
-
this._queues[type].push(node);
|
|
612
|
-
schedule();
|
|
613
|
-
}
|
|
614
|
-
run(type) {
|
|
615
|
-
if (this._queues[type].length) {
|
|
616
|
-
if (type === EFFECT_PURE) {
|
|
617
|
-
runPureQueue(this._queues[type]);
|
|
618
|
-
this._queues[type] = [];
|
|
619
|
-
} else {
|
|
620
|
-
const effects = this._queues[type];
|
|
621
|
-
this._queues[type] = [];
|
|
622
|
-
runEffectQueue(effects);
|
|
623
|
-
}
|
|
624
|
-
}
|
|
625
|
-
let rerun = false;
|
|
626
|
-
for (let i = 0; i < this._children.length; i++) {
|
|
627
|
-
rerun = this._children[i].run(type) || rerun;
|
|
628
|
-
}
|
|
629
|
-
if (type === EFFECT_PURE && this._queues[type].length)
|
|
630
|
-
return true;
|
|
631
|
-
}
|
|
632
|
-
flush() {
|
|
633
|
-
if (this._running)
|
|
634
|
-
return;
|
|
635
|
-
this._running = true;
|
|
636
|
-
try {
|
|
637
|
-
while (this.run(EFFECT_PURE)) {
|
|
638
|
-
}
|
|
639
|
-
incrementClock();
|
|
640
|
-
scheduled = false;
|
|
641
|
-
this.run(EFFECT_RENDER);
|
|
642
|
-
this.run(EFFECT_USER);
|
|
643
|
-
} finally {
|
|
644
|
-
this._running = false;
|
|
645
|
-
}
|
|
646
|
-
}
|
|
647
|
-
addChild(child) {
|
|
648
|
-
this._children.push(child);
|
|
649
|
-
}
|
|
650
|
-
removeChild(child) {
|
|
651
|
-
const index = this._children.indexOf(child);
|
|
652
|
-
if (index >= 0)
|
|
653
|
-
this._children.splice(index, 1);
|
|
654
|
-
}
|
|
655
|
-
};
|
|
656
|
-
var globalQueue = new Queue();
|
|
657
|
-
function flushSync() {
|
|
658
|
-
let count = 0;
|
|
659
|
-
while (scheduled) {
|
|
660
|
-
if (++count === 1e5)
|
|
661
|
-
throw new Error("Potential Infinite Loop Detected.");
|
|
662
|
-
globalQueue.flush();
|
|
703
|
+
notStale = prevNotStale;
|
|
663
704
|
}
|
|
664
705
|
}
|
|
665
706
|
function createBoundary(fn, queue) {
|
|
666
707
|
const owner = new Owner();
|
|
667
|
-
const parentQueue = owner._queue
|
|
708
|
+
const parentQueue = owner._queue;
|
|
668
709
|
parentQueue.addChild(owner._queue = queue);
|
|
669
710
|
onCleanup(() => parentQueue.removeChild(owner._queue));
|
|
670
711
|
return compute(owner, fn, null);
|
|
671
712
|
}
|
|
672
|
-
function runTop(node) {
|
|
673
|
-
const ancestors = [];
|
|
674
|
-
for (let current = node; current !== null; current = current._parent) {
|
|
675
|
-
if (current._state !== STATE_CLEAN) {
|
|
676
|
-
ancestors.push(current);
|
|
677
|
-
}
|
|
678
|
-
}
|
|
679
|
-
for (let i = ancestors.length - 1; i >= 0; i--) {
|
|
680
|
-
if (ancestors[i]._state !== STATE_DISPOSED)
|
|
681
|
-
ancestors[i]._updateIfNecessary();
|
|
682
|
-
}
|
|
683
|
-
}
|
|
684
|
-
function runPureQueue(queue) {
|
|
685
|
-
for (let i = 0; i < queue.length; i++) {
|
|
686
|
-
if (queue[i]._state !== STATE_CLEAN)
|
|
687
|
-
runTop(queue[i]);
|
|
688
|
-
}
|
|
689
|
-
}
|
|
690
|
-
function runEffectQueue(queue) {
|
|
691
|
-
for (let i = 0; i < queue.length; i++)
|
|
692
|
-
queue[i]._runEffect();
|
|
693
|
-
}
|
|
694
713
|
|
|
695
714
|
// src/core/effect.ts
|
|
696
715
|
var Effect = class extends Computation {
|
|
697
716
|
_effect;
|
|
698
|
-
|
|
717
|
+
_onerror;
|
|
699
718
|
_cleanup;
|
|
700
719
|
_modified = false;
|
|
701
720
|
_prevValue;
|
|
702
721
|
_type;
|
|
703
|
-
_queue;
|
|
704
722
|
constructor(initialValue, compute2, effect, error, options) {
|
|
705
723
|
super(initialValue, compute2, options);
|
|
706
724
|
this._effect = effect;
|
|
707
|
-
this.
|
|
725
|
+
this._onerror = error;
|
|
708
726
|
this._prevValue = initialValue;
|
|
709
727
|
this._type = options?.render ? EFFECT_RENDER : EFFECT_USER;
|
|
710
|
-
this.
|
|
728
|
+
if (this._type === EFFECT_RENDER) {
|
|
729
|
+
this._compute = (p) => latest(() => compute2(p));
|
|
730
|
+
}
|
|
711
731
|
if (!options?.defer) {
|
|
712
732
|
this._updateIfNecessary();
|
|
713
733
|
this._type === EFFECT_USER ? this._queue.enqueue(this._type, this) : this._runEffect();
|
|
@@ -716,10 +736,12 @@ var Effect = class extends Computation {
|
|
|
716
736
|
console.warn("Effects created outside a reactive context will never be disposed");
|
|
717
737
|
}
|
|
718
738
|
write(value, flags = 0) {
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
this.
|
|
739
|
+
if (this._state == STATE_DIRTY) {
|
|
740
|
+
const currentFlags = this._stateFlags;
|
|
741
|
+
this._stateFlags = flags;
|
|
742
|
+
if (this._type === EFFECT_RENDER && (flags & LOADING_BIT) !== (currentFlags & LOADING_BIT)) {
|
|
743
|
+
this._queue._update?.(this);
|
|
744
|
+
}
|
|
723
745
|
}
|
|
724
746
|
if (value === UNCHANGED)
|
|
725
747
|
return this._value;
|
|
@@ -742,7 +764,7 @@ var Effect = class extends Computation {
|
|
|
742
764
|
}
|
|
743
765
|
if (this._type === EFFECT_USER) {
|
|
744
766
|
try {
|
|
745
|
-
return this.
|
|
767
|
+
return this._onerror ? this._cleanup = this._onerror(error) : console.error(new EffectError(this._effect, error));
|
|
746
768
|
} catch (e) {
|
|
747
769
|
error = e;
|
|
748
770
|
}
|
|
@@ -754,7 +776,7 @@ var Effect = class extends Computation {
|
|
|
754
776
|
return;
|
|
755
777
|
this._effect = void 0;
|
|
756
778
|
this._prevValue = void 0;
|
|
757
|
-
this.
|
|
779
|
+
this._onerror = void 0;
|
|
758
780
|
this._cleanup?.();
|
|
759
781
|
this._cleanup = void 0;
|
|
760
782
|
super._disposeNode();
|
|
@@ -774,11 +796,9 @@ var Effect = class extends Computation {
|
|
|
774
796
|
}
|
|
775
797
|
};
|
|
776
798
|
var EagerComputation = class extends Computation {
|
|
777
|
-
_queue;
|
|
778
799
|
constructor(initialValue, compute2, options) {
|
|
779
800
|
super(initialValue, compute2, options);
|
|
780
|
-
this.
|
|
781
|
-
this._updateIfNecessary();
|
|
801
|
+
!options?.defer && this._updateIfNecessary();
|
|
782
802
|
if (!this._parent)
|
|
783
803
|
console.warn("Eager Computations created outside a reactive context will never be disposed");
|
|
784
804
|
}
|
|
@@ -790,6 +810,20 @@ var EagerComputation = class extends Computation {
|
|
|
790
810
|
super._notify(state, skipQueue);
|
|
791
811
|
}
|
|
792
812
|
};
|
|
813
|
+
var ProjectionComputation = class extends Computation {
|
|
814
|
+
constructor(compute2) {
|
|
815
|
+
super(null, compute2);
|
|
816
|
+
if (!this._parent)
|
|
817
|
+
console.warn("Eager Computations created outside a reactive context will never be disposed");
|
|
818
|
+
}
|
|
819
|
+
_notify(state, skipQueue) {
|
|
820
|
+
if (this._state >= state && !this._forceNotify)
|
|
821
|
+
return;
|
|
822
|
+
if (this._state === STATE_CLEAN && !skipQueue)
|
|
823
|
+
this._queue.enqueue(EFFECT_PURE, this);
|
|
824
|
+
super._notify(state, true);
|
|
825
|
+
}
|
|
826
|
+
};
|
|
793
827
|
|
|
794
828
|
// src/core/suspense.ts
|
|
795
829
|
var SuspenseQueue = class extends Queue {
|
|
@@ -820,8 +854,9 @@ var SuspenseQueue = class extends Queue {
|
|
|
820
854
|
var LiveComputation = class extends EagerComputation {
|
|
821
855
|
write(value, flags = 0) {
|
|
822
856
|
const currentFlags = this._stateFlags;
|
|
857
|
+
const dirty = this._state === STATE_DIRTY;
|
|
823
858
|
super.write(value, flags);
|
|
824
|
-
if ((flags & LOADING_BIT) !== (currentFlags & LOADING_BIT)) {
|
|
859
|
+
if (dirty && (flags & LOADING_BIT) !== (currentFlags & LOADING_BIT)) {
|
|
825
860
|
this._queue._update?.(this);
|
|
826
861
|
}
|
|
827
862
|
return this._value;
|
|
@@ -869,13 +904,14 @@ function createMemo(compute2, value, options) {
|
|
|
869
904
|
node = void 0;
|
|
870
905
|
} else if (!node._parent && !node._observers?.length) {
|
|
871
906
|
node.dispose();
|
|
872
|
-
node._state =
|
|
907
|
+
node._state = STATE_DIRTY;
|
|
873
908
|
}
|
|
874
909
|
}
|
|
875
910
|
return resolvedValue;
|
|
876
911
|
};
|
|
877
912
|
}
|
|
878
913
|
function createAsync(compute2, value, options) {
|
|
914
|
+
let uninitialized = value === void 0;
|
|
879
915
|
const lhs = new EagerComputation(
|
|
880
916
|
{
|
|
881
917
|
_value: value
|
|
@@ -902,14 +938,16 @@ function createAsync(compute2, value, options) {
|
|
|
902
938
|
}
|
|
903
939
|
return w.call(this);
|
|
904
940
|
};
|
|
905
|
-
signal.write(UNCHANGED, LOADING_BIT);
|
|
941
|
+
signal.write(UNCHANGED, LOADING_BIT | (uninitialized ? UNINITIALIZED_BIT : 0));
|
|
906
942
|
if (isPromise) {
|
|
907
943
|
source.then(
|
|
908
944
|
(value3) => {
|
|
945
|
+
uninitialized = false;
|
|
909
946
|
signal.write(value3, 0, true);
|
|
910
947
|
},
|
|
911
948
|
(error) => {
|
|
912
|
-
|
|
949
|
+
uninitialized = true;
|
|
950
|
+
signal._setError(error);
|
|
913
951
|
}
|
|
914
952
|
);
|
|
915
953
|
} else {
|
|
@@ -961,15 +999,30 @@ function runWithOwner(owner, run) {
|
|
|
961
999
|
}
|
|
962
1000
|
function createErrorBoundary(fn, fallback) {
|
|
963
1001
|
const owner = new Owner();
|
|
964
|
-
const error = new Computation(
|
|
965
|
-
const
|
|
966
|
-
|
|
1002
|
+
const error = new Computation(void 0, null);
|
|
1003
|
+
const nodes = /* @__PURE__ */ new Set();
|
|
1004
|
+
function handler(err, node) {
|
|
1005
|
+
if (nodes.has(node))
|
|
1006
|
+
return;
|
|
1007
|
+
compute(
|
|
1008
|
+
node,
|
|
1009
|
+
() => onCleanup(() => {
|
|
1010
|
+
nodes.delete(node);
|
|
1011
|
+
if (!nodes.size)
|
|
1012
|
+
error.write(void 0);
|
|
1013
|
+
}),
|
|
1014
|
+
null
|
|
1015
|
+
);
|
|
1016
|
+
nodes.add(node);
|
|
1017
|
+
if (nodes.size === 1)
|
|
1018
|
+
error.write({ _error: err });
|
|
1019
|
+
}
|
|
967
1020
|
owner._handlers = owner._handlers ? [handler, ...owner._handlers] : [handler];
|
|
968
1021
|
const guarded = compute(
|
|
969
1022
|
owner,
|
|
970
1023
|
() => {
|
|
971
|
-
const c = new Computation(
|
|
972
|
-
const f = new
|
|
1024
|
+
const c = new Computation(void 0, fn);
|
|
1025
|
+
const f = new EagerComputation(void 0, () => flatten(c.read()), { defer: true });
|
|
973
1026
|
f._setError = function(error2) {
|
|
974
1027
|
this.handleError(error2);
|
|
975
1028
|
};
|
|
@@ -984,23 +1037,28 @@ function createErrorBoundary(fn, fallback) {
|
|
|
984
1037
|
return resolved;
|
|
985
1038
|
}
|
|
986
1039
|
return fallback(error.read()._error, () => {
|
|
987
|
-
|
|
988
|
-
|
|
1040
|
+
incrementClock();
|
|
1041
|
+
for (let node of nodes) {
|
|
1042
|
+
node._state = STATE_DIRTY;
|
|
1043
|
+
node._queue?.enqueue(node._type, node);
|
|
1044
|
+
}
|
|
989
1045
|
});
|
|
990
1046
|
});
|
|
991
1047
|
return decision.read.bind(decision);
|
|
992
1048
|
}
|
|
993
1049
|
function resolve(fn) {
|
|
994
1050
|
return new Promise((res, rej) => {
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1051
|
+
createRoot((dispose) => {
|
|
1052
|
+
new EagerComputation(void 0, () => {
|
|
1053
|
+
try {
|
|
1054
|
+
res(fn());
|
|
1055
|
+
} catch (err) {
|
|
1056
|
+
if (err instanceof NotReadyError)
|
|
1057
|
+
throw err;
|
|
1058
|
+
rej(err);
|
|
1059
|
+
}
|
|
1060
|
+
dispose();
|
|
1061
|
+
});
|
|
1004
1062
|
});
|
|
1005
1063
|
});
|
|
1006
1064
|
}
|
|
@@ -1019,18 +1077,9 @@ function createReaction(effect, error, options) {
|
|
|
1019
1077
|
}
|
|
1020
1078
|
|
|
1021
1079
|
// src/store/projection.ts
|
|
1022
|
-
var ProjectionComputation = class extends EagerComputation {
|
|
1023
|
-
_notify(state, skipQueue) {
|
|
1024
|
-
if (this._state >= state && !this._forceNotify)
|
|
1025
|
-
return;
|
|
1026
|
-
if (this._state === STATE_CLEAN && !skipQueue)
|
|
1027
|
-
this._queue.enqueue(EFFECT_PURE, this);
|
|
1028
|
-
super._notify(state, true);
|
|
1029
|
-
}
|
|
1030
|
-
};
|
|
1031
1080
|
function createProjection(fn, initialValue = {}) {
|
|
1032
1081
|
const [store, setStore] = createStore(initialValue);
|
|
1033
|
-
const node = new ProjectionComputation(
|
|
1082
|
+
const node = new ProjectionComputation(() => {
|
|
1034
1083
|
setStore(fn);
|
|
1035
1084
|
});
|
|
1036
1085
|
const wrapped = /* @__PURE__ */ new WeakMap();
|
|
@@ -1676,4 +1725,4 @@ function compare(key, a, b) {
|
|
|
1676
1725
|
return key ? key(a) === key(b) : true;
|
|
1677
1726
|
}
|
|
1678
1727
|
|
|
1679
|
-
export { $PROXY, $RAW, $TARGET, $TRACK, Computation, ContextNotFoundError, NoOwnerError, NotReadyError, Owner, Queue, SUPPORTS_PROXY, catchError, createAsync, createBoundary, createContext, createEffect, createErrorBoundary, createMemo, createProjection, createReaction, createRenderEffect, createRoot, createSignal, createStore, createSuspense, flatten, flushSync, getContext, getObserver, getOwner, hasContext, hasUpdated, isEqual,
|
|
1728
|
+
export { $PROXY, $RAW, $TARGET, $TRACK, Computation, ContextNotFoundError, NoOwnerError, NotReadyError, Owner, Queue, SUPPORTS_PROXY, catchError, createAsync, createBoundary, createContext, createEffect, createErrorBoundary, createMemo, createProjection, createReaction, createRenderEffect, createRoot, createSignal, createStore, createSuspense, flatten, flushSync, getContext, getObserver, getOwner, hasContext, hasUpdated, isEqual, isStale, isWrappable, latest, mapArray, merge, omit, onCleanup, reconcile, repeat, resolve, runWithOwner, setContext, untrack, unwrap };
|