@solidjs/signals 0.6.4 → 0.7.0

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
@@ -982,46 +982,6 @@ function latest(fn, fallback) {
982
982
  notStale = prevNotStale;
983
983
  }
984
984
  }
985
- function runWithObserver(observer, run) {
986
- const prevSources = newSources, prevSourcesIndex = newSourcesIndex, prevFlags = newFlags;
987
- newSources = null;
988
- newSourcesIndex = observer._sources ? observer._sources.length : 0;
989
- newFlags = 0;
990
- try {
991
- return compute(observer, run, observer);
992
- } catch (error) {
993
- if (error instanceof NotReadyError) {
994
- observer.write(
995
- UNCHANGED,
996
- newFlags | LOADING_BIT | observer._stateFlags & UNINITIALIZED_BIT
997
- );
998
- } else {
999
- observer._setError(error);
1000
- }
1001
- } finally {
1002
- if (newSources) {
1003
- if (newSourcesIndex > 0) {
1004
- observer._sources.length = newSourcesIndex + newSources.length;
1005
- for (let i = 0; i < newSources.length; i++) {
1006
- observer._sources[newSourcesIndex + i] = newSources[i];
1007
- }
1008
- } else {
1009
- observer._sources = newSources;
1010
- }
1011
- let source;
1012
- for (let i = newSourcesIndex; i < observer._sources.length; i++) {
1013
- source = observer._sources[i];
1014
- if (!source._observers)
1015
- source._observers = [observer];
1016
- else
1017
- source._observers.push(observer);
1018
- }
1019
- }
1020
- newSources = prevSources;
1021
- newSourcesIndex = prevSourcesIndex;
1022
- newFlags = prevFlags;
1023
- }
1024
- }
1025
985
  function compute(owner, fn, observer) {
1026
986
  const prevOwner = setOwner(owner), prevObserver = currentObserver, prevMask = currentMask, prevNotStale = notStale;
1027
987
  currentObserver = observer;
@@ -1140,6 +1100,39 @@ var Effect = class extends Computation {
1140
1100
  this._state !== STATE_CLEAN && runTop(this);
1141
1101
  }
1142
1102
  };
1103
+ var TrackedEffect = class extends Computation {
1104
+ _type = EFFECT_USER;
1105
+ _cleanup;
1106
+ constructor(compute2, options) {
1107
+ super(void 0, () => {
1108
+ this._cleanup?.();
1109
+ this._cleanup = latest(compute2);
1110
+ return void 0;
1111
+ }, options);
1112
+ getQueue(this).enqueue(this._type, this._run.bind(this));
1113
+ if (!this._parent)
1114
+ console.warn("Effects created outside a reactive context will never be disposed");
1115
+ }
1116
+ _notify(state, skipQueue) {
1117
+ if (this._state >= state || skipQueue)
1118
+ return;
1119
+ if (this._state === STATE_CLEAN || this._cloned && !ActiveTransition)
1120
+ getQueue(this).enqueue(this._type, this._run.bind(this));
1121
+ this._state = state;
1122
+ }
1123
+ _disposeNode() {
1124
+ if (this._state === STATE_DISPOSED)
1125
+ return;
1126
+ this._cleanup?.();
1127
+ this._cleanup = void 0;
1128
+ getQueue(this).notify(this, ERROR_BIT | LOADING_BIT, 0);
1129
+ super._disposeNode();
1130
+ }
1131
+ _run(type) {
1132
+ if (type)
1133
+ this._state !== STATE_CLEAN && runTop(this);
1134
+ }
1135
+ };
1143
1136
  var EagerComputation = class extends Computation {
1144
1137
  constructor(initialValue, compute2, options) {
1145
1138
  super(initialValue, compute2, options);
@@ -1195,15 +1188,8 @@ function runTop(node) {
1195
1188
  // src/signals.ts
1196
1189
  function createSignal(first, second, third) {
1197
1190
  if (typeof first === "function") {
1198
- const memo = createMemo((p) => {
1199
- const node2 = new Computation(
1200
- first(p ? untrack(p[0]) : second),
1201
- null,
1202
- third
1203
- );
1204
- return [node2.read.bind(node2), node2.write.bind(node2)];
1205
- });
1206
- return [() => memo()[0](), (value) => memo()[1](value)];
1191
+ const node2 = new Computation(second, first, third);
1192
+ return [node2.read.bind(node2), node2.write.bind(node2)];
1207
1193
  }
1208
1194
  const o = getOwner();
1209
1195
  const needsId = o?.id != null;
@@ -1315,7 +1301,7 @@ function createEffect(compute2, effect, value, options) {
1315
1301
  void new Effect(
1316
1302
  value,
1317
1303
  compute2,
1318
- effect.effect ? effect.effect : effect,
1304
+ effect.effect || effect,
1319
1305
  effect.error,
1320
1306
  { ...options, name: options?.name ?? "effect" }
1321
1307
  );
@@ -1326,6 +1312,29 @@ function createRenderEffect(compute2, effect, value, options) {
1326
1312
  ...{ ...options, name: options?.name ?? "effect" }
1327
1313
  });
1328
1314
  }
1315
+ function createTrackedEffect(compute2, options) {
1316
+ void new TrackedEffect(compute2, options);
1317
+ }
1318
+ function createReaction(effect, options) {
1319
+ let cleanup = void 0;
1320
+ onCleanup(() => cleanup?.());
1321
+ return (tracking) => {
1322
+ const node = new Effect(
1323
+ void 0,
1324
+ tracking,
1325
+ () => {
1326
+ cleanup?.();
1327
+ cleanup = (effect.effect || effect)?.();
1328
+ node.dispose(true);
1329
+ },
1330
+ effect.error,
1331
+ {
1332
+ defer: true,
1333
+ ...{ ...options, name: options?.name ?? "effect" }
1334
+ }
1335
+ );
1336
+ };
1337
+ }
1329
1338
  function createRoot(init, options) {
1330
1339
  const owner = new Owner(options?.id);
1331
1340
  return compute(owner, !init.length ? init : () => init(() => owner.dispose()), null);
@@ -1349,23 +1358,20 @@ function resolve(fn) {
1349
1358
  });
1350
1359
  });
1351
1360
  }
1352
- function createPending() {
1353
- const node = new Computation(false, null);
1354
- const reset = () => node.write(false);
1355
- function write() {
1361
+ function createOptimistic(first, second, third) {
1362
+ const node = typeof first === "function" ? new Computation(second, first, third) : new Computation(first, null, second);
1363
+ const reset = () => node.write(first);
1364
+ function write(v) {
1356
1365
  if (!ActiveTransition)
1357
- return false;
1366
+ throw new Error("createOptimistic can only be updated inside a transition");
1358
1367
  ActiveTransition.addOptimistic(reset);
1359
- queueMicrotask(() => reset._transition && node.write(true));
1360
- }
1361
- function read() {
1362
- const v = node.read();
1363
- return ActiveTransition ? false : v;
1368
+ cloneGraph(node, true);
1369
+ queueMicrotask(() => reset._transition && node.write(v));
1364
1370
  }
1365
- return [read, write];
1371
+ return [node.read.bind(node), write];
1366
1372
  }
1367
1373
  function useTransition() {
1368
- const [pending, setPending] = createPending();
1374
+ const [pending, setPending] = createOptimistic(false);
1369
1375
  function start(fn) {
1370
1376
  transition((resume) => {
1371
1377
  setPending(true);
@@ -1831,7 +1837,7 @@ function deep(store) {
1831
1837
  }
1832
1838
 
1833
1839
  // src/store/optimistic.ts
1834
- function createOptimistic(first, second, options) {
1840
+ function createOptimisticStore(first, second, options) {
1835
1841
  const derived = typeof first === "function";
1836
1842
  const { store, node } = derived ? createProjectionInternal(first, second, options) : createProjectionInternal(() => {
1837
1843
  }, first);
@@ -1845,7 +1851,7 @@ function createOptimistic(first, second, options) {
1845
1851
  );
1846
1852
  const write = (v) => {
1847
1853
  if (!ActiveTransition)
1848
- throw new Error("createOptimistic can only be updated inside a transition");
1854
+ throw new Error("createOptimisticStore can only be updated inside a transition");
1849
1855
  ActiveTransition.addOptimistic(reset);
1850
1856
  cloneGraph(node, true);
1851
1857
  queueMicrotask(() => reset._transition && storeSetter(store, v));
@@ -2456,4 +2462,4 @@ function flattenArray(children, results = [], options) {
2456
2462
  return needsUnwrap;
2457
2463
  }
2458
2464
 
2459
- export { $PROXY, $TARGET, $TRACK, Computation, ContextNotFoundError, NoOwnerError, NotReadyError, Owner, Queue, SUPPORTS_PROXY, createAsync, createBoundary, createContext, createEffect, createErrorBoundary, createMemo, createOptimistic, createProjection, createRenderEffect, createRoot, createSignal, createStore, createSuspense, deep, flatten, flush, getContext, getObserver, getOwner, hasContext, hasUpdated, isEqual, isPending, isWrappable, latest, mapArray, merge, omit, onCleanup, reconcile, repeat, resolve, runWithObserver, runWithOwner, setContext, snapshot, transition, untrack, useTransition };
2465
+ export { $PROXY, $TARGET, $TRACK, Computation, ContextNotFoundError, NoOwnerError, NotReadyError, Owner, Queue, SUPPORTS_PROXY, createAsync, createBoundary, createContext, createEffect, createErrorBoundary, createMemo, createOptimistic, createOptimisticStore, createProjection, createReaction, createRenderEffect, createRoot, createSignal, createStore, createSuspense, createTrackedEffect, deep, flatten, flush, getContext, getObserver, getOwner, hasContext, hasUpdated, isEqual, isPending, isWrappable, latest, mapArray, merge, omit, onCleanup, reconcile, repeat, resolve, runWithOwner, setContext, snapshot, transition, untrack, useTransition };