@solidjs/signals 0.0.3 → 0.0.4

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/node.cjs CHANGED
@@ -27,7 +27,8 @@ function isUndefined(value) {
27
27
  var STATE_CLEAN = 0;
28
28
  var STATE_CHECK = 1;
29
29
  var STATE_DIRTY = 2;
30
- var STATE_DISPOSED = 3;
30
+ var STATE_UNINITIALIZED = 3;
31
+ var STATE_DISPOSED = 4;
31
32
  var EFFECT_PURE = 0;
32
33
  var EFFECT_RENDER = 1;
33
34
  var EFFECT_USER = 2;
@@ -48,9 +49,9 @@ var Owner = class {
48
49
  // We flatten the owner tree into a linked list so that we don't need a pointer to .firstChild
49
50
  // However, the children are actually added in reverse creation order
50
51
  // See comment at the top of the file for an example of the _nextSibling traversal
51
- r = null;
52
- m = null;
53
52
  o = null;
53
+ m = null;
54
+ p = null;
54
55
  a = STATE_CLEAN;
55
56
  h = null;
56
57
  n = defaultContext;
@@ -61,10 +62,10 @@ var Owner = class {
61
62
  currentOwner.append(this);
62
63
  }
63
64
  append(child) {
64
- child.r = this;
65
65
  child.o = this;
66
+ child.p = this;
66
67
  if (this.m)
67
- this.m.o = child;
68
+ this.m.p = child;
68
69
  child.m = this.m;
69
70
  this.m = child;
70
71
  if (child.n !== this.n) {
@@ -79,8 +80,8 @@ var Owner = class {
79
80
  dispose(self = true) {
80
81
  if (this.a === STATE_DISPOSED)
81
82
  return;
82
- let head = self ? this.o || this.r : this, current = this.m, next = null;
83
- while (current && current.r === this) {
83
+ let head = self ? this.p || this.o : this, current = this.m, next = null;
84
+ while (current && current.o === this) {
84
85
  current.dispose(true);
85
86
  current.x();
86
87
  next = current.m;
@@ -90,15 +91,15 @@ var Owner = class {
90
91
  if (self)
91
92
  this.x();
92
93
  if (current)
93
- current.o = !self ? this : this.o;
94
+ current.p = !self ? this : this.p;
94
95
  if (head)
95
96
  head.m = current;
96
97
  }
97
98
  x() {
98
- if (this.o)
99
- this.o.m = null;
100
- this.r = null;
99
+ if (this.p)
100
+ this.p.m = null;
101
101
  this.o = null;
102
+ this.p = null;
102
103
  this.n = defaultContext;
103
104
  this.i = null;
104
105
  this.a = STATE_DISPOSED;
@@ -158,17 +159,18 @@ function setContext(context, value, owner = currentOwner) {
158
159
  function hasContext(context, owner = currentOwner) {
159
160
  return !isUndefined(owner == null ? void 0 : owner.n[context.id]);
160
161
  }
161
- function onCleanup(disposable) {
162
+ function onCleanup(fn) {
162
163
  if (!currentOwner)
163
- return;
164
+ return fn;
164
165
  const node = currentOwner;
165
166
  if (!node.h) {
166
- node.h = disposable;
167
+ node.h = fn;
167
168
  } else if (Array.isArray(node.h)) {
168
- node.h.push(disposable);
169
+ node.h.push(fn);
169
170
  } else {
170
- node.h = [node.h, disposable];
171
+ node.h = [node.h, fn];
171
172
  }
173
+ return fn;
172
174
  }
173
175
 
174
176
  // src/core/flags.ts
@@ -198,32 +200,32 @@ var Computation = class extends Owner {
198
200
  b = null;
199
201
  c = null;
200
202
  d;
201
- D;
203
+ E;
202
204
  // Used in __DEV__ mode, hopefully removed in production
203
205
  V;
204
206
  // Using false is an optimization as an alternative to _equals: () => false
205
207
  // which could enable more efficient DIRTY notification
206
- E = isEqual;
208
+ F = isEqual;
207
209
  N;
208
210
  /** Whether the computation is an error or has ancestors that are unresolved */
209
211
  j = 0;
210
212
  /** Which flags raised by sources are handled, vs. being passed through. */
211
213
  A = DEFAULT_FLAGS;
212
- F = null;
213
214
  G = null;
214
- H = -1;
215
+ H = null;
216
+ I = -1;
215
217
  constructor(initialValue, compute2, options) {
216
218
  super(compute2 === null);
217
- this.D = compute2;
218
- this.a = compute2 ? STATE_DIRTY : STATE_CLEAN;
219
+ this.E = compute2;
220
+ this.a = compute2 ? STATE_UNINITIALIZED : STATE_CLEAN;
219
221
  this.d = initialValue;
220
222
  if ((options == null ? void 0 : options.equals) !== void 0)
221
- this.E = options.equals;
223
+ this.F = options.equals;
222
224
  if (options == null ? void 0 : options.unobserved)
223
225
  this.N = options == null ? void 0 : options.unobserved;
224
226
  }
225
227
  O() {
226
- if (this.D)
228
+ if (this.E)
227
229
  this.s();
228
230
  if (!this.b || this.b.length)
229
231
  track(this);
@@ -262,30 +264,30 @@ var Computation = class extends Owner {
262
264
  * loading state changes
263
265
  */
264
266
  loading() {
265
- if (this.G === null) {
266
- this.G = loadingState(this);
267
+ if (this.H === null) {
268
+ this.H = loadingState(this);
267
269
  }
268
- return this.G.read();
270
+ return this.H.read();
269
271
  }
270
272
  /**
271
273
  * Return true if the computation is the computation threw an error
272
274
  * Triggers re-execution of the computation when the error state changes
273
275
  */
274
276
  error() {
275
- if (this.F === null) {
276
- this.F = errorState(this);
277
+ if (this.G === null) {
278
+ this.G = errorState(this);
277
279
  }
278
- return this.F.read();
280
+ return this.G.read();
279
281
  }
280
282
  /** Update the computation with a new value. */
281
283
  write(value, flags = 0, raw = false) {
282
284
  const newValue = !raw && typeof value === "function" ? value(this.d) : value;
283
- const valueChanged = newValue !== UNCHANGED && (!!(flags & ERROR_BIT) || this.E === false || !this.E(this.d, newValue));
285
+ const valueChanged = newValue !== UNCHANGED && (!!(flags & ERROR_BIT) || this.a === STATE_UNINITIALIZED || this.F === false || !this.F(this.d, newValue));
284
286
  if (valueChanged)
285
287
  this.d = newValue;
286
288
  const changedFlagsMask = this.j ^ flags, changedFlags = changedFlagsMask & flags;
287
289
  this.j = flags;
288
- this.H = clock + 1;
290
+ this.I = clock + 1;
289
291
  if (this.c) {
290
292
  for (let i = 0; i < this.c.length; i++) {
291
293
  if (valueChanged) {
@@ -365,7 +367,7 @@ var Computation = class extends Owner {
365
367
  }
366
368
  }
367
369
  }
368
- if (this.a === STATE_DIRTY) {
370
+ if (this.a === STATE_DIRTY || this.a === STATE_UNINITIALIZED) {
369
371
  update(this);
370
372
  } else {
371
373
  this.write(UNCHANGED, observerFlags);
@@ -384,7 +386,7 @@ var Computation = class extends Owner {
384
386
  }
385
387
  };
386
388
  function loadingState(node) {
387
- const prevOwner = setOwner(node.r);
389
+ const prevOwner = setOwner(node.o);
388
390
  const options = void 0;
389
391
  const computation = new Computation(
390
392
  void 0,
@@ -400,7 +402,7 @@ function loadingState(node) {
400
402
  return computation;
401
403
  }
402
404
  function errorState(node) {
403
- const prevOwner = setOwner(node.r);
405
+ const prevOwner = setOwner(node.o);
404
406
  const options = void 0;
405
407
  const computation = new Computation(
406
408
  void 0,
@@ -425,7 +427,7 @@ function track(computation) {
425
427
  newSources.push(computation);
426
428
  }
427
429
  if (updateCheck) {
428
- updateCheck.d = computation.H > currentObserver.H;
430
+ updateCheck.d = computation.I > currentObserver.I;
429
431
  }
430
432
  }
431
433
  }
@@ -437,7 +439,7 @@ function update(node) {
437
439
  try {
438
440
  node.dispose(false);
439
441
  node.emptyDisposal();
440
- const result = compute(node, node.D, node);
442
+ const result = compute(node, node.E, node);
441
443
  node.write(result, newFlags, true);
442
444
  } catch (error) {
443
445
  if (error instanceof NotReadyError) {
@@ -546,10 +548,11 @@ function schedule() {
546
548
  if (scheduled)
547
549
  return;
548
550
  scheduled = true;
549
- queueMicrotask(flushSync);
551
+ if (!globalQueue.B)
552
+ queueMicrotask(flushSync);
550
553
  }
551
554
  var Queue = class {
552
- I = false;
555
+ B = false;
553
556
  u = [[], [], []];
554
557
  z = [];
555
558
  enqueue(type, node) {
@@ -574,16 +577,17 @@ var Queue = class {
574
577
  }
575
578
  }
576
579
  flush() {
577
- if (this.I)
580
+ if (this.B)
578
581
  return;
579
- this.I = true;
582
+ this.B = true;
580
583
  try {
581
584
  this.run(EFFECT_PURE);
582
585
  incrementClock();
586
+ scheduled = false;
583
587
  this.run(EFFECT_RENDER);
584
588
  this.run(EFFECT_USER);
585
589
  } finally {
586
- this.I = false;
590
+ this.B = false;
587
591
  }
588
592
  }
589
593
  addChild(child) {
@@ -597,8 +601,9 @@ var Queue = class {
597
601
  };
598
602
  var globalQueue = new Queue();
599
603
  function flushSync() {
600
- globalQueue.flush();
601
- scheduled = false;
604
+ while (scheduled) {
605
+ globalQueue.flush();
606
+ }
602
607
  }
603
608
  function createBoundary(fn, queue) {
604
609
  const owner = new Owner();
@@ -609,7 +614,7 @@ function createBoundary(fn, queue) {
609
614
  }
610
615
  function runTop(node) {
611
616
  const ancestors = [];
612
- for (let current = node; current !== null; current = current.r) {
617
+ for (let current = node; current !== null; current = current.o) {
613
618
  if (current.a !== STATE_CLEAN) {
614
619
  ancestors.push(current);
615
620
  }
@@ -634,18 +639,18 @@ function runEffectQueue(queue) {
634
639
  var Effect = class extends Computation {
635
640
  J;
636
641
  K = false;
637
- B;
638
642
  C;
643
+ D;
639
644
  e;
640
645
  constructor(initialValue, compute2, effect, options) {
641
646
  var _a;
642
647
  super(initialValue, compute2, options);
643
648
  this.J = effect;
644
- this.B = initialValue;
645
- this.C = (options == null ? void 0 : options.render) ? EFFECT_RENDER : EFFECT_USER;
649
+ this.C = initialValue;
650
+ this.D = (options == null ? void 0 : options.render) ? EFFECT_RENDER : EFFECT_USER;
646
651
  this.e = ((_a = getOwner()) == null ? void 0 : _a.e) || globalQueue;
647
652
  this.s();
648
- this.C === EFFECT_USER ? this.e.enqueue(this.C, this) : this.R();
653
+ this.D === EFFECT_USER ? this.e.enqueue(this.D, this) : this.R();
649
654
  }
650
655
  write(value, flags = 0) {
651
656
  var _a, _b;
@@ -664,7 +669,7 @@ var Effect = class extends Computation {
664
669
  if (this.a >= state)
665
670
  return;
666
671
  if (this.a === STATE_CLEAN)
667
- this.e.enqueue(this.C, this);
672
+ this.e.enqueue(this.D, this);
668
673
  this.a = state;
669
674
  }
670
675
  Q(error) {
@@ -672,13 +677,13 @@ var Effect = class extends Computation {
672
677
  }
673
678
  x() {
674
679
  this.J = void 0;
675
- this.B = void 0;
680
+ this.C = void 0;
676
681
  super.x();
677
682
  }
678
683
  R() {
679
684
  if (this.K && this.a !== STATE_DISPOSED) {
680
- this.J(this.d, this.B);
681
- this.B = this.d;
685
+ this.J(this.d, this.C);
686
+ this.C = this.d;
682
687
  this.K = false;
683
688
  }
684
689
  }
@@ -703,24 +708,24 @@ var EagerComputation = class extends Computation {
703
708
  // src/core/suspense.ts
704
709
  var SuspenseQueue = class extends Queue {
705
710
  f = /* @__PURE__ */ new Set();
706
- p = false;
711
+ q = false;
707
712
  L = new Computation(false, null);
708
713
  run(type) {
709
- if (type && this.p)
714
+ if (type && this.q)
710
715
  return;
711
716
  super.run(type);
712
717
  }
713
718
  T(node) {
714
719
  if (node.j & LOADING_BIT) {
715
720
  this.f.add(node);
716
- if (!this.p) {
717
- this.p = true;
721
+ if (!this.q) {
722
+ this.q = true;
718
723
  queueMicrotask(() => this.L.write(true));
719
724
  }
720
725
  } else {
721
726
  this.f.delete(node);
722
727
  if (this.f.size === 0) {
723
- this.p = false;
728
+ this.q = false;
724
729
  queueMicrotask(() => this.L.write(false));
725
730
  }
726
731
  }
@@ -729,7 +734,7 @@ var SuspenseQueue = class extends Queue {
729
734
  function createSuspense(fn, fallbackFn) {
730
735
  const queue = new SuspenseQueue();
731
736
  const tree = createBoundary(fn, queue);
732
- const equality = new Computation(null, () => queue.L.read() || queue.p);
737
+ const equality = new Computation(null, () => queue.L.read() || queue.q);
733
738
  const comp = new Computation(null, () => equality.read() ? untrack(fallbackFn) : tree);
734
739
  return comp.read.bind(comp);
735
740
  }
@@ -750,14 +755,36 @@ function createSignal(first, second, third) {
750
755
  const node = new Computation(first, null, second);
751
756
  return [node.read.bind(node), node.write.bind(node)];
752
757
  }
753
- function createAsync(fn, initial, options) {
758
+ function createMemo(compute2, value, options) {
759
+ let node = new Computation(
760
+ value,
761
+ compute2,
762
+ options
763
+ );
764
+ let resolvedValue;
765
+ return () => {
766
+ var _a, _b;
767
+ if (node) {
768
+ resolvedValue = node.wait();
769
+ if (!((_a = node.b) == null ? void 0 : _a.length)) {
770
+ node.dispose();
771
+ node = void 0;
772
+ } else if (!node.o && !((_b = node.c) == null ? void 0 : _b.length)) {
773
+ node.dispose();
774
+ node.a = STATE_UNINITIALIZED;
775
+ }
776
+ }
777
+ return resolvedValue;
778
+ };
779
+ }
780
+ function createAsync(compute2, value, options) {
754
781
  const lhs = new EagerComputation(
755
782
  {
756
- d: initial
783
+ d: value
757
784
  },
758
785
  (p) => {
759
- const value = p == null ? void 0 : p.d;
760
- const source = fn(value);
786
+ const value2 = p == null ? void 0 : p.d;
787
+ const source = compute2(value2);
761
788
  const isPromise = source instanceof Promise;
762
789
  const iterator = source[Symbol.asyncIterator];
763
790
  if (!isPromise && !iterator) {
@@ -768,12 +795,12 @@ function createAsync(fn, initial, options) {
768
795
  d: source
769
796
  };
770
797
  }
771
- const signal = new Computation(value, null, options);
798
+ const signal = new Computation(value2, null, options);
772
799
  signal.write(UNCHANGED, LOADING_BIT);
773
800
  if (isPromise) {
774
801
  source.then(
775
- (value2) => {
776
- signal.write(value2, 0);
802
+ (value3) => {
803
+ signal.write(value3, 0);
777
804
  },
778
805
  (error) => {
779
806
  signal.write(error, ERROR_BIT);
@@ -784,10 +811,10 @@ function createAsync(fn, initial, options) {
784
811
  onCleanup(() => abort = true);
785
812
  (async () => {
786
813
  try {
787
- for await (let value2 of source) {
814
+ for await (let value3 of source) {
788
815
  if (abort)
789
816
  return;
790
- signal.write(value2, 0);
817
+ signal.write(value3, 0);
791
818
  }
792
819
  } catch (error) {
793
820
  signal.write(error, ERROR_BIT);
@@ -799,29 +826,16 @@ function createAsync(fn, initial, options) {
799
826
  );
800
827
  return () => lhs.wait().wait();
801
828
  }
802
- function createMemo(compute2, initialValue, options) {
803
- let node = new Computation(initialValue, compute2, options);
804
- let value;
805
- return () => {
806
- var _a;
807
- if (node) {
808
- value = node.wait();
809
- if (!((_a = node.b) == null ? void 0 : _a.length))
810
- node = void 0;
811
- }
812
- return value;
813
- };
814
- }
815
- function createEffect(compute2, effect, initialValue, options) {
829
+ function createEffect(compute2, effect, value, options) {
816
830
  void new Effect(
817
- initialValue,
831
+ value,
818
832
  compute2,
819
833
  effect,
820
834
  void 0
821
835
  );
822
836
  }
823
- function createRenderEffect(compute2, effect, initialValue, options) {
824
- void new Effect(initialValue, compute2, effect, {
837
+ function createRenderEffect(compute2, effect, value, options) {
838
+ void new Effect(value, compute2, effect, {
825
839
  render: true,
826
840
  ...void 0
827
841
  });
@@ -1312,14 +1326,14 @@ function mapArray(list, map, options) {
1312
1326
  S: new Owner(),
1313
1327
  w: 0,
1314
1328
  U: list,
1315
- q: [],
1329
+ r: [],
1316
1330
  M: map,
1317
1331
  k: [],
1318
1332
  f: [],
1319
1333
  y: keyFn,
1320
1334
  g: keyFn || (options == null ? void 0 : options.keyed) === false ? [] : void 0,
1321
1335
  l: map.length > 1 ? [] : void 0,
1322
- p: options == null ? void 0 : options.fallback
1336
+ q: options == null ? void 0 : options.fallback
1323
1337
  });
1324
1338
  }
1325
1339
  function updateKeyedMap() {
@@ -1345,31 +1359,35 @@ function updateKeyedMap() {
1345
1359
  if (this.w !== 0) {
1346
1360
  this.S.dispose(false);
1347
1361
  this.f = [];
1348
- this.q = [];
1362
+ this.r = [];
1349
1363
  this.k = [];
1350
1364
  this.w = 0;
1351
1365
  this.g && (this.g = []);
1352
1366
  this.l && (this.l = []);
1353
1367
  }
1354
- if (this.p && !this.k[0]) {
1355
- this.k[0] = compute(this.f[0] = new Owner(), this.p, null);
1368
+ if (this.q && !this.k[0]) {
1369
+ this.k[0] = compute(
1370
+ this.f[0] = new Owner(),
1371
+ this.q,
1372
+ null
1373
+ );
1356
1374
  }
1357
1375
  } else if (this.w === 0) {
1358
1376
  if (this.f[0])
1359
1377
  this.f[0].dispose();
1360
1378
  this.k = new Array(newLen);
1361
1379
  for (j = 0; j < newLen; j++) {
1362
- this.q[j] = newItems[j];
1380
+ this.r[j] = newItems[j];
1363
1381
  this.k[j] = compute(this.f[j] = new Owner(), mapper, null);
1364
1382
  }
1365
1383
  this.w = newLen;
1366
1384
  } else {
1367
1385
  let start, end, newEnd, item, key, newIndices, newIndicesNext, temp = new Array(newLen), tempNodes = new Array(newLen), tempRows = this.g ? new Array(newLen) : void 0, tempIndexes = this.l ? new Array(newLen) : void 0;
1368
- for (start = 0, end = Math.min(this.w, newLen); start < end && (this.q[start] === newItems[start] || this.g && compare(this.y, this.q[start], newItems[start])); start++) {
1386
+ for (start = 0, end = Math.min(this.w, newLen); start < end && (this.r[start] === newItems[start] || this.g && compare(this.y, this.r[start], newItems[start])); start++) {
1369
1387
  if (this.g)
1370
1388
  this.g[start].write(newItems[start]);
1371
1389
  }
1372
- for (end = this.w - 1, newEnd = newLen - 1; end >= start && newEnd >= start && (this.q[end] === newItems[newEnd] || this.g && compare(this.y, this.q[end], newItems[newEnd])); end--, newEnd--) {
1390
+ for (end = this.w - 1, newEnd = newLen - 1; end >= start && newEnd >= start && (this.r[end] === newItems[newEnd] || this.g && compare(this.y, this.r[end], newItems[newEnd])); end--, newEnd--) {
1373
1391
  temp[newEnd] = this.k[end];
1374
1392
  tempNodes[newEnd] = this.f[end];
1375
1393
  tempRows && (tempRows[newEnd] = this.g[end]);
@@ -1385,7 +1403,7 @@ function updateKeyedMap() {
1385
1403
  newIndices.set(key, j);
1386
1404
  }
1387
1405
  for (i = start; i <= end; i++) {
1388
- item = this.q[i];
1406
+ item = this.r[i];
1389
1407
  key = this.y ? this.y(item) : item;
1390
1408
  j = newIndices.get(key);
1391
1409
  if (j !== void 0 && j !== -1) {
@@ -1415,7 +1433,7 @@ function updateKeyedMap() {
1415
1433
  }
1416
1434
  }
1417
1435
  this.k = this.k.slice(0, this.w = newLen);
1418
- this.q = newItems.slice(0);
1436
+ this.r = newItems.slice(0);
1419
1437
  }
1420
1438
  });
1421
1439
  return this.k;