@solidjs/signals 0.7.0 → 0.7.2

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
@@ -200,7 +200,7 @@ var Transition = class _Transition {
200
200
  }
201
201
  }
202
202
  flush() {
203
- if (this._running)
203
+ if (this._running || this._done)
204
204
  return;
205
205
  this._running = true;
206
206
  let currentTransition = ActiveTransition;
@@ -239,6 +239,9 @@ var Transition = class _Transition {
239
239
  this._queues[0].push.apply(this._queues[0], queue._queues[0]);
240
240
  this._queues[1].push.apply(this._queues[1], queue._queues[1]);
241
241
  this._pureQueue.push.apply(this._pureQueue, queue._pureQueue);
242
+ queue._queues[0].length = 0;
243
+ queue._queues[1].length = 0;
244
+ queue._pureQueue.length = 0;
242
245
  for (let i = 0; i < queue._children.length; i++) {
243
246
  const og = this._children.find((c) => c._cloned === queue._children[i]._cloned);
244
247
  if (og)
@@ -271,6 +274,7 @@ var Transition = class _Transition {
271
274
  (async function() {
272
275
  let temp, value;
273
276
  while (!(temp = result.next(value)).done) {
277
+ transition2 = ActiveTransition;
274
278
  if (temp.value instanceof Promise) {
275
279
  transition2._promises.add(temp.value);
276
280
  try {
@@ -316,7 +320,9 @@ function transition(fn) {
316
320
  let t = new Transition();
317
321
  queueMicrotask(() => t.runTransition(() => fn((fn2) => t.runTransition(fn2))));
318
322
  }
319
- function cloneGraph(node, optimistic) {
323
+ function cloneGraph(node) {
324
+ if (node._optimistic)
325
+ return node;
320
326
  if (node._transition) {
321
327
  if (node._transition !== ActiveTransition) {
322
328
  mergeTransitions(node._transition, ActiveTransition);
@@ -330,20 +336,19 @@ function cloneGraph(node, optimistic) {
330
336
  _nextSibling: null,
331
337
  _observers: null,
332
338
  _sources: node._sources ? [...node._sources] : null,
333
- _cloned: node,
334
- _optimistic: !!optimistic
339
+ _cloned: node
335
340
  });
336
341
  delete clone._prevValue;
337
342
  ActiveTransition._sources.set(node, clone);
338
343
  node._transition = ActiveTransition;
339
- if (!optimistic && node._sources) {
344
+ if (node._sources) {
340
345
  for (let i = 0; i < node._sources.length; i++)
341
346
  node._sources[i]._observers.push(clone);
342
347
  }
343
348
  if (node._observers) {
344
349
  clone._observers = [];
345
350
  for (let i = 0, length = node._observers.length; i < length; i++) {
346
- !node._observers[i]._cloned && clone._observers.push(cloneGraph(node._observers[i], optimistic));
351
+ !node._observers[i]._cloned && clone._observers.push(cloneGraph(node._observers[i]));
347
352
  }
348
353
  }
349
354
  return clone;
@@ -455,12 +460,6 @@ function finishTransition(transition2) {
455
460
  }
456
461
  if (clone._sources)
457
462
  replaceSourceObservers(clone, transition2);
458
- if (clone._optimistic) {
459
- clone.dispose();
460
- clone.emptyDisposal();
461
- delete source._transition;
462
- continue;
463
- }
464
463
  if (clone._updated || clone._state === STATE_DISPOSED) {
465
464
  source.dispose(clone._state === STATE_DISPOSED);
466
465
  source.emptyDisposal();
@@ -750,7 +749,7 @@ var Computation = class extends Owner {
750
749
  const changedFlagsMask = this._stateFlags ^ flags, changedFlags = changedFlagsMask & flags;
751
750
  this._stateFlags = flags;
752
751
  this._time = clock + 1;
753
- if (this._observers) {
752
+ if (this._observers && !(this._optimistic && ActiveTransition)) {
754
753
  for (let i = 0; i < this._observers.length; i++) {
755
754
  if (valueChanged) {
756
755
  this._observers[i]._notify(STATE_DIRTY);
@@ -769,7 +768,7 @@ var Computation = class extends Owner {
769
768
  return;
770
769
  this._forceNotify = !!skipQueue;
771
770
  this._state = state;
772
- if (this._observers) {
771
+ if (this._observers && !(this._optimistic && ActiveTransition)) {
773
772
  for (let i = 0; i < this._observers.length; i++) {
774
773
  this._observers[i]._notify(STATE_CHECK, skipQueue);
775
774
  }
@@ -784,7 +783,7 @@ var Computation = class extends Owner {
784
783
  _notifyFlags(mask, newFlags2) {
785
784
  if (this._state >= STATE_DIRTY)
786
785
  return;
787
- if (mask & this._handlerMask) {
786
+ if (mask & this._handlerMask || this._optimistic && ActiveTransition) {
788
787
  this._notify(STATE_DIRTY);
789
788
  return;
790
789
  }
@@ -1167,7 +1166,10 @@ var FirewallComputation = class extends Computation {
1167
1166
  this._forceNotify = !!skipQueue;
1168
1167
  }
1169
1168
  _run() {
1169
+ const prevFlags = this._stateFlags;
1170
1170
  this._state !== STATE_CLEAN && runTop(this);
1171
+ if (ActiveTransition && this._optimistic && this._stateFlags !== prevFlags)
1172
+ getQueue(this).notify(this, LOADING_BIT | ERROR_BIT, this._stateFlags);
1171
1173
  }
1172
1174
  };
1173
1175
  function runTop(node) {
@@ -1189,7 +1191,10 @@ function runTop(node) {
1189
1191
  function createSignal(first, second, third) {
1190
1192
  if (typeof first === "function") {
1191
1193
  const node2 = new Computation(second, first, third);
1192
- return [node2.read.bind(node2), node2.write.bind(node2)];
1194
+ return [node2.read.bind(node2), (v) => {
1195
+ node2._updateIfNecessary();
1196
+ return node2.write(v);
1197
+ }];
1193
1198
  }
1194
1199
  const o = getOwner();
1195
1200
  const needsId = o?.id != null;
@@ -1359,14 +1364,24 @@ function resolve(fn) {
1359
1364
  });
1360
1365
  }
1361
1366
  function createOptimistic(first, second, third) {
1362
- const node = typeof first === "function" ? new Computation(second, first, third) : new Computation(first, null, second);
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;
1363
1374
  const reset = () => node.write(first);
1364
1375
  function write(v) {
1365
1376
  if (!ActiveTransition)
1366
1377
  throw new Error("createOptimistic can only be updated inside a transition");
1367
1378
  ActiveTransition.addOptimistic(reset);
1368
- cloneGraph(node, true);
1369
- queueMicrotask(() => reset._transition && node.write(v));
1379
+ queueMicrotask(() => {
1380
+ if (reset._transition) {
1381
+ node._updateIfNecessary();
1382
+ node.write(v);
1383
+ }
1384
+ });
1370
1385
  }
1371
1386
  return [node.read.bind(node), write];
1372
1387
  }
@@ -1839,8 +1854,14 @@ function deep(store) {
1839
1854
  // src/store/optimistic.ts
1840
1855
  function createOptimisticStore(first, second, options) {
1841
1856
  const derived = typeof first === "function";
1842
- const { store, node } = derived ? createProjectionInternal(first, second, options) : createProjectionInternal(() => {
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(() => {
1843
1863
  }, first);
1864
+ node._optimistic = true;
1844
1865
  const reset = () => storeSetter(
1845
1866
  store,
1846
1867
  reconcile(
@@ -1853,7 +1874,6 @@ function createOptimisticStore(first, second, options) {
1853
1874
  if (!ActiveTransition)
1854
1875
  throw new Error("createOptimisticStore can only be updated inside a transition");
1855
1876
  ActiveTransition.addOptimistic(reset);
1856
- cloneGraph(node, true);
1857
1877
  queueMicrotask(() => reset._transition && storeSetter(store, v));
1858
1878
  };
1859
1879
  return [store, write];