@solidjs/signals 0.7.2 → 0.7.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/dev.js CHANGED
@@ -1,5 +1,9 @@
1
1
  // src/core/error.ts
2
2
  var NotReadyError = class extends Error {
3
+ constructor(node) {
4
+ super();
5
+ this.cause = node;
6
+ }
3
7
  };
4
8
  var NoOwnerError = class extends Error {
5
9
  constructor() {
@@ -172,8 +176,10 @@ var Transition = class _Transition {
172
176
  _running = false;
173
177
  _scheduled = false;
174
178
  _cloned = globalQueue;
179
+ _signal;
175
180
  created = clock;
176
- constructor() {
181
+ constructor(signal) {
182
+ this._signal = signal;
177
183
  this._clonedQueues.set(globalQueue, this);
178
184
  for (const child of globalQueue._children) {
179
185
  cloneQueue(child, this, this);
@@ -202,6 +208,7 @@ var Transition = class _Transition {
202
208
  flush() {
203
209
  if (this._running || this._done)
204
210
  return;
211
+ globalQueue.flush();
205
212
  this._running = true;
206
213
  let currentTransition = ActiveTransition;
207
214
  ActiveTransition = this;
@@ -316,13 +323,14 @@ var Transition = class _Transition {
316
323
  this._optimistic.add(fn);
317
324
  }
318
325
  };
319
- function transition(fn) {
320
- let t = new Transition();
321
- queueMicrotask(() => t.runTransition(() => fn((fn2) => t.runTransition(fn2))));
322
- }
323
326
  function cloneGraph(node) {
324
- if (node._optimistic)
327
+ if (node._optimistic) {
328
+ if (node._state !== STATE_DISPOSED) {
329
+ node._optimistic._init?.();
330
+ ActiveTransition.addOptimistic(node._optimistic);
331
+ }
325
332
  return node;
333
+ }
326
334
  if (node._transition) {
327
335
  if (node._transition !== ActiveTransition) {
328
336
  mergeTransitions(node._transition, ActiveTransition);
@@ -484,6 +492,7 @@ function finishTransition(transition2) {
484
492
  delete reset._transition;
485
493
  reset();
486
494
  }
495
+ transition2._signal.write(true);
487
496
  globalQueue.flush();
488
497
  }
489
498
 
@@ -658,7 +667,7 @@ var Computation = class extends Owner {
658
667
  _forceNotify = false;
659
668
  _transition;
660
669
  _cloned;
661
- _optimistic = false;
670
+ _optimistic;
662
671
  constructor(initialValue, compute2, options) {
663
672
  super(options?.id, compute2 === null);
664
673
  this._compute = compute2;
@@ -722,11 +731,11 @@ var Computation = class extends Owner {
722
731
  this._updateIfNecessary();
723
732
  }
724
733
  if ((notStale || this._stateFlags & UNINITIALIZED_BIT) && this._stateFlags & LOADING_BIT) {
725
- track(this);
726
- throw new NotReadyError();
734
+ throw new NotReadyError(this);
727
735
  }
728
- if (staleCheck && this._stateFlags & LOADING_BIT) {
736
+ if (staleCheck && (this._stateFlags & LOADING_BIT || this._transition)) {
729
737
  staleCheck._value = true;
738
+ this._transition?._signal.read();
730
739
  }
731
740
  return this._read();
732
741
  }
@@ -889,6 +898,12 @@ function update(node) {
889
898
  node.write(result, newFlags, true);
890
899
  } catch (error) {
891
900
  if (error instanceof NotReadyError) {
901
+ if (error.cause !== node)
902
+ compute(
903
+ node,
904
+ () => track(error.cause),
905
+ node
906
+ );
892
907
  node.write(UNCHANGED, newFlags | LOADING_BIT | node._stateFlags & UNINITIALIZED_BIT);
893
908
  } else {
894
909
  node._setError(error);
@@ -962,8 +977,17 @@ function isPending(fn, loadingValue) {
962
977
  if (!currentObserver)
963
978
  return pendingCheck(fn, loadingValue);
964
979
  const c = new Computation(void 0, () => pendingCheck(fn, loadingValue));
980
+ c._optimistic = () => c.write(false);
981
+ c._optimistic._init = () => globalQueue.enqueue(0, () => c._optimistic._transition && c.write(true));
965
982
  c._handlerMask |= LOADING_BIT;
966
- return c.read();
983
+ const res = c.wait();
984
+ c._disposal = () => {
985
+ if (c._optimistic._transition) {
986
+ c._optimistic._transition._optimistic.delete(c._optimistic);
987
+ delete c._optimistic._transition;
988
+ }
989
+ };
990
+ return res;
967
991
  }
968
992
  function latest(fn, fallback) {
969
993
  const argLength = arguments.length;
@@ -1103,11 +1127,15 @@ var TrackedEffect = class extends Computation {
1103
1127
  _type = EFFECT_USER;
1104
1128
  _cleanup;
1105
1129
  constructor(compute2, options) {
1106
- super(void 0, () => {
1107
- this._cleanup?.();
1108
- this._cleanup = latest(compute2);
1109
- return void 0;
1110
- }, options);
1130
+ super(
1131
+ void 0,
1132
+ () => {
1133
+ this._cleanup?.();
1134
+ this._cleanup = latest(compute2);
1135
+ return void 0;
1136
+ },
1137
+ options
1138
+ );
1111
1139
  getQueue(this).enqueue(this._type, this._run.bind(this));
1112
1140
  if (!this._parent)
1113
1141
  console.warn("Effects created outside a reactive context will never be disposed");
@@ -1168,8 +1196,11 @@ var FirewallComputation = class extends Computation {
1168
1196
  _run() {
1169
1197
  const prevFlags = this._stateFlags;
1170
1198
  this._state !== STATE_CLEAN && runTop(this);
1171
- if (ActiveTransition && this._optimistic && this._stateFlags !== prevFlags)
1199
+ if (ActiveTransition && this._optimistic && (this._stateFlags !== prevFlags || this._stateFlags !== this._optimistic.flags)) {
1172
1200
  getQueue(this).notify(this, LOADING_BIT | ERROR_BIT, this._stateFlags);
1201
+ this._optimistic.flags = this._stateFlags;
1202
+ this._stateFlags = prevFlags;
1203
+ }
1173
1204
  }
1174
1205
  };
1175
1206
  function runTop(node) {
@@ -1191,10 +1222,13 @@ function runTop(node) {
1191
1222
  function createSignal(first, second, third) {
1192
1223
  if (typeof first === "function") {
1193
1224
  const node2 = new Computation(second, first, third);
1194
- return [node2.read.bind(node2), (v) => {
1195
- node2._updateIfNecessary();
1196
- return node2.write(v);
1197
- }];
1225
+ return [
1226
+ node2.wait.bind(node2),
1227
+ (v) => {
1228
+ node2._updateIfNecessary();
1229
+ return node2.write(v);
1230
+ }
1231
+ ];
1198
1232
  }
1199
1233
  const o = getOwner();
1200
1234
  const needsId = o?.id != null;
@@ -1286,7 +1320,7 @@ function createAsync(compute2, value, options) {
1286
1320
  }
1287
1321
  })();
1288
1322
  }
1289
- throw new NotReadyError();
1323
+ throw new NotReadyError(getOwner());
1290
1324
  },
1291
1325
  options
1292
1326
  );
@@ -1364,26 +1398,33 @@ function resolve(fn) {
1364
1398
  });
1365
1399
  }
1366
1400
  function createOptimistic(first, second, third) {
1367
- const node = typeof first === "function" ? new Computation(second, (prev) => {
1368
- const res = first(prev);
1369
- if (reset._transition)
1370
- return prev;
1371
- return res;
1372
- }, third) : new Computation(first, null, second);
1373
- node._optimistic = true;
1374
- const reset = () => node.write(first);
1401
+ const node = typeof first === "function" ? new Computation(
1402
+ second,
1403
+ (prev) => {
1404
+ const res = first(prev);
1405
+ if (node._optimistic._transition)
1406
+ return prev;
1407
+ return res;
1408
+ },
1409
+ third
1410
+ ) : new Computation(first, null, second);
1411
+ node._optimistic = () => node.write(first);
1375
1412
  function write(v) {
1376
1413
  if (!ActiveTransition)
1377
1414
  throw new Error("createOptimistic can only be updated inside a transition");
1378
- ActiveTransition.addOptimistic(reset);
1415
+ ActiveTransition.addOptimistic(node._optimistic);
1379
1416
  queueMicrotask(() => {
1380
- if (reset._transition) {
1417
+ if (node._optimistic._transition) {
1381
1418
  node._updateIfNecessary();
1382
1419
  node.write(v);
1383
1420
  }
1384
1421
  });
1385
1422
  }
1386
- return [node.read.bind(node), write];
1423
+ return [node.wait.bind(node), write];
1424
+ }
1425
+ function transition(fn) {
1426
+ let t = new Transition(new Computation(void 0, null));
1427
+ queueMicrotask(() => t.runTransition(() => fn((fn2) => t.runTransition(fn2))));
1387
1428
  }
1388
1429
  function useTransition() {
1389
1430
  const [pending, setPending] = createOptimistic(false);
@@ -1854,21 +1895,20 @@ function deep(store) {
1854
1895
  // src/store/optimistic.ts
1855
1896
  function createOptimisticStore(first, second, options) {
1856
1897
  const derived = typeof first === "function";
1857
- const { store, node } = derived ? createProjectionInternal((draft) => {
1858
- const res = first(draft);
1859
- if (reset._transition)
1860
- return draft;
1861
- return res;
1862
- }, second, options) : createProjectionInternal(() => {
1898
+ const { store, node } = derived ? createProjectionInternal(
1899
+ (draft) => {
1900
+ const res = first(draft);
1901
+ if (reset._transition)
1902
+ return draft;
1903
+ return res;
1904
+ },
1905
+ second,
1906
+ options
1907
+ ) : createProjectionInternal(() => {
1863
1908
  }, first);
1864
- node._optimistic = true;
1865
1909
  const reset = () => storeSetter(
1866
1910
  store,
1867
- reconcile(
1868
- derived ? first(store) || store : first,
1869
- options?.key || "id",
1870
- options?.all
1871
- )
1911
+ reconcile(derived ? first(store) || store : first, options?.key || "id", options?.all)
1872
1912
  );
1873
1913
  const write = (v) => {
1874
1914
  if (!ActiveTransition)
@@ -1876,6 +1916,7 @@ function createOptimisticStore(first, second, options) {
1876
1916
  ActiveTransition.addOptimistic(reset);
1877
1917
  queueMicrotask(() => reset._transition && storeSetter(store, v));
1878
1918
  };
1919
+ node._optimistic = reset;
1879
1920
  return [store, write];
1880
1921
  }
1881
1922
 
@@ -2302,7 +2343,7 @@ function createBoundChildren(owner, fn, queue, mask) {
2302
2343
  owner,
2303
2344
  () => {
2304
2345
  const c = new Computation(void 0, fn);
2305
- return new BoundaryComputation(() => flatten(c.wait()), mask);
2346
+ return new BoundaryComputation(() => latest(() => flatten(c.wait())), mask);
2306
2347
  },
2307
2348
  null
2308
2349
  );