@shadow-js/core 0.1.0 → 1.0.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/CHANGELOG.md +17 -0
- package/LICENSE +2 -2
- package/README.md +134 -331
- package/dist/dev/action.d.ts +34 -0
- package/dist/dev/action.js +89 -0
- package/dist/dev/action.js.map +1 -0
- package/dist/dev/cache.d.ts +33 -0
- package/dist/dev/cache.js +61 -0
- package/dist/dev/cache.js.map +1 -0
- package/dist/dev/context.d.ts +104 -0
- package/dist/dev/context.js +110 -0
- package/dist/dev/context.js.map +1 -0
- package/dist/dev/effect.d.ts +120 -0
- package/dist/dev/effect.js +110 -0
- package/dist/dev/effect.js.map +1 -0
- package/dist/dev/env.d.ts +27 -0
- package/dist/dev/env.js +30 -0
- package/dist/dev/env.js.map +1 -0
- package/dist/dev/events.d.ts +26 -0
- package/dist/dev/events.js +32 -0
- package/dist/dev/events.js.map +1 -0
- package/dist/dev/extend/index.d.ts +329 -0
- package/dist/dev/extend/index.js +316 -0
- package/dist/dev/extend/index.js.map +1 -0
- package/dist/dev/external-store.d.ts +58 -0
- package/dist/dev/external-store.js +59 -0
- package/dist/dev/external-store.js.map +1 -0
- package/dist/dev/index.d.ts +29 -0
- package/dist/dev/index.js +26 -0
- package/dist/dev/internal/concurrency.d.ts +211 -0
- package/dist/dev/internal/concurrency.js +631 -0
- package/dist/dev/internal/concurrency.js.map +1 -0
- package/dist/dev/internal/devtools.d.ts +69 -0
- package/dist/dev/internal/devtools.js +76 -0
- package/dist/dev/internal/devtools.js.map +1 -0
- package/dist/dev/internal/diagnostics.d.ts +88 -0
- package/dist/dev/internal/diagnostics.js +101 -0
- package/dist/dev/internal/diagnostics.js.map +1 -0
- package/dist/dev/internal/dispose.d.ts +26 -0
- package/dist/dev/internal/dispose.js +48 -0
- package/dist/dev/internal/dispose.js.map +1 -0
- package/dist/dev/internal/flags.js +10 -0
- package/dist/dev/internal/flags.js.map +1 -0
- package/dist/dev/internal/graph.d.ts +231 -0
- package/dist/dev/internal/graph.js +577 -0
- package/dist/dev/internal/graph.js.map +1 -0
- package/dist/dev/internal/hydration-flag.d.ts +31 -0
- package/dist/dev/internal/hydration-flag.js +45 -0
- package/dist/dev/internal/hydration-flag.js.map +1 -0
- package/dist/dev/internal/messages.d.ts +54 -0
- package/dist/dev/internal/messages.js +53 -0
- package/dist/dev/internal/messages.js.map +1 -0
- package/dist/dev/internal/owner.d.ts +140 -0
- package/dist/dev/internal/owner.js +289 -0
- package/dist/dev/internal/owner.js.map +1 -0
- package/dist/dev/internal/queue.js +45 -0
- package/dist/dev/internal/queue.js.map +1 -0
- package/dist/dev/internal/scheduler.d.ts +111 -0
- package/dist/dev/internal/scheduler.js +286 -0
- package/dist/dev/internal/scheduler.js.map +1 -0
- package/dist/dev/internal/state.d.ts +139 -0
- package/dist/dev/internal/state.js +27 -0
- package/dist/dev/internal/state.js.map +1 -0
- package/dist/dev/internal/suspension.d.ts +41 -0
- package/dist/dev/internal/suspension.js +44 -0
- package/dist/dev/internal/suspension.js.map +1 -0
- package/dist/dev/internal/types.d.ts +44 -0
- package/dist/dev/list.d.ts +91 -0
- package/dist/dev/list.js +281 -0
- package/dist/dev/list.js.map +1 -0
- package/dist/dev/memo.d.ts +58 -0
- package/dist/dev/memo.js +40 -0
- package/dist/dev/memo.js.map +1 -0
- package/dist/dev/optimistic.d.ts +24 -0
- package/dist/dev/optimistic.js +44 -0
- package/dist/dev/optimistic.js.map +1 -0
- package/dist/dev/ownership.d.ts +202 -0
- package/dist/dev/ownership.js +260 -0
- package/dist/dev/ownership.js.map +1 -0
- package/dist/dev/props.d.ts +132 -0
- package/dist/dev/props.js +281 -0
- package/dist/dev/props.js.map +1 -0
- package/dist/dev/reaction.d.ts +27 -0
- package/dist/dev/reaction.js +60 -0
- package/dist/dev/reaction.js.map +1 -0
- package/dist/dev/reducer.d.ts +39 -0
- package/dist/dev/reducer.js +38 -0
- package/dist/dev/reducer.js.map +1 -0
- package/dist/dev/refs.d.ts +50 -0
- package/dist/dev/refs.js +57 -0
- package/dist/dev/refs.js.map +1 -0
- package/dist/dev/resource.d.ts +133 -0
- package/dist/dev/resource.js +176 -0
- package/dist/dev/resource.js.map +1 -0
- package/dist/dev/selector.d.ts +32 -0
- package/dist/dev/selector.js +117 -0
- package/dist/dev/selector.js.map +1 -0
- package/dist/dev/signal.d.ts +40 -0
- package/dist/dev/signal.js +38 -0
- package/dist/dev/signal.js.map +1 -0
- package/dist/dev/store/index.d.ts +5 -0
- package/dist/dev/store/index.js +4 -0
- package/dist/dev/store/nodes.js +191 -0
- package/dist/dev/store/nodes.js.map +1 -0
- package/dist/dev/store/proxies.js +159 -0
- package/dist/dev/store/proxies.js.map +1 -0
- package/dist/dev/store/read.d.ts +37 -0
- package/dist/dev/store/read.js +71 -0
- package/dist/dev/store/read.js.map +1 -0
- package/dist/dev/store/reconcile.d.ts +71 -0
- package/dist/dev/store/reconcile.js +185 -0
- package/dist/dev/store/reconcile.js.map +1 -0
- package/dist/dev/store/types.d.ts +104 -0
- package/dist/dev/store/use-store.d.ts +58 -0
- package/dist/dev/store/use-store.js +167 -0
- package/dist/dev/store/use-store.js.map +1 -0
- package/dist/dev/suspense.d.ts +99 -0
- package/dist/dev/suspense.js +44 -0
- package/dist/dev/suspense.js.map +1 -0
- package/dist/dev/transitions.d.ts +75 -0
- package/dist/dev/transitions.js +100 -0
- package/dist/dev/transitions.js.map +1 -0
- package/dist/prod/action.js +2 -0
- package/dist/prod/action.js.map +1 -0
- package/dist/prod/cache.js +2 -0
- package/dist/prod/cache.js.map +1 -0
- package/dist/prod/context.js +2 -0
- package/dist/prod/context.js.map +1 -0
- package/dist/prod/effect.js +2 -0
- package/dist/prod/effect.js.map +1 -0
- package/dist/prod/env.js +2 -0
- package/dist/prod/env.js.map +1 -0
- package/dist/prod/events.js +2 -0
- package/dist/prod/events.js.map +1 -0
- package/dist/prod/extend/index.js +2 -0
- package/dist/prod/extend/index.js.map +1 -0
- package/dist/prod/external-store.js +2 -0
- package/dist/prod/external-store.js.map +1 -0
- package/dist/prod/index.js +1 -0
- package/dist/prod/internal/concurrency.js +2 -0
- package/dist/prod/internal/concurrency.js.map +1 -0
- package/dist/prod/internal/devtools.js +2 -0
- package/dist/prod/internal/devtools.js.map +1 -0
- package/dist/prod/internal/diagnostics.js +2 -0
- package/dist/prod/internal/diagnostics.js.map +1 -0
- package/dist/prod/internal/dispose.js +2 -0
- package/dist/prod/internal/dispose.js.map +1 -0
- package/dist/prod/internal/flags.js +2 -0
- package/dist/prod/internal/flags.js.map +1 -0
- package/dist/prod/internal/graph.js +2 -0
- package/dist/prod/internal/graph.js.map +1 -0
- package/dist/prod/internal/hydration-flag.js +2 -0
- package/dist/prod/internal/hydration-flag.js.map +1 -0
- package/dist/prod/internal/messages.js +2 -0
- package/dist/prod/internal/messages.js.map +1 -0
- package/dist/prod/internal/owner.js +2 -0
- package/dist/prod/internal/owner.js.map +1 -0
- package/dist/prod/internal/queue.js +2 -0
- package/dist/prod/internal/queue.js.map +1 -0
- package/dist/prod/internal/scheduler.js +2 -0
- package/dist/prod/internal/scheduler.js.map +1 -0
- package/dist/prod/internal/state.js +2 -0
- package/dist/prod/internal/state.js.map +1 -0
- package/dist/prod/internal/suspension.js +2 -0
- package/dist/prod/internal/suspension.js.map +1 -0
- package/dist/prod/list.js +2 -0
- package/dist/prod/list.js.map +1 -0
- package/dist/prod/memo.js +2 -0
- package/dist/prod/memo.js.map +1 -0
- package/dist/prod/optimistic.js +2 -0
- package/dist/prod/optimistic.js.map +1 -0
- package/dist/prod/ownership.js +2 -0
- package/dist/prod/ownership.js.map +1 -0
- package/dist/prod/props.js +2 -0
- package/dist/prod/props.js.map +1 -0
- package/dist/prod/reaction.js +2 -0
- package/dist/prod/reaction.js.map +1 -0
- package/dist/prod/reducer.js +2 -0
- package/dist/prod/reducer.js.map +1 -0
- package/dist/prod/refs.js +2 -0
- package/dist/prod/refs.js.map +1 -0
- package/dist/prod/resource.js +2 -0
- package/dist/prod/resource.js.map +1 -0
- package/dist/prod/selector.js +2 -0
- package/dist/prod/selector.js.map +1 -0
- package/dist/prod/signal.js +2 -0
- package/dist/prod/signal.js.map +1 -0
- package/dist/prod/store/index.js +1 -0
- package/dist/prod/store/nodes.js +2 -0
- package/dist/prod/store/nodes.js.map +1 -0
- package/dist/prod/store/proxies.js +2 -0
- package/dist/prod/store/proxies.js.map +1 -0
- package/dist/prod/store/read.js +2 -0
- package/dist/prod/store/read.js.map +1 -0
- package/dist/prod/store/reconcile.js +2 -0
- package/dist/prod/store/reconcile.js.map +1 -0
- package/dist/prod/store/use-store.js +2 -0
- package/dist/prod/store/use-store.js.map +1 -0
- package/dist/prod/suspense.js +2 -0
- package/dist/prod/suspense.js.map +1 -0
- package/dist/prod/transitions.js +2 -0
- package/dist/prod/transitions.js.map +1 -0
- package/dist/server/action.js +2 -0
- package/dist/server/action.js.map +1 -0
- package/dist/server/cache.js +2 -0
- package/dist/server/cache.js.map +1 -0
- package/dist/server/context.js +2 -0
- package/dist/server/context.js.map +1 -0
- package/dist/server/effect.js +2 -0
- package/dist/server/effect.js.map +1 -0
- package/dist/server/env.js +2 -0
- package/dist/server/env.js.map +1 -0
- package/dist/server/events.js +2 -0
- package/dist/server/events.js.map +1 -0
- package/dist/server/extend/index.js +2 -0
- package/dist/server/extend/index.js.map +1 -0
- package/dist/server/external-store.js +2 -0
- package/dist/server/external-store.js.map +1 -0
- package/dist/server/index.js +1 -0
- package/dist/server/internal/concurrency.js +2 -0
- package/dist/server/internal/concurrency.js.map +1 -0
- package/dist/server/internal/devtools.js +2 -0
- package/dist/server/internal/devtools.js.map +1 -0
- package/dist/server/internal/diagnostics.js +2 -0
- package/dist/server/internal/diagnostics.js.map +1 -0
- package/dist/server/internal/dispose.js +2 -0
- package/dist/server/internal/dispose.js.map +1 -0
- package/dist/server/internal/flags.js +2 -0
- package/dist/server/internal/flags.js.map +1 -0
- package/dist/server/internal/graph.js +2 -0
- package/dist/server/internal/graph.js.map +1 -0
- package/dist/server/internal/hydration-flag.js +2 -0
- package/dist/server/internal/hydration-flag.js.map +1 -0
- package/dist/server/internal/messages.js +2 -0
- package/dist/server/internal/messages.js.map +1 -0
- package/dist/server/internal/owner.js +2 -0
- package/dist/server/internal/owner.js.map +1 -0
- package/dist/server/internal/queue.js +2 -0
- package/dist/server/internal/queue.js.map +1 -0
- package/dist/server/internal/scheduler.js +2 -0
- package/dist/server/internal/scheduler.js.map +1 -0
- package/dist/server/internal/state.js +2 -0
- package/dist/server/internal/state.js.map +1 -0
- package/dist/server/internal/suspension.js +2 -0
- package/dist/server/internal/suspension.js.map +1 -0
- package/dist/server/list.js +2 -0
- package/dist/server/list.js.map +1 -0
- package/dist/server/memo.js +2 -0
- package/dist/server/memo.js.map +1 -0
- package/dist/server/optimistic.js +2 -0
- package/dist/server/optimistic.js.map +1 -0
- package/dist/server/ownership.js +2 -0
- package/dist/server/ownership.js.map +1 -0
- package/dist/server/props.js +2 -0
- package/dist/server/props.js.map +1 -0
- package/dist/server/reaction.js +2 -0
- package/dist/server/reaction.js.map +1 -0
- package/dist/server/reducer.js +2 -0
- package/dist/server/reducer.js.map +1 -0
- package/dist/server/refs.js +2 -0
- package/dist/server/refs.js.map +1 -0
- package/dist/server/resource.js +2 -0
- package/dist/server/resource.js.map +1 -0
- package/dist/server/selector.js +2 -0
- package/dist/server/selector.js.map +1 -0
- package/dist/server/signal.js +2 -0
- package/dist/server/signal.js.map +1 -0
- package/dist/server/store/index.js +1 -0
- package/dist/server/store/nodes.js +2 -0
- package/dist/server/store/nodes.js.map +1 -0
- package/dist/server/store/proxies.js +2 -0
- package/dist/server/store/proxies.js.map +1 -0
- package/dist/server/store/read.js +2 -0
- package/dist/server/store/read.js.map +1 -0
- package/dist/server/store/reconcile.js +2 -0
- package/dist/server/store/reconcile.js.map +1 -0
- package/dist/server/store/use-store.js +2 -0
- package/dist/server/store/use-store.js.map +1 -0
- package/dist/server/suspense.js +2 -0
- package/dist/server/suspense.js.map +1 -0
- package/dist/server/transitions.js +2 -0
- package/dist/server/transitions.js.map +1 -0
- package/dist/server-dev/action.js +89 -0
- package/dist/server-dev/action.js.map +1 -0
- package/dist/server-dev/cache.js +61 -0
- package/dist/server-dev/cache.js.map +1 -0
- package/dist/server-dev/context.js +107 -0
- package/dist/server-dev/context.js.map +1 -0
- package/dist/server-dev/effect.js +55 -0
- package/dist/server-dev/effect.js.map +1 -0
- package/dist/server-dev/env.js +30 -0
- package/dist/server-dev/env.js.map +1 -0
- package/dist/server-dev/events.js +32 -0
- package/dist/server-dev/events.js.map +1 -0
- package/dist/server-dev/extend/index.js +316 -0
- package/dist/server-dev/extend/index.js.map +1 -0
- package/dist/server-dev/external-store.js +45 -0
- package/dist/server-dev/external-store.js.map +1 -0
- package/dist/server-dev/index.js +26 -0
- package/dist/server-dev/internal/concurrency.js +632 -0
- package/dist/server-dev/internal/concurrency.js.map +1 -0
- package/dist/server-dev/internal/devtools.js +76 -0
- package/dist/server-dev/internal/devtools.js.map +1 -0
- package/dist/server-dev/internal/diagnostics.js +101 -0
- package/dist/server-dev/internal/diagnostics.js.map +1 -0
- package/dist/server-dev/internal/dispose.js +48 -0
- package/dist/server-dev/internal/dispose.js.map +1 -0
- package/dist/server-dev/internal/flags.js +10 -0
- package/dist/server-dev/internal/flags.js.map +1 -0
- package/dist/server-dev/internal/graph.js +577 -0
- package/dist/server-dev/internal/graph.js.map +1 -0
- package/dist/server-dev/internal/hydration-flag.js +45 -0
- package/dist/server-dev/internal/hydration-flag.js.map +1 -0
- package/dist/server-dev/internal/messages.js +53 -0
- package/dist/server-dev/internal/messages.js.map +1 -0
- package/dist/server-dev/internal/owner.js +289 -0
- package/dist/server-dev/internal/owner.js.map +1 -0
- package/dist/server-dev/internal/queue.js +45 -0
- package/dist/server-dev/internal/queue.js.map +1 -0
- package/dist/server-dev/internal/scheduler.js +286 -0
- package/dist/server-dev/internal/scheduler.js.map +1 -0
- package/dist/server-dev/internal/state.js +27 -0
- package/dist/server-dev/internal/state.js.map +1 -0
- package/dist/server-dev/internal/suspension.js +44 -0
- package/dist/server-dev/internal/suspension.js.map +1 -0
- package/dist/server-dev/list.js +281 -0
- package/dist/server-dev/list.js.map +1 -0
- package/dist/server-dev/memo.js +40 -0
- package/dist/server-dev/memo.js.map +1 -0
- package/dist/server-dev/optimistic.js +44 -0
- package/dist/server-dev/optimistic.js.map +1 -0
- package/dist/server-dev/ownership.js +260 -0
- package/dist/server-dev/ownership.js.map +1 -0
- package/dist/server-dev/props.js +281 -0
- package/dist/server-dev/props.js.map +1 -0
- package/dist/server-dev/reaction.js +60 -0
- package/dist/server-dev/reaction.js.map +1 -0
- package/dist/server-dev/reducer.js +38 -0
- package/dist/server-dev/reducer.js.map +1 -0
- package/dist/server-dev/refs.js +57 -0
- package/dist/server-dev/refs.js.map +1 -0
- package/dist/server-dev/resource.js +175 -0
- package/dist/server-dev/resource.js.map +1 -0
- package/dist/server-dev/selector.js +81 -0
- package/dist/server-dev/selector.js.map +1 -0
- package/dist/server-dev/signal.js +38 -0
- package/dist/server-dev/signal.js.map +1 -0
- package/dist/server-dev/store/index.js +4 -0
- package/dist/server-dev/store/nodes.js +191 -0
- package/dist/server-dev/store/nodes.js.map +1 -0
- package/dist/server-dev/store/proxies.js +159 -0
- package/dist/server-dev/store/proxies.js.map +1 -0
- package/dist/server-dev/store/read.js +71 -0
- package/dist/server-dev/store/read.js.map +1 -0
- package/dist/server-dev/store/reconcile.js +185 -0
- package/dist/server-dev/store/reconcile.js.map +1 -0
- package/dist/server-dev/store/use-store.js +167 -0
- package/dist/server-dev/store/use-store.js.map +1 -0
- package/dist/server-dev/suspense.js +44 -0
- package/dist/server-dev/suspense.js.map +1 -0
- package/dist/server-dev/transitions.js +85 -0
- package/dist/server-dev/transitions.js.map +1 -0
- package/package.json +111 -50
- package/dist/chunk-6Y3CCWOY.js +0 -887
- package/dist/chunk-6Y3CCWOY.js.map +0 -1
- package/dist/chunk-B5FTYDUG.js +0 -3
- package/dist/chunk-B5FTYDUG.js.map +0 -1
- package/dist/index.cjs +0 -2000
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -609
- package/dist/index.d.ts +0 -609
- package/dist/index.js +0 -1094
- package/dist/index.js.map +0 -1
- package/dist/jsx-Bugil6CJ.d.cts +0 -151
- package/dist/jsx-Bugil6CJ.d.ts +0 -151
- package/dist/jsx-dev-runtime.cjs +0 -742
- package/dist/jsx-dev-runtime.cjs.map +0 -1
- package/dist/jsx-dev-runtime.d.cts +0 -2
- package/dist/jsx-dev-runtime.d.ts +0 -2
- package/dist/jsx-dev-runtime.js +0 -4
- package/dist/jsx-dev-runtime.js.map +0 -1
- package/dist/jsx-runtime.cjs +0 -742
- package/dist/jsx-runtime.cjs.map +0 -1
- package/dist/jsx-runtime.d.cts +0 -36
- package/dist/jsx-runtime.d.ts +0 -36
- package/dist/jsx-runtime.js +0 -4
- package/dist/jsx-runtime.js.map +0 -1
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { Reconciler } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Options of {@link reconcile}.
|
|
4
|
+
* @since 1.0.0
|
|
5
|
+
*/
|
|
6
|
+
export interface ReconcileOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Identity of array items: the name of a field (`"id"`) or a function returning the identity.
|
|
9
|
+
* Items without an identity (not an object, or the field is absent, or the function returns
|
|
10
|
+
* `undefined`) are matched by position.
|
|
11
|
+
* @default "id"
|
|
12
|
+
*/
|
|
13
|
+
key?: string | ((item: unknown) => unknown);
|
|
14
|
+
/**
|
|
15
|
+
* Keep keys, and array items, that `next` does not contain. When false, keys missing from `next`
|
|
16
|
+
* are deleted and arrays are truncated to its length; when true, unmatched old array items are
|
|
17
|
+
* kept after the new ones.
|
|
18
|
+
* @default false
|
|
19
|
+
*/
|
|
20
|
+
merge?: boolean;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Describes a new value for a store. Passed to `setStore`, it changes only what differs from the
|
|
24
|
+
* current state: objects are merged key by key, arrays are matched by `key` so retained items keep
|
|
25
|
+
* their identity, and unchanged leaves are not written. Nothing is applied until `setStore` runs.
|
|
26
|
+
* Use it to apply fresh server data to a store without re-rendering what did not change.
|
|
27
|
+
*
|
|
28
|
+
* @param next The desired data. Store proxies are accepted and unwrapped.
|
|
29
|
+
* @param options Item identity (`key`) and whether to keep absent keys (`merge`). Undefined uses
|
|
30
|
+
* the defaults (`key: "id"`, `merge: false`).
|
|
31
|
+
* @returns A reconciler to pass to `setStore`, alone or as the last argument of a path.
|
|
32
|
+
* @throws TypeError (production message `SHADOW_STORE_RECONCILE`) when the returned reconciler is
|
|
33
|
+
* called directly instead of being passed to `setStore`. `setStore` throws TypeError
|
|
34
|
+
* (`SHADOW_STORE_RECONCILE_SHAPE`) when `next` is not the same kind of container (object or
|
|
35
|
+
* array) as the value it reconciles.
|
|
36
|
+
* @example
|
|
37
|
+
* import { reconcile, useStore } from "@shadow-js/core";
|
|
38
|
+
*
|
|
39
|
+
* interface Todo { id: number; title: string; done: boolean }
|
|
40
|
+
* const [todos, setTodos] = useStore({ items: [] as Todo[] });
|
|
41
|
+
*
|
|
42
|
+
* async function refresh(): Promise<void> {
|
|
43
|
+
* const response = await fetch("/api/todos");
|
|
44
|
+
* setTodos("items", reconcile((await response.json()) as Todo[], { key: "id" }));
|
|
45
|
+
* }
|
|
46
|
+
* @see useStore
|
|
47
|
+
* @since 1.0.0
|
|
48
|
+
*/
|
|
49
|
+
export declare function reconcile<T>(next: T, options?: ReconcileOptions): Reconciler<T>;
|
|
50
|
+
/**
|
|
51
|
+
* Marks a draft updater. It is the function itself (`setStore(produce((d) => { d.n += 1 }))` is
|
|
52
|
+
* `setStore((d) => { d.n += 1 })`); it exists so code written for other libraries migrates as is.
|
|
53
|
+
*
|
|
54
|
+
* @param update The draft updater: it mutates the draft it receives.
|
|
55
|
+
* @returns `update`, unchanged.
|
|
56
|
+
* @example
|
|
57
|
+
* import { produce, useStore } from "@shadow-js/core";
|
|
58
|
+
*
|
|
59
|
+
* const [cart, setCart] = useStore({ items: [] as string[], count: 0 });
|
|
60
|
+
* setCart(
|
|
61
|
+
* produce((draft) => {
|
|
62
|
+
* draft.items.push("apple");
|
|
63
|
+
* draft.count += 1;
|
|
64
|
+
* }),
|
|
65
|
+
* );
|
|
66
|
+
* console.log(cart.count); // 1
|
|
67
|
+
* @see useStore
|
|
68
|
+
* @since 1.0.0
|
|
69
|
+
*/
|
|
70
|
+
export declare function produce<T>(update: (draft: T) => void): (draft: T) => void;
|
|
71
|
+
//# sourceMappingURL=reconcile.d.ts.map
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { deleteRaw, isReservedKey, isWrappable, unwrapValue, writeRaw } from "./nodes.js";
|
|
2
|
+
// src/store/reconcile.ts
|
|
3
|
+
/** Each reconciler function maps to its spec. Keys are the reconciler functions themselves. */
|
|
4
|
+
const SPECS = /* @__PURE__ */ new WeakMap();
|
|
5
|
+
/**
|
|
6
|
+
* Looks up the spec of a reconciler created by {@link reconcile}. Not re-exported by any entry: the
|
|
7
|
+
* store setter uses it to tell a reconciler from a draft updater.
|
|
8
|
+
*
|
|
9
|
+
* @param update Any function or object passed to `setStore`.
|
|
10
|
+
* @returns The spec, or `undefined` when `update` is not a `reconcile()` result.
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
function specOf(update) {
|
|
14
|
+
return SPECS.get(update);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Describes a new value for a store. Passed to `setStore`, it changes only what differs from the
|
|
18
|
+
* current state: objects are merged key by key, arrays are matched by `key` so retained items keep
|
|
19
|
+
* their identity, and unchanged leaves are not written. Nothing is applied until `setStore` runs.
|
|
20
|
+
* Use it to apply fresh server data to a store without re-rendering what did not change.
|
|
21
|
+
*
|
|
22
|
+
* @param next The desired data. Store proxies are accepted and unwrapped.
|
|
23
|
+
* @param options Item identity (`key`) and whether to keep absent keys (`merge`). Undefined uses
|
|
24
|
+
* the defaults (`key: "id"`, `merge: false`).
|
|
25
|
+
* @returns A reconciler to pass to `setStore`, alone or as the last argument of a path.
|
|
26
|
+
* @throws TypeError (production message `SHADOW_STORE_RECONCILE`) when the returned reconciler is
|
|
27
|
+
* called directly instead of being passed to `setStore`. `setStore` throws TypeError
|
|
28
|
+
* (`SHADOW_STORE_RECONCILE_SHAPE`) when `next` is not the same kind of container (object or
|
|
29
|
+
* array) as the value it reconciles.
|
|
30
|
+
* @example
|
|
31
|
+
* import { reconcile, useStore } from "@shadow-js/core";
|
|
32
|
+
*
|
|
33
|
+
* interface Todo { id: number; title: string; done: boolean }
|
|
34
|
+
* const [todos, setTodos] = useStore({ items: [] as Todo[] });
|
|
35
|
+
*
|
|
36
|
+
* async function refresh(): Promise<void> {
|
|
37
|
+
* const response = await fetch("/api/todos");
|
|
38
|
+
* setTodos("items", reconcile((await response.json()) as Todo[], { key: "id" }));
|
|
39
|
+
* }
|
|
40
|
+
* @see useStore
|
|
41
|
+
* @since 1.0.0
|
|
42
|
+
*/
|
|
43
|
+
function reconcile(next, options) {
|
|
44
|
+
const spec = {
|
|
45
|
+
next,
|
|
46
|
+
key: options?.key ?? "id",
|
|
47
|
+
merge: options?.merge === true
|
|
48
|
+
};
|
|
49
|
+
const reconciler = () => {
|
|
50
|
+
throw new TypeError("ShadowStore: a reconcile() result must be passed to setStore()");
|
|
51
|
+
};
|
|
52
|
+
SPECS.set(reconciler, spec);
|
|
53
|
+
return reconciler;
|
|
54
|
+
}
|
|
55
|
+
/** Field name or function giving an item's identity; `undefined` means the item has none. */
|
|
56
|
+
function identityOf(item, key) {
|
|
57
|
+
if (!isWrappable(item)) return void 0;
|
|
58
|
+
if (typeof key === "function") return key(item);
|
|
59
|
+
return Object.hasOwn(item, key) ? Reflect.get(item, key) : void 0;
|
|
60
|
+
}
|
|
61
|
+
/** Same container kind: both arrays or both objects. */
|
|
62
|
+
function sameKind(a, b) {
|
|
63
|
+
return Array.isArray(a) === Array.isArray(b);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Applies a reconciler to the store root. The diff is iterative: pending (target, source) pairs
|
|
67
|
+
* live on an explicit stack, so data nested thousands of levels deep cannot overflow the call
|
|
68
|
+
* stack. Each pair is visited once, which also terminates on cyclic input. Not re-exported by any
|
|
69
|
+
* entry: only the store setter calls it, inside its transaction.
|
|
70
|
+
*
|
|
71
|
+
* @param root The raw (unwrapped) object or array to update in place.
|
|
72
|
+
* @param spec The reconciler's spec, from {@link specOf}.
|
|
73
|
+
* @param label The store label for messages (` (name)`, or an empty string).
|
|
74
|
+
* @throws TypeError (production message `SHADOW_STORE_RECONCILE_SHAPE`) when `spec.next` is not the
|
|
75
|
+
* same kind of container as `root`.
|
|
76
|
+
* @internal
|
|
77
|
+
*/
|
|
78
|
+
function applyReconcile(root, spec, label) {
|
|
79
|
+
const next = unwrapValue(spec.next);
|
|
80
|
+
if (!isWrappable(next) || !sameKind(root, next)) throw new TypeError(`ShadowStore${label}: reconcile() needs data of the same shape as the store root (an object or an array)`);
|
|
81
|
+
const stack = [];
|
|
82
|
+
const seen = /* @__PURE__ */ new Map();
|
|
83
|
+
const enqueue = (target, source) => {
|
|
84
|
+
let targets = seen.get(source);
|
|
85
|
+
if (targets === void 0) {
|
|
86
|
+
targets = /* @__PURE__ */ new Set();
|
|
87
|
+
seen.set(source, targets);
|
|
88
|
+
}
|
|
89
|
+
if (targets.has(target)) return;
|
|
90
|
+
targets.add(target);
|
|
91
|
+
stack.push({
|
|
92
|
+
target,
|
|
93
|
+
source
|
|
94
|
+
});
|
|
95
|
+
};
|
|
96
|
+
enqueue(root, next);
|
|
97
|
+
for (let frame = stack.pop(); frame !== void 0; frame = stack.pop()) if (Array.isArray(frame.source)) mergeArray(frame.target, frame.source, spec, enqueue);
|
|
98
|
+
else mergeObject(frame.target, frame.source, spec, enqueue);
|
|
99
|
+
}
|
|
100
|
+
function mergeObject(target, source, spec, enqueue) {
|
|
101
|
+
for (const key of Object.keys(source)) {
|
|
102
|
+
if (isReservedKey(key)) continue;
|
|
103
|
+
const value = unwrapValue(Reflect.get(source, key));
|
|
104
|
+
const had = Object.hasOwn(target, key);
|
|
105
|
+
const current = had ? Reflect.get(target, key) : void 0;
|
|
106
|
+
if (had && Object.is(current, value)) continue;
|
|
107
|
+
if (had && isWrappable(current) && isWrappable(value) && sameKind(current, value)) {
|
|
108
|
+
enqueue(current, value);
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
writeRaw(target, key, value);
|
|
112
|
+
}
|
|
113
|
+
if (spec.merge) return;
|
|
114
|
+
for (const key of Object.keys(target)) if (!Object.hasOwn(source, key)) deleteRaw(target, key);
|
|
115
|
+
}
|
|
116
|
+
function mergeArray(target, source, spec, enqueue) {
|
|
117
|
+
const list = target;
|
|
118
|
+
const incoming = source;
|
|
119
|
+
const original = list.slice();
|
|
120
|
+
const identities = original.map((item) => identityOf(item, spec.key));
|
|
121
|
+
const byIdentity = /* @__PURE__ */ new Map();
|
|
122
|
+
for (let index = 0; index < original.length; index += 1) {
|
|
123
|
+
const identity = identities[index];
|
|
124
|
+
if (identity !== void 0 && !byIdentity.has(identity)) byIdentity.set(identity, index);
|
|
125
|
+
}
|
|
126
|
+
const used = new Array(original.length).fill(false);
|
|
127
|
+
const matches = [];
|
|
128
|
+
for (let position = 0; position < incoming.length; position += 1) {
|
|
129
|
+
const identity = identityOf(unwrapValue(incoming[position]), spec.key);
|
|
130
|
+
let match = -1;
|
|
131
|
+
if (identity !== void 0) {
|
|
132
|
+
const found = byIdentity.get(identity);
|
|
133
|
+
if (found !== void 0 && used[found] !== true) match = found;
|
|
134
|
+
} else if (position < original.length && identities[position] === void 0) {
|
|
135
|
+
if (used[position] !== true) match = position;
|
|
136
|
+
}
|
|
137
|
+
if (match !== -1) used[match] = true;
|
|
138
|
+
matches.push(match);
|
|
139
|
+
}
|
|
140
|
+
const values = [];
|
|
141
|
+
for (let position = 0; position < incoming.length; position += 1) {
|
|
142
|
+
const item = unwrapValue(incoming[position]);
|
|
143
|
+
const match = matches[position] ?? -1;
|
|
144
|
+
if (match === -1) {
|
|
145
|
+
values.push(item);
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
const previous = original[match];
|
|
149
|
+
if (isWrappable(previous) && isWrappable(item) && sameKind(previous, item)) {
|
|
150
|
+
if (previous !== item) enqueue(previous, item);
|
|
151
|
+
values.push(previous);
|
|
152
|
+
} else values.push(item);
|
|
153
|
+
}
|
|
154
|
+
if (spec.merge) {
|
|
155
|
+
for (let index = 0; index < original.length; index += 1) if (used[index] !== true) values.push(original[index]);
|
|
156
|
+
}
|
|
157
|
+
for (let position = 0; position < values.length; position += 1) writeRaw(target, String(position), values[position]);
|
|
158
|
+
if (list.length > values.length) writeRaw(target, "length", values.length);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Marks a draft updater. It is the function itself (`setStore(produce((d) => { d.n += 1 }))` is
|
|
162
|
+
* `setStore((d) => { d.n += 1 })`); it exists so code written for other libraries migrates as is.
|
|
163
|
+
*
|
|
164
|
+
* @param update The draft updater: it mutates the draft it receives.
|
|
165
|
+
* @returns `update`, unchanged.
|
|
166
|
+
* @example
|
|
167
|
+
* import { produce, useStore } from "@shadow-js/core";
|
|
168
|
+
*
|
|
169
|
+
* const [cart, setCart] = useStore({ items: [] as string[], count: 0 });
|
|
170
|
+
* setCart(
|
|
171
|
+
* produce((draft) => {
|
|
172
|
+
* draft.items.push("apple");
|
|
173
|
+
* draft.count += 1;
|
|
174
|
+
* }),
|
|
175
|
+
* );
|
|
176
|
+
* console.log(cart.count); // 1
|
|
177
|
+
* @see useStore
|
|
178
|
+
* @since 1.0.0
|
|
179
|
+
*/
|
|
180
|
+
function produce(update) {
|
|
181
|
+
return update;
|
|
182
|
+
}
|
|
183
|
+
export { applyReconcile, produce, reconcile, specOf };
|
|
184
|
+
|
|
185
|
+
//# sourceMappingURL=reconcile.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reconcile.js","names":[],"sources":["../../../src/store/reconcile.ts"],"sourcesContent":["import { deleteRaw, isReservedKey, isWrappable, unwrapValue, writeRaw } from \"./nodes\";\nimport type { Reconciler } from \"./types\";\n\n/**\n * Options of {@link reconcile}.\n * @since 1.0.0\n */\nexport interface ReconcileOptions {\n /**\n * Identity of array items: the name of a field (`\"id\"`) or a function returning the identity.\n * Items without an identity (not an object, or the field is absent, or the function returns\n * `undefined`) are matched by position.\n * @default \"id\"\n */\n key?: string | ((item: unknown) => unknown);\n /**\n * Keep keys, and array items, that `next` does not contain. When false, keys missing from `next`\n * are deleted and arrays are truncated to its length; when true, unmatched old array items are\n * kept after the new ones.\n * @default false\n */\n merge?: boolean;\n}\n\n/**\n * What a reconciler carries from `reconcile()` to `setStore`. Not re-exported by any entry: only\n * the store setter reads it.\n * @internal\n */\nexport interface ReconcileSpec {\n /** The desired data, as passed to `reconcile()` (it may still be a store proxy). */\n readonly next: unknown;\n /** Identity of array items, resolved from {@link ReconcileOptions.key}. */\n readonly key: string | ((item: unknown) => unknown);\n /** Whether keys and items absent from `next` are kept, from {@link ReconcileOptions.merge}. */\n readonly merge: boolean;\n}\n\n/** Each reconciler function maps to its spec. Keys are the reconciler functions themselves. */\nconst SPECS: WeakMap<object, ReconcileSpec> = new WeakMap();\n\n/**\n * Looks up the spec of a reconciler created by {@link reconcile}. Not re-exported by any entry: the\n * store setter uses it to tell a reconciler from a draft updater.\n *\n * @param update Any function or object passed to `setStore`.\n * @returns The spec, or `undefined` when `update` is not a `reconcile()` result.\n * @internal\n */\nexport function specOf(update: object): ReconcileSpec | undefined {\n return SPECS.get(update);\n}\n\n/**\n * Describes a new value for a store. Passed to `setStore`, it changes only what differs from the\n * current state: objects are merged key by key, arrays are matched by `key` so retained items keep\n * their identity, and unchanged leaves are not written. Nothing is applied until `setStore` runs.\n * Use it to apply fresh server data to a store without re-rendering what did not change.\n *\n * @param next The desired data. Store proxies are accepted and unwrapped.\n * @param options Item identity (`key`) and whether to keep absent keys (`merge`). Undefined uses\n * the defaults (`key: \"id\"`, `merge: false`).\n * @returns A reconciler to pass to `setStore`, alone or as the last argument of a path.\n * @throws TypeError (production message `SHADOW_STORE_RECONCILE`) when the returned reconciler is\n * called directly instead of being passed to `setStore`. `setStore` throws TypeError\n * (`SHADOW_STORE_RECONCILE_SHAPE`) when `next` is not the same kind of container (object or\n * array) as the value it reconciles.\n * @example\n * import { reconcile, useStore } from \"@shadow-js/core\";\n *\n * interface Todo { id: number; title: string; done: boolean }\n * const [todos, setTodos] = useStore({ items: [] as Todo[] });\n *\n * async function refresh(): Promise<void> {\n * const response = await fetch(\"/api/todos\");\n * setTodos(\"items\", reconcile((await response.json()) as Todo[], { key: \"id\" }));\n * }\n * @see useStore\n * @since 1.0.0\n */\nexport function reconcile<T>(next: T, options?: ReconcileOptions): Reconciler<T> {\n const spec: ReconcileSpec = {\n next,\n key: options?.key ?? \"id\",\n merge: options?.merge === true,\n };\n const reconciler: Reconciler<T> = () => {\n throw new TypeError(\n __DEV__\n ? \"ShadowStore: a reconcile() result must be passed to setStore()\"\n : \"SHADOW_STORE_RECONCILE\",\n );\n };\n SPECS.set(reconciler, spec);\n return reconciler;\n}\n\ninterface Frame {\n readonly target: object;\n readonly source: object;\n}\n\n/** Field name or function giving an item's identity; `undefined` means the item has none. */\nfunction identityOf(item: unknown, key: string | ((item: unknown) => unknown)): unknown {\n if (!isWrappable(item)) return undefined;\n if (typeof key === \"function\") return key(item);\n return Object.hasOwn(item, key) ? Reflect.get(item, key) : undefined;\n}\n\n/** Same container kind: both arrays or both objects. */\nfunction sameKind(a: object, b: object): boolean {\n return Array.isArray(a) === Array.isArray(b);\n}\n\n/**\n * Applies a reconciler to the store root. The diff is iterative: pending (target, source) pairs\n * live on an explicit stack, so data nested thousands of levels deep cannot overflow the call\n * stack. Each pair is visited once, which also terminates on cyclic input. Not re-exported by any\n * entry: only the store setter calls it, inside its transaction.\n *\n * @param root The raw (unwrapped) object or array to update in place.\n * @param spec The reconciler's spec, from {@link specOf}.\n * @param label The store label for messages (` (name)`, or an empty string).\n * @throws TypeError (production message `SHADOW_STORE_RECONCILE_SHAPE`) when `spec.next` is not the\n * same kind of container as `root`.\n * @internal\n */\nexport function applyReconcile(root: object, spec: ReconcileSpec, label: string): void {\n const next = unwrapValue(spec.next);\n if (!isWrappable(next) || !sameKind(root, next)) {\n throw new TypeError(\n __DEV__\n ? `ShadowStore${label}: reconcile() needs data of the same shape as the store root (an object or an array)`\n : \"SHADOW_STORE_RECONCILE_SHAPE\",\n );\n }\n const stack: Frame[] = [];\n const seen = new Map<object, Set<object>>();\n const enqueue = (target: object, source: object): void => {\n let targets = seen.get(source);\n if (targets === undefined) {\n targets = new Set();\n seen.set(source, targets);\n }\n if (targets.has(target)) return;\n targets.add(target);\n stack.push({ target, source });\n };\n\n enqueue(root, next);\n for (let frame = stack.pop(); frame !== undefined; frame = stack.pop()) {\n if (Array.isArray(frame.source)) mergeArray(frame.target, frame.source, spec, enqueue);\n else mergeObject(frame.target, frame.source, spec, enqueue);\n }\n}\n\nfunction mergeObject(\n target: object,\n source: object,\n spec: ReconcileSpec,\n enqueue: (target: object, source: object) => void,\n): void {\n for (const key of Object.keys(source)) {\n if (isReservedKey(key)) continue;\n const value = unwrapValue(Reflect.get(source, key));\n const had = Object.hasOwn(target, key);\n const current: unknown = had ? Reflect.get(target, key) : undefined;\n if (had && Object.is(current, value)) continue;\n if (had && isWrappable(current) && isWrappable(value) && sameKind(current, value)) {\n enqueue(current, value);\n continue;\n }\n writeRaw(target, key, value);\n }\n if (spec.merge) return;\n for (const key of Object.keys(target)) {\n if (!Object.hasOwn(source, key)) deleteRaw(target, key);\n }\n}\n\nfunction mergeArray(\n target: object,\n source: object,\n spec: ReconcileSpec,\n enqueue: (target: object, source: object) => void,\n): void {\n const list = target as unknown[];\n const incoming = source as unknown[];\n // Copy first: matching reads the old positions, while the writes below move items.\n const original = list.slice();\n const identities = original.map((item) => identityOf(item, spec.key));\n const byIdentity = new Map<unknown, number>();\n for (let index = 0; index < original.length; index += 1) {\n const identity = identities[index];\n if (identity !== undefined && !byIdentity.has(identity)) byIdentity.set(identity, index);\n }\n\n const used: boolean[] = new Array<boolean>(original.length).fill(false);\n const matches: number[] = [];\n for (let position = 0; position < incoming.length; position += 1) {\n const item = unwrapValue(incoming[position]);\n const identity = identityOf(item, spec.key);\n let match = -1;\n if (identity !== undefined) {\n const found = byIdentity.get(identity);\n if (found !== undefined && used[found] !== true) match = found;\n } else if (position < original.length && identities[position] === undefined) {\n if (used[position] !== true) match = position;\n }\n if (match !== -1) used[match] = true;\n matches.push(match);\n }\n\n const values: unknown[] = [];\n for (let position = 0; position < incoming.length; position += 1) {\n const item = unwrapValue(incoming[position]);\n const match = matches[position] ?? -1;\n if (match === -1) {\n values.push(item);\n continue;\n }\n const previous = original[match];\n if (isWrappable(previous) && isWrappable(item) && sameKind(previous, item)) {\n if (previous !== item) enqueue(previous, item);\n values.push(previous);\n } else {\n values.push(item);\n }\n }\n if (spec.merge) {\n for (let index = 0; index < original.length; index += 1) {\n if (used[index] !== true) values.push(original[index]);\n }\n }\n\n for (let position = 0; position < values.length; position += 1) {\n writeRaw(target, String(position), values[position]);\n }\n if (list.length > values.length) writeRaw(target, \"length\", values.length);\n}\n\n/**\n * Marks a draft updater. It is the function itself (`setStore(produce((d) => { d.n += 1 }))` is\n * `setStore((d) => { d.n += 1 })`); it exists so code written for other libraries migrates as is.\n *\n * @param update The draft updater: it mutates the draft it receives.\n * @returns `update`, unchanged.\n * @example\n * import { produce, useStore } from \"@shadow-js/core\";\n *\n * const [cart, setCart] = useStore({ items: [] as string[], count: 0 });\n * setCart(\n * produce((draft) => {\n * draft.items.push(\"apple\");\n * draft.count += 1;\n * }),\n * );\n * console.log(cart.count); // 1\n * @see useStore\n * @since 1.0.0\n */\nexport function produce<T>(update: (draft: T) => void): (draft: T) => void {\n return update;\n}\n"],"mappings":";;;AAuCA,MAAM,wBAAwC,IAAI,QAAQ;;;;;;;;;AAU1D,SAAgB,OAAO,Q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SAAgB,QAAW,QAAgD;CACzE,OAAO;AACT"}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { Accessor, Setter } from "../internal/types.js";
|
|
2
|
+
/** Values that are never wrapped in a store view: primitives, functions and built-in objects. */
|
|
3
|
+
type Opaque = string | number | bigint | boolean | symbol | null | undefined | ((...args: never[]) => unknown) | Date | RegExp | Map<unknown, unknown> | Set<unknown> | WeakMap<object, unknown> | WeakSet<object> | Promise<unknown>;
|
|
4
|
+
/**
|
|
5
|
+
* Read-only view of `T`. Reads through it subscribe to the exact property read; writes are a type
|
|
6
|
+
* error and throw at runtime outside `setStore`.
|
|
7
|
+
* @since 1.0.0
|
|
8
|
+
*/
|
|
9
|
+
export type Store<T> = T extends Opaque ? T : { readonly [K in keyof T]: Store<T[K]>; };
|
|
10
|
+
/**
|
|
11
|
+
* Writable counterpart of a value: `readonly` is removed at every depth. Used for the raw results
|
|
12
|
+
* of `unwrap` and `snapshot`, because inference from a {@link Store} type yields a readonly shape.
|
|
13
|
+
* @since 1.0.0
|
|
14
|
+
*/
|
|
15
|
+
export type Mutable<T> = T extends Opaque ? T : { -readonly [K in keyof T]: Mutable<T[K]>; };
|
|
16
|
+
/** Brand that marks a `reconcile()` result at the type level. Never present at runtime. */
|
|
17
|
+
declare const reconcilerBrand: unique symbol;
|
|
18
|
+
/**
|
|
19
|
+
* Value produced by {@link reconcile}. It can only be passed to a store's `setStore`.
|
|
20
|
+
* @since 1.0.0
|
|
21
|
+
*/
|
|
22
|
+
export type Reconciler<T> = ((draft: T) => void) & {
|
|
23
|
+
readonly [reconcilerBrand]?: (value: T) => void;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* True for values `useStore` wraps in a deep, fine-grained store (plain objects and arrays). Used by
|
|
27
|
+
* {@link StoreResult}; not re-exported by an entry.
|
|
28
|
+
* @since 1.0.0
|
|
29
|
+
*/
|
|
30
|
+
export type IsPlainData<T> = [T] extends [Opaque] ? false : [T] extends [readonly unknown[]] ? true : [T] extends [object] ? true : false;
|
|
31
|
+
/**
|
|
32
|
+
* True when `T` mixes plain data with opaque members (for example `Todo[] | null`). Used by
|
|
33
|
+
* {@link StoreResult} and {@link NotMixed}; not re-exported by an entry.
|
|
34
|
+
* @since 1.0.0
|
|
35
|
+
*/
|
|
36
|
+
export type IsMixed<T> = [T] extends [Opaque] ? false : [Extract<T, Opaque>] extends [never] ? false : true;
|
|
37
|
+
/** Index forms of a path step on an array: a position, a filter, several positions or a range. */
|
|
38
|
+
type ArrayStep<U> = number | ((item: U, index: number) => boolean) | readonly number[] | {
|
|
39
|
+
from?: number;
|
|
40
|
+
to?: number;
|
|
41
|
+
by?: number;
|
|
42
|
+
};
|
|
43
|
+
type Step<T> = T extends readonly (infer U)[] ? ArrayStep<U> : keyof T | readonly (keyof T)[];
|
|
44
|
+
type Next<T, K> = T extends readonly (infer U)[] ? U : K extends readonly (infer E)[] ? E extends keyof T ? T[E] : never : K extends keyof T ? T[K] : never;
|
|
45
|
+
/**
|
|
46
|
+
* What a path's last step accepts: a value, an updater, a `reconcile()` result, or a partial object
|
|
47
|
+
* that is merged. Used by {@link SetStore}; not re-exported by an entry.
|
|
48
|
+
* @since 1.0.0
|
|
49
|
+
*/
|
|
50
|
+
export type Leaf<V> = V | ((previous: V) => V) | Reconciler<V> | (V extends object ? Partial<V> : never);
|
|
51
|
+
/**
|
|
52
|
+
* Updates a store. Every form is one batched, fine-grained transaction:
|
|
53
|
+
*
|
|
54
|
+
* - `setStore((draft) => { draft.items.push(x) })` mutates a draft; returning a value replaces
|
|
55
|
+
* (reconciles) instead.
|
|
56
|
+
* - `setStore({ title: "x" })` merges keys into an object; `setStore([...])` replaces an array.
|
|
57
|
+
* - `setStore(reconcile(next))` diffs `next` into the store.
|
|
58
|
+
* - `setStore("todos", 2, "done", true)` writes at a path of up to four steps, fully type-checked;
|
|
59
|
+
* a step may be a key, several keys, an array filter `(item) => item.done`, or a range
|
|
60
|
+
* `{ from, to, by }`; the last argument may be a value, an updater, or a partial object that is
|
|
61
|
+
* merged. Deeper or dynamic paths go through a draft function.
|
|
62
|
+
* @since 1.0.0
|
|
63
|
+
*/
|
|
64
|
+
export interface SetStore<T> {
|
|
65
|
+
/**
|
|
66
|
+
* Draft form: `update` mutates a draft of the whole store. Returning a value instead replaces
|
|
67
|
+
* the store with it (an array is reconciled, a plain object is merged).
|
|
68
|
+
*/
|
|
69
|
+
(update: (draft: T) => void | T | Partial<T>): void;
|
|
70
|
+
/** Reconcile form: diffs the data given to {@link reconcile} into the store. */
|
|
71
|
+
(update: Reconciler<T>): void;
|
|
72
|
+
/** Value form: an array replaces an array store (diffed in place); an object merges its keys. */
|
|
73
|
+
(value: T extends readonly unknown[] ? T : Partial<T>): void;
|
|
74
|
+
/** Path form with one step: `a` selects the keys or items, `value` is the leaf update. */
|
|
75
|
+
<A extends Step<T>>(a: A, value: Leaf<Next<T, A>>): void;
|
|
76
|
+
/** Path form with two steps. */
|
|
77
|
+
<A extends Step<T>, B extends Step<Next<T, A>>>(a: A, b: B, value: Leaf<Next<Next<T, A>, B>>): void;
|
|
78
|
+
/** Path form with three steps. */
|
|
79
|
+
<A extends Step<T>, B extends Step<Next<T, A>>, C extends Step<Next<Next<T, A>, B>>>(a: A, b: B, c: C, value: Leaf<Next<Next<Next<T, A>, B>, C>>): void;
|
|
80
|
+
/** Path form with four steps, the deepest typed path; deeper updates use the draft form. */
|
|
81
|
+
<A extends Step<T>, B extends Step<Next<T, A>>, C extends Step<Next<Next<T, A>, B>>, D extends Step<Next<Next<Next<T, A>, B>, C>>>(a: A, b: B, c: C, d: D, value: Leaf<Next<Next<Next<Next<T, A>, B>, C>, D>>): void;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* What `useStore(initial)` returns: `[store, setStore]` for plain data, and an `[accessor, setter]`
|
|
85
|
+
* pair for a single value, including a value that may be plain data or `null` (`Todo[] | null`).
|
|
86
|
+
* @since 1.0.0
|
|
87
|
+
*/
|
|
88
|
+
export type StoreResult<T> = IsMixed<T> extends true ? [Accessor<Store<T>>, Setter<T>] : IsPlainData<T> extends true ? [Store<Mutable<T>>, SetStore<Mutable<T>>] : [Accessor<T>, Setter<T>];
|
|
89
|
+
/**
|
|
90
|
+
* Constraint of the first `useStore` overload: `T` must not mix plain data with opaque members.
|
|
91
|
+
* A union such as `Todo[] | null` is a single value that starts as one of its non-data members
|
|
92
|
+
* and is replaced by the setter (second overload), because data passed as the initial value would
|
|
93
|
+
* make a store at run time.
|
|
94
|
+
* @since 1.0.0
|
|
95
|
+
*/
|
|
96
|
+
export type NotMixed<T> = IsMixed<T> extends true ? never : unknown;
|
|
97
|
+
/**
|
|
98
|
+
* The initial value of a mixed `T`: only its opaque members (`null`, `undefined`, primitives). The
|
|
99
|
+
* parameter type of the second `useStore` overload; not re-exported by an entry.
|
|
100
|
+
* @since 1.0.0
|
|
101
|
+
*/
|
|
102
|
+
export type MixedInitial<T> = Extract<T, Opaque>;
|
|
103
|
+
export {};
|
|
104
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { MixedInitial, NotMixed, StoreResult } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Options of {@link useStore}.
|
|
4
|
+
* @since 1.0.0
|
|
5
|
+
*/
|
|
6
|
+
export interface StoreOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Label shown in error messages and by devtools.
|
|
9
|
+
* @default undefined (the store is unnamed)
|
|
10
|
+
*/
|
|
11
|
+
name?: string;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The state hook. Pass a single value for a reactive cell, or plain data for a deep store:
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* const [count, setCount] = useStore(0); // count() / setCount(n => n + 1)
|
|
18
|
+
* const [todos, setTodos] = useStore({ items: [] as Todo[] });
|
|
19
|
+
* todos.items[0]?.done; // tracks exactly this property
|
|
20
|
+
* setTodos((draft) => { draft.items.push(todo); }); // notifies only what changed
|
|
21
|
+
* setTodos("items", (t) => t.id === 3, "done", true); // path form
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* Reads inside effects, memos and JSX subscribe to the exact value or property read; writes notify
|
|
25
|
+
* exactly the readers of what changed, in one batch. A store wraps plain objects and arrays in
|
|
26
|
+
* place; class instances, `Date`, `Map`, `Set` and functions are single values. A value that may
|
|
27
|
+
* be `null` or another type (`Todo[] | null`) is a single value too: it is replaced as a whole, and
|
|
28
|
+
* plain data assigned to it is read through the same fine-grained proxies a store uses.
|
|
29
|
+
*
|
|
30
|
+
* @param initial Initial value or plain data. Plain objects and arrays are wrapped in place, not
|
|
31
|
+
* copied.
|
|
32
|
+
* @param options `name` labels the store in messages. Undefined uses the default (unnamed).
|
|
33
|
+
* @returns `[read, write]` for a single value, `[store, setStore]` for plain data.
|
|
34
|
+
* @throws ShadowError `WRITE_IN_MEMO` from the setter, in development builds only, when it is called
|
|
35
|
+
* while a {@link useMemo} computation runs. `setStore` throws TypeError (production messages
|
|
36
|
+
* `SHADOW_STORE_*`) for an update it cannot apply: no argument, a bad path step, a path through a
|
|
37
|
+
* value that is not an object, a reserved key such as `__proto__`, or a non-array value for an
|
|
38
|
+
* array store. Writing to the store view itself throws TypeError (`SHADOW_STORE_READ_ONLY`).
|
|
39
|
+
* @example
|
|
40
|
+
* import { useStore } from "@shadow-js/core";
|
|
41
|
+
*
|
|
42
|
+
* interface Todo { id: number; title: string; done: boolean }
|
|
43
|
+
*
|
|
44
|
+
* const [count, setCount] = useStore(0);
|
|
45
|
+
* const [todos, setTodos] = useStore({ items: [] as Todo[] });
|
|
46
|
+
* setCount((n) => n + 1);
|
|
47
|
+
* setTodos((draft) => {
|
|
48
|
+
* draft.items.push({ id: 1, title: "Write docs", done: false });
|
|
49
|
+
* });
|
|
50
|
+
* setTodos("items", (todo) => todo.id === 1, "done", true);
|
|
51
|
+
* console.log(count(), todos.items[0]?.done); // 1 true
|
|
52
|
+
* @see useSignal
|
|
53
|
+
* @see reconcile
|
|
54
|
+
* @since 1.0.0
|
|
55
|
+
*/
|
|
56
|
+
export declare function useStore<T extends NotMixed<T>>(initial: T, options?: StoreOptions): StoreResult<T>;
|
|
57
|
+
export declare function useStore<T>(initial: MixedInitial<T>, options?: StoreOptions): StoreResult<T>;
|
|
58
|
+
//# sourceMappingURL=use-store.d.ts.map
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { DEVTOOLS_EVENT, report } from "../internal/devtools.js";
|
|
2
|
+
import { ShadowError } from "../internal/diagnostics.js";
|
|
3
|
+
import { group } from "../internal/scheduler.js";
|
|
4
|
+
import { untrack } from "../ownership.js";
|
|
5
|
+
import { useSignal } from "../signal.js";
|
|
6
|
+
import { currentOwner, isComputingMemo } from "../extend/index.js";
|
|
7
|
+
import { isReservedKey, isWrappable, unwrapValue } from "./nodes.js";
|
|
8
|
+
import { snapshot } from "./read.js";
|
|
9
|
+
import { applyReconcile, reconcile, specOf } from "./reconcile.js";
|
|
10
|
+
import { Transaction, draftOf, readProxyOf } from "./proxies.js";
|
|
11
|
+
// src/store/use-store.ts
|
|
12
|
+
function isPlainObject(value) {
|
|
13
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
|
|
14
|
+
const prototype = Object.getPrototypeOf(value);
|
|
15
|
+
return prototype === Object.prototype || prototype === null;
|
|
16
|
+
}
|
|
17
|
+
function rangeKeys(range, length) {
|
|
18
|
+
const from = range.from ?? 0;
|
|
19
|
+
const to = range.to ?? length - 1;
|
|
20
|
+
const by = range.by ?? 1;
|
|
21
|
+
if (!Number.isFinite(by) || by === 0) throw new TypeError("ShadowStore: a range needs a non-zero `by`");
|
|
22
|
+
const keys = [];
|
|
23
|
+
for (let i = from; by > 0 ? i <= to : i >= to; i += by) {
|
|
24
|
+
keys.push(i);
|
|
25
|
+
if (keys.length > length + 1) break;
|
|
26
|
+
}
|
|
27
|
+
return keys;
|
|
28
|
+
}
|
|
29
|
+
/** The keys one path step addresses on `target` (a draft). */
|
|
30
|
+
function stepKeys(target, step, label) {
|
|
31
|
+
const keys = [];
|
|
32
|
+
if (Array.isArray(target)) {
|
|
33
|
+
if (typeof step === "number") keys.push(step);
|
|
34
|
+
else if (typeof step === "function") {
|
|
35
|
+
const filter = step;
|
|
36
|
+
for (let i = 0; i < target.length; i += 1) if (filter(target[i], i)) keys.push(i);
|
|
37
|
+
} else if (Array.isArray(step)) keys.push(...step);
|
|
38
|
+
else if (typeof step === "object" && step !== null) keys.push(...rangeKeys(step, target.length));
|
|
39
|
+
else throw new TypeError(`ShadowStore${label}: bad path step for an array`);
|
|
40
|
+
} else if (typeof step === "string" || typeof step === "number") keys.push(step);
|
|
41
|
+
else if (Array.isArray(step)) keys.push(...step);
|
|
42
|
+
else throw new TypeError(`ShadowStore${label}: bad path step for an object`);
|
|
43
|
+
for (const key of keys) if (typeof key === "string" && isReservedKey(key)) throw new TypeError(`ShadowStore${label}: the key "${key}" cannot be used in a path`);
|
|
44
|
+
return keys;
|
|
45
|
+
}
|
|
46
|
+
/** Writes `value` (or the updater's result) at the end of a path, merging plain objects. */
|
|
47
|
+
function applyPath(root, path, label) {
|
|
48
|
+
const value = path[path.length - 1];
|
|
49
|
+
const steps = path.slice(0, -1);
|
|
50
|
+
const walk = (target, depth) => {
|
|
51
|
+
for (const key of stepKeys(target, steps[depth], label)) {
|
|
52
|
+
const holder = target;
|
|
53
|
+
if (depth === steps.length - 1) {
|
|
54
|
+
const previous = holder[key];
|
|
55
|
+
const spec = typeof value === "function" ? specOf(value) : void 0;
|
|
56
|
+
if (spec !== void 0) {
|
|
57
|
+
if (isWrappable(previous)) applyReconcile(unwrapValue(previous), spec, label);
|
|
58
|
+
else holder[key] = spec.next;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
const next = typeof value === "function" ? value(previous) : value;
|
|
62
|
+
if (isPlainObject(next) && isWrappable(previous) && !Array.isArray(previous)) mergeInto(previous, next, label);
|
|
63
|
+
else holder[key] = next;
|
|
64
|
+
} else {
|
|
65
|
+
const child = holder[key];
|
|
66
|
+
if (!isWrappable(child)) throw new TypeError(`ShadowStore${label}: the path goes through a value that is not an object`);
|
|
67
|
+
walk(child, depth + 1);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
walk(root, 0);
|
|
72
|
+
}
|
|
73
|
+
function mergeInto(target, source, label) {
|
|
74
|
+
for (const key of Object.keys(source)) {
|
|
75
|
+
if (isReservedKey(key)) throw new TypeError(`ShadowStore${label}: the key "${key}" cannot be assigned`);
|
|
76
|
+
target[key] = source[key];
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Devtools handles of the stores, by store root. Dev builds only: the WeakMap is unused (and dropped)
|
|
81
|
+
* in production, where `__DEV__` is false.
|
|
82
|
+
*/
|
|
83
|
+
const storeHandles = /* @__PURE__ */ new WeakMap();
|
|
84
|
+
function createDeepStore(initial, label) {
|
|
85
|
+
const root = unwrapValue(initial);
|
|
86
|
+
const store = readProxyOf(root);
|
|
87
|
+
const apply = (...args) => {
|
|
88
|
+
if (isComputingMemo()) throw new ShadowError("WRITE_IN_MEMO");
|
|
89
|
+
if (args.length === 0) throw new TypeError(`ShadowStore${label}: setStore() needs an argument`);
|
|
90
|
+
const first = args[0];
|
|
91
|
+
group(() => untrack(() => {
|
|
92
|
+
if (args.length > 1) {
|
|
93
|
+
const tx = new Transaction(label);
|
|
94
|
+
try {
|
|
95
|
+
applyPath(draftOf(root, tx), args, label);
|
|
96
|
+
} finally {
|
|
97
|
+
tx.open = false;
|
|
98
|
+
}
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (typeof first === "function") {
|
|
102
|
+
const spec = specOf(first);
|
|
103
|
+
if (spec !== void 0) {
|
|
104
|
+
applyReconcile(root, spec, label);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const tx = new Transaction(label);
|
|
108
|
+
let result;
|
|
109
|
+
try {
|
|
110
|
+
result = first(draftOf(root, tx));
|
|
111
|
+
} finally {
|
|
112
|
+
tx.open = false;
|
|
113
|
+
}
|
|
114
|
+
if (result !== void 0) replaceWith(root, result, label);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
replaceWith(root, first, label);
|
|
118
|
+
}));
|
|
119
|
+
};
|
|
120
|
+
const setStore = apply;
|
|
121
|
+
{
|
|
122
|
+
const handle = {
|
|
123
|
+
name: label === "" ? "store" : label.replace(/^ \((.*)\)$/, "$1"),
|
|
124
|
+
snapshot: () => snapshot(store),
|
|
125
|
+
set: (path, value) => {
|
|
126
|
+
apply(...path, value);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
const anchored = storeHandles.get(root);
|
|
130
|
+
if (anchored === void 0) storeHandles.set(root, [handle]);
|
|
131
|
+
else anchored.push(handle);
|
|
132
|
+
report(DEVTOOLS_EVENT.STORE_CREATED, handle, currentOwner());
|
|
133
|
+
}
|
|
134
|
+
return [store, setStore];
|
|
135
|
+
}
|
|
136
|
+
/** `setStore(value)`: an array replaces (diffed in place), a plain object merges its keys. */
|
|
137
|
+
function replaceWith(root, value, label) {
|
|
138
|
+
if (Array.isArray(root)) {
|
|
139
|
+
if (!Array.isArray(value)) throw new TypeError(`ShadowStore${label}: an array store is updated with an array or a function`);
|
|
140
|
+
const spec = specOf(reconcile(value));
|
|
141
|
+
if (spec !== void 0) applyReconcile(root, spec, label);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (!isPlainObject(value)) throw new TypeError(`ShadowStore${label}: setStore() expects an object, a function or a reconcile() result`);
|
|
145
|
+
const tx = new Transaction(label);
|
|
146
|
+
try {
|
|
147
|
+
mergeInto(draftOf(root, tx), value, label);
|
|
148
|
+
} finally {
|
|
149
|
+
tx.open = false;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
function useStore(initial, options) {
|
|
153
|
+
const label = options?.name === void 0 ? "" : ` (${options.name})`;
|
|
154
|
+
const root = unwrapValue(initial);
|
|
155
|
+
if (isWrappable(root)) return createDeepStore(root, label);
|
|
156
|
+
const [read, write] = useSignal(wrapCell(initial), options?.name === void 0 ? void 0 : { name: options.name });
|
|
157
|
+
const set = (next) => write(typeof next === "function" ? (previous) => wrapCell(next(previous)) : wrapCell(next));
|
|
158
|
+
return [read, set];
|
|
159
|
+
}
|
|
160
|
+
/** The value a single-value cell holds: plain data becomes its (stable) read proxy. */
|
|
161
|
+
function wrapCell(value) {
|
|
162
|
+
const raw = unwrapValue(value);
|
|
163
|
+
return isWrappable(raw) ? readProxyOf(raw) : value;
|
|
164
|
+
}
|
|
165
|
+
export { useStore };
|
|
166
|
+
|
|
167
|
+
//# sourceMappingURL=use-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-store.js","names":[],"sources":["../../../src/store/use-store.ts"],"sourcesContent":["import {\n currentOwner,\n DEVTOOLS_EVENT,\n devtoolsReport,\n group,\n isComputingMemo,\n ShadowError,\n} from \"../extend\";\nimport { untrack } from \"../ownership\";\nimport { useSignal } from \"../signal\";\nimport { isReservedKey, isWrappable, unwrapValue } from \"./nodes\";\nimport { draftOf, readProxyOf, Transaction } from \"./proxies\";\nimport { snapshot } from \"./read\";\nimport { applyReconcile, reconcile, specOf } from \"./reconcile\";\nimport type { MixedInitial, NotMixed, SetStore, Store, StoreResult } from \"./types\";\n\n/**\n * Options of {@link useStore}.\n * @since 1.0.0\n */\nexport interface StoreOptions {\n /**\n * Label shown in error messages and by devtools.\n * @default undefined (the store is unnamed)\n */\n name?: string;\n}\n\nfunction isPlainObject(value: unknown): value is Record<string, unknown> {\n if (typeof value !== \"object\" || value === null || Array.isArray(value)) return false;\n const prototype = Object.getPrototypeOf(value);\n return prototype === Object.prototype || prototype === null;\n}\n\nfunction rangeKeys(range: { from?: number; to?: number; by?: number }, length: number): number[] {\n const from = range.from ?? 0;\n const to = range.to ?? length - 1;\n const by = range.by ?? 1;\n if (!Number.isFinite(by) || by === 0)\n throw new TypeError(\n __DEV__ ? \"ShadowStore: a range needs a non-zero `by`\" : \"SHADOW_STORE_RANGE\",\n );\n const keys: number[] = [];\n for (let i = from; by > 0 ? i <= to : i >= to; i += by) {\n keys.push(i);\n if (keys.length > length + 1) break;\n }\n return keys;\n}\n\n/** The keys one path step addresses on `target` (a draft). */\nfunction stepKeys(target: object, step: unknown, label: string): Array<string | number> {\n const keys: Array<string | number> = [];\n if (Array.isArray(target)) {\n if (typeof step === \"number\") keys.push(step);\n else if (typeof step === \"function\") {\n const filter = step as (item: unknown, index: number) => boolean;\n for (let i = 0; i < target.length; i += 1) if (filter(target[i], i)) keys.push(i);\n } else if (Array.isArray(step)) keys.push(...(step as number[]));\n else if (typeof step === \"object\" && step !== null)\n keys.push(...rangeKeys(step, target.length));\n else\n throw new TypeError(\n __DEV__ ? `ShadowStore${label}: bad path step for an array` : \"SHADOW_STORE_PATH_ARRAY\",\n );\n } else if (typeof step === \"string\" || typeof step === \"number\") {\n keys.push(step);\n } else if (Array.isArray(step)) {\n keys.push(...(step as Array<string | number>));\n } else {\n throw new TypeError(\n __DEV__ ? `ShadowStore${label}: bad path step for an object` : \"SHADOW_STORE_PATH_OBJECT\",\n );\n }\n for (const key of keys) {\n if (typeof key === \"string\" && isReservedKey(key)) {\n throw new TypeError(\n __DEV__\n ? `ShadowStore${label}: the key \"${key}\" cannot be used in a path`\n : \"SHADOW_STORE_PATH_KEY\",\n );\n }\n }\n return keys;\n}\n\n/** Writes `value` (or the updater's result) at the end of a path, merging plain objects. */\nfunction applyPath(root: object, path: unknown[], label: string): void {\n const value = path[path.length - 1];\n const steps = path.slice(0, -1);\n const walk = (target: object, depth: number): void => {\n for (const key of stepKeys(target, steps[depth], label)) {\n const holder = target as Record<string | number, unknown>;\n if (depth === steps.length - 1) {\n const previous = holder[key];\n const spec = typeof value === \"function\" ? specOf(value) : undefined;\n if (spec !== undefined) {\n // reconcile(...) at a path diffs only that sub-tree.\n if (isWrappable(previous)) applyReconcile(unwrapValue(previous) as object, spec, label);\n else holder[key] = spec.next;\n continue;\n }\n const next =\n typeof value === \"function\" ? (value as (p: unknown) => unknown)(previous) : value;\n if (isPlainObject(next) && isWrappable(previous) && !Array.isArray(previous)) {\n mergeInto(previous as Record<string, unknown>, next, label);\n } else {\n holder[key] = next;\n }\n } else {\n const child = holder[key];\n if (!isWrappable(child)) {\n throw new TypeError(\n __DEV__\n ? `ShadowStore${label}: the path goes through a value that is not an object`\n : \"SHADOW_STORE_PATH_VALUE\",\n );\n }\n walk(child, depth + 1);\n }\n }\n };\n walk(root, 0);\n}\n\nfunction mergeInto(\n target: Record<string, unknown>,\n source: Record<string, unknown>,\n label: string,\n): void {\n for (const key of Object.keys(source)) {\n if (isReservedKey(key)) {\n throw new TypeError(\n __DEV__\n ? `ShadowStore${label}: the key \"${key}\" cannot be assigned`\n : \"SHADOW_STORE_ASSIGN_KEY\",\n );\n }\n target[key] = source[key];\n }\n}\n\n/**\n * Devtools handles of the stores, by store root. Dev builds only: the WeakMap is unused (and dropped)\n * in production, where `__DEV__` is false.\n */\nconst storeHandles = /* @__PURE__ */ new WeakMap<object, Array<{ readonly name: string }>>();\n\nfunction createDeepStore<T extends object>(initial: T, label: string): [Store<T>, SetStore<T>] {\n const root = unwrapValue(initial) as object;\n const store = readProxyOf(root) as Store<T>;\n\n // One untyped entry point for every form of update. It is assignable to SetStore<T> because its\n // parameters accept anything: the typed overloads are the public contract, and the devtools\n // path setter below calls this function directly with a path it built at run time.\n const apply = (...args: unknown[]): void => {\n if (__DEV__ && isComputingMemo()) throw new ShadowError(\"WRITE_IN_MEMO\");\n if (args.length === 0)\n throw new TypeError(\n __DEV__ ? `ShadowStore${label}: setStore() needs an argument` : \"SHADOW_STORE_NO_ARGUMENT\",\n );\n const first = args[0];\n group(() =>\n untrack(() => {\n if (args.length > 1) {\n const tx = new Transaction(label);\n try {\n applyPath(draftOf(root, tx), args, label);\n } finally {\n tx.open = false;\n }\n return;\n }\n if (typeof first === \"function\") {\n const spec = specOf(first);\n if (spec !== undefined) {\n applyReconcile(root, spec, label);\n return;\n }\n const tx = new Transaction(label);\n let result: unknown;\n try {\n result = (first as (draft: unknown) => unknown)(draftOf(root, tx));\n } finally {\n tx.open = false;\n }\n if (result !== undefined) replaceWith(root, result, label);\n return;\n }\n replaceWith(root, first, label);\n }),\n );\n };\n const setStore: SetStore<T> = apply;\n if (__DEV__) {\n const handle = {\n name: label === \"\" ? \"store\" : label.replace(/^ \\((.*)\\)$/, \"$1\"),\n snapshot: () => snapshot(store),\n set: (path: readonly (string | number)[], value: unknown) => {\n apply(...path, value);\n },\n };\n // Devtools hold the handle weakly, so the handle must stay reachable exactly as long as the store.\n // The store's proxy and its setter both reach `root`, and a WeakMap value lives as long as its key.\n // A root shared by two stores keeps both handles.\n const anchored = storeHandles.get(root);\n if (anchored === undefined) storeHandles.set(root, [handle]);\n else anchored.push(handle);\n devtoolsReport(DEVTOOLS_EVENT.STORE_CREATED, handle, currentOwner());\n }\n return [store, setStore];\n}\n\n/** `setStore(value)`: an array replaces (diffed in place), a plain object merges its keys. */\nfunction replaceWith(root: object, value: unknown, label: string): void {\n if (Array.isArray(root)) {\n if (!Array.isArray(value)) {\n throw new TypeError(\n __DEV__\n ? `ShadowStore${label}: an array store is updated with an array or a function`\n : \"SHADOW_STORE_ARRAY_UPDATE\",\n );\n }\n const spec = specOf(reconcile(value));\n if (spec !== undefined) applyReconcile(root, spec, label);\n return;\n }\n if (!isPlainObject(value)) {\n throw new TypeError(\n __DEV__\n ? `ShadowStore${label}: setStore() expects an object, a function or a reconcile() result`\n : \"SHADOW_STORE_UPDATE_ARGUMENT\",\n );\n }\n const tx = new Transaction(label);\n try {\n mergeInto(draftOf(root, tx) as Record<string, unknown>, value, label);\n } finally {\n tx.open = false;\n }\n}\n\n/**\n * The state hook. Pass a single value for a reactive cell, or plain data for a deep store:\n *\n * ```ts\n * const [count, setCount] = useStore(0); // count() / setCount(n => n + 1)\n * const [todos, setTodos] = useStore({ items: [] as Todo[] });\n * todos.items[0]?.done; // tracks exactly this property\n * setTodos((draft) => { draft.items.push(todo); }); // notifies only what changed\n * setTodos(\"items\", (t) => t.id === 3, \"done\", true); // path form\n * ```\n *\n * Reads inside effects, memos and JSX subscribe to the exact value or property read; writes notify\n * exactly the readers of what changed, in one batch. A store wraps plain objects and arrays in\n * place; class instances, `Date`, `Map`, `Set` and functions are single values. A value that may\n * be `null` or another type (`Todo[] | null`) is a single value too: it is replaced as a whole, and\n * plain data assigned to it is read through the same fine-grained proxies a store uses.\n *\n * @param initial Initial value or plain data. Plain objects and arrays are wrapped in place, not\n * copied.\n * @param options `name` labels the store in messages. Undefined uses the default (unnamed).\n * @returns `[read, write]` for a single value, `[store, setStore]` for plain data.\n * @throws ShadowError `WRITE_IN_MEMO` from the setter, in development builds only, when it is called\n * while a {@link useMemo} computation runs. `setStore` throws TypeError (production messages\n * `SHADOW_STORE_*`) for an update it cannot apply: no argument, a bad path step, a path through a\n * value that is not an object, a reserved key such as `__proto__`, or a non-array value for an\n * array store. Writing to the store view itself throws TypeError (`SHADOW_STORE_READ_ONLY`).\n * @example\n * import { useStore } from \"@shadow-js/core\";\n *\n * interface Todo { id: number; title: string; done: boolean }\n *\n * const [count, setCount] = useStore(0);\n * const [todos, setTodos] = useStore({ items: [] as Todo[] });\n * setCount((n) => n + 1);\n * setTodos((draft) => {\n * draft.items.push({ id: 1, title: \"Write docs\", done: false });\n * });\n * setTodos(\"items\", (todo) => todo.id === 1, \"done\", true);\n * console.log(count(), todos.items[0]?.done); // 1 true\n * @see useSignal\n * @see reconcile\n * @since 1.0.0\n */\nexport function useStore<T extends NotMixed<T>>(initial: T, options?: StoreOptions): StoreResult<T>;\nexport function useStore<T>(initial: MixedInitial<T>, options?: StoreOptions): StoreResult<T>;\nexport function useStore(initial: unknown, options?: StoreOptions): unknown {\n const label = options?.name === undefined ? \"\" : ` (${options.name})`;\n const root = unwrapValue(initial);\n if (isWrappable(root)) return createDeepStore(root, label);\n // A single value, which may be plain data later (`Todo[] | null`): objects and arrays that are\n // assigned are wrapped in read proxies, so `todos()?.[0]?.done` tracks exactly what it reads.\n const [read, write] = useSignal<unknown>(\n wrapCell(initial),\n options?.name === undefined ? undefined : { name: options.name },\n );\n const set = (next: unknown): unknown =>\n write(\n typeof next === \"function\"\n ? (previous: unknown) => wrapCell((next as (previous: unknown) => unknown)(previous))\n : wrapCell(next),\n );\n return [read, set];\n}\n\n/** The value a single-value cell holds: plain data becomes its (stable) read proxy. */\nfunction wrapCell(value: unknown): unknown {\n const raw = unwrapValue(value);\n return isWrappable(raw) ? readProxyOf(raw) : value;\n}\n"],"mappings":";;;;;;;;;;;AA4BA,SAAS,cAAc,OAAkD;CACvE,IAAI,OAAO,UAAU,YAAY,UAAU,QAAQ,MAAM,QAAQ,KAAK,GAAG,OAAO;CAChF,MAAM,YAAY,OAAO,eAAe,KAAK;CAC7C,OAAO,cAAc,OAAO,aAAa,cAAc;AACzD;AAEA,SAAS,UAAU,OAAoD,QAA0B;CAC/F,MAAM,OAAO,MAAM,QAAQ;CAC3B,MAAM,KAAK,MAAM,MAAM,SAAS;CAChC,MAAM,KAAK,MAAM,MAAM;CACvB,IAAI,CAAC,OAAO,SAAS,EAAE,KAAK,OAAO,GACjC,MAAM,IAAI,UACE,4CACZ;CACF,MAAM,OAAiB,CAAC;CACxB,KAAK,IAAI,IAAI,MAAM,KAAK,IAAI,KAAK,KAAK,KAAK,IAAI,KAAK,IAAI;EACtD,KAAK,KAAK,CAAC;EACX,IAAI,KAAK,SAAS,SAAS,GAAG;CAChC;CACA,OAAO;AACT;;AAGA,SAAS,SAAS,QAAgB,MAAe,OAAuC;CACtF,MAAM,OAA+B,CAAC;CACtC,IAAI,MAAM,QAAQ,MAAM,GAAG;EACzB,IAAI,OAAO,SAAS,UAAU,KAAK,KAAK,IAAI;OACvC,IAAI,OAAO,SAAS,YAAY;GACnC,MAAM,SAAS;GACf,KAAK,IAAI,IAAI,GAAG,IAAI,OAAO,QAAQ,KAAK,GAAG,IAAI,OAAO,OAAO,IAAI,CA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