@solidjs/signals 0.0.9 → 0.0.11

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 CHANGED
@@ -33,6 +33,107 @@ var EFFECT_RENDER = 1;
33
33
  var EFFECT_USER = 2;
34
34
  var SUPPORTS_PROXY = typeof Proxy === "function";
35
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
+ created = clock;
57
+ enqueue(type, node) {
58
+ this._queues[0].push(node);
59
+ if (type)
60
+ this._queues[type].push(node);
61
+ schedule();
62
+ }
63
+ run(type) {
64
+ if (this._queues[type].length) {
65
+ if (type === EFFECT_PURE) {
66
+ runPureQueue(this._queues[type]);
67
+ this._queues[type] = [];
68
+ } else {
69
+ const effects = this._queues[type];
70
+ this._queues[type] = [];
71
+ runEffectQueue(effects);
72
+ }
73
+ }
74
+ let rerun = false;
75
+ for (let i = 0; i < this._children.length; i++) {
76
+ rerun = this._children[i].run(type) || rerun;
77
+ }
78
+ if (type === EFFECT_PURE && this._queues[type].length)
79
+ return true;
80
+ }
81
+ flush() {
82
+ if (this._running)
83
+ return;
84
+ this._running = true;
85
+ try {
86
+ while (this.run(EFFECT_PURE)) {
87
+ }
88
+ incrementClock();
89
+ scheduled = false;
90
+ this.run(EFFECT_RENDER);
91
+ this.run(EFFECT_USER);
92
+ } finally {
93
+ this._running = false;
94
+ }
95
+ }
96
+ addChild(child) {
97
+ this._children.push(child);
98
+ }
99
+ removeChild(child) {
100
+ const index = this._children.indexOf(child);
101
+ if (index >= 0)
102
+ this._children.splice(index, 1);
103
+ }
104
+ };
105
+ var globalQueue = new Queue();
106
+ function flushSync() {
107
+ let count = 0;
108
+ while (scheduled) {
109
+ if (++count === 1e5)
110
+ throw new Error("Potential Infinite Loop Detected.");
111
+ globalQueue.flush();
112
+ }
113
+ }
114
+ function runTop(node) {
115
+ const ancestors = [];
116
+ for (let current = node; current !== null; current = current._parent) {
117
+ if (current._state !== STATE_CLEAN) {
118
+ ancestors.push(current);
119
+ }
120
+ }
121
+ for (let i = ancestors.length - 1; i >= 0; i--) {
122
+ if (ancestors[i]._state !== STATE_DISPOSED)
123
+ ancestors[i]._updateIfNecessary();
124
+ }
125
+ }
126
+ function runPureQueue(queue) {
127
+ for (let i = 0; i < queue.length; i++) {
128
+ if (queue[i]._state !== STATE_CLEAN)
129
+ runTop(queue[i]);
130
+ }
131
+ }
132
+ function runEffectQueue(queue) {
133
+ for (let i = 0; i < queue.length; i++)
134
+ queue[i]._runEffect();
135
+ }
136
+
36
137
  // src/core/utils.ts
37
138
  function isUndefined(value) {
38
139
  return typeof value === "undefined";
@@ -114,7 +215,7 @@ var Owner = class {
114
215
  _disposal = null;
115
216
  _context = defaultContext;
116
217
  _handlers = null;
117
- _queue = null;
218
+ _queue = globalQueue;
118
219
  constructor(signal = false) {
119
220
  if (currentOwner && !signal)
120
221
  currentOwner.append(this);
@@ -182,7 +283,7 @@ var Owner = class {
182
283
  let i = 0, len = this._handlers.length;
183
284
  for (i = 0; i < len; i++) {
184
285
  try {
185
- this._handlers[i](error);
286
+ this._handlers[i](error, this);
186
287
  break;
187
288
  } catch (e) {
188
289
  error = e;
@@ -246,19 +347,12 @@ var currentMask = DEFAULT_FLAGS;
246
347
  var newSources = null;
247
348
  var newSourcesIndex = 0;
248
349
  var newFlags = 0;
249
- var clock = 0;
250
350
  var notStale = false;
251
351
  var updateCheck = null;
252
352
  var staleCheck = null;
253
353
  function getObserver() {
254
354
  return currentObserver;
255
355
  }
256
- function getClock() {
257
- return clock;
258
- }
259
- function incrementClock() {
260
- clock++;
261
- }
262
356
  var UNCHANGED = Symbol("unchanged" );
263
357
  var Computation = class extends Owner {
264
358
  _sources = null;
@@ -292,8 +386,12 @@ var Computation = class extends Owner {
292
386
  this._unobserved = options?.unobserved;
293
387
  }
294
388
  _read() {
295
- if (this._compute)
296
- this._updateIfNecessary();
389
+ if (this._compute) {
390
+ if (this._stateFlags & ERROR_BIT && this._time <= getClock())
391
+ update(this);
392
+ else
393
+ this._updateIfNecessary();
394
+ }
297
395
  if (!this._compute || this._sources?.length)
298
396
  track(this);
299
397
  newFlags |= this._stateFlags & ~currentMask;
@@ -318,13 +416,13 @@ var Computation = class extends Owner {
318
416
  * before continuing
319
417
  */
320
418
  wait() {
321
- if (this._compute && this._stateFlags & ERROR_BIT && this._time <= clock) {
419
+ if (this._compute && this._stateFlags & ERROR_BIT && this._time <= getClock()) {
322
420
  update(this);
323
421
  }
324
422
  if ((notStale || this._stateFlags & UNINITIALIZED_BIT) && this.loading()) {
325
423
  throw new NotReadyError();
326
424
  }
327
- if (staleCheck && this._stateFlags & LOADING_BIT)
425
+ if (staleCheck && this.loading())
328
426
  staleCheck._value = true;
329
427
  return this._read();
330
428
  }
@@ -351,7 +449,7 @@ var Computation = class extends Owner {
351
449
  }
352
450
  const changedFlagsMask = this._stateFlags ^ flags, changedFlags = changedFlagsMask & flags;
353
451
  this._stateFlags = flags;
354
- this._time = clock + 1;
452
+ this._time = getClock() + 1;
355
453
  if (this._observers) {
356
454
  for (let i = 0; i < this._observers.length; i++) {
357
455
  if (valueChanged) {
@@ -524,7 +622,7 @@ function update(node) {
524
622
  newSources = prevSources;
525
623
  newSourcesIndex = prevSourcesIndex;
526
624
  newFlags = prevFlags;
527
- node._time = clock + 1;
625
+ node._time = getClock() + 1;
528
626
  node._state = STATE_CLEAN;
529
627
  }
530
628
  }
@@ -606,106 +704,13 @@ function compute(owner, fn, observer) {
606
704
  notStale = prevNotStale;
607
705
  }
608
706
  }
609
-
610
- // src/core/scheduler.ts
611
- var scheduled = false;
612
- function schedule() {
613
- if (scheduled)
614
- return;
615
- scheduled = true;
616
- if (!globalQueue._running)
617
- queueMicrotask(flushSync);
618
- }
619
- var Queue = class {
620
- _running = false;
621
- _queues = [[], [], []];
622
- _children = [];
623
- enqueue(type, node) {
624
- this._queues[0].push(node);
625
- if (type)
626
- this._queues[type].push(node);
627
- schedule();
628
- }
629
- run(type) {
630
- if (this._queues[type].length) {
631
- if (type === EFFECT_PURE) {
632
- runPureQueue(this._queues[type]);
633
- this._queues[type] = [];
634
- } else {
635
- const effects = this._queues[type];
636
- this._queues[type] = [];
637
- runEffectQueue(effects);
638
- }
639
- }
640
- let rerun = false;
641
- for (let i = 0; i < this._children.length; i++) {
642
- rerun = this._children[i].run(type) || rerun;
643
- }
644
- if (type === EFFECT_PURE && this._queues[type].length)
645
- return true;
646
- }
647
- flush() {
648
- if (this._running)
649
- return;
650
- this._running = true;
651
- try {
652
- while (this.run(EFFECT_PURE)) {
653
- }
654
- incrementClock();
655
- scheduled = false;
656
- this.run(EFFECT_RENDER);
657
- this.run(EFFECT_USER);
658
- } finally {
659
- this._running = false;
660
- }
661
- }
662
- addChild(child) {
663
- this._children.push(child);
664
- }
665
- removeChild(child) {
666
- const index = this._children.indexOf(child);
667
- if (index >= 0)
668
- this._children.splice(index, 1);
669
- }
670
- };
671
- var globalQueue = new Queue();
672
- function flushSync() {
673
- let count = 0;
674
- while (scheduled) {
675
- if (++count === 1e5)
676
- throw new Error("Potential Infinite Loop Detected.");
677
- globalQueue.flush();
678
- }
679
- }
680
707
  function createBoundary(fn, queue) {
681
708
  const owner = new Owner();
682
- const parentQueue = owner._queue || globalQueue;
709
+ const parentQueue = owner._queue;
683
710
  parentQueue.addChild(owner._queue = queue);
684
711
  onCleanup(() => parentQueue.removeChild(owner._queue));
685
712
  return compute(owner, fn, null);
686
713
  }
687
- function runTop(node) {
688
- const ancestors = [];
689
- for (let current = node; current !== null; current = current._parent) {
690
- if (current._state !== STATE_CLEAN) {
691
- ancestors.push(current);
692
- }
693
- }
694
- for (let i = ancestors.length - 1; i >= 0; i--) {
695
- if (ancestors[i]._state !== STATE_DISPOSED)
696
- ancestors[i]._updateIfNecessary();
697
- }
698
- }
699
- function runPureQueue(queue) {
700
- for (let i = 0; i < queue.length; i++) {
701
- if (queue[i]._state !== STATE_CLEAN)
702
- runTop(queue[i]);
703
- }
704
- }
705
- function runEffectQueue(queue) {
706
- for (let i = 0; i < queue.length; i++)
707
- queue[i]._runEffect();
708
- }
709
714
 
710
715
  // src/core/effect.ts
711
716
  var Effect = class extends Computation {
@@ -715,7 +720,6 @@ var Effect = class extends Computation {
715
720
  _modified = false;
716
721
  _prevValue;
717
722
  _type;
718
- _queue;
719
723
  constructor(initialValue, compute2, effect, error, options) {
720
724
  super(initialValue, compute2, options);
721
725
  this._effect = effect;
@@ -723,9 +727,8 @@ var Effect = class extends Computation {
723
727
  this._prevValue = initialValue;
724
728
  this._type = options?.render ? EFFECT_RENDER : EFFECT_USER;
725
729
  if (this._type === EFFECT_RENDER) {
726
- this._compute = (p) => latest(() => compute2(p));
730
+ this._compute = (p) => getClock() > this._queue.created ? latest(() => compute2(p)) : compute2(p);
727
731
  }
728
- this._queue = getOwner()?._queue || globalQueue;
729
732
  if (!options?.defer) {
730
733
  this._updateIfNecessary();
731
734
  this._type === EFFECT_USER ? this._queue.enqueue(this._type, this) : this._runEffect();
@@ -794,11 +797,9 @@ var Effect = class extends Computation {
794
797
  }
795
798
  };
796
799
  var EagerComputation = class extends Computation {
797
- _queue;
798
800
  constructor(initialValue, compute2, options) {
799
801
  super(initialValue, compute2, options);
800
- this._queue = getOwner()?._queue || globalQueue;
801
- this._updateIfNecessary();
802
+ !options?.defer && this._updateIfNecessary();
802
803
  if (!this._parent)
803
804
  console.warn("Eager Computations created outside a reactive context will never be disposed");
804
805
  }
@@ -811,10 +812,8 @@ var EagerComputation = class extends Computation {
811
812
  }
812
813
  };
813
814
  var ProjectionComputation = class extends Computation {
814
- _queue;
815
815
  constructor(compute2) {
816
816
  super(null, compute2);
817
- this._queue = getOwner()?._queue || globalQueue;
818
817
  if (!this._parent)
819
818
  console.warn("Eager Computations created outside a reactive context will never be disposed");
820
819
  }
@@ -856,8 +855,9 @@ var SuspenseQueue = class extends Queue {
856
855
  var LiveComputation = class extends EagerComputation {
857
856
  write(value, flags = 0) {
858
857
  const currentFlags = this._stateFlags;
858
+ const dirty = this._state === STATE_DIRTY;
859
859
  super.write(value, flags);
860
- if ((flags & LOADING_BIT) !== (currentFlags & LOADING_BIT)) {
860
+ if (dirty && (flags & LOADING_BIT) !== (currentFlags & LOADING_BIT)) {
861
861
  this._queue._update?.(this);
862
862
  }
863
863
  return this._value;
@@ -1000,15 +1000,30 @@ function runWithOwner(owner, run) {
1000
1000
  }
1001
1001
  function createErrorBoundary(fn, fallback) {
1002
1002
  const owner = new Owner();
1003
- const error = new Computation(null, null);
1004
- const reset = new Computation(null, null, { equals: false });
1005
- const handler = (err) => error.write({ _error: err });
1003
+ const error = new Computation(void 0, null);
1004
+ const nodes = /* @__PURE__ */ new Set();
1005
+ function handler(err, node) {
1006
+ if (nodes.has(node))
1007
+ return;
1008
+ compute(
1009
+ node,
1010
+ () => onCleanup(() => {
1011
+ nodes.delete(node);
1012
+ if (!nodes.size)
1013
+ error.write(void 0);
1014
+ }),
1015
+ null
1016
+ );
1017
+ nodes.add(node);
1018
+ if (nodes.size === 1)
1019
+ error.write({ _error: err });
1020
+ }
1006
1021
  owner._handlers = owner._handlers ? [handler, ...owner._handlers] : [handler];
1007
1022
  const guarded = compute(
1008
1023
  owner,
1009
1024
  () => {
1010
- const c = new Computation(null, () => (reset.read(), fn()));
1011
- const f = new Computation(null, () => flatten(c.read()));
1025
+ const c = new Computation(void 0, fn);
1026
+ const f = new EagerComputation(void 0, () => flatten(c.read()), { defer: true });
1012
1027
  f._setError = function(error2) {
1013
1028
  this.handleError(error2);
1014
1029
  };
@@ -1023,23 +1038,28 @@ function createErrorBoundary(fn, fallback) {
1023
1038
  return resolved;
1024
1039
  }
1025
1040
  return fallback(error.read()._error, () => {
1026
- error.write(null);
1027
- reset.write(null);
1041
+ incrementClock();
1042
+ for (let node of nodes) {
1043
+ node._state = STATE_DIRTY;
1044
+ node._queue?.enqueue(node._type, node);
1045
+ }
1028
1046
  });
1029
1047
  });
1030
1048
  return decision.read.bind(decision);
1031
1049
  }
1032
1050
  function resolve(fn) {
1033
1051
  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);
1052
+ createRoot((dispose) => {
1053
+ new EagerComputation(void 0, () => {
1054
+ try {
1055
+ res(fn());
1056
+ } catch (err) {
1057
+ if (err instanceof NotReadyError)
1058
+ throw err;
1059
+ rej(err);
1060
+ }
1061
+ dispose();
1062
+ });
1043
1063
  });
1044
1064
  });
1045
1065
  }