@solidjs/signals 0.4.10 → 0.5.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
@@ -1387,7 +1387,22 @@ function storeSetter(store, fn) {
1387
1387
  Writing = /* @__PURE__ */ new Set();
1388
1388
  Writing.add(store);
1389
1389
  try {
1390
- fn(store);
1390
+ const value = fn(store);
1391
+ if (value !== store && value !== void 0) {
1392
+ if (Array.isArray(value)) {
1393
+ for (let i = 0, len = value.length; i < len; i++)
1394
+ store[i] = value[i];
1395
+ store.length = value.length;
1396
+ } else {
1397
+ const keys = /* @__PURE__ */ new Set([...Object.keys(store), ...Object.keys(value)]);
1398
+ keys.forEach((key) => {
1399
+ if (key in value)
1400
+ store[key] = value[key];
1401
+ else
1402
+ delete store[key];
1403
+ });
1404
+ }
1405
+ }
1391
1406
  } finally {
1392
1407
  Writing.clear();
1393
1408
  Writing = prevWriting;
@@ -1951,48 +1966,33 @@ function tryCatch(fn) {
1951
1966
  return [e];
1952
1967
  }
1953
1968
  }
1954
- function createOptimistic(initial, compute2, key) {
1955
- if (!compute2) {
1956
- let write2 = function(v) {
1957
- if (!ActiveTransition)
1958
- throw new Error("createOptimistic can only be updated inside a transition");
1959
- ActiveTransition.addOptimistic(reset2);
1960
- queueMicrotask(() => reset2._transition && node.write(v));
1961
- };
1962
- var write = write2;
1963
- const node = new Computation(initial, null);
1964
- const reset2 = () => node.write(initial);
1965
- return [node.read.bind(node), write2];
1969
+ function createOptimistic(initial) {
1970
+ const node = new Computation(initial, null);
1971
+ const reset = () => node.write(initial);
1972
+ function write(v) {
1973
+ if (!ActiveTransition)
1974
+ throw new Error("createOptimistic can only be updated inside a transition");
1975
+ ActiveTransition.addOptimistic(reset);
1976
+ queueMicrotask(() => reset._transition && node.write(v));
1966
1977
  }
1978
+ return [node.read.bind(node), write];
1979
+ }
1980
+ function createOptimisticStore(first, second) {
1967
1981
  let store, setStore;
1968
- if (typeof initial === "function") {
1969
- [store, setStore] = createStore(
1970
- (s) => {
1971
- const value = initial();
1972
- if (!ActiveTransition)
1973
- reconcile({ value }, key)(s);
1974
- },
1975
- { value: void 0 }
1976
- );
1982
+ if (typeof first === "function") {
1983
+ [store, setStore] = createStore((s) => {
1984
+ const value = first(s);
1985
+ if (!ActiveTransition)
1986
+ return value;
1987
+ }, {});
1977
1988
  } else
1978
- [store, setStore] = createStore({ value: initial });
1979
- const reset = () => setStore(
1980
- (s) => reconcile(
1981
- { value: typeof initial === "function" ? initial() : initial },
1982
- key
1983
- )(s)
1984
- );
1985
- let lastChange = void 0;
1989
+ [store, setStore] = createStore(first);
1990
+ const reset = () => setStore(() => typeof first === "function" ? first(second) : first);
1986
1991
  function write(v) {
1987
1992
  if (!ActiveTransition)
1988
1993
  throw new Error("createOptimistic can only be updated inside a transition");
1989
1994
  ActiveTransition.addOptimistic(reset);
1990
- queueMicrotask(
1991
- () => reset._transition && setStore((s) => {
1992
- lastChange = typeof v === "function" ? v(lastChange) : v;
1993
- compute2(s.value, lastChange);
1994
- })
1995
- );
1995
+ queueMicrotask(() => reset._transition && setStore(v));
1996
1996
  }
1997
1997
  return [() => store.value, write];
1998
1998
  }
@@ -2325,7 +2325,7 @@ function createCollectionBoundary(type, fn, fallback) {
2325
2325
  const decision = new Computation(void 0, () => {
2326
2326
  if (!queue._disabled.read()) {
2327
2327
  const resolved = tree.read();
2328
- if (!queue._disabled.read())
2328
+ if (!untrack(() => queue._disabled.read()))
2329
2329
  return resolved;
2330
2330
  }
2331
2331
  return fallback(queue);
@@ -2404,4 +2404,4 @@ function flattenArray(children, results = [], options) {
2404
2404
  return needsUnwrap;
2405
2405
  }
2406
2406
 
2407
- 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, tryCatch, untrack };
2407
+ export { $PROXY, $TARGET, $TRACK, Computation, ContextNotFoundError, NoOwnerError, NotReadyError, Owner, Queue, SUPPORTS_PROXY, createAsync, createBoundary, createContext, createEffect, createErrorBoundary, createMemo, createOptimistic, createOptimisticStore, 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, tryCatch, untrack };
package/dist/node.cjs CHANGED
@@ -1385,7 +1385,22 @@ function storeSetter(store, fn) {
1385
1385
  Writing = /* @__PURE__ */ new Set();
1386
1386
  Writing.add(store);
1387
1387
  try {
1388
- fn(store);
1388
+ const value = fn(store);
1389
+ if (value !== store && value !== void 0) {
1390
+ if (Array.isArray(value)) {
1391
+ for (let i = 0, len = value.length; i < len; i++)
1392
+ store[i] = value[i];
1393
+ store.length = value.length;
1394
+ } else {
1395
+ const keys = /* @__PURE__ */ new Set([...Object.keys(store), ...Object.keys(value)]);
1396
+ keys.forEach((key) => {
1397
+ if (key in value)
1398
+ store[key] = value[key];
1399
+ else
1400
+ delete store[key];
1401
+ });
1402
+ }
1403
+ }
1389
1404
  } finally {
1390
1405
  Writing.clear();
1391
1406
  Writing = prevWriting;
@@ -1954,48 +1969,33 @@ function tryCatch(fn) {
1954
1969
  return [e];
1955
1970
  }
1956
1971
  }
1957
- function createOptimistic(initial, compute2, key) {
1958
- if (!compute2) {
1959
- let write2 = function(v) {
1960
- if (!ActiveTransition)
1961
- throw new Error("createOptimistic can only be updated inside a transition");
1962
- ActiveTransition.addOptimistic(reset2);
1963
- queueMicrotask(() => reset2.j && node.write(v));
1964
- };
1965
- var write = write2;
1966
- const node = new Computation(initial, null);
1967
- const reset2 = () => node.write(initial);
1968
- return [node.read.bind(node), write2];
1972
+ function createOptimistic(initial) {
1973
+ const node = new Computation(initial, null);
1974
+ const reset = () => node.write(initial);
1975
+ function write(v) {
1976
+ if (!ActiveTransition)
1977
+ throw new Error("createOptimistic can only be updated inside a transition");
1978
+ ActiveTransition.addOptimistic(reset);
1979
+ queueMicrotask(() => reset.j && node.write(v));
1969
1980
  }
1981
+ return [node.read.bind(node), write];
1982
+ }
1983
+ function createOptimisticStore(first, second) {
1970
1984
  let store, setStore;
1971
- if (typeof initial === "function") {
1972
- [store, setStore] = createStore(
1973
- (s) => {
1974
- const value = initial();
1975
- if (!ActiveTransition)
1976
- reconcile({ value }, key)(s);
1977
- },
1978
- { value: void 0 }
1979
- );
1985
+ if (typeof first === "function") {
1986
+ [store, setStore] = createStore((s) => {
1987
+ const value = first(s);
1988
+ if (!ActiveTransition)
1989
+ return value;
1990
+ }, {});
1980
1991
  } else
1981
- [store, setStore] = createStore({ value: initial });
1982
- const reset = () => setStore(
1983
- (s) => reconcile(
1984
- { value: typeof initial === "function" ? initial() : initial },
1985
- key
1986
- )(s)
1987
- );
1988
- let lastChange = void 0;
1992
+ [store, setStore] = createStore(first);
1993
+ const reset = () => setStore(() => typeof first === "function" ? first(second) : first);
1989
1994
  function write(v) {
1990
1995
  if (!ActiveTransition)
1991
1996
  throw new Error("createOptimistic can only be updated inside a transition");
1992
1997
  ActiveTransition.addOptimistic(reset);
1993
- queueMicrotask(
1994
- () => reset.j && setStore((s) => {
1995
- lastChange = typeof v === "function" ? v(lastChange) : v;
1996
- compute2(s.value, lastChange);
1997
- })
1998
- );
1998
+ queueMicrotask(() => reset.j && setStore(v));
1999
1999
  }
2000
2000
  return [() => store.value, write];
2001
2001
  }
@@ -2329,7 +2329,7 @@ function createCollectionBoundary(type, fn, fallback) {
2329
2329
  const decision = new Computation(void 0, () => {
2330
2330
  if (!queue.u.read()) {
2331
2331
  const resolved = tree.read();
2332
- if (!queue.u.read())
2332
+ if (!untrack(() => queue.u.read()))
2333
2333
  return resolved;
2334
2334
  }
2335
2335
  return fallback(queue);
@@ -2426,6 +2426,7 @@ exports.createEffect = createEffect;
2426
2426
  exports.createErrorBoundary = createErrorBoundary;
2427
2427
  exports.createMemo = createMemo;
2428
2428
  exports.createOptimistic = createOptimistic;
2429
+ exports.createOptimisticStore = createOptimisticStore;
2429
2430
  exports.createProjection = createProjection;
2430
2431
  exports.createRenderEffect = createRenderEffect;
2431
2432
  exports.createRoot = createRoot;
package/dist/prod.js CHANGED
@@ -1375,7 +1375,22 @@ function storeSetter(store, fn) {
1375
1375
  Writing = /* @__PURE__ */ new Set();
1376
1376
  Writing.add(store);
1377
1377
  try {
1378
- fn(store);
1378
+ const value = fn(store);
1379
+ if (value !== store && value !== void 0) {
1380
+ if (Array.isArray(value)) {
1381
+ for (let i = 0, len = value.length; i < len; i++)
1382
+ store[i] = value[i];
1383
+ store.length = value.length;
1384
+ } else {
1385
+ const keys = /* @__PURE__ */ new Set([...Object.keys(store), ...Object.keys(value)]);
1386
+ keys.forEach((key) => {
1387
+ if (key in value)
1388
+ store[key] = value[key];
1389
+ else
1390
+ delete store[key];
1391
+ });
1392
+ }
1393
+ }
1379
1394
  } finally {
1380
1395
  Writing.clear();
1381
1396
  Writing = prevWriting;
@@ -1939,48 +1954,33 @@ function tryCatch(fn) {
1939
1954
  return [e];
1940
1955
  }
1941
1956
  }
1942
- function createOptimistic(initial, compute2, key) {
1943
- if (!compute2) {
1944
- let write2 = function(v) {
1945
- if (!ActiveTransition)
1946
- throw new Error("createOptimistic can only be updated inside a transition");
1947
- ActiveTransition.addOptimistic(reset2);
1948
- queueMicrotask(() => reset2.j && node.write(v));
1949
- };
1950
- var write = write2;
1951
- const node = new Computation(initial, null);
1952
- const reset2 = () => node.write(initial);
1953
- return [node.read.bind(node), write2];
1957
+ function createOptimistic(initial) {
1958
+ const node = new Computation(initial, null);
1959
+ const reset = () => node.write(initial);
1960
+ function write(v) {
1961
+ if (!ActiveTransition)
1962
+ throw new Error("createOptimistic can only be updated inside a transition");
1963
+ ActiveTransition.addOptimistic(reset);
1964
+ queueMicrotask(() => reset.j && node.write(v));
1954
1965
  }
1966
+ return [node.read.bind(node), write];
1967
+ }
1968
+ function createOptimisticStore(first, second) {
1955
1969
  let store, setStore;
1956
- if (typeof initial === "function") {
1957
- [store, setStore] = createStore(
1958
- (s) => {
1959
- const value = initial();
1960
- if (!ActiveTransition)
1961
- reconcile({ value }, key)(s);
1962
- },
1963
- { value: void 0 }
1964
- );
1970
+ if (typeof first === "function") {
1971
+ [store, setStore] = createStore((s) => {
1972
+ const value = first(s);
1973
+ if (!ActiveTransition)
1974
+ return value;
1975
+ }, {});
1965
1976
  } else
1966
- [store, setStore] = createStore({ value: initial });
1967
- const reset = () => setStore(
1968
- (s) => reconcile(
1969
- { value: typeof initial === "function" ? initial() : initial },
1970
- key
1971
- )(s)
1972
- );
1973
- let lastChange = void 0;
1977
+ [store, setStore] = createStore(first);
1978
+ const reset = () => setStore(() => typeof first === "function" ? first(second) : first);
1974
1979
  function write(v) {
1975
1980
  if (!ActiveTransition)
1976
1981
  throw new Error("createOptimistic can only be updated inside a transition");
1977
1982
  ActiveTransition.addOptimistic(reset);
1978
- queueMicrotask(
1979
- () => reset.j && setStore((s) => {
1980
- lastChange = typeof v === "function" ? v(lastChange) : v;
1981
- compute2(s.value, lastChange);
1982
- })
1983
- );
1983
+ queueMicrotask(() => reset.j && setStore(v));
1984
1984
  }
1985
1985
  return [() => store.value, write];
1986
1986
  }
@@ -2313,7 +2313,7 @@ function createCollectionBoundary(type, fn, fallback) {
2313
2313
  const decision = new Computation(void 0, () => {
2314
2314
  if (!queue.u.read()) {
2315
2315
  const resolved = tree.read();
2316
- if (!queue.u.read())
2316
+ if (!untrack(() => queue.u.read()))
2317
2317
  return resolved;
2318
2318
  }
2319
2319
  return fallback(queue);
@@ -2392,4 +2392,4 @@ function flattenArray(children, results = [], options) {
2392
2392
  return needsUnwrap;
2393
2393
  }
2394
2394
 
2395
- 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, tryCatch, untrack };
2395
+ export { $PROXY, $TARGET, $TRACK, Computation, ContextNotFoundError, NoOwnerError, NotReadyError, Owner, Queue, SUPPORTS_PROXY, createAsync, createBoundary, createContext, createEffect, createErrorBoundary, createMemo, createOptimistic, createOptimisticStore, 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, tryCatch, untrack };
@@ -1,5 +1,6 @@
1
1
  import type { SignalOptions } from "./core/index.js";
2
2
  import { Owner } from "./core/index.js";
3
+ import { type Store, type StoreSetter } from "./store/index.js";
3
4
  export type Accessor<T> = () => T;
4
5
  export type Setter<in out T> = {
5
6
  <U extends T>(...args: undefined extends T ? [] : [value: Exclude<U, Function> | ((prev: T) => U)]): undefined extends T ? undefined : U;
@@ -164,10 +165,18 @@ export declare function tryCatch<T, E = Error>(fn: () => T): TryCatchResult<T, E
164
165
  * and then revert it back to the previous value at end of transition.
165
166
  *
166
167
  * @param initial The initial value of the signal.
167
- * @param compute An optional function to compute the next value based on the previous value and change.
168
+ *
169
+ * @returns A tuple containing an accessor for the current value and a setter function to apply changes.
170
+ */
171
+ export declare function createOptimistic<T>(initial: Exclude<T, Function>): Signal<T>;
172
+ /**
173
+ * Creates an optimistic signal that can be used to optimistically update a value
174
+ * and then revert it back to the previous value at end of transition.
175
+ *
176
+ * @param initial The initial value of the signal.
168
177
  * @param options Optional signal options.
169
178
  *
170
179
  * @returns A tuple containing an accessor for the current value and a setter function to apply changes.
171
180
  */
172
- export declare function createOptimistic<T>(initial: Exclude<T, Function>, compute?: never): [Accessor<T>, (value: T | ((v?: T) => T)) => void];
173
- export declare function createOptimistic<T extends object, U>(initial: T | Accessor<T>, compute: (prev: T, change: U) => void, key: string | ((item: any) => any)): [Accessor<T>, (value: U | ((v?: U) => U)) => void];
181
+ export declare function createOptimisticStore<T extends object = {}>(initial: T | Store<T>): [get: Store<T>, set: StoreSetter<T>];
182
+ export declare function createOptimisticStore<T extends object = {}>(fn: (store: T) => void, initial: T | Store<T>): [get: Store<T>, set: StoreSetter<T>];
@@ -1,6 +1,6 @@
1
1
  import { Computation } from "../core/index.js";
2
2
  export type Store<T> = Readonly<T>;
3
- export type StoreSetter<T> = (fn: (state: T) => void) => void;
3
+ export type StoreSetter<T> = (fn: (state: T) => T | void) => void;
4
4
  type DataNode = Computation<any>;
5
5
  type DataNodes = Record<PropertyKey, DataNode>;
6
6
  export declare const $TRACK: unique symbol, $DEEP: unique symbol, $TARGET: unique symbol, $PROXY: unique symbol, $DELETED: unique symbol;
@@ -26,7 +26,7 @@ export declare function isWrappable<T>(obj: T | NotWrappable): obj is T;
26
26
  export declare function getKeys(source: Record<PropertyKey, any>, override: Record<PropertyKey, any> | undefined, enumerable?: boolean): PropertyKey[];
27
27
  export declare function getPropertyDescriptor(source: Record<PropertyKey, any>, override: Record<PropertyKey, any> | undefined, property: PropertyKey): PropertyDescriptor | undefined;
28
28
  export declare const storeTraps: ProxyHandler<StoreNode>;
29
- export declare function storeSetter<T extends object>(store: Store<T>, fn: (draft: T) => void): void;
29
+ export declare function storeSetter<T extends object>(store: Store<T>, fn: (draft: T) => T | void): void;
30
30
  export declare function createStore<T extends object = {}>(store: T | Store<T>): [get: Store<T>, set: StoreSetter<T>];
31
31
  export declare function createStore<T extends object = {}>(fn: (store: T) => void, store: T | Store<T>): [get: Store<T>, set: StoreSetter<T>];
32
32
  export declare function deep<T extends object>(store: Store<T>): Store<any>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solidjs/signals",
3
- "version": "0.4.10",
3
+ "version": "0.5.0",
4
4
  "description": "",
5
5
  "author": "Ryan Carniato",
6
6
  "license": "MIT",