@solidjs/signals 0.6.3 → 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
@@ -176,7 +176,7 @@ var Transition = class _Transition {
176
176
  constructor() {
177
177
  this._clonedQueues.set(globalQueue, this);
178
178
  for (const child of globalQueue._children) {
179
- cloneQueue(child, this, this._clonedQueues);
179
+ cloneQueue(child, this, this);
180
180
  }
181
181
  }
182
182
  enqueue(type, fn) {
@@ -276,8 +276,7 @@ var Transition = class _Transition {
276
276
  try {
277
277
  value = await temp.value;
278
278
  } finally {
279
- while (transition2._done instanceof _Transition)
280
- transition2 = transition2._done;
279
+ transition2 = latestTransition(transition2);
281
280
  transition2._promises.delete(temp.value);
282
281
  }
283
282
  ActiveTransition = transition2;
@@ -291,8 +290,7 @@ var Transition = class _Transition {
291
290
  if (result instanceof Promise) {
292
291
  transition2._promises.add(result);
293
292
  result.finally(() => {
294
- while (transition2._done instanceof _Transition)
295
- transition2 = transition2._done;
293
+ transition2 = latestTransition(transition2);
296
294
  transition2._promises.delete(result);
297
295
  ActiveTransition = null;
298
296
  finishTransition(transition2);
@@ -318,7 +316,7 @@ function transition(fn) {
318
316
  let t = new Transition();
319
317
  queueMicrotask(() => t.runTransition(() => fn((fn2) => t.runTransition(fn2))));
320
318
  }
321
- function cloneGraph(node) {
319
+ function cloneGraph(node, optimistic) {
322
320
  if (node._transition) {
323
321
  if (node._transition !== ActiveTransition) {
324
322
  mergeTransitions(node._transition, ActiveTransition);
@@ -332,48 +330,58 @@ function cloneGraph(node) {
332
330
  _nextSibling: null,
333
331
  _observers: null,
334
332
  _sources: node._sources ? [...node._sources] : null,
335
- _cloned: node
333
+ _cloned: node,
334
+ _optimistic: !!optimistic
336
335
  });
336
+ delete clone._prevValue;
337
337
  ActiveTransition._sources.set(node, clone);
338
338
  node._transition = ActiveTransition;
339
- if (node._sources) {
339
+ if (!optimistic && node._sources) {
340
340
  for (let i = 0; i < node._sources.length; i++)
341
341
  node._sources[i]._observers.push(clone);
342
342
  }
343
343
  if (node._observers) {
344
344
  clone._observers = [];
345
345
  for (let i = 0, length = node._observers.length; i < length; i++) {
346
- !node._observers[i]._cloned && clone._observers.push(cloneGraph(node._observers[i]));
346
+ !node._observers[i]._cloned && clone._observers.push(cloneGraph(node._observers[i], optimistic));
347
347
  }
348
348
  }
349
349
  return clone;
350
350
  }
351
+ function latestTransition(t) {
352
+ while (t._done instanceof Transition)
353
+ t = t._done;
354
+ return t;
355
+ }
351
356
  function replaceSourceObservers(node, transition2) {
352
357
  let source;
358
+ let transitionSource;
353
359
  let swap;
354
360
  for (let i = 0; i < node._sources.length; i++) {
355
- source = transition2._sources.get(node._sources[i]) || node._sources[i];
361
+ transitionSource = transition2._sources.get(node._sources[i]);
362
+ source = transitionSource || node._sources[i];
356
363
  if (source._observers && (swap = source._observers.indexOf(node)) !== -1) {
357
- const remove = source._observers.indexOf(node._cloned) > -1;
358
- source._observers[swap] = !remove ? node._cloned : source._observers[source._observers.length - 1];
359
- remove && source._observers.pop();
364
+ source._observers[swap] = transitionSource ? node._cloned : source._observers[source._observers.length - 1];
365
+ !transitionSource && source._observers.pop();
360
366
  }
361
367
  }
362
368
  }
363
- function cloneQueue(queue, parent, clonedQueues) {
369
+ function cloneQueue(queue, parent, transition2) {
364
370
  const clone = Object.create(Object.getPrototypeOf(queue));
365
371
  Object.assign(clone, queue, {
366
372
  _cloned: queue,
367
373
  _parent: parent,
368
374
  _children: [],
369
375
  enqueue(type, fn) {
370
- ActiveTransition?.enqueue(type, fn);
376
+ transition2 = latestTransition(transition2);
377
+ transition2.enqueue(type, fn);
371
378
  },
372
379
  notify(node, type, flags) {
373
380
  node = node._cloned || node;
374
381
  if (!clone._collectionType || type & LOADING_BIT) {
375
382
  type &= ~LOADING_BIT;
376
- ActiveTransition?.notify(node, LOADING_BIT, flags);
383
+ transition2 = latestTransition(transition2);
384
+ transition2.notify(node, LOADING_BIT, flags);
377
385
  if (!type)
378
386
  return true;
379
387
  }
@@ -381,9 +389,9 @@ function cloneQueue(queue, parent, clonedQueues) {
381
389
  }
382
390
  });
383
391
  parent._children.push(clone);
384
- clonedQueues.set(queue, clone);
392
+ transition2._clonedQueues.set(queue, clone);
385
393
  for (const child of queue._children) {
386
- cloneQueue(child, clone, clonedQueues);
394
+ cloneQueue(child, clone, transition2);
387
395
  }
388
396
  }
389
397
  function resolveQueues(children) {
@@ -447,6 +455,12 @@ function finishTransition(transition2) {
447
455
  }
448
456
  if (clone._sources)
449
457
  replaceSourceObservers(clone, transition2);
458
+ if (clone._optimistic) {
459
+ clone.dispose();
460
+ clone.emptyDisposal();
461
+ delete source._transition;
462
+ continue;
463
+ }
450
464
  if (clone._updated || clone._state === STATE_DISPOSED) {
451
465
  source.dispose(clone._state === STATE_DISPOSED);
452
466
  source.emptyDisposal();
@@ -645,6 +659,7 @@ var Computation = class extends Owner {
645
659
  _forceNotify = false;
646
660
  _transition;
647
661
  _cloned;
662
+ _optimistic = false;
648
663
  constructor(initialValue, compute2, options) {
649
664
  super(options?.id, compute2 === null);
650
665
  this._compute = compute2;
@@ -654,10 +669,8 @@ var Computation = class extends Owner {
654
669
  this._name = options?.name ?? (this._compute ? "computed" : "signal");
655
670
  if (options?.equals !== void 0)
656
671
  this._equals = options.equals;
657
- if (options?.pureWrite)
658
- this._pureWrite = true;
659
- if (options?.unobserved)
660
- this._unobserved = options?.unobserved;
672
+ this._pureWrite = !!options?.pureWrite;
673
+ this._unobserved = options?.unobserved;
661
674
  if (ActiveTransition) {
662
675
  this._transition = ActiveTransition;
663
676
  ActiveTransition._sources.set(this, this);
@@ -969,46 +982,6 @@ function latest(fn, fallback) {
969
982
  notStale = prevNotStale;
970
983
  }
971
984
  }
972
- function runWithObserver(observer, run) {
973
- const prevSources = newSources, prevSourcesIndex = newSourcesIndex, prevFlags = newFlags;
974
- newSources = null;
975
- newSourcesIndex = observer._sources ? observer._sources.length : 0;
976
- newFlags = 0;
977
- try {
978
- return compute(observer, run, observer);
979
- } catch (error) {
980
- if (error instanceof NotReadyError) {
981
- observer.write(
982
- UNCHANGED,
983
- newFlags | LOADING_BIT | observer._stateFlags & UNINITIALIZED_BIT
984
- );
985
- } else {
986
- observer._setError(error);
987
- }
988
- } finally {
989
- if (newSources) {
990
- if (newSourcesIndex > 0) {
991
- observer._sources.length = newSourcesIndex + newSources.length;
992
- for (let i = 0; i < newSources.length; i++) {
993
- observer._sources[newSourcesIndex + i] = newSources[i];
994
- }
995
- } else {
996
- observer._sources = newSources;
997
- }
998
- let source;
999
- for (let i = newSourcesIndex; i < observer._sources.length; i++) {
1000
- source = observer._sources[i];
1001
- if (!source._observers)
1002
- source._observers = [observer];
1003
- else
1004
- source._observers.push(observer);
1005
- }
1006
- }
1007
- newSources = prevSources;
1008
- newSourcesIndex = prevSourcesIndex;
1009
- newFlags = prevFlags;
1010
- }
1011
- }
1012
985
  function compute(owner, fn, observer) {
1013
986
  const prevOwner = setOwner(owner), prevObserver = currentObserver, prevMask = currentMask, prevNotStale = notStale;
1014
987
  currentObserver = observer;
@@ -1065,7 +1038,7 @@ var Effect = class extends Computation {
1065
1038
  _notify(state, skipQueue) {
1066
1039
  if (this._state >= state || skipQueue)
1067
1040
  return;
1068
- if (this._state === STATE_CLEAN)
1041
+ if (this._state === STATE_CLEAN || this._cloned && !ActiveTransition)
1069
1042
  getQueue(this).enqueue(this._type, this._run.bind(this));
1070
1043
  this._state = state;
1071
1044
  }
@@ -1127,6 +1100,39 @@ var Effect = class extends Computation {
1127
1100
  this._state !== STATE_CLEAN && runTop(this);
1128
1101
  }
1129
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
+ };
1130
1136
  var EagerComputation = class extends Computation {
1131
1137
  constructor(initialValue, compute2, options) {
1132
1138
  super(initialValue, compute2, options);
@@ -1179,12 +1185,208 @@ function runTop(node) {
1179
1185
  }
1180
1186
  }
1181
1187
 
1188
+ // src/signals.ts
1189
+ function createSignal(first, second, third) {
1190
+ if (typeof first === "function") {
1191
+ const node2 = new Computation(second, first, third);
1192
+ return [node2.read.bind(node2), node2.write.bind(node2)];
1193
+ }
1194
+ const o = getOwner();
1195
+ const needsId = o?.id != null;
1196
+ const node = new Computation(
1197
+ first,
1198
+ null,
1199
+ needsId ? { id: o.getNextChildId(), ...second } : second
1200
+ );
1201
+ return [node.read.bind(node), node.write.bind(node)];
1202
+ }
1203
+ function createMemo(compute2, value, options) {
1204
+ let node = new Computation(
1205
+ value,
1206
+ compute2,
1207
+ options
1208
+ );
1209
+ let resolvedValue;
1210
+ return () => {
1211
+ if (node) {
1212
+ if (node._state === STATE_DISPOSED) {
1213
+ node = void 0;
1214
+ return resolvedValue;
1215
+ }
1216
+ resolvedValue = node.wait();
1217
+ if (!node._sources?.length && node._nextSibling?._parent !== node && !(node._stateFlags & UNINITIALIZED_BIT)) {
1218
+ node.dispose();
1219
+ node = void 0;
1220
+ }
1221
+ }
1222
+ return resolvedValue;
1223
+ };
1224
+ }
1225
+ function createAsync(compute2, value, options) {
1226
+ let refreshing = false;
1227
+ const node = new EagerComputation(
1228
+ value,
1229
+ (p) => {
1230
+ const source = compute2(p, refreshing);
1231
+ refreshing = false;
1232
+ const isPromise = source instanceof Promise;
1233
+ const iterator = source[Symbol.asyncIterator];
1234
+ if (!isPromise && !iterator) {
1235
+ return source;
1236
+ }
1237
+ let abort = false;
1238
+ onCleanup(() => abort = true);
1239
+ let transition2 = ActiveTransition;
1240
+ if (isPromise) {
1241
+ source.then(
1242
+ (value3) => {
1243
+ if (abort)
1244
+ return;
1245
+ if (transition2)
1246
+ return transition2.runTransition(() => {
1247
+ node.write(value3, 0, true);
1248
+ }, true);
1249
+ node.write(value3, 0, true);
1250
+ },
1251
+ (error) => {
1252
+ if (abort)
1253
+ return;
1254
+ if (transition2)
1255
+ return transition2.runTransition(() => node._setError(error), true);
1256
+ node._setError(error);
1257
+ }
1258
+ );
1259
+ } else {
1260
+ (async () => {
1261
+ try {
1262
+ for await (let value3 of source) {
1263
+ if (abort)
1264
+ return;
1265
+ if (transition2)
1266
+ return transition2.runTransition(() => {
1267
+ node.write(value3, 0, true);
1268
+ transition2 = null;
1269
+ }, true);
1270
+ node.write(value3, 0, true);
1271
+ }
1272
+ } catch (error) {
1273
+ if (abort)
1274
+ return;
1275
+ if (transition2)
1276
+ return transition2.runTransition(() => {
1277
+ node._setError(error);
1278
+ transition2 = null;
1279
+ }, true);
1280
+ node._setError(error);
1281
+ }
1282
+ })();
1283
+ }
1284
+ throw new NotReadyError();
1285
+ },
1286
+ options
1287
+ );
1288
+ const read = node.wait.bind(node);
1289
+ read.refresh = () => {
1290
+ let n = node;
1291
+ if (ActiveTransition && !node._cloned) {
1292
+ n = cloneGraph(node);
1293
+ }
1294
+ n._state = STATE_DIRTY;
1295
+ refreshing = true;
1296
+ n._updateIfNecessary();
1297
+ };
1298
+ return read;
1299
+ }
1300
+ function createEffect(compute2, effect, value, options) {
1301
+ void new Effect(
1302
+ value,
1303
+ compute2,
1304
+ effect.effect || effect,
1305
+ effect.error,
1306
+ { ...options, name: options?.name ?? "effect" }
1307
+ );
1308
+ }
1309
+ function createRenderEffect(compute2, effect, value, options) {
1310
+ void new Effect(value, compute2, effect, void 0, {
1311
+ render: true,
1312
+ ...{ ...options, name: options?.name ?? "effect" }
1313
+ });
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
+ }
1338
+ function createRoot(init, options) {
1339
+ const owner = new Owner(options?.id);
1340
+ return compute(owner, !init.length ? init : () => init(() => owner.dispose()), null);
1341
+ }
1342
+ function runWithOwner(owner, run) {
1343
+ return compute(owner, run, null);
1344
+ }
1345
+ function resolve(fn) {
1346
+ return new Promise((res, rej) => {
1347
+ createRoot((dispose) => {
1348
+ new EagerComputation(void 0, () => {
1349
+ try {
1350
+ res(fn());
1351
+ } catch (err) {
1352
+ if (err instanceof NotReadyError)
1353
+ throw err;
1354
+ rej(err);
1355
+ }
1356
+ dispose();
1357
+ });
1358
+ });
1359
+ });
1360
+ }
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) {
1365
+ if (!ActiveTransition)
1366
+ throw new Error("createOptimistic can only be updated inside a transition");
1367
+ ActiveTransition.addOptimistic(reset);
1368
+ cloneGraph(node, true);
1369
+ queueMicrotask(() => reset._transition && node.write(v));
1370
+ }
1371
+ return [node.read.bind(node), write];
1372
+ }
1373
+ function useTransition() {
1374
+ const [pending, setPending] = createOptimistic(false);
1375
+ function start(fn) {
1376
+ transition((resume) => {
1377
+ setPending(true);
1378
+ return fn(resume);
1379
+ });
1380
+ }
1381
+ return [pending, start];
1382
+ }
1383
+
1182
1384
  // src/store/reconcile.ts
1183
1385
  function unwrap(value) {
1184
1386
  return value?.[$TARGET]?.[STORE_NODE] ?? value;
1185
1387
  }
1186
- function getOverrideValue(value, override, key) {
1187
- return override && key in override ? override[key] : value[key];
1388
+ function getOverrideValue(value, override, nodes, key) {
1389
+ return nodes && key in nodes ? nodes[key].read() : override && key in override ? override[key] : value[key];
1188
1390
  }
1189
1391
  function getAllKeys(value, override, next) {
1190
1392
  const keys = getKeys(value, override);
@@ -1197,6 +1399,7 @@ function applyState(next, state, keyFn, all) {
1197
1399
  return;
1198
1400
  const previous = target[STORE_VALUE];
1199
1401
  const override = target[STORE_OVERRIDE];
1402
+ let nodes = target[STORE_NODE];
1200
1403
  if (next === previous && !override)
1201
1404
  return;
1202
1405
  (target[STORE_LOOKUP] || storeLookup).set(next, target[$PROXY]);
@@ -1204,14 +1407,14 @@ function applyState(next, state, keyFn, all) {
1204
1407
  target[STORE_OVERRIDE] = void 0;
1205
1408
  if (Array.isArray(previous)) {
1206
1409
  let changed = false;
1207
- const prevLength = getOverrideValue(previous, override, "length");
1410
+ const prevLength = getOverrideValue(previous, override, nodes, "length");
1208
1411
  if (next.length && prevLength && next[0] && keyFn(next[0]) != null) {
1209
1412
  let i, j, start, end, newEnd, item, newIndicesNext, keyVal;
1210
- for (start = 0, end = Math.min(prevLength, next.length); start < end && ((item = getOverrideValue(previous, override, start)) === next[start] || item && next[start] && keyFn(item) === keyFn(next[start])); start++) {
1413
+ for (start = 0, end = Math.min(prevLength, next.length); start < end && ((item = getOverrideValue(previous, override, nodes, start)) === next[start] || item && next[start] && keyFn(item) === keyFn(next[start])); start++) {
1211
1414
  applyState(next[start], wrap(item, target), keyFn, all);
1212
1415
  }
1213
1416
  const temp = new Array(next.length), newIndices = /* @__PURE__ */ new Map();
1214
- for (end = prevLength - 1, newEnd = next.length - 1; end >= start && newEnd >= start && ((item = getOverrideValue(previous, override, end)) === next[newEnd] || item && next[newEnd] && keyFn(item) === keyFn(next[newEnd])); end--, newEnd--) {
1417
+ for (end = prevLength - 1, newEnd = next.length - 1; end >= start && newEnd >= start && ((item = getOverrideValue(previous, override, nodes, end)) === next[newEnd] || item && next[newEnd] && keyFn(item) === keyFn(next[newEnd])); end--, newEnd--) {
1215
1418
  temp[newEnd] = item;
1216
1419
  }
1217
1420
  if (start > newEnd || start > end) {
@@ -1238,7 +1441,7 @@ function applyState(next, state, keyFn, all) {
1238
1441
  newIndices.set(keyVal, j);
1239
1442
  }
1240
1443
  for (i = start; i <= end; i++) {
1241
- item = getOverrideValue(previous, override, i);
1444
+ item = getOverrideValue(previous, override, nodes, i);
1242
1445
  keyVal = item ? keyFn(item) : item;
1243
1446
  j = newIndices.get(keyVal);
1244
1447
  if (j !== void 0 && j !== -1) {
@@ -1259,7 +1462,7 @@ function applyState(next, state, keyFn, all) {
1259
1462
  changed = true;
1260
1463
  } else if (prevLength && next.length) {
1261
1464
  for (let i = 0, len = next.length; i < len; i++) {
1262
- const item = getOverrideValue(previous, override, i);
1465
+ const item = getOverrideValue(previous, override, nodes, i);
1263
1466
  isWrappable(item) && applyState(next[i], wrap(item, target), keyFn, all);
1264
1467
  }
1265
1468
  }
@@ -1270,14 +1473,13 @@ function applyState(next, state, keyFn, all) {
1270
1473
  changed && target[STORE_NODE][$TRACK]?.write(void 0);
1271
1474
  return;
1272
1475
  }
1273
- let nodes = target[STORE_NODE];
1274
1476
  if (nodes) {
1275
1477
  const tracked = nodes[$TRACK];
1276
1478
  const keys = tracked || all ? getAllKeys(previous, override, next) : Object.keys(nodes);
1277
1479
  for (let i = 0, len = keys.length; i < len; i++) {
1278
1480
  const key = keys[i];
1279
1481
  const node = nodes[key];
1280
- const previousValue = unwrap(getOverrideValue(previous, override, key));
1482
+ const previousValue = unwrap(getOverrideValue(previous, override, nodes, key));
1281
1483
  let nextValue = unwrap(next[key]);
1282
1484
  if (previousValue === nextValue)
1283
1485
  continue;
@@ -1308,16 +1510,7 @@ function reconcile(value, key, all = false) {
1308
1510
  }
1309
1511
 
1310
1512
  // src/store/projection.ts
1311
- function createProjection(fn, initialValue = {}, options) {
1312
- let wrappedStore;
1313
- const node = new FirewallComputation(() => {
1314
- storeSetter(wrappedStore, (s) => {
1315
- const value = fn(s);
1316
- if (value !== s && value !== void 0) {
1317
- reconcile(value, options?.key || "id", options?.all)(s);
1318
- }
1319
- });
1320
- });
1513
+ function createProjectionInternal(fn, initialValue = {}, options) {
1321
1514
  const wrappedMap = /* @__PURE__ */ new WeakMap();
1322
1515
  const traps = {
1323
1516
  ...storeTraps,
@@ -1328,7 +1521,7 @@ function createProjection(fn, initialValue = {}, options) {
1328
1521
  return storeTraps.get(target, property, receiver);
1329
1522
  }
1330
1523
  };
1331
- function wrapProjection(source) {
1524
+ const wrapProjection = (source) => {
1332
1525
  if (wrappedMap.has(source))
1333
1526
  return wrappedMap.get(source);
1334
1527
  if (source[$TARGET]?.[STORE_WRAP] === wrapProjection)
@@ -1339,8 +1532,20 @@ function createProjection(fn, initialValue = {}, options) {
1339
1532
  });
1340
1533
  wrappedMap.set(source, wrapped);
1341
1534
  return wrapped;
1342
- }
1343
- return wrappedStore = wrapProjection(initialValue);
1535
+ };
1536
+ const wrappedStore = wrapProjection(initialValue);
1537
+ const node = new FirewallComputation(() => {
1538
+ storeSetter(wrappedStore, (s) => {
1539
+ const value = fn(s);
1540
+ if (value !== s && value !== void 0) {
1541
+ reconcile(value, options?.key || "id", options?.all)(s);
1542
+ }
1543
+ });
1544
+ });
1545
+ return { store: wrappedStore, node };
1546
+ }
1547
+ function createProjection(fn, initialValue = {}, options) {
1548
+ return createProjectionInternal(fn, initialValue, options).store;
1344
1549
  }
1345
1550
 
1346
1551
  // src/store/store.ts
@@ -1476,7 +1681,7 @@ var storeTraps = {
1476
1681
  untrack(() => {
1477
1682
  const state = target[STORE_VALUE];
1478
1683
  const base = state[property];
1479
- const prev = target[STORE_OVERRIDE]?.[property] || base;
1684
+ const prev = target[STORE_OVERRIDE] && property in target[STORE_OVERRIDE] ? target[STORE_OVERRIDE][property] : base;
1480
1685
  const value = rawValue?.[$TARGET]?.[STORE_VALUE] ?? rawValue;
1481
1686
  if (prev === value)
1482
1687
  return true;
@@ -1508,7 +1713,7 @@ var storeTraps = {
1508
1713
  deleteProperty(target, property) {
1509
1714
  if (Writing?.has(target[$PROXY]) && target[STORE_OVERRIDE]?.[property] !== $DELETED) {
1510
1715
  untrack(() => {
1511
- const prev = target[STORE_OVERRIDE]?.[property] || target[STORE_VALUE][property];
1716
+ const prev = target[STORE_OVERRIDE] && property in target[STORE_OVERRIDE] ? target[STORE_OVERRIDE][property] : target[STORE_VALUE][property];
1512
1717
  if (property in target[STORE_VALUE]) {
1513
1718
  (target[STORE_OVERRIDE] || (target[STORE_OVERRIDE] = /* @__PURE__ */ Object.create(null)))[property] = $DELETED;
1514
1719
  } else if (target[STORE_OVERRIDE] && property in target[STORE_OVERRIDE]) {
@@ -1567,7 +1772,7 @@ function storeSetter(store, fn) {
1567
1772
  }
1568
1773
  }
1569
1774
  function createStore(first, second, options) {
1570
- const derived = typeof first === "function", wrappedStore = derived ? createProjection(first, second, options) : wrap(first);
1775
+ const derived = typeof first === "function", wrappedStore = derived ? createProjectionInternal(first, second, options).store : wrap(first);
1571
1776
  return [wrappedStore, (fn) => storeSetter(wrappedStore, fn)];
1572
1777
  }
1573
1778
  function recursivelyNotify(state, lookup) {
@@ -1631,6 +1836,29 @@ function deep(store) {
1631
1836
  return store[$DEEP];
1632
1837
  }
1633
1838
 
1839
+ // src/store/optimistic.ts
1840
+ function createOptimisticStore(first, second, options) {
1841
+ const derived = typeof first === "function";
1842
+ const { store, node } = derived ? createProjectionInternal(first, second, options) : createProjectionInternal(() => {
1843
+ }, first);
1844
+ const reset = () => storeSetter(
1845
+ store,
1846
+ reconcile(
1847
+ derived ? first(store) || store : first,
1848
+ options?.key || "id",
1849
+ options?.all
1850
+ )
1851
+ );
1852
+ const write = (v) => {
1853
+ if (!ActiveTransition)
1854
+ throw new Error("createOptimisticStore can only be updated inside a transition");
1855
+ ActiveTransition.addOptimistic(reset);
1856
+ cloneGraph(node, true);
1857
+ queueMicrotask(() => reset._transition && storeSetter(store, v));
1858
+ };
1859
+ return [store, write];
1860
+ }
1861
+
1634
1862
  // src/store/utils.ts
1635
1863
  function snapshot(item, map, lookup) {
1636
1864
  let target, isArray, override, result, unwrapped, v;
@@ -1831,215 +2059,6 @@ function omit(props, ...keys) {
1831
2059
  return result;
1832
2060
  }
1833
2061
 
1834
- // src/signals.ts
1835
- function createSignal(first, second, third) {
1836
- if (typeof first === "function") {
1837
- const memo = createMemo((p) => {
1838
- const node2 = new Computation(
1839
- first(p ? untrack(p[0]) : second),
1840
- null,
1841
- third
1842
- );
1843
- return [node2.read.bind(node2), node2.write.bind(node2)];
1844
- });
1845
- return [() => memo()[0](), (value) => memo()[1](value)];
1846
- }
1847
- const o = getOwner();
1848
- const needsId = o?.id != null;
1849
- const node = new Computation(
1850
- first,
1851
- null,
1852
- needsId ? { id: o.getNextChildId(), ...second } : second
1853
- );
1854
- return [node.read.bind(node), node.write.bind(node)];
1855
- }
1856
- function createMemo(compute2, value, options) {
1857
- let node = new Computation(
1858
- value,
1859
- compute2,
1860
- options
1861
- );
1862
- let resolvedValue;
1863
- return () => {
1864
- if (node) {
1865
- if (node._state === STATE_DISPOSED) {
1866
- node = void 0;
1867
- return resolvedValue;
1868
- }
1869
- resolvedValue = node.wait();
1870
- if (!node._sources?.length && node._nextSibling?._parent !== node && !(node._stateFlags & UNINITIALIZED_BIT)) {
1871
- node.dispose();
1872
- node = void 0;
1873
- }
1874
- }
1875
- return resolvedValue;
1876
- };
1877
- }
1878
- function createAsync(compute2, value, options) {
1879
- let refreshing = false;
1880
- const node = new EagerComputation(
1881
- value,
1882
- (p) => {
1883
- const source = compute2(p, refreshing);
1884
- refreshing = false;
1885
- const isPromise = source instanceof Promise;
1886
- const iterator = source[Symbol.asyncIterator];
1887
- if (!isPromise && !iterator) {
1888
- return source;
1889
- }
1890
- let abort = false;
1891
- onCleanup(() => abort = true);
1892
- let transition2 = ActiveTransition;
1893
- if (isPromise) {
1894
- source.then(
1895
- (value3) => {
1896
- if (abort)
1897
- return;
1898
- if (transition2)
1899
- return transition2.runTransition(() => {
1900
- node.write(value3, 0, true);
1901
- }, true);
1902
- node.write(value3, 0, true);
1903
- },
1904
- (error) => {
1905
- if (abort)
1906
- return;
1907
- if (transition2)
1908
- return transition2.runTransition(() => node._setError(error), true);
1909
- node._setError(error);
1910
- }
1911
- );
1912
- } else {
1913
- (async () => {
1914
- try {
1915
- for await (let value3 of source) {
1916
- if (abort)
1917
- return;
1918
- if (transition2)
1919
- return transition2.runTransition(() => {
1920
- node.write(value3, 0, true);
1921
- transition2 = null;
1922
- }, true);
1923
- node.write(value3, 0, true);
1924
- }
1925
- } catch (error) {
1926
- if (abort)
1927
- return;
1928
- if (transition2)
1929
- return transition2.runTransition(() => {
1930
- node._setError(error);
1931
- transition2 = null;
1932
- }, true);
1933
- node._setError(error);
1934
- }
1935
- })();
1936
- }
1937
- throw new NotReadyError();
1938
- },
1939
- options
1940
- );
1941
- const read = node.wait.bind(node);
1942
- read.refresh = () => {
1943
- let n = node;
1944
- if (ActiveTransition && !node._cloned) {
1945
- n = cloneGraph(node);
1946
- }
1947
- n._state = STATE_DIRTY;
1948
- refreshing = true;
1949
- n._updateIfNecessary();
1950
- };
1951
- return read;
1952
- }
1953
- function createEffect(compute2, effect, value, options) {
1954
- void new Effect(
1955
- value,
1956
- compute2,
1957
- effect.effect ? effect.effect : effect,
1958
- effect.error,
1959
- { ...options, name: options?.name ?? "effect" }
1960
- );
1961
- }
1962
- function createRenderEffect(compute2, effect, value, options) {
1963
- void new Effect(value, compute2, effect, void 0, {
1964
- render: true,
1965
- ...{ ...options, name: options?.name ?? "effect" }
1966
- });
1967
- }
1968
- function createRoot(init, options) {
1969
- const owner = new Owner(options?.id);
1970
- return compute(owner, !init.length ? init : () => init(() => owner.dispose()), null);
1971
- }
1972
- function runWithOwner(owner, run) {
1973
- return compute(owner, run, null);
1974
- }
1975
- function resolve(fn) {
1976
- return new Promise((res, rej) => {
1977
- createRoot((dispose) => {
1978
- new EagerComputation(void 0, () => {
1979
- try {
1980
- res(fn());
1981
- } catch (err) {
1982
- if (err instanceof NotReadyError)
1983
- throw err;
1984
- rej(err);
1985
- }
1986
- dispose();
1987
- });
1988
- });
1989
- });
1990
- }
1991
- function createPending() {
1992
- const node = new Computation(false, null);
1993
- const reset = () => node.write(false);
1994
- function write() {
1995
- if (!ActiveTransition)
1996
- return false;
1997
- ActiveTransition.addOptimistic(reset);
1998
- queueMicrotask(() => reset._transition && node.write(true));
1999
- }
2000
- function read() {
2001
- const v = node.read();
2002
- return ActiveTransition ? false : v;
2003
- }
2004
- return [read, write];
2005
- }
2006
- function useTransition() {
2007
- const [pending, setPending] = createPending();
2008
- function start(fn) {
2009
- transition((resume) => {
2010
- setPending(true);
2011
- return fn(resume);
2012
- });
2013
- }
2014
- return [pending, start];
2015
- }
2016
- function createOptimistic(first, second, options) {
2017
- let store, setStore;
2018
- if (typeof first === "function") {
2019
- [store, setStore] = createStore((s) => {
2020
- const value = first(s);
2021
- if (!ActiveTransition)
2022
- return value;
2023
- ActiveTransition.addOptimistic(reset);
2024
- }, {});
2025
- } else
2026
- [store, setStore] = createStore(first);
2027
- const reset = () => setStore(
2028
- reconcile(
2029
- typeof first === "function" ? first(second) : first,
2030
- options?.key || "id",
2031
- options?.all
2032
- )
2033
- );
2034
- function write(v) {
2035
- if (!ActiveTransition)
2036
- throw new Error("createOptimistic can only be updated inside a transition");
2037
- ActiveTransition.addOptimistic(reset);
2038
- queueMicrotask(() => reset._transition && setStore(v));
2039
- }
2040
- return [store, write];
2041
- }
2042
-
2043
2062
  // src/map.ts
2044
2063
  function mapArray(list, map, options) {
2045
2064
  const keyFn = typeof options?.keyed === "function" ? options.keyed : void 0;
@@ -2282,8 +2301,6 @@ var ConditionalQueue = class extends Queue {
2282
2301
  return super.run(type);
2283
2302
  }
2284
2303
  notify(node, type, flags) {
2285
- if (ActiveTransition && ActiveTransition._clonedQueues.has(this))
2286
- return ActiveTransition._clonedQueues.get(this).notify(node, type, flags);
2287
2304
  if (this._disabled.read()) {
2288
2305
  if (type & LOADING_BIT) {
2289
2306
  if (flags & LOADING_BIT) {
@@ -2322,8 +2339,6 @@ var CollectionQueue = class extends Queue {
2322
2339
  return super.run(type);
2323
2340
  }
2324
2341
  notify(node, type, flags) {
2325
- if (ActiveTransition && ActiveTransition._clonedQueues.has(this))
2326
- return ActiveTransition._clonedQueues.get(this).notify(node, type, flags);
2327
2342
  if (!(type & this._collectionType))
2328
2343
  return super.notify(node, type, flags);
2329
2344
  if (flags & this._collectionType) {
@@ -2447,4 +2462,4 @@ function flattenArray(children, results = [], options) {
2447
2462
  return needsUnwrap;
2448
2463
  }
2449
2464
 
2450
- 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 };