@solidjs/signals 0.6.4 → 0.7.1

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,11 @@ 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), (v) => {
1193
+ node2._updateIfNecessary();
1194
+ return node2.write(v);
1195
+ }];
1207
1196
  }
1208
1197
  const o = getOwner();
1209
1198
  const needsId = o?.id != null;
@@ -1315,7 +1304,7 @@ function createEffect(compute2, effect, value, options) {
1315
1304
  void new Effect(
1316
1305
  value,
1317
1306
  compute2,
1318
- effect.effect ? effect.effect : effect,
1307
+ effect.effect || effect,
1319
1308
  effect.error,
1320
1309
  { ...options, name: options?.name ?? "effect" }
1321
1310
  );
@@ -1326,6 +1315,29 @@ function createRenderEffect(compute2, effect, value, options) {
1326
1315
  ...{ ...options, name: options?.name ?? "effect" }
1327
1316
  });
1328
1317
  }
1318
+ function createTrackedEffect(compute2, options) {
1319
+ void new TrackedEffect(compute2, options);
1320
+ }
1321
+ function createReaction(effect, options) {
1322
+ let cleanup = void 0;
1323
+ onCleanup(() => cleanup?.());
1324
+ return (tracking) => {
1325
+ const node = new Effect(
1326
+ void 0,
1327
+ tracking,
1328
+ () => {
1329
+ cleanup?.();
1330
+ cleanup = (effect.effect || effect)?.();
1331
+ node.dispose(true);
1332
+ },
1333
+ effect.error,
1334
+ {
1335
+ defer: true,
1336
+ ...{ ...options, name: options?.name ?? "effect" }
1337
+ }
1338
+ );
1339
+ };
1340
+ }
1329
1341
  function createRoot(init, options) {
1330
1342
  const owner = new Owner(options?.id);
1331
1343
  return compute(owner, !init.length ? init : () => init(() => owner.dispose()), null);
@@ -1349,23 +1361,25 @@ function resolve(fn) {
1349
1361
  });
1350
1362
  });
1351
1363
  }
1352
- function createPending() {
1353
- const node = new Computation(false, null);
1354
- const reset = () => node.write(false);
1355
- function write() {
1364
+ function createOptimistic(first, second, third) {
1365
+ const node = typeof first === "function" ? new Computation(second, first, third) : new Computation(first, null, second);
1366
+ const reset = () => node.write(first);
1367
+ function write(v) {
1356
1368
  if (!ActiveTransition)
1357
- return false;
1369
+ throw new Error("createOptimistic can only be updated inside a transition");
1358
1370
  ActiveTransition.addOptimistic(reset);
1359
- queueMicrotask(() => reset._transition && node.write(true));
1360
- }
1361
- function read() {
1362
- const v = node.read();
1363
- return ActiveTransition ? false : v;
1371
+ cloneGraph(node, true);
1372
+ queueMicrotask(() => {
1373
+ if (reset._transition) {
1374
+ node._updateIfNecessary();
1375
+ node.write(v);
1376
+ }
1377
+ });
1364
1378
  }
1365
- return [read, write];
1379
+ return [node.read.bind(node), write];
1366
1380
  }
1367
1381
  function useTransition() {
1368
- const [pending, setPending] = createPending();
1382
+ const [pending, setPending] = createOptimistic(false);
1369
1383
  function start(fn) {
1370
1384
  transition((resume) => {
1371
1385
  setPending(true);
@@ -1831,7 +1845,7 @@ function deep(store) {
1831
1845
  }
1832
1846
 
1833
1847
  // src/store/optimistic.ts
1834
- function createOptimistic(first, second, options) {
1848
+ function createOptimisticStore(first, second, options) {
1835
1849
  const derived = typeof first === "function";
1836
1850
  const { store, node } = derived ? createProjectionInternal(first, second, options) : createProjectionInternal(() => {
1837
1851
  }, first);
@@ -1845,7 +1859,7 @@ function createOptimistic(first, second, options) {
1845
1859
  );
1846
1860
  const write = (v) => {
1847
1861
  if (!ActiveTransition)
1848
- throw new Error("createOptimistic can only be updated inside a transition");
1862
+ throw new Error("createOptimisticStore can only be updated inside a transition");
1849
1863
  ActiveTransition.addOptimistic(reset);
1850
1864
  cloneGraph(node, true);
1851
1865
  queueMicrotask(() => reset._transition && storeSetter(store, v));
@@ -2456,4 +2470,4 @@ function flattenArray(children, results = [], options) {
2456
2470
  return needsUnwrap;
2457
2471
  }
2458
2472
 
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 };
2473
+ 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 };