@solidjs/signals 0.0.7 → 0.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/dist/dev.js +272 -107
- package/dist/node.cjs +557 -389
- package/dist/prod.js +549 -389
- package/dist/types/core/constants.d.ts +1 -2
- package/dist/types/core/core.d.ts +14 -13
- package/dist/types/core/effect.d.ts +12 -4
- package/dist/types/core/error.d.ts +3 -0
- package/dist/types/core/flags.d.ts +3 -0
- package/dist/types/core/index.d.ts +2 -2
- package/dist/types/core/scheduler.d.ts +0 -1
- package/dist/types/index.d.ts +2 -2
- package/dist/types/map.d.ts +9 -1
- package/dist/types/signals.d.ts +24 -3
- package/dist/types/store/index.d.ts +2 -1
- package/dist/types/store/projection.d.ts +6 -0
- package/dist/types/store/store.d.ts +0 -6
- package/package.json +1 -1
package/dist/dev.js
CHANGED
|
@@ -13,13 +13,21 @@ var ContextNotFoundError = class extends Error {
|
|
|
13
13
|
);
|
|
14
14
|
}
|
|
15
15
|
};
|
|
16
|
+
var EffectError = class extends Error {
|
|
17
|
+
constructor(effect, cause) {
|
|
18
|
+
super(`Uncaught error while running effect:
|
|
19
|
+
|
|
20
|
+
${effect.toString()}
|
|
21
|
+
` );
|
|
22
|
+
this.cause = cause;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
16
25
|
|
|
17
26
|
// src/core/constants.ts
|
|
18
27
|
var STATE_CLEAN = 0;
|
|
19
28
|
var STATE_CHECK = 1;
|
|
20
29
|
var STATE_DIRTY = 2;
|
|
21
|
-
var
|
|
22
|
-
var STATE_DISPOSED = 4;
|
|
30
|
+
var STATE_DISPOSED = 3;
|
|
23
31
|
var EFFECT_PURE = 0;
|
|
24
32
|
var EFFECT_RENDER = 1;
|
|
25
33
|
var EFFECT_USER = 2;
|
|
@@ -228,6 +236,8 @@ var ERROR_OFFSET = 0;
|
|
|
228
236
|
var ERROR_BIT = 1 << ERROR_OFFSET;
|
|
229
237
|
var LOADING_OFFSET = 1;
|
|
230
238
|
var LOADING_BIT = 1 << LOADING_OFFSET;
|
|
239
|
+
var UNINITIALIZED_OFFSET = 2;
|
|
240
|
+
var UNINITIALIZED_BIT = 1 << UNINITIALIZED_OFFSET;
|
|
231
241
|
var DEFAULT_FLAGS = ERROR_BIT;
|
|
232
242
|
|
|
233
243
|
// src/core/core.ts
|
|
@@ -237,11 +247,15 @@ var newSources = null;
|
|
|
237
247
|
var newSourcesIndex = 0;
|
|
238
248
|
var newFlags = 0;
|
|
239
249
|
var clock = 0;
|
|
240
|
-
var
|
|
250
|
+
var notStale = false;
|
|
241
251
|
var updateCheck = null;
|
|
252
|
+
var staleCheck = null;
|
|
242
253
|
function getObserver() {
|
|
243
254
|
return currentObserver;
|
|
244
255
|
}
|
|
256
|
+
function getClock() {
|
|
257
|
+
return clock;
|
|
258
|
+
}
|
|
245
259
|
function incrementClock() {
|
|
246
260
|
clock++;
|
|
247
261
|
}
|
|
@@ -250,6 +264,7 @@ var Computation = class extends Owner {
|
|
|
250
264
|
_sources = null;
|
|
251
265
|
_observers = null;
|
|
252
266
|
_value;
|
|
267
|
+
_error;
|
|
253
268
|
_compute;
|
|
254
269
|
// Used in __DEV__ mode, hopefully removed in production
|
|
255
270
|
_name;
|
|
@@ -261,13 +276,14 @@ var Computation = class extends Owner {
|
|
|
261
276
|
_stateFlags = 0;
|
|
262
277
|
/** Which flags raised by sources are handled, vs. being passed through. */
|
|
263
278
|
_handlerMask = DEFAULT_FLAGS;
|
|
264
|
-
_error = null;
|
|
265
279
|
_loading = null;
|
|
266
280
|
_time = -1;
|
|
281
|
+
_forceNotify = false;
|
|
267
282
|
constructor(initialValue, compute2, options) {
|
|
268
283
|
super(compute2 === null);
|
|
269
284
|
this._compute = compute2;
|
|
270
|
-
this._state = compute2 ?
|
|
285
|
+
this._state = compute2 ? STATE_DIRTY : STATE_CLEAN;
|
|
286
|
+
this._stateFlags = compute2 && initialValue === void 0 ? UNINITIALIZED_BIT : 0;
|
|
271
287
|
this._value = initialValue;
|
|
272
288
|
this._name = options?.name ?? (this._compute ? "computed" : "signal");
|
|
273
289
|
if (options?.equals !== void 0)
|
|
@@ -282,7 +298,7 @@ var Computation = class extends Owner {
|
|
|
282
298
|
track(this);
|
|
283
299
|
newFlags |= this._stateFlags & ~currentMask;
|
|
284
300
|
if (this._stateFlags & ERROR_BIT) {
|
|
285
|
-
throw this.
|
|
301
|
+
throw this._error;
|
|
286
302
|
} else {
|
|
287
303
|
return this._value;
|
|
288
304
|
}
|
|
@@ -302,9 +318,14 @@ var Computation = class extends Owner {
|
|
|
302
318
|
* before continuing
|
|
303
319
|
*/
|
|
304
320
|
wait() {
|
|
305
|
-
if (
|
|
321
|
+
if (this._compute && this._stateFlags & ERROR_BIT && this._time <= clock) {
|
|
322
|
+
update(this);
|
|
323
|
+
}
|
|
324
|
+
if ((notStale || this._stateFlags & UNINITIALIZED_BIT) && this.loading()) {
|
|
306
325
|
throw new NotReadyError();
|
|
307
326
|
}
|
|
327
|
+
if (staleCheck && this._stateFlags & LOADING_BIT)
|
|
328
|
+
staleCheck._value = true;
|
|
308
329
|
return this._read();
|
|
309
330
|
}
|
|
310
331
|
/**
|
|
@@ -320,22 +341,14 @@ var Computation = class extends Owner {
|
|
|
320
341
|
}
|
|
321
342
|
return this._loading.read();
|
|
322
343
|
}
|
|
323
|
-
/**
|
|
324
|
-
* Return true if the computation is the computation threw an error
|
|
325
|
-
* Triggers re-execution of the computation when the error state changes
|
|
326
|
-
*/
|
|
327
|
-
error() {
|
|
328
|
-
if (this._error === null) {
|
|
329
|
-
this._error = errorState(this);
|
|
330
|
-
}
|
|
331
|
-
return this._error.read();
|
|
332
|
-
}
|
|
333
344
|
/** Update the computation with a new value. */
|
|
334
345
|
write(value, flags = 0, raw = false) {
|
|
335
346
|
const newValue = !raw && typeof value === "function" ? value(this._value) : value;
|
|
336
|
-
const valueChanged = newValue !== UNCHANGED && (!!(
|
|
337
|
-
if (valueChanged)
|
|
347
|
+
const valueChanged = newValue !== UNCHANGED && (!!(this._stateFlags & UNINITIALIZED_BIT) || this._equals === false || !this._equals(this._value, newValue));
|
|
348
|
+
if (valueChanged) {
|
|
338
349
|
this._value = newValue;
|
|
350
|
+
this._error = void 0;
|
|
351
|
+
}
|
|
339
352
|
const changedFlagsMask = this._stateFlags ^ flags, changedFlags = changedFlagsMask & flags;
|
|
340
353
|
this._stateFlags = flags;
|
|
341
354
|
this._time = clock + 1;
|
|
@@ -353,13 +366,14 @@ var Computation = class extends Owner {
|
|
|
353
366
|
/**
|
|
354
367
|
* Set the current node's state, and recursively mark all of this node's observers as STATE_CHECK
|
|
355
368
|
*/
|
|
356
|
-
_notify(state) {
|
|
357
|
-
if (this._state >= state)
|
|
369
|
+
_notify(state, skipQueue) {
|
|
370
|
+
if (this._state >= state && !this._forceNotify)
|
|
358
371
|
return;
|
|
372
|
+
this._forceNotify = !!skipQueue;
|
|
359
373
|
this._state = state;
|
|
360
374
|
if (this._observers) {
|
|
361
375
|
for (let i = 0; i < this._observers.length; i++) {
|
|
362
|
-
this._observers[i]._notify(STATE_CHECK);
|
|
376
|
+
this._observers[i]._notify(STATE_CHECK, skipQueue);
|
|
363
377
|
}
|
|
364
378
|
}
|
|
365
379
|
}
|
|
@@ -392,7 +406,8 @@ var Computation = class extends Owner {
|
|
|
392
406
|
}
|
|
393
407
|
}
|
|
394
408
|
_setError(error) {
|
|
395
|
-
this.
|
|
409
|
+
this._error = error;
|
|
410
|
+
this.write(UNCHANGED, this._stateFlags & ~LOADING_BIT | ERROR_BIT | UNINITIALIZED_BIT);
|
|
396
411
|
}
|
|
397
412
|
/**
|
|
398
413
|
* This is the core part of the reactivity system, which makes sure that the values are updated
|
|
@@ -418,7 +433,7 @@ var Computation = class extends Owner {
|
|
|
418
433
|
}
|
|
419
434
|
}
|
|
420
435
|
}
|
|
421
|
-
if (this._state === STATE_DIRTY
|
|
436
|
+
if (this._state === STATE_DIRTY) {
|
|
422
437
|
update(this);
|
|
423
438
|
} else {
|
|
424
439
|
this.write(UNCHANGED, observerFlags);
|
|
@@ -452,22 +467,6 @@ function loadingState(node) {
|
|
|
452
467
|
setOwner(prevOwner);
|
|
453
468
|
return computation;
|
|
454
469
|
}
|
|
455
|
-
function errorState(node) {
|
|
456
|
-
const prevOwner = setOwner(node._parent);
|
|
457
|
-
const options = { name: node._name ? `error ${node._name}` : "error" } ;
|
|
458
|
-
const computation = new Computation(
|
|
459
|
-
void 0,
|
|
460
|
-
() => {
|
|
461
|
-
track(node);
|
|
462
|
-
node._updateIfNecessary();
|
|
463
|
-
return !!(node._stateFlags & ERROR_BIT);
|
|
464
|
-
},
|
|
465
|
-
options
|
|
466
|
-
);
|
|
467
|
-
computation._handlerMask = ERROR_BIT;
|
|
468
|
-
setOwner(prevOwner);
|
|
469
|
-
return computation;
|
|
470
|
-
}
|
|
471
470
|
function track(computation) {
|
|
472
471
|
if (currentObserver) {
|
|
473
472
|
if (!newSources && currentObserver._sources && currentObserver._sources[newSourcesIndex] === computation) {
|
|
@@ -494,7 +493,7 @@ function update(node) {
|
|
|
494
493
|
node.write(result, newFlags, true);
|
|
495
494
|
} catch (error) {
|
|
496
495
|
if (error instanceof NotReadyError) {
|
|
497
|
-
node.write(UNCHANGED, newFlags | LOADING_BIT);
|
|
496
|
+
node.write(UNCHANGED, newFlags | LOADING_BIT | node._stateFlags & UNINITIALIZED_BIT);
|
|
498
497
|
} else {
|
|
499
498
|
node._setError(error);
|
|
500
499
|
}
|
|
@@ -525,6 +524,7 @@ function update(node) {
|
|
|
525
524
|
newSources = prevSources;
|
|
526
525
|
newSourcesIndex = prevSourcesIndex;
|
|
527
526
|
newFlags = prevFlags;
|
|
527
|
+
node._time = clock + 1;
|
|
528
528
|
node._state = STATE_CLEAN;
|
|
529
529
|
}
|
|
530
530
|
}
|
|
@@ -560,35 +560,50 @@ function hasUpdated(fn) {
|
|
|
560
560
|
updateCheck = current;
|
|
561
561
|
}
|
|
562
562
|
}
|
|
563
|
-
function
|
|
563
|
+
function isStale(fn) {
|
|
564
|
+
const current = staleCheck;
|
|
565
|
+
staleCheck = { _value: false };
|
|
564
566
|
try {
|
|
565
|
-
fn
|
|
566
|
-
return
|
|
567
|
-
} catch
|
|
568
|
-
|
|
567
|
+
latest(fn);
|
|
568
|
+
return staleCheck._value;
|
|
569
|
+
} catch {
|
|
570
|
+
} finally {
|
|
571
|
+
staleCheck = current;
|
|
569
572
|
}
|
|
573
|
+
return false;
|
|
570
574
|
}
|
|
571
575
|
function latest(fn) {
|
|
572
576
|
const prevFlags = newFlags;
|
|
573
|
-
|
|
577
|
+
const prevNotStale = notStale;
|
|
578
|
+
notStale = false;
|
|
574
579
|
try {
|
|
575
580
|
return fn();
|
|
576
|
-
} catch {
|
|
577
581
|
} finally {
|
|
578
582
|
newFlags = prevFlags;
|
|
579
|
-
|
|
583
|
+
notStale = prevNotStale;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
function catchError(fn) {
|
|
587
|
+
try {
|
|
588
|
+
fn();
|
|
589
|
+
} catch (e) {
|
|
590
|
+
if (e instanceof NotReadyError)
|
|
591
|
+
throw e;
|
|
592
|
+
return e;
|
|
580
593
|
}
|
|
581
594
|
}
|
|
582
|
-
function compute(owner,
|
|
583
|
-
const prevOwner = setOwner(owner), prevObserver = currentObserver, prevMask = currentMask;
|
|
595
|
+
function compute(owner, fn, observer) {
|
|
596
|
+
const prevOwner = setOwner(owner), prevObserver = currentObserver, prevMask = currentMask, prevNotStale = notStale;
|
|
584
597
|
currentObserver = observer;
|
|
585
598
|
currentMask = observer?._handlerMask ?? DEFAULT_FLAGS;
|
|
599
|
+
notStale = true;
|
|
586
600
|
try {
|
|
587
|
-
return
|
|
601
|
+
return fn(observer ? observer._value : void 0);
|
|
588
602
|
} finally {
|
|
589
603
|
setOwner(prevOwner);
|
|
590
604
|
currentObserver = prevObserver;
|
|
591
605
|
currentMask = prevMask;
|
|
606
|
+
notStale = prevNotStale;
|
|
592
607
|
}
|
|
593
608
|
}
|
|
594
609
|
|
|
@@ -636,7 +651,6 @@ var Queue = class {
|
|
|
636
651
|
try {
|
|
637
652
|
while (this.run(EFFECT_PURE)) {
|
|
638
653
|
}
|
|
639
|
-
;
|
|
640
654
|
incrementClock();
|
|
641
655
|
scheduled = false;
|
|
642
656
|
this.run(EFFECT_RENDER);
|
|
@@ -655,22 +669,14 @@ var Queue = class {
|
|
|
655
669
|
}
|
|
656
670
|
};
|
|
657
671
|
var globalQueue = new Queue();
|
|
658
|
-
var globalTasks = [];
|
|
659
672
|
function flushSync() {
|
|
660
673
|
let count = 0;
|
|
661
674
|
while (scheduled) {
|
|
662
675
|
if (++count === 1e5)
|
|
663
676
|
throw new Error("Potential Infinite Loop Detected.");
|
|
664
677
|
globalQueue.flush();
|
|
665
|
-
for (let i = 0; i < globalTasks.length; i++)
|
|
666
|
-
globalTasks[i]();
|
|
667
|
-
globalTasks.length = 0;
|
|
668
678
|
}
|
|
669
679
|
}
|
|
670
|
-
function queueTask(fn) {
|
|
671
|
-
globalTasks.push(fn);
|
|
672
|
-
schedule();
|
|
673
|
-
}
|
|
674
680
|
function createBoundary(fn, queue) {
|
|
675
681
|
const owner = new Owner();
|
|
676
682
|
const parentQueue = owner._queue || globalQueue;
|
|
@@ -704,26 +710,36 @@ function runEffectQueue(queue) {
|
|
|
704
710
|
// src/core/effect.ts
|
|
705
711
|
var Effect = class extends Computation {
|
|
706
712
|
_effect;
|
|
713
|
+
_onerror;
|
|
714
|
+
_cleanup;
|
|
707
715
|
_modified = false;
|
|
708
716
|
_prevValue;
|
|
709
717
|
_type;
|
|
710
718
|
_queue;
|
|
711
|
-
constructor(initialValue, compute2, effect, options) {
|
|
719
|
+
constructor(initialValue, compute2, effect, error, options) {
|
|
712
720
|
super(initialValue, compute2, options);
|
|
713
721
|
this._effect = effect;
|
|
722
|
+
this._onerror = error;
|
|
714
723
|
this._prevValue = initialValue;
|
|
715
724
|
this._type = options?.render ? EFFECT_RENDER : EFFECT_USER;
|
|
725
|
+
if (this._type === EFFECT_RENDER) {
|
|
726
|
+
this._compute = (p) => latest(() => compute2(p));
|
|
727
|
+
}
|
|
716
728
|
this._queue = getOwner()?._queue || globalQueue;
|
|
717
|
-
|
|
718
|
-
|
|
729
|
+
if (!options?.defer) {
|
|
730
|
+
this._updateIfNecessary();
|
|
731
|
+
this._type === EFFECT_USER ? this._queue.enqueue(this._type, this) : this._runEffect();
|
|
732
|
+
}
|
|
719
733
|
if (!this._parent)
|
|
720
734
|
console.warn("Effects created outside a reactive context will never be disposed");
|
|
721
735
|
}
|
|
722
736
|
write(value, flags = 0) {
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
this.
|
|
737
|
+
if (this._state == STATE_DIRTY) {
|
|
738
|
+
const currentFlags = this._stateFlags;
|
|
739
|
+
this._stateFlags = flags;
|
|
740
|
+
if (this._type === EFFECT_RENDER && (flags & LOADING_BIT) !== (currentFlags & LOADING_BIT)) {
|
|
741
|
+
this._queue._update?.(this);
|
|
742
|
+
}
|
|
727
743
|
}
|
|
728
744
|
if (value === UNCHANGED)
|
|
729
745
|
return this._value;
|
|
@@ -731,26 +747,49 @@ var Effect = class extends Computation {
|
|
|
731
747
|
this._modified = true;
|
|
732
748
|
return value;
|
|
733
749
|
}
|
|
734
|
-
_notify(state) {
|
|
735
|
-
if (this._state >= state)
|
|
750
|
+
_notify(state, skipQueue) {
|
|
751
|
+
if (this._state >= state || skipQueue)
|
|
736
752
|
return;
|
|
737
753
|
if (this._state === STATE_CLEAN)
|
|
738
754
|
this._queue.enqueue(this._type, this);
|
|
739
755
|
this._state = state;
|
|
740
756
|
}
|
|
741
757
|
_setError(error) {
|
|
758
|
+
this._cleanup?.();
|
|
759
|
+
if (this._stateFlags & LOADING_BIT) {
|
|
760
|
+
this._stateFlags = 0;
|
|
761
|
+
this._queue._update?.(this);
|
|
762
|
+
}
|
|
763
|
+
if (this._type === EFFECT_USER) {
|
|
764
|
+
try {
|
|
765
|
+
return this._onerror ? this._cleanup = this._onerror(error) : console.error(new EffectError(this._effect, error));
|
|
766
|
+
} catch (e) {
|
|
767
|
+
error = e;
|
|
768
|
+
}
|
|
769
|
+
}
|
|
742
770
|
this.handleError(error);
|
|
743
771
|
}
|
|
744
772
|
_disposeNode() {
|
|
773
|
+
if (this._state === STATE_DISPOSED)
|
|
774
|
+
return;
|
|
745
775
|
this._effect = void 0;
|
|
746
776
|
this._prevValue = void 0;
|
|
777
|
+
this._onerror = void 0;
|
|
778
|
+
this._cleanup?.();
|
|
779
|
+
this._cleanup = void 0;
|
|
747
780
|
super._disposeNode();
|
|
748
781
|
}
|
|
749
782
|
_runEffect() {
|
|
750
783
|
if (this._modified && this._state !== STATE_DISPOSED) {
|
|
751
|
-
this.
|
|
752
|
-
|
|
753
|
-
|
|
784
|
+
this._cleanup?.();
|
|
785
|
+
try {
|
|
786
|
+
this._cleanup = this._effect(this._value, this._prevValue);
|
|
787
|
+
} catch (e) {
|
|
788
|
+
this.handleError(e);
|
|
789
|
+
} finally {
|
|
790
|
+
this._prevValue = this._value;
|
|
791
|
+
this._modified = false;
|
|
792
|
+
}
|
|
754
793
|
}
|
|
755
794
|
}
|
|
756
795
|
};
|
|
@@ -763,12 +802,28 @@ var EagerComputation = class extends Computation {
|
|
|
763
802
|
if (!this._parent)
|
|
764
803
|
console.warn("Eager Computations created outside a reactive context will never be disposed");
|
|
765
804
|
}
|
|
766
|
-
_notify(state) {
|
|
767
|
-
if (this._state >= state)
|
|
805
|
+
_notify(state, skipQueue) {
|
|
806
|
+
if (this._state >= state && !this._forceNotify)
|
|
768
807
|
return;
|
|
769
|
-
if (this._state === STATE_CLEAN)
|
|
808
|
+
if (this._state === STATE_CLEAN && !skipQueue)
|
|
809
|
+
this._queue.enqueue(EFFECT_PURE, this);
|
|
810
|
+
super._notify(state, skipQueue);
|
|
811
|
+
}
|
|
812
|
+
};
|
|
813
|
+
var ProjectionComputation = class extends Computation {
|
|
814
|
+
_queue;
|
|
815
|
+
constructor(compute2) {
|
|
816
|
+
super(null, compute2);
|
|
817
|
+
this._queue = getOwner()?._queue || globalQueue;
|
|
818
|
+
if (!this._parent)
|
|
819
|
+
console.warn("Eager Computations created outside a reactive context will never be disposed");
|
|
820
|
+
}
|
|
821
|
+
_notify(state, skipQueue) {
|
|
822
|
+
if (this._state >= state && !this._forceNotify)
|
|
823
|
+
return;
|
|
824
|
+
if (this._state === STATE_CLEAN && !skipQueue)
|
|
770
825
|
this._queue.enqueue(EFFECT_PURE, this);
|
|
771
|
-
super._notify(state);
|
|
826
|
+
super._notify(state, true);
|
|
772
827
|
}
|
|
773
828
|
};
|
|
774
829
|
|
|
@@ -787,13 +842,13 @@ var SuspenseQueue = class extends Queue {
|
|
|
787
842
|
this._nodes.add(node);
|
|
788
843
|
if (!this._fallback) {
|
|
789
844
|
this._fallback = true;
|
|
790
|
-
|
|
845
|
+
this._signal.write(true);
|
|
791
846
|
}
|
|
792
847
|
} else {
|
|
793
848
|
this._nodes.delete(node);
|
|
794
849
|
if (this._nodes.size === 0) {
|
|
795
850
|
this._fallback = false;
|
|
796
|
-
|
|
851
|
+
this._signal.write(false);
|
|
797
852
|
}
|
|
798
853
|
}
|
|
799
854
|
}
|
|
@@ -850,13 +905,14 @@ function createMemo(compute2, value, options) {
|
|
|
850
905
|
node = void 0;
|
|
851
906
|
} else if (!node._parent && !node._observers?.length) {
|
|
852
907
|
node.dispose();
|
|
853
|
-
node._state =
|
|
908
|
+
node._state = STATE_DIRTY;
|
|
854
909
|
}
|
|
855
910
|
}
|
|
856
911
|
return resolvedValue;
|
|
857
912
|
};
|
|
858
913
|
}
|
|
859
914
|
function createAsync(compute2, value, options) {
|
|
915
|
+
let uninitialized = value === void 0;
|
|
860
916
|
const lhs = new EagerComputation(
|
|
861
917
|
{
|
|
862
918
|
_value: value
|
|
@@ -875,14 +931,24 @@ function createAsync(compute2, value, options) {
|
|
|
875
931
|
};
|
|
876
932
|
}
|
|
877
933
|
const signal = new Computation(value2, null, options);
|
|
878
|
-
signal.
|
|
934
|
+
const w = signal.wait;
|
|
935
|
+
signal.wait = function() {
|
|
936
|
+
if (signal._stateFlags & ERROR_BIT && signal._time <= getClock()) {
|
|
937
|
+
lhs._notify(STATE_DIRTY);
|
|
938
|
+
throw new NotReadyError();
|
|
939
|
+
}
|
|
940
|
+
return w.call(this);
|
|
941
|
+
};
|
|
942
|
+
signal.write(UNCHANGED, LOADING_BIT | (uninitialized ? UNINITIALIZED_BIT : 0));
|
|
879
943
|
if (isPromise) {
|
|
880
944
|
source.then(
|
|
881
945
|
(value3) => {
|
|
946
|
+
uninitialized = false;
|
|
882
947
|
signal.write(value3, 0, true);
|
|
883
948
|
},
|
|
884
949
|
(error) => {
|
|
885
|
-
|
|
950
|
+
uninitialized = true;
|
|
951
|
+
signal._setError(error);
|
|
886
952
|
}
|
|
887
953
|
);
|
|
888
954
|
} else {
|
|
@@ -905,16 +971,17 @@ function createAsync(compute2, value, options) {
|
|
|
905
971
|
);
|
|
906
972
|
return () => lhs.wait().wait();
|
|
907
973
|
}
|
|
908
|
-
function createEffect(compute2, effect, value, options) {
|
|
974
|
+
function createEffect(compute2, effect, error, value, options) {
|
|
909
975
|
void new Effect(
|
|
910
976
|
value,
|
|
911
977
|
compute2,
|
|
912
978
|
effect,
|
|
979
|
+
error,
|
|
913
980
|
{ name: options?.name ?? "effect" }
|
|
914
981
|
);
|
|
915
982
|
}
|
|
916
983
|
function createRenderEffect(compute2, effect, value, options) {
|
|
917
|
-
void new Effect(value, compute2, effect, {
|
|
984
|
+
void new Effect(value, compute2, effect, void 0, {
|
|
918
985
|
render: true,
|
|
919
986
|
...{ name: options?.name ?? "effect" }
|
|
920
987
|
});
|
|
@@ -962,6 +1029,62 @@ function createErrorBoundary(fn, fallback) {
|
|
|
962
1029
|
});
|
|
963
1030
|
return decision.read.bind(decision);
|
|
964
1031
|
}
|
|
1032
|
+
function resolve(fn) {
|
|
1033
|
+
return new Promise((res, rej) => {
|
|
1034
|
+
let node = new EagerComputation(void 0, () => {
|
|
1035
|
+
try {
|
|
1036
|
+
res(fn());
|
|
1037
|
+
} catch (err) {
|
|
1038
|
+
if (err instanceof NotReadyError)
|
|
1039
|
+
throw err;
|
|
1040
|
+
rej(err);
|
|
1041
|
+
}
|
|
1042
|
+
node.dispose(true);
|
|
1043
|
+
});
|
|
1044
|
+
});
|
|
1045
|
+
}
|
|
1046
|
+
function createReaction(effect, error, options) {
|
|
1047
|
+
const node = new Effect(void 0, () => {
|
|
1048
|
+
}, effect, error, {
|
|
1049
|
+
defer: true,
|
|
1050
|
+
...{ name: options?.name ?? "reaction" }
|
|
1051
|
+
});
|
|
1052
|
+
return (tracking) => {
|
|
1053
|
+
node._compute = tracking;
|
|
1054
|
+
node._state = STATE_DIRTY;
|
|
1055
|
+
node._updateIfNecessary();
|
|
1056
|
+
node._compute = null;
|
|
1057
|
+
};
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
// src/store/projection.ts
|
|
1061
|
+
function createProjection(fn, initialValue = {}) {
|
|
1062
|
+
const [store, setStore] = createStore(initialValue);
|
|
1063
|
+
const node = new ProjectionComputation(() => {
|
|
1064
|
+
setStore(fn);
|
|
1065
|
+
});
|
|
1066
|
+
const wrapped = /* @__PURE__ */ new WeakMap();
|
|
1067
|
+
return wrap(store, node, wrapped);
|
|
1068
|
+
}
|
|
1069
|
+
function wrap(source, node, wrapped) {
|
|
1070
|
+
if (wrapped.has(source))
|
|
1071
|
+
return wrapped.get(source);
|
|
1072
|
+
const wrap3 = new Proxy(source, {
|
|
1073
|
+
get(target, property) {
|
|
1074
|
+
node.read();
|
|
1075
|
+
const v = target[property];
|
|
1076
|
+
return isWrappable(v) ? wrap3(v, node, wrapped) : v;
|
|
1077
|
+
},
|
|
1078
|
+
set() {
|
|
1079
|
+
throw new Error("Projections are readonly");
|
|
1080
|
+
},
|
|
1081
|
+
deleteProperty() {
|
|
1082
|
+
throw new Error("Projections are readonly");
|
|
1083
|
+
}
|
|
1084
|
+
});
|
|
1085
|
+
wrapped.set(source, wrap3);
|
|
1086
|
+
return wrap3;
|
|
1087
|
+
}
|
|
965
1088
|
|
|
966
1089
|
// src/store/store.ts
|
|
967
1090
|
var $RAW = Symbol("STORE_RAW" );
|
|
@@ -971,7 +1094,7 @@ var $PROXY = Symbol("STORE_PROXY" );
|
|
|
971
1094
|
var STORE_VALUE = "v";
|
|
972
1095
|
var STORE_NODE = "n";
|
|
973
1096
|
var STORE_HAS = "h";
|
|
974
|
-
function
|
|
1097
|
+
function wrap2(value) {
|
|
975
1098
|
let p = value[$PROXY];
|
|
976
1099
|
if (!p) {
|
|
977
1100
|
let target;
|
|
@@ -1080,7 +1203,7 @@ var proxyTraps = {
|
|
|
1080
1203
|
}
|
|
1081
1204
|
if (Writing.has(storeValue)) {
|
|
1082
1205
|
const value2 = tracked ? tracked._value : storeValue[property];
|
|
1083
|
-
return isWrappable(value2) ? (Writing.add(value2[$RAW] || value2),
|
|
1206
|
+
return isWrappable(value2) ? (Writing.add(value2[$RAW] || value2), wrap2(value2)) : value2;
|
|
1084
1207
|
}
|
|
1085
1208
|
let value = tracked ? nodes[property].read() : storeValue[property];
|
|
1086
1209
|
if (!tracked) {
|
|
@@ -1088,10 +1211,10 @@ var proxyTraps = {
|
|
|
1088
1211
|
let proto;
|
|
1089
1212
|
return !Array.isArray(storeValue) && (proto = Object.getPrototypeOf(storeValue)) && proto !== Object.prototype ? value.bind(storeValue) : value;
|
|
1090
1213
|
} else if (getObserver()) {
|
|
1091
|
-
value = getNode(nodes, property, isWrappable(value) ?
|
|
1214
|
+
value = getNode(nodes, property, isWrappable(value) ? wrap2(value) : value).read();
|
|
1092
1215
|
}
|
|
1093
1216
|
}
|
|
1094
|
-
return isWrappable(value) ?
|
|
1217
|
+
return isWrappable(value) ? wrap2(value) : value;
|
|
1095
1218
|
},
|
|
1096
1219
|
has(target, property) {
|
|
1097
1220
|
if (property === $RAW || property === $PROXY || property === $TRACK || property === "__proto__")
|
|
@@ -1133,13 +1256,16 @@ function setProperty(state, property, value, deleting = false) {
|
|
|
1133
1256
|
const nodes = getNodes(target, STORE_NODE);
|
|
1134
1257
|
let node;
|
|
1135
1258
|
if (node = nodes[property])
|
|
1136
|
-
node.write(isWrappable(value) ?
|
|
1259
|
+
node.write(isWrappable(value) ? wrap2(value) : value);
|
|
1137
1260
|
Array.isArray(state) && state.length !== len && (node = nodes.length) && node.write(state.length);
|
|
1138
1261
|
(node = nodes[$TRACK]) && node.write(void 0);
|
|
1139
1262
|
}
|
|
1140
1263
|
function createStore(first, second) {
|
|
1141
|
-
const derived = typeof first === "function", store = derived ? second : first
|
|
1142
|
-
|
|
1264
|
+
const derived = typeof first === "function", store = derived ? second : first;
|
|
1265
|
+
if (derived)
|
|
1266
|
+
return createProjection(first, store);
|
|
1267
|
+
const unwrappedStore = unwrap(store, false);
|
|
1268
|
+
const wrappedStore = wrap2(unwrappedStore);
|
|
1143
1269
|
const setStore = (fn) => {
|
|
1144
1270
|
try {
|
|
1145
1271
|
Writing.add(unwrappedStore);
|
|
@@ -1148,15 +1274,8 @@ function createStore(first, second) {
|
|
|
1148
1274
|
Writing.clear();
|
|
1149
1275
|
}
|
|
1150
1276
|
};
|
|
1151
|
-
if (derived) {
|
|
1152
|
-
new EagerComputation(void 0, () => setStore(first));
|
|
1153
|
-
}
|
|
1154
1277
|
return [wrappedStore, setStore];
|
|
1155
1278
|
}
|
|
1156
|
-
function createProjection(fn, initialValue = {}) {
|
|
1157
|
-
const [store] = createStore(fn, initialValue);
|
|
1158
|
-
return store;
|
|
1159
|
-
}
|
|
1160
1279
|
|
|
1161
1280
|
// src/store/reconcile.ts
|
|
1162
1281
|
function applyState(next, state, keyFn) {
|
|
@@ -1177,7 +1296,7 @@ function applyState(next, state, keyFn) {
|
|
|
1177
1296
|
if (next.length && previous.length && next[0] && keyFn(next[0]) != null) {
|
|
1178
1297
|
let i, j, start, end, newEnd, item, newIndicesNext, keyVal;
|
|
1179
1298
|
for (start = 0, end = Math.min(previous.length, next.length); start < end && (previous[start] === next[start] || previous[start] && next[start] && keyFn(previous[start]) === keyFn(next[start])); start++) {
|
|
1180
|
-
applyState(next[start],
|
|
1299
|
+
applyState(next[start], wrap2(previous[start]), keyFn);
|
|
1181
1300
|
}
|
|
1182
1301
|
const temp = new Array(next.length), newIndices = /* @__PURE__ */ new Map();
|
|
1183
1302
|
for (end = previous.length - 1, newEnd = next.length - 1; end >= start && newEnd >= start && (previous[end] === next[newEnd] || previous[end] && next[newEnd] && keyFn(previous[end]) === keyFn(next[newEnd])); end--, newEnd--) {
|
|
@@ -1186,11 +1305,11 @@ function applyState(next, state, keyFn) {
|
|
|
1186
1305
|
if (start > newEnd || start > end) {
|
|
1187
1306
|
for (j = start; j <= newEnd; j++) {
|
|
1188
1307
|
changed = true;
|
|
1189
|
-
target[STORE_NODE][j]?.write(
|
|
1308
|
+
target[STORE_NODE][j]?.write(wrap2(next[j]));
|
|
1190
1309
|
}
|
|
1191
1310
|
for (; j < next.length; j++) {
|
|
1192
1311
|
changed = true;
|
|
1193
|
-
const wrapped =
|
|
1312
|
+
const wrapped = wrap2(temp[j]);
|
|
1194
1313
|
target[STORE_NODE][j]?.write(wrapped);
|
|
1195
1314
|
applyState(next[j], wrapped, keyFn);
|
|
1196
1315
|
}
|
|
@@ -1218,17 +1337,17 @@ function applyState(next, state, keyFn) {
|
|
|
1218
1337
|
}
|
|
1219
1338
|
for (j = start; j < next.length; j++) {
|
|
1220
1339
|
if (j in temp) {
|
|
1221
|
-
const wrapped =
|
|
1340
|
+
const wrapped = wrap2(temp[j]);
|
|
1222
1341
|
target[STORE_NODE][j]?.write(wrapped);
|
|
1223
1342
|
applyState(next[j], wrapped, keyFn);
|
|
1224
1343
|
} else
|
|
1225
|
-
target[STORE_NODE][j]?.write(
|
|
1344
|
+
target[STORE_NODE][j]?.write(wrap2(next[j]));
|
|
1226
1345
|
}
|
|
1227
1346
|
if (start < next.length)
|
|
1228
1347
|
changed = true;
|
|
1229
1348
|
} else if (previous.length && next.length) {
|
|
1230
1349
|
for (let i = 0, len = next.length; i < len; i++) {
|
|
1231
|
-
isWrappable(previous[i]) && applyState(next[i],
|
|
1350
|
+
isWrappable(previous[i]) && applyState(next[i], wrap2(previous[i]), keyFn);
|
|
1232
1351
|
}
|
|
1233
1352
|
}
|
|
1234
1353
|
if (previous.length !== next.length) {
|
|
@@ -1248,9 +1367,9 @@ function applyState(next, state, keyFn) {
|
|
|
1248
1367
|
if (previousValue === nextValue)
|
|
1249
1368
|
continue;
|
|
1250
1369
|
if (!previousValue || !isWrappable(previousValue) || keyFn(previousValue) != null && keyFn(previousValue) !== keyFn(nextValue))
|
|
1251
|
-
node.write(isWrappable(nextValue) ?
|
|
1370
|
+
node.write(isWrappable(nextValue) ? wrap2(nextValue) : nextValue);
|
|
1252
1371
|
else
|
|
1253
|
-
applyState(nextValue,
|
|
1372
|
+
applyState(nextValue, wrap2(previousValue), keyFn);
|
|
1254
1373
|
}
|
|
1255
1374
|
}
|
|
1256
1375
|
if (nodes = target[STORE_HAS]) {
|
|
@@ -1537,8 +1656,54 @@ function updateKeyedMap() {
|
|
|
1537
1656
|
});
|
|
1538
1657
|
return this._mappings;
|
|
1539
1658
|
}
|
|
1659
|
+
function repeat(count, map, options) {
|
|
1660
|
+
return updateRepeat.bind({
|
|
1661
|
+
_owner: new Owner(),
|
|
1662
|
+
_len: 0,
|
|
1663
|
+
_count: count,
|
|
1664
|
+
_map: map,
|
|
1665
|
+
_nodes: [],
|
|
1666
|
+
_mappings: [],
|
|
1667
|
+
_fallback: options?.fallback
|
|
1668
|
+
});
|
|
1669
|
+
}
|
|
1670
|
+
function updateRepeat() {
|
|
1671
|
+
const newLen = this._count();
|
|
1672
|
+
runWithOwner(this._owner, () => {
|
|
1673
|
+
if (newLen === 0) {
|
|
1674
|
+
if (this._len !== 0) {
|
|
1675
|
+
this._owner.dispose(false);
|
|
1676
|
+
this._nodes = [];
|
|
1677
|
+
this._mappings = [];
|
|
1678
|
+
this._len = 0;
|
|
1679
|
+
}
|
|
1680
|
+
if (this._fallback && !this._mappings[0]) {
|
|
1681
|
+
this._mappings[0] = compute(
|
|
1682
|
+
this._nodes[0] = new Owner(),
|
|
1683
|
+
this._fallback,
|
|
1684
|
+
null
|
|
1685
|
+
);
|
|
1686
|
+
}
|
|
1687
|
+
} else {
|
|
1688
|
+
if (this._len === 0 && this._nodes[0])
|
|
1689
|
+
this._nodes[0].dispose();
|
|
1690
|
+
for (let i = this._len; i < newLen; i++) {
|
|
1691
|
+
this._mappings[i] = compute(
|
|
1692
|
+
this._nodes[i] = new Owner(),
|
|
1693
|
+
() => this._map(i),
|
|
1694
|
+
null
|
|
1695
|
+
);
|
|
1696
|
+
}
|
|
1697
|
+
for (let i = newLen; i < this._len; i++)
|
|
1698
|
+
this._nodes[i].dispose();
|
|
1699
|
+
this._mappings = this._mappings.slice(0, newLen);
|
|
1700
|
+
this._len = newLen;
|
|
1701
|
+
}
|
|
1702
|
+
});
|
|
1703
|
+
return this._mappings;
|
|
1704
|
+
}
|
|
1540
1705
|
function compare(key, a, b) {
|
|
1541
1706
|
return key ? key(a) === key(b) : true;
|
|
1542
1707
|
}
|
|
1543
1708
|
|
|
1544
|
-
export { $PROXY, $RAW, $TARGET, $TRACK, Computation, ContextNotFoundError, NoOwnerError, NotReadyError, Owner, Queue, SUPPORTS_PROXY, createAsync, createBoundary, createContext, createEffect, createErrorBoundary, createMemo, createProjection, createRenderEffect, createRoot, createSignal, createStore, createSuspense, flatten, flushSync, getContext, getObserver, getOwner, hasContext, hasUpdated, isEqual,
|
|
1709
|
+
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 };
|