@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,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A value accepted by {@link mergeProps}: an object, a function that returns one (called at every
|
|
3
|
+
* read, so it can be reactive), or `null`, `undefined` or `false`, which are skipped. Use it to type
|
|
4
|
+
* a helper that collects sources before merging them.
|
|
5
|
+
* @since 1.0.0
|
|
6
|
+
*/
|
|
7
|
+
export type MergeSource = object | null | undefined | false;
|
|
8
|
+
/** The props object a source stands for; functions contribute their return type. */
|
|
9
|
+
type PropsOf<S> = S extends ((...args: never[]) => infer R) ? Exclude<R, null | undefined | false> : S extends null | undefined | false ? object : S;
|
|
10
|
+
/** Later keys replace earlier ones; a later key that may be `undefined` keeps the earlier type too. */
|
|
11
|
+
type Overlay<A, B> = { [K in keyof A | keyof B]: K extends keyof B ? K extends keyof A ? undefined extends B[K] ? Exclude<B[K], undefined> | A[K] : B[K] : B[K] : K extends keyof A ? A[K] : never; };
|
|
12
|
+
/**
|
|
13
|
+
* The type of `mergeProps(...sources)`, folded left to right: a later source's key replaces an
|
|
14
|
+
* earlier one, and a later key that may be `undefined` keeps the earlier type as well.
|
|
15
|
+
* @since 1.0.0
|
|
16
|
+
*/
|
|
17
|
+
export type MergeProps<T extends readonly unknown[], Acc = object> = T extends readonly [infer Head, ...infer Rest] ? MergeProps<Rest, Overlay<Acc, PropsOf<Head>>> : Acc;
|
|
18
|
+
/**
|
|
19
|
+
* The type of `splitProps(props, ...keyLists)`: one object per key list, picking those keys, then
|
|
20
|
+
* the rest without any listed key.
|
|
21
|
+
* @since 1.0.0
|
|
22
|
+
*/
|
|
23
|
+
export type SplitProps<T extends object, K extends readonly (readonly (keyof T)[])[]> = [...{ [I in keyof K]: Pick<T, Extract<K[I][number], keyof T>>; }, Omit<T, K[number][number]>];
|
|
24
|
+
/**
|
|
25
|
+
* The value returned by {@link children}: calling it yields one leaf, an array of several leaves,
|
|
26
|
+
* or `undefined` for none; `toArray()` always returns an array. The methods mirror React's
|
|
27
|
+
* `Children` helpers.
|
|
28
|
+
* @since 1.0.0
|
|
29
|
+
*/
|
|
30
|
+
export interface ResolvedChildren {
|
|
31
|
+
/**
|
|
32
|
+
* The current children: one leaf, an array of several leaves, or `undefined` when there is none.
|
|
33
|
+
* The read is tracked like a memo read.
|
|
34
|
+
*/
|
|
35
|
+
(): unknown;
|
|
36
|
+
/** A fresh array of the leaves, without `null`, `undefined` or booleans. */
|
|
37
|
+
toArray(): unknown[];
|
|
38
|
+
/** Number of leaves, zero or more (the React `Children.count`). */
|
|
39
|
+
count(): number;
|
|
40
|
+
/** Maps each leaf with its index; the callback runs untracked (React `Children.map`). */
|
|
41
|
+
map<U>(fn: (leaf: unknown, index: number) => U): U[];
|
|
42
|
+
/** Visits each leaf with its index; the callback runs untracked (React `Children.forEach`). */
|
|
43
|
+
forEach(fn: (leaf: unknown, index: number) => void): void;
|
|
44
|
+
/**
|
|
45
|
+
* The single leaf (React `Children.only`). Throws a TypeError (production message
|
|
46
|
+
* `SHADOW_CHILDREN_ONLY`) when there is not exactly one leaf.
|
|
47
|
+
*/
|
|
48
|
+
only(): unknown;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Merges props objects into one getter-preserving object. Reading a key returns the value from the
|
|
52
|
+
* right-most source that has it, unless that value is `undefined`, in which case the next source
|
|
53
|
+
* to the left is tried. Nothing is copied at merge time, so reads stay reactive. Use it to give
|
|
54
|
+
* component props defaults without reading them in the component body.
|
|
55
|
+
*
|
|
56
|
+
* Sources may be objects, `null`, `undefined` or `false` (skipped), or functions that return an
|
|
57
|
+
* object. A function is called on every read, which is how spreads such as `{...props()}` stay
|
|
58
|
+
* reactive. Keys named `__proto__`, `constructor` or `prototype` are ignored.
|
|
59
|
+
*
|
|
60
|
+
* @param sources The props objects, from lowest to highest priority.
|
|
61
|
+
* @returns A read-only object whose keys are the union of the sources' own enumerable keys. With a
|
|
62
|
+
* function source, the key set is recomputed on every access.
|
|
63
|
+
* @throws TypeError (production message `SHADOW_MERGE_SOURCES`) when a source is not an object, a
|
|
64
|
+
* function, `null`, `undefined` or `false`. A read of the result throws TypeError (production
|
|
65
|
+
* message `SHADOW_MERGE_SOURCE`) when a function source returns anything else.
|
|
66
|
+
* @example
|
|
67
|
+
* import { mergeProps } from "@shadow-js/core";
|
|
68
|
+
*
|
|
69
|
+
* function Button(props: { size?: "sm" | "md"; label: string }) {
|
|
70
|
+
* const merged = mergeProps({ size: "md" as const }, props);
|
|
71
|
+
* return <button class={`btn-${merged.size}`}>{merged.label}</button>;
|
|
72
|
+
* }
|
|
73
|
+
* @see splitProps
|
|
74
|
+
* @since 1.0.0
|
|
75
|
+
*/
|
|
76
|
+
export declare function mergeProps<T extends readonly MergeSource[]>(...sources: T): MergeProps<T>;
|
|
77
|
+
/**
|
|
78
|
+
* Splits props into one getter-preserving object per key list, followed by an object holding every
|
|
79
|
+
* other key. Each group reads its keys from `props` at access time, so reactivity is preserved. A
|
|
80
|
+
* key listed in two groups appears in both and never in the rest. A listed key that `props` does not
|
|
81
|
+
* have is still present in its group, as `undefined`. Use it to separate the props a component
|
|
82
|
+
* consumes from the ones it forwards to an element.
|
|
83
|
+
*
|
|
84
|
+
* @param props The props to split; plain objects, Proxies and {@link mergeProps} results work.
|
|
85
|
+
* @param keys One or more key lists. Keys named `__proto__`, `constructor` or `prototype` are
|
|
86
|
+
* ignored.
|
|
87
|
+
* @returns `[...groups, rest]`, in the order the key lists were given.
|
|
88
|
+
* @example
|
|
89
|
+
* import { splitProps } from "@shadow-js/core";
|
|
90
|
+
*
|
|
91
|
+
* function Field(props: { label: string; value: string; placeholder?: string }) {
|
|
92
|
+
* const [own, input] = splitProps(props, ["label"]);
|
|
93
|
+
* return (
|
|
94
|
+
* <label>
|
|
95
|
+
* {own.label}
|
|
96
|
+
* <input {...input} />
|
|
97
|
+
* </label>
|
|
98
|
+
* );
|
|
99
|
+
* }
|
|
100
|
+
* @see mergeProps
|
|
101
|
+
* @since 1.0.0
|
|
102
|
+
*/
|
|
103
|
+
export declare function splitProps<T extends object, const K extends readonly (readonly (keyof T)[])[]>(props: T, ...keys: K): SplitProps<T, K>;
|
|
104
|
+
/**
|
|
105
|
+
* Memoizes a `props.children` getter so that it is read at most once per change of its
|
|
106
|
+
* dependencies. Arrays are flattened, accessors are called, and `null`, `undefined` and booleans
|
|
107
|
+
* are dropped. The memo is created in the current owner, so nodes created while resolving are
|
|
108
|
+
* disposed with it. Use it when a component inspects or rearranges its children.
|
|
109
|
+
*
|
|
110
|
+
* @param fn The lazy children accessor, usually `() => props.children`.
|
|
111
|
+
* @returns A {@link ResolvedChildren} accessor with `toArray()`, `count()`, `map()`, `forEach()`
|
|
112
|
+
* and `only()`.
|
|
113
|
+
* @throws TypeError from the returned accessor and its methods when the children contain an array
|
|
114
|
+
* that contains itself (production message `SHADOW_CHILDREN_CYCLE`) or more than 1000 nested
|
|
115
|
+
* accessors (`SHADOW_CHILDREN_LAYERS`). `only()` throws TypeError (`SHADOW_CHILDREN_ONLY`) unless
|
|
116
|
+
* there is exactly one leaf.
|
|
117
|
+
* @example
|
|
118
|
+
* import { children } from "@shadow-js/core";
|
|
119
|
+
*
|
|
120
|
+
* function Steps(props: { children?: unknown }) {
|
|
121
|
+
* const steps = children(() => props.children);
|
|
122
|
+
* return (
|
|
123
|
+
* <ol aria-label={`${steps.count()} steps`}>
|
|
124
|
+
* {steps.map((step) => <li>{step as Node | string}</li>)}
|
|
125
|
+
* </ol>
|
|
126
|
+
* );
|
|
127
|
+
* }
|
|
128
|
+
* @since 1.0.0
|
|
129
|
+
*/
|
|
130
|
+
export declare function children(fn: () => unknown): ResolvedChildren;
|
|
131
|
+
export {};
|
|
132
|
+
//# sourceMappingURL=props.d.ts.map
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import { untrack } from "./ownership.js";
|
|
2
|
+
import { useMemo } from "./memo.js";
|
|
3
|
+
// src/props.ts
|
|
4
|
+
/**
|
|
5
|
+
* Keys that never take part in a merge or a split. Reading them through a props object that came
|
|
6
|
+
* from JSON or from user input could reach `Object.prototype`, so they are skipped everywhere.
|
|
7
|
+
*/
|
|
8
|
+
const UNSAFE_KEYS = /* @__PURE__ */ new Set([
|
|
9
|
+
"__proto__",
|
|
10
|
+
"constructor",
|
|
11
|
+
"prototype"
|
|
12
|
+
]);
|
|
13
|
+
/**
|
|
14
|
+
* Results of mergeProps (and splitProps over them) that have at least one function source. Their
|
|
15
|
+
* key set can change between reads, so splitProps builds a live rest object for them. A WeakSet is
|
|
16
|
+
* bounded by the garbage collector.
|
|
17
|
+
*/
|
|
18
|
+
const DYNAMIC_PROPS = /* @__PURE__ */ new WeakSet();
|
|
19
|
+
/** Longest chain of nested accessors that children() follows before it gives up. */
|
|
20
|
+
const MAX_FUNCTION_LAYERS = 1e3;
|
|
21
|
+
/** Whether a source stands for nothing and is skipped. */
|
|
22
|
+
function isEmptySource(value) {
|
|
23
|
+
return value === null || value === void 0 || value === false;
|
|
24
|
+
}
|
|
25
|
+
/** Own enumerable string and symbol keys, minus the unsafe ones. Reads no getter. */
|
|
26
|
+
function ownEnumerableKeys(source) {
|
|
27
|
+
return Reflect.ownKeys(source).filter((key) => !UNSAFE_KEYS.has(key) && Object.prototype.propertyIsEnumerable.call(source, key));
|
|
28
|
+
}
|
|
29
|
+
/** Defines an enumerable getter; the target is a fresh object, so this never hits a setter. */
|
|
30
|
+
function defineGetter(target, key, read) {
|
|
31
|
+
Object.defineProperty(target, key, {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
configurable: true,
|
|
34
|
+
get: read
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
/** The object a source stands for at this moment, or `undefined` when it is skipped. */
|
|
38
|
+
function resolveSource(source) {
|
|
39
|
+
const value = typeof source === "function" ? source() : source;
|
|
40
|
+
if (isEmptySource(value)) return void 0;
|
|
41
|
+
if (typeof value === "object" && value !== null) return value;
|
|
42
|
+
throw new TypeError("mergeProps: a source function must return an object, null, undefined or false");
|
|
43
|
+
}
|
|
44
|
+
/** The sources that stand for an object right now, in order; skipped ones are dropped. */
|
|
45
|
+
function resolveAll(sources) {
|
|
46
|
+
const resolved = [];
|
|
47
|
+
for (const source of sources) {
|
|
48
|
+
const object = resolveSource(source);
|
|
49
|
+
if (object !== void 0) resolved.push(object);
|
|
50
|
+
}
|
|
51
|
+
return resolved;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Reads one merged key: the right-most object that has the key and a value other than `undefined`
|
|
55
|
+
* wins. Each getter is read at most once.
|
|
56
|
+
*/
|
|
57
|
+
function readMerged(objects, key) {
|
|
58
|
+
for (let index = objects.length - 1; index >= 0; index -= 1) {
|
|
59
|
+
const source = objects[index];
|
|
60
|
+
if (source === void 0 || !(key in source)) continue;
|
|
61
|
+
const value = Reflect.get(source, key);
|
|
62
|
+
if (value !== void 0) return value;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/** Every merged key in first-occurrence order. Reads no getter. */
|
|
66
|
+
function mergedKeys(objects) {
|
|
67
|
+
const seen = /* @__PURE__ */ new Set();
|
|
68
|
+
const keys = [];
|
|
69
|
+
for (const source of objects) for (const key of ownEnumerableKeys(source)) {
|
|
70
|
+
if (seen.has(key)) continue;
|
|
71
|
+
seen.add(key);
|
|
72
|
+
keys.push(key);
|
|
73
|
+
}
|
|
74
|
+
return keys;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* A Proxy whose key set and values are recomputed on every access: `load` runs once per trap, so
|
|
78
|
+
* a function source is called once per access. The target is an ordinary object, so prototype
|
|
79
|
+
* lookups keep their usual meaning, and every write is refused because props are read-only.
|
|
80
|
+
*/
|
|
81
|
+
/**
|
|
82
|
+
* How deep the live reads are running, and the state each live view computed during the outermost
|
|
83
|
+
* read. A view's traps read the views beneath it, and those read the views beneath them: without a
|
|
84
|
+
* cache every trap recomputes each level below, and a few nested components (a chain of `{...props}`
|
|
85
|
+
* forwards) take exponential time. Within one outermost read a view's state is computed once, so a
|
|
86
|
+
* read costs time linear in the number of views. The cache is dropped when that read returns, so each
|
|
87
|
+
* read still sees the current values.
|
|
88
|
+
*/
|
|
89
|
+
let readDepth = 0;
|
|
90
|
+
const readStates = /* @__PURE__ */ new Map();
|
|
91
|
+
function stateOf(view, load) {
|
|
92
|
+
const known = readStates.get(view);
|
|
93
|
+
if (known !== void 0) return known;
|
|
94
|
+
const state = load();
|
|
95
|
+
readStates.set(view, state);
|
|
96
|
+
return state;
|
|
97
|
+
}
|
|
98
|
+
function duringRead(run) {
|
|
99
|
+
readDepth += 1;
|
|
100
|
+
try {
|
|
101
|
+
return run();
|
|
102
|
+
} finally {
|
|
103
|
+
readDepth -= 1;
|
|
104
|
+
if (readDepth === 0) readStates.clear();
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function liveView(load) {
|
|
108
|
+
const target = {};
|
|
109
|
+
return new Proxy(target, {
|
|
110
|
+
get(_target, key) {
|
|
111
|
+
return duringRead(() => {
|
|
112
|
+
const state = stateOf(target, load);
|
|
113
|
+
return state.keys.has(key) ? state.read(key) : Reflect.get(target, key);
|
|
114
|
+
});
|
|
115
|
+
},
|
|
116
|
+
has(_target, key) {
|
|
117
|
+
return duringRead(() => stateOf(target, load).keys.has(key) || Reflect.has(target, key));
|
|
118
|
+
},
|
|
119
|
+
ownKeys() {
|
|
120
|
+
return duringRead(() => [...stateOf(target, load).keys]);
|
|
121
|
+
},
|
|
122
|
+
getOwnPropertyDescriptor(_target, key) {
|
|
123
|
+
return duringRead(() => {
|
|
124
|
+
if (!stateOf(target, load).keys.has(key)) return void 0;
|
|
125
|
+
return {
|
|
126
|
+
enumerable: true,
|
|
127
|
+
configurable: true,
|
|
128
|
+
get: () => duringRead(() => stateOf(target, load).read(key))
|
|
129
|
+
};
|
|
130
|
+
});
|
|
131
|
+
},
|
|
132
|
+
set: () => false,
|
|
133
|
+
defineProperty: () => false,
|
|
134
|
+
deleteProperty: () => false
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
function mergeProps(...sources) {
|
|
138
|
+
const inputs = [];
|
|
139
|
+
let dynamic = false;
|
|
140
|
+
for (const source of sources) {
|
|
141
|
+
if (isEmptySource(source)) continue;
|
|
142
|
+
if (typeof source === "function") dynamic = true;
|
|
143
|
+
else if (typeof source !== "object") throw new TypeError("mergeProps: sources must be objects, functions, or null, undefined or false");
|
|
144
|
+
inputs.push(source);
|
|
145
|
+
}
|
|
146
|
+
if (!dynamic) {
|
|
147
|
+
const objects = resolveAll(inputs);
|
|
148
|
+
const merged = {};
|
|
149
|
+
for (const key of mergedKeys(objects)) defineGetter(merged, key, () => readMerged(objects, key));
|
|
150
|
+
return merged;
|
|
151
|
+
}
|
|
152
|
+
const view = liveView(() => {
|
|
153
|
+
const objects = resolveAll(inputs);
|
|
154
|
+
return {
|
|
155
|
+
keys: new Set(mergedKeys(objects)),
|
|
156
|
+
read: (key) => readMerged(objects, key)
|
|
157
|
+
};
|
|
158
|
+
});
|
|
159
|
+
DYNAMIC_PROPS.add(view);
|
|
160
|
+
return view;
|
|
161
|
+
}
|
|
162
|
+
function splitProps(props, ...keys) {
|
|
163
|
+
const listed = /* @__PURE__ */ new Set();
|
|
164
|
+
const groups = [];
|
|
165
|
+
for (const group of keys) {
|
|
166
|
+
const picked = {};
|
|
167
|
+
for (const key of group) {
|
|
168
|
+
if (UNSAFE_KEYS.has(key)) continue;
|
|
169
|
+
listed.add(key);
|
|
170
|
+
defineGetter(picked, key, () => Reflect.get(props, key));
|
|
171
|
+
}
|
|
172
|
+
groups.push(picked);
|
|
173
|
+
}
|
|
174
|
+
const restKeys = () => ownEnumerableKeys(props).filter((key) => !listed.has(key));
|
|
175
|
+
const readRest = (key) => Reflect.get(props, key);
|
|
176
|
+
if (DYNAMIC_PROPS.has(props)) {
|
|
177
|
+
const rest = liveView(() => ({
|
|
178
|
+
keys: new Set(restKeys()),
|
|
179
|
+
read: readRest
|
|
180
|
+
}));
|
|
181
|
+
return [...groups, rest];
|
|
182
|
+
}
|
|
183
|
+
const rest = {};
|
|
184
|
+
for (const key of restKeys()) defineGetter(rest, key, () => readRest(key));
|
|
185
|
+
return [...groups, rest];
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Resolves children without recursion: arrays flatten in order, accessors are called until a plain
|
|
189
|
+
* value appears, and `null`, `undefined` and booleans are dropped. A cyclic array or more than
|
|
190
|
+
* {@link MAX_FUNCTION_LAYERS} nested accessors is a TypeError.
|
|
191
|
+
*/
|
|
192
|
+
function flattenChildren(fn) {
|
|
193
|
+
const leaves = [];
|
|
194
|
+
const frames = [];
|
|
195
|
+
const open = /* @__PURE__ */ new Set();
|
|
196
|
+
let value = fn;
|
|
197
|
+
let layer = 0;
|
|
198
|
+
for (;;) {
|
|
199
|
+
if (typeof value === "function") {
|
|
200
|
+
if (layer >= MAX_FUNCTION_LAYERS) throw new TypeError(`children(): more than ${MAX_FUNCTION_LAYERS} nested accessors; a function child may return itself`);
|
|
201
|
+
value = value();
|
|
202
|
+
layer += 1;
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
if (Array.isArray(value)) {
|
|
206
|
+
if (open.has(value)) throw new TypeError("children(): an array contains itself");
|
|
207
|
+
open.add(value);
|
|
208
|
+
frames.push({
|
|
209
|
+
items: value,
|
|
210
|
+
next: 0,
|
|
211
|
+
layer
|
|
212
|
+
});
|
|
213
|
+
} else if (value !== null && value !== void 0 && typeof value !== "boolean") leaves.push(value);
|
|
214
|
+
let advanced = false;
|
|
215
|
+
while (!advanced) {
|
|
216
|
+
const top = frames.at(-1);
|
|
217
|
+
if (top === void 0) return leaves;
|
|
218
|
+
if (top.next < top.items.length) {
|
|
219
|
+
value = top.items[top.next];
|
|
220
|
+
top.next += 1;
|
|
221
|
+
layer = top.layer;
|
|
222
|
+
advanced = true;
|
|
223
|
+
} else {
|
|
224
|
+
frames.pop();
|
|
225
|
+
open.delete(top.items);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Memoizes a `props.children` getter so that it is read at most once per change of its
|
|
232
|
+
* dependencies. Arrays are flattened, accessors are called, and `null`, `undefined` and booleans
|
|
233
|
+
* are dropped. The memo is created in the current owner, so nodes created while resolving are
|
|
234
|
+
* disposed with it. Use it when a component inspects or rearranges its children.
|
|
235
|
+
*
|
|
236
|
+
* @param fn The lazy children accessor, usually `() => props.children`.
|
|
237
|
+
* @returns A {@link ResolvedChildren} accessor with `toArray()`, `count()`, `map()`, `forEach()`
|
|
238
|
+
* and `only()`.
|
|
239
|
+
* @throws TypeError from the returned accessor and its methods when the children contain an array
|
|
240
|
+
* that contains itself (production message `SHADOW_CHILDREN_CYCLE`) or more than 1000 nested
|
|
241
|
+
* accessors (`SHADOW_CHILDREN_LAYERS`). `only()` throws TypeError (`SHADOW_CHILDREN_ONLY`) unless
|
|
242
|
+
* there is exactly one leaf.
|
|
243
|
+
* @example
|
|
244
|
+
* import { children } from "@shadow-js/core";
|
|
245
|
+
*
|
|
246
|
+
* function Steps(props: { children?: unknown }) {
|
|
247
|
+
* const steps = children(() => props.children);
|
|
248
|
+
* return (
|
|
249
|
+
* <ol aria-label={`${steps.count()} steps`}>
|
|
250
|
+
* {steps.map((step) => <li>{step as Node | string}</li>)}
|
|
251
|
+
* </ol>
|
|
252
|
+
* );
|
|
253
|
+
* }
|
|
254
|
+
* @since 1.0.0
|
|
255
|
+
*/
|
|
256
|
+
function children(fn) {
|
|
257
|
+
const memo = useMemo(() => flattenChildren(fn), { allowWrites: true });
|
|
258
|
+
const accessor = () => {
|
|
259
|
+
const leaves = memo();
|
|
260
|
+
if (leaves.length === 1) return leaves[0];
|
|
261
|
+
return leaves.length === 0 ? void 0 : leaves;
|
|
262
|
+
};
|
|
263
|
+
return Object.assign(accessor, {
|
|
264
|
+
toArray: () => [...memo()],
|
|
265
|
+
count: () => memo().length,
|
|
266
|
+
map: (fn) => memo().map((leaf, index) => untrack(() => fn(leaf, index))),
|
|
267
|
+
forEach: (fn) => {
|
|
268
|
+
memo().forEach((leaf, index) => {
|
|
269
|
+
untrack(() => fn(leaf, index));
|
|
270
|
+
});
|
|
271
|
+
},
|
|
272
|
+
only: () => {
|
|
273
|
+
const leaves = memo();
|
|
274
|
+
if (leaves.length !== 1) throw new TypeError(`children().only(): expected one child, received ${leaves.length}`);
|
|
275
|
+
return leaves[0];
|
|
276
|
+
}
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
export { children, mergeProps, splitProps };
|
|
280
|
+
|
|
281
|
+
//# sourceMappingURL=props.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"props.js","names":[],"sources":["../../src/props.ts"],"sourcesContent":["import { useMemo } from \"./memo\";\nimport { untrack } from \"./ownership\";\n\n/**\n * Keys that never take part in a merge or a split. Reading them through a props object that came\n * from JSON or from user input could reach `Object.prototype`, so they are skipped everywhere.\n */\nconst UNSAFE_KEYS: ReadonlySet<PropertyKey> = new Set<PropertyKey>([\n \"__proto__\",\n \"constructor\",\n \"prototype\",\n]);\n\n/**\n * Results of mergeProps (and splitProps over them) that have at least one function source. Their\n * key set can change between reads, so splitProps builds a live rest object for them. A WeakSet is\n * bounded by the garbage collector.\n */\nconst DYNAMIC_PROPS = new WeakSet<object>();\n\n/** Longest chain of nested accessors that children() follows before it gives up. */\nconst MAX_FUNCTION_LAYERS = 1000;\n\n/**\n * A value accepted by {@link mergeProps}: an object, a function that returns one (called at every\n * read, so it can be reactive), or `null`, `undefined` or `false`, which are skipped. Use it to type\n * a helper that collects sources before merging them.\n * @since 1.0.0\n */\nexport type MergeSource = object | null | undefined | false;\n\n/** The props object a source stands for; functions contribute their return type. */\ntype PropsOf<S> = S extends (...args: never[]) => infer R\n ? Exclude<R, null | undefined | false>\n : S extends null | undefined | false\n ? object\n : S;\n\n/** Later keys replace earlier ones; a later key that may be `undefined` keeps the earlier type too. */\ntype Overlay<A, B> = {\n [K in keyof A | keyof B]: K extends keyof B\n ? K extends keyof A\n ? undefined extends B[K]\n ? Exclude<B[K], undefined> | A[K]\n : B[K]\n : B[K]\n : K extends keyof A\n ? A[K]\n : never;\n};\n\n/**\n * The type of `mergeProps(...sources)`, folded left to right: a later source's key replaces an\n * earlier one, and a later key that may be `undefined` keeps the earlier type as well.\n * @since 1.0.0\n */\nexport type MergeProps<T extends readonly unknown[], Acc = object> = T extends readonly [\n infer Head,\n ...infer Rest,\n]\n ? MergeProps<Rest, Overlay<Acc, PropsOf<Head>>>\n : Acc;\n\n/**\n * The type of `splitProps(props, ...keyLists)`: one object per key list, picking those keys, then\n * the rest without any listed key.\n * @since 1.0.0\n */\nexport type SplitProps<T extends object, K extends readonly (readonly (keyof T)[])[]> = [\n ...{ [I in keyof K]: Pick<T, Extract<K[I][number], keyof T>> },\n Omit<T, K[number][number]>,\n];\n\n/**\n * The value returned by {@link children}: calling it yields one leaf, an array of several leaves,\n * or `undefined` for none; `toArray()` always returns an array. The methods mirror React's\n * `Children` helpers.\n * @since 1.0.0\n */\nexport interface ResolvedChildren {\n /**\n * The current children: one leaf, an array of several leaves, or `undefined` when there is none.\n * The read is tracked like a memo read.\n */\n (): unknown;\n /** A fresh array of the leaves, without `null`, `undefined` or booleans. */\n toArray(): unknown[];\n /** Number of leaves, zero or more (the React `Children.count`). */\n count(): number;\n /** Maps each leaf with its index; the callback runs untracked (React `Children.map`). */\n map<U>(fn: (leaf: unknown, index: number) => U): U[];\n /** Visits each leaf with its index; the callback runs untracked (React `Children.forEach`). */\n forEach(fn: (leaf: unknown, index: number) => void): void;\n /**\n * The single leaf (React `Children.only`). Throws a TypeError (production message\n * `SHADOW_CHILDREN_ONLY`) when there is not exactly one leaf.\n */\n only(): unknown;\n}\n\n/** Whether a source stands for nothing and is skipped. */\nfunction isEmptySource(value: unknown): value is null | undefined | false {\n return value === null || value === undefined || value === false;\n}\n\n/** Own enumerable string and symbol keys, minus the unsafe ones. Reads no getter. */\nfunction ownEnumerableKeys(source: object): Key[] {\n return Reflect.ownKeys(source).filter(\n (key) => !UNSAFE_KEYS.has(key) && Object.prototype.propertyIsEnumerable.call(source, key),\n );\n}\n\n/** Defines an enumerable getter; the target is a fresh object, so this never hits a setter. */\nfunction defineGetter(target: object, key: PropertyKey, read: () => unknown): void {\n Object.defineProperty(target, key, { enumerable: true, configurable: true, get: read });\n}\n\n/** The object a source stands for at this moment, or `undefined` when it is skipped. */\nfunction resolveSource(source: object): object | undefined {\n const value: unknown = typeof source === \"function\" ? (source as () => unknown)() : source;\n if (isEmptySource(value)) return undefined;\n if (typeof value === \"object\" && value !== null) return value;\n throw new TypeError(\n __DEV__\n ? \"mergeProps: a source function must return an object, null, undefined or false\"\n : \"SHADOW_MERGE_SOURCE\",\n );\n}\n\n/** The sources that stand for an object right now, in order; skipped ones are dropped. */\nfunction resolveAll(sources: readonly object[]): object[] {\n const resolved: object[] = [];\n for (const source of sources) {\n const object = resolveSource(source);\n if (object !== undefined) resolved.push(object);\n }\n return resolved;\n}\n\n/**\n * Reads one merged key: the right-most object that has the key and a value other than `undefined`\n * wins. Each getter is read at most once.\n */\nfunction readMerged(objects: readonly object[], key: Key): unknown {\n for (let index = objects.length - 1; index >= 0; index -= 1) {\n const source = objects[index];\n if (source === undefined || !(key in source)) continue;\n const value: unknown = Reflect.get(source, key);\n if (value !== undefined) return value;\n }\n return undefined;\n}\n\n/** Every merged key in first-occurrence order. Reads no getter. */\nfunction mergedKeys(objects: readonly object[]): Key[] {\n const seen = new Set<Key>();\n const keys: Key[] = [];\n for (const source of objects) {\n for (const key of ownEnumerableKeys(source)) {\n if (seen.has(key)) continue;\n seen.add(key);\n keys.push(key);\n }\n }\n return keys;\n}\n\n/** A property name as a Proxy trap sees it. */\ntype Key = string | symbol;\n\n/** The key set and the reader that a live view uses for one access. */\ninterface LiveState {\n readonly keys: ReadonlySet<Key>;\n read(key: Key): unknown;\n}\n\n/**\n * A Proxy whose key set and values are recomputed on every access: `load` runs once per trap, so\n * a function source is called once per access. The target is an ordinary object, so prototype\n * lookups keep their usual meaning, and every write is refused because props are read-only.\n */\n/**\n * How deep the live reads are running, and the state each live view computed during the outermost\n * read. A view's traps read the views beneath it, and those read the views beneath them: without a\n * cache every trap recomputes each level below, and a few nested components (a chain of `{...props}`\n * forwards) take exponential time. Within one outermost read a view's state is computed once, so a\n * read costs time linear in the number of views. The cache is dropped when that read returns, so each\n * read still sees the current values.\n */\nlet readDepth = 0;\nconst readStates = new Map<object, LiveState>();\n\nfunction stateOf(view: object, load: () => LiveState): LiveState {\n const known = readStates.get(view);\n if (known !== undefined) return known;\n const state = load();\n readStates.set(view, state);\n return state;\n}\n\nfunction duringRead<T>(run: () => T): T {\n readDepth += 1;\n try {\n return run();\n } finally {\n readDepth -= 1;\n if (readDepth === 0) readStates.clear();\n }\n}\n\nfunction liveView(load: () => LiveState): object {\n const target: object = {};\n return new Proxy(target, {\n get(_target, key) {\n return duringRead(() => {\n const state = stateOf(target, load);\n return state.keys.has(key) ? state.read(key) : (Reflect.get(target, key) as unknown);\n });\n },\n has(_target, key) {\n return duringRead(() => stateOf(target, load).keys.has(key) || Reflect.has(target, key));\n },\n ownKeys() {\n return duringRead(() => [...stateOf(target, load).keys]);\n },\n getOwnPropertyDescriptor(_target, key) {\n return duringRead(() => {\n if (!stateOf(target, load).keys.has(key)) return undefined;\n return {\n enumerable: true,\n configurable: true,\n get: () => duringRead(() => stateOf(target, load).read(key)),\n };\n });\n },\n set: () => false,\n defineProperty: () => false,\n deleteProperty: () => false,\n });\n}\n\n/**\n * Merges props objects into one getter-preserving object. Reading a key returns the value from the\n * right-most source that has it, unless that value is `undefined`, in which case the next source\n * to the left is tried. Nothing is copied at merge time, so reads stay reactive. Use it to give\n * component props defaults without reading them in the component body.\n *\n * Sources may be objects, `null`, `undefined` or `false` (skipped), or functions that return an\n * object. A function is called on every read, which is how spreads such as `{...props()}` stay\n * reactive. Keys named `__proto__`, `constructor` or `prototype` are ignored.\n *\n * @param sources The props objects, from lowest to highest priority.\n * @returns A read-only object whose keys are the union of the sources' own enumerable keys. With a\n * function source, the key set is recomputed on every access.\n * @throws TypeError (production message `SHADOW_MERGE_SOURCES`) when a source is not an object, a\n * function, `null`, `undefined` or `false`. A read of the result throws TypeError (production\n * message `SHADOW_MERGE_SOURCE`) when a function source returns anything else.\n * @example\n * import { mergeProps } from \"@shadow-js/core\";\n *\n * function Button(props: { size?: \"sm\" | \"md\"; label: string }) {\n * const merged = mergeProps({ size: \"md\" as const }, props);\n * return <button class={`btn-${merged.size}`}>{merged.label}</button>;\n * }\n * @see splitProps\n * @since 1.0.0\n */\nexport function mergeProps<T extends readonly MergeSource[]>(...sources: T): MergeProps<T>;\nexport function mergeProps(...sources: readonly MergeSource[]): object {\n const inputs: object[] = [];\n let dynamic = false;\n for (const source of sources) {\n if (isEmptySource(source)) continue;\n if (typeof source === \"function\") {\n dynamic = true;\n } else if (typeof source !== \"object\") {\n throw new TypeError(\n __DEV__\n ? \"mergeProps: sources must be objects, functions, or null, undefined or false\"\n : \"SHADOW_MERGE_SOURCES\",\n );\n }\n inputs.push(source);\n }\n if (!dynamic) {\n const objects = resolveAll(inputs);\n const merged: object = {};\n for (const key of mergedKeys(objects)) {\n defineGetter(merged, key, () => readMerged(objects, key));\n }\n return merged;\n }\n const view = liveView(() => {\n const objects = resolveAll(inputs);\n return { keys: new Set(mergedKeys(objects)), read: (key) => readMerged(objects, key) };\n });\n DYNAMIC_PROPS.add(view);\n return view;\n}\n\n/**\n * Splits props into one getter-preserving object per key list, followed by an object holding every\n * other key. Each group reads its keys from `props` at access time, so reactivity is preserved. A\n * key listed in two groups appears in both and never in the rest. A listed key that `props` does not\n * have is still present in its group, as `undefined`. Use it to separate the props a component\n * consumes from the ones it forwards to an element.\n *\n * @param props The props to split; plain objects, Proxies and {@link mergeProps} results work.\n * @param keys One or more key lists. Keys named `__proto__`, `constructor` or `prototype` are\n * ignored.\n * @returns `[...groups, rest]`, in the order the key lists were given.\n * @example\n * import { splitProps } from \"@shadow-js/core\";\n *\n * function Field(props: { label: string; value: string; placeholder?: string }) {\n * const [own, input] = splitProps(props, [\"label\"]);\n * return (\n * <label>\n * {own.label}\n * <input {...input} />\n * </label>\n * );\n * }\n * @see mergeProps\n * @since 1.0.0\n */\nexport function splitProps<T extends object, const K extends readonly (readonly (keyof T)[])[]>(\n props: T,\n ...keys: K\n): SplitProps<T, K>;\nexport function splitProps(props: object, ...keys: readonly (readonly PropertyKey[])[]): object[] {\n const listed = new Set<PropertyKey>();\n const groups: object[] = [];\n for (const group of keys) {\n const picked: object = {};\n for (const key of group) {\n if (UNSAFE_KEYS.has(key)) continue;\n listed.add(key);\n defineGetter(picked, key, () => Reflect.get(props, key) as unknown);\n }\n groups.push(picked);\n }\n const restKeys = (): Key[] => ownEnumerableKeys(props).filter((key) => !listed.has(key));\n const readRest = (key: Key): unknown => Reflect.get(props, key) as unknown;\n if (DYNAMIC_PROPS.has(props)) {\n const rest = liveView(() => ({ keys: new Set(restKeys()), read: readRest }));\n return [...groups, rest];\n }\n const rest: object = {};\n for (const key of restKeys()) defineGetter(rest, key, () => readRest(key));\n return [...groups, rest];\n}\n\n/** One level of a children walk: an array being read and the function-layer depth it sits at. */\ninterface Frame {\n readonly items: readonly unknown[];\n next: number;\n readonly layer: number;\n}\n\n/**\n * Resolves children without recursion: arrays flatten in order, accessors are called until a plain\n * value appears, and `null`, `undefined` and booleans are dropped. A cyclic array or more than\n * {@link MAX_FUNCTION_LAYERS} nested accessors is a TypeError.\n */\nfunction flattenChildren(fn: () => unknown): unknown[] {\n const leaves: unknown[] = [];\n const frames: Frame[] = [];\n const open = new Set<readonly unknown[]>();\n let value: unknown = fn;\n let layer = 0;\n for (;;) {\n if (typeof value === \"function\") {\n if (layer >= MAX_FUNCTION_LAYERS) {\n throw new TypeError(\n __DEV__\n ? `children(): more than ${MAX_FUNCTION_LAYERS} nested accessors; a function child may return itself`\n : \"SHADOW_CHILDREN_LAYERS\",\n );\n }\n value = (value as () => unknown)();\n layer += 1;\n continue;\n }\n if (Array.isArray(value)) {\n if (open.has(value))\n throw new TypeError(\n __DEV__ ? \"children(): an array contains itself\" : \"SHADOW_CHILDREN_CYCLE\",\n );\n open.add(value);\n frames.push({ items: value, next: 0, layer });\n } else if (value !== null && value !== undefined && typeof value !== \"boolean\") {\n leaves.push(value);\n }\n let advanced = false;\n while (!advanced) {\n const top = frames.at(-1);\n if (top === undefined) return leaves;\n if (top.next < top.items.length) {\n value = top.items[top.next];\n top.next += 1;\n layer = top.layer;\n advanced = true;\n } else {\n frames.pop();\n open.delete(top.items);\n }\n }\n }\n}\n\n/**\n * Memoizes a `props.children` getter so that it is read at most once per change of its\n * dependencies. Arrays are flattened, accessors are called, and `null`, `undefined` and booleans\n * are dropped. The memo is created in the current owner, so nodes created while resolving are\n * disposed with it. Use it when a component inspects or rearranges its children.\n *\n * @param fn The lazy children accessor, usually `() => props.children`.\n * @returns A {@link ResolvedChildren} accessor with `toArray()`, `count()`, `map()`, `forEach()`\n * and `only()`.\n * @throws TypeError from the returned accessor and its methods when the children contain an array\n * that contains itself (production message `SHADOW_CHILDREN_CYCLE`) or more than 1000 nested\n * accessors (`SHADOW_CHILDREN_LAYERS`). `only()` throws TypeError (`SHADOW_CHILDREN_ONLY`) unless\n * there is exactly one leaf.\n * @example\n * import { children } from \"@shadow-js/core\";\n *\n * function Steps(props: { children?: unknown }) {\n * const steps = children(() => props.children);\n * return (\n * <ol aria-label={`${steps.count()} steps`}>\n * {steps.map((step) => <li>{step as Node | string}</li>)}\n * </ol>\n * );\n * }\n * @since 1.0.0\n */\nexport function children(fn: () => unknown): ResolvedChildren {\n const memo = useMemo(() => flattenChildren(fn), { allowWrites: true });\n const accessor = (): unknown => {\n const leaves = memo();\n if (leaves.length === 1) return leaves[0];\n return leaves.length === 0 ? undefined : leaves;\n };\n return Object.assign(accessor, {\n toArray: (): unknown[] => [...memo()],\n count: (): number => memo().length,\n map: <U>(fn: (leaf: unknown, index: number) => U): U[] =>\n memo().map((leaf, index) => untrack(() => fn(leaf, index))),\n forEach: (fn: (leaf: unknown, index: number) => void): void => {\n memo().forEach((leaf, index) => {\n untrack(() => fn(leaf, index));\n });\n },\n only: (): unknown => {\n const leaves = memo();\n if (leaves.length !== 1) {\n throw new TypeError(\n __DEV__\n ? `children().only(): expected one child, received ${leaves.length}`\n : \"SHADOW_CHILDREN_ONLY\",\n );\n }\n return leaves[0];\n },\n });\n}\n"],"mappings":";;;;;;;AAOA,MAAM,8BAAwC,IAAI,IAAiB;CACjE;CACA;CACA;AACF,CAAC;;;;;;AAOD,MAAM,gCAAgB,IAAI,QAAgB;;AAG1C,MAAM,sBAAsB;;AAgF5B,SAAS,cAAc,OAAmD;CACxE,OAAO,UAAU,QAAQ,UAAU,KAAA,KAAa,UAAU;AAC5D;;AAGA,SAAS,kBAAkB,QAAuB;CAChD,OAAO,QAAQ,QAAQ,MAAM,CAAC,CAAC,QAC5B,QAAQ,CAAC,YAAY,IAAI,GAAG,KAAK,OAAO,UAAU,qBAAqB,KAAK,QAAQ,GAAG,CAC1F;AACF;;AAGA,SAAS,aAAa,QAAgB,KAAkB,MAA2B;CACjF,OAAO,eAAe,QAAQ,KAAK;EAAE,YAAY;EAAM,c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K,CAAC,CAAC;EAC5D,UAAU,OAAqD;GAC7D,KAAK,CAAC,CAAC,SAAS,MAAM,UAAU;IAC9B,cAAc,GAAG,MAAM,KAAK,CAAC;GAC/B,CAAC;EACH;EACA,YAAqB;GACnB,MAAM,SAAS,KAAK;GACpB,IAAI,OAAO,WAAW,GACpB,MAAM,IAAI,UAEJ,mDAAmD,OAAO,QAEhE;GAEF,OAAO,OAAO;EAChB;CACF,CAAC;AACH"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a tracker that reports the first change of what it reads. `useReaction(onInvalidate)`
|
|
3
|
+
* returns `track(fn)`: `fn` runs at once (synchronously) and the signals it reads are recorded.
|
|
4
|
+
* The first change of any of them calls `onInvalidate` once, untracked, and then nothing is
|
|
5
|
+
* observed until `track` is called again. The ShadowJS form of Solid's `createReaction`.
|
|
6
|
+
*
|
|
7
|
+
* Use it when the work that follows a change is not a pure rerun of `fn` (for example, re-fetching
|
|
8
|
+
* after a change and then deciding what to track next). For ordinary effects use `useEffect`.
|
|
9
|
+
*
|
|
10
|
+
* @param onInvalidate Called once, untracked, after a change of a tracked read.
|
|
11
|
+
* @returns `track(fn)`. Each call replaces the previous tracking. Tracking lives in a child of the
|
|
12
|
+
* owner that called `useReaction`, so it stops when that owner is disposed.
|
|
13
|
+
* @example
|
|
14
|
+
* import { useReaction, useSignal } from "@shadow-js/core";
|
|
15
|
+
*
|
|
16
|
+
* function Results() {
|
|
17
|
+
* const [filters] = useSignal({ query: "" });
|
|
18
|
+
* const [stale, setStale] = useSignal(false);
|
|
19
|
+
* const track = useReaction(() => setStale(true));
|
|
20
|
+
* track(() => filters()); // the first change of filters marks the results stale
|
|
21
|
+
* return <p>{stale() ? "Refresh to update" : "Up to date"}</p>;
|
|
22
|
+
* }
|
|
23
|
+
* @see useEffect
|
|
24
|
+
* @since 1.0.0
|
|
25
|
+
*/
|
|
26
|
+
export declare function useReaction(onInvalidate: () => void): (track: () => void) => void;
|
|
27
|
+
//# sourceMappingURL=reaction.d.ts.map
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { untrack } from "./ownership.js";
|
|
2
|
+
import { disposeTree } from "./internal/dispose.js";
|
|
3
|
+
import { createComputation, createOwner, currentOwner, runInScope } from "./extend/index.js";
|
|
4
|
+
// src/reaction.ts
|
|
5
|
+
/**
|
|
6
|
+
* Creates a tracker that reports the first change of what it reads. `useReaction(onInvalidate)`
|
|
7
|
+
* returns `track(fn)`: `fn` runs at once (synchronously) and the signals it reads are recorded.
|
|
8
|
+
* The first change of any of them calls `onInvalidate` once, untracked, and then nothing is
|
|
9
|
+
* observed until `track` is called again. The ShadowJS form of Solid's `createReaction`.
|
|
10
|
+
*
|
|
11
|
+
* Use it when the work that follows a change is not a pure rerun of `fn` (for example, re-fetching
|
|
12
|
+
* after a change and then deciding what to track next). For ordinary effects use `useEffect`.
|
|
13
|
+
*
|
|
14
|
+
* @param onInvalidate Called once, untracked, after a change of a tracked read.
|
|
15
|
+
* @returns `track(fn)`. Each call replaces the previous tracking. Tracking lives in a child of the
|
|
16
|
+
* owner that called `useReaction`, so it stops when that owner is disposed.
|
|
17
|
+
* @example
|
|
18
|
+
* import { useReaction, useSignal } from "@shadow-js/core";
|
|
19
|
+
*
|
|
20
|
+
* function Results() {
|
|
21
|
+
* const [filters] = useSignal({ query: "" });
|
|
22
|
+
* const [stale, setStale] = useSignal(false);
|
|
23
|
+
* const track = useReaction(() => setStale(true));
|
|
24
|
+
* track(() => filters()); // the first change of filters marks the results stale
|
|
25
|
+
* return <p>{stale() ? "Refresh to update" : "Up to date"}</p>;
|
|
26
|
+
* }
|
|
27
|
+
* @see useEffect
|
|
28
|
+
* @since 1.0.0
|
|
29
|
+
*/
|
|
30
|
+
function useReaction(onInvalidate) {
|
|
31
|
+
const owner = currentOwner();
|
|
32
|
+
let scope = null;
|
|
33
|
+
return (track) => {
|
|
34
|
+
if (scope !== null) disposeTree(scope);
|
|
35
|
+
const next = createOwner(owner);
|
|
36
|
+
scope = next;
|
|
37
|
+
let tracking = true;
|
|
38
|
+
runInScope({
|
|
39
|
+
owner: next,
|
|
40
|
+
listener: null
|
|
41
|
+
}, () => {
|
|
42
|
+
createComputation({
|
|
43
|
+
kind: "effect",
|
|
44
|
+
render: true,
|
|
45
|
+
owner: next,
|
|
46
|
+
fn: () => {
|
|
47
|
+
if (tracking) {
|
|
48
|
+
tracking = false;
|
|
49
|
+
track();
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
untrack(onInvalidate);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
export { useReaction };
|
|
59
|
+
|
|
60
|
+
//# sourceMappingURL=reaction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reaction.js","names":[],"sources":["../../src/reaction.ts"],"sourcesContent":["import {\n createComputation,\n createOwner,\n currentOwner,\n disposeOwner,\n type Owner,\n runInScope,\n} from \"./extend\";\nimport { untrack } from \"./ownership\";\n\n/**\n * Creates a tracker that reports the first change of what it reads. `useReaction(onInvalidate)`\n * returns `track(fn)`: `fn` runs at once (synchronously) and the signals it reads are recorded.\n * The first change of any of them calls `onInvalidate` once, untracked, and then nothing is\n * observed until `track` is called again. The ShadowJS form of Solid's `createReaction`.\n *\n * Use it when the work that follows a change is not a pure rerun of `fn` (for example, re-fetching\n * after a change and then deciding what to track next). For ordinary effects use `useEffect`.\n *\n * @param onInvalidate Called once, untracked, after a change of a tracked read.\n * @returns `track(fn)`. Each call replaces the previous tracking. Tracking lives in a child of the\n * owner that called `useReaction`, so it stops when that owner is disposed.\n * @example\n * import { useReaction, useSignal } from \"@shadow-js/core\";\n *\n * function Results() {\n * const [filters] = useSignal({ query: \"\" });\n * const [stale, setStale] = useSignal(false);\n * const track = useReaction(() => setStale(true));\n * track(() => filters()); // the first change of filters marks the results stale\n * return <p>{stale() ? \"Refresh to update\" : \"Up to date\"}</p>;\n * }\n * @see useEffect\n * @since 1.0.0\n */\nexport function useReaction(onInvalidate: () => void): (track: () => void) => void {\n const owner = currentOwner();\n let scope: Owner | null = null;\n return (track: () => void): void => {\n if (scope !== null) disposeOwner(scope);\n const next = createOwner(owner);\n scope = next;\n let tracking = true;\n runInScope({ owner: next, listener: null }, () => {\n createComputation({\n kind: \"effect\",\n render: true,\n owner: next,\n fn: () => {\n if (tracking) {\n tracking = false;\n track();\n return undefined;\n }\n untrack(onInvalidate);\n return undefined;\n },\n });\n });\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA,SAAgB,YAAY,cAAuD;CACjF,MAAM,QAAQ,aAAa;CAC3B,IAAI,QAAsB;CAC1B,QAAQ,UAA4B;EAClC,IAAI,UAAU,MAAM,YAAa,KAAK;EACtC,MAAM,OAAO,YAAY,KAAK;EAC9B,QAAQ;EACR,IAAI,WAAW;EACf,WAAW;GAAE,OAAO;GAAM,UAAU;EAAK,SAAS;GAChD,kBAAkB;IAChB,MAAM;IACN,QAAQ;IACR,OAAO;IACP,UAAU;KACR,IAAI,UAAU;MACZ,WAAW;MACX,MAAM;MACN;KACF;KACA,QAAQ,YAAY;IAEtB;GACF,CAAC;EACH,CAAC;CACH;AACF"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Accessor, Equals } from "./internal/types.js";
|
|
2
|
+
/** Options of {@link useReducer}. @since 1.0.0 */
|
|
3
|
+
export interface ReducerOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Decides whether a new state is a change: readers are notified only when it returns `false`.
|
|
6
|
+
* @default Object.is
|
|
7
|
+
*/
|
|
8
|
+
equals?: Equals<unknown>;
|
|
9
|
+
/**
|
|
10
|
+
* Label shown by devtools and diagnostics.
|
|
11
|
+
* @default undefined (the state is unnamed)
|
|
12
|
+
*/
|
|
13
|
+
name?: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A state cell whose writes go through a reducer: `dispatch(action)` stores
|
|
17
|
+
* `reducer(state, action)`. Equivalent to `useSignal` with the transition written in one place,
|
|
18
|
+
* which keeps every state change of a complex value (a form, a queue) in one function. Reads are
|
|
19
|
+
* fresh immediately after `dispatch`, like every write.
|
|
20
|
+
*
|
|
21
|
+
* @param reducer Pure function `(state, action) => next`.
|
|
22
|
+
* @param initialState Starting state, stored as is.
|
|
23
|
+
* @param options Equality override and a devtools name. Undefined uses the defaults.
|
|
24
|
+
* @returns `[state, dispatch]`; `state()` tracks, `dispatch(action)` returns nothing. The reducer
|
|
25
|
+
* runs untracked, once per dispatch.
|
|
26
|
+
* @throws ShadowError `WRITE_IN_MEMO` from `dispatch`, in development builds only, when it is called
|
|
27
|
+
* while a {@link useMemo} computation runs. `dispatch` also rethrows what the reducer throws.
|
|
28
|
+
* @example
|
|
29
|
+
* import { useReducer } from "@shadow-js/core";
|
|
30
|
+
*
|
|
31
|
+
* type Action = "inc" | "dec";
|
|
32
|
+
* const [count, dispatch] = useReducer((n: number, a: Action) => (a === "inc" ? n + 1 : n - 1), 0);
|
|
33
|
+
* dispatch("inc");
|
|
34
|
+
* console.log(count()); // 1
|
|
35
|
+
* @see useSignal
|
|
36
|
+
* @since 1.0.0
|
|
37
|
+
*/
|
|
38
|
+
export declare function useReducer<S, A>(reducer: (state: S, action: A) => S, initialState: S, options?: ReducerOptions): [Accessor<S>, (action: A) => void];
|
|
39
|
+
//# sourceMappingURL=reducer.d.ts.map
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { useSignal } from "./signal.js";
|
|
2
|
+
// src/reducer.ts
|
|
3
|
+
/**
|
|
4
|
+
* A state cell whose writes go through a reducer: `dispatch(action)` stores
|
|
5
|
+
* `reducer(state, action)`. Equivalent to `useSignal` with the transition written in one place,
|
|
6
|
+
* which keeps every state change of a complex value (a form, a queue) in one function. Reads are
|
|
7
|
+
* fresh immediately after `dispatch`, like every write.
|
|
8
|
+
*
|
|
9
|
+
* @param reducer Pure function `(state, action) => next`.
|
|
10
|
+
* @param initialState Starting state, stored as is.
|
|
11
|
+
* @param options Equality override and a devtools name. Undefined uses the defaults.
|
|
12
|
+
* @returns `[state, dispatch]`; `state()` tracks, `dispatch(action)` returns nothing. The reducer
|
|
13
|
+
* runs untracked, once per dispatch.
|
|
14
|
+
* @throws ShadowError `WRITE_IN_MEMO` from `dispatch`, in development builds only, when it is called
|
|
15
|
+
* while a {@link useMemo} computation runs. `dispatch` also rethrows what the reducer throws.
|
|
16
|
+
* @example
|
|
17
|
+
* import { useReducer } from "@shadow-js/core";
|
|
18
|
+
*
|
|
19
|
+
* type Action = "inc" | "dec";
|
|
20
|
+
* const [count, dispatch] = useReducer((n: number, a: Action) => (a === "inc" ? n + 1 : n - 1), 0);
|
|
21
|
+
* dispatch("inc");
|
|
22
|
+
* console.log(count()); // 1
|
|
23
|
+
* @see useSignal
|
|
24
|
+
* @since 1.0.0
|
|
25
|
+
*/
|
|
26
|
+
function useReducer(reducer, initialState, options) {
|
|
27
|
+
const [state, setState] = useSignal(initialState, {
|
|
28
|
+
equals: options?.equals ?? Object.is,
|
|
29
|
+
...options?.name === void 0 ? {} : { name: options.name }
|
|
30
|
+
});
|
|
31
|
+
const dispatch = (action) => {
|
|
32
|
+
setState((previous) => reducer(previous, action));
|
|
33
|
+
};
|
|
34
|
+
return [state, dispatch];
|
|
35
|
+
}
|
|
36
|
+
export { useReducer };
|
|
37
|
+
|
|
38
|
+
//# sourceMappingURL=reducer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reducer.js","names":[],"sources":["../../src/reducer.ts"],"sourcesContent":["import type { Accessor, Equals } from \"./extend\";\nimport { useSignal } from \"./signal\";\n\n/** Options of {@link useReducer}. @since 1.0.0 */\nexport interface ReducerOptions {\n /**\n * Decides whether a new state is a change: readers are notified only when it returns `false`.\n * @default Object.is\n */\n equals?: Equals<unknown>;\n /**\n * Label shown by devtools and diagnostics.\n * @default undefined (the state is unnamed)\n */\n name?: string;\n}\n\n/**\n * A state cell whose writes go through a reducer: `dispatch(action)` stores\n * `reducer(state, action)`. Equivalent to `useSignal` with the transition written in one place,\n * which keeps every state change of a complex value (a form, a queue) in one function. Reads are\n * fresh immediately after `dispatch`, like every write.\n *\n * @param reducer Pure function `(state, action) => next`.\n * @param initialState Starting state, stored as is.\n * @param options Equality override and a devtools name. Undefined uses the defaults.\n * @returns `[state, dispatch]`; `state()` tracks, `dispatch(action)` returns nothing. The reducer\n * runs untracked, once per dispatch.\n * @throws ShadowError `WRITE_IN_MEMO` from `dispatch`, in development builds only, when it is called\n * while a {@link useMemo} computation runs. `dispatch` also rethrows what the reducer throws.\n * @example\n * import { useReducer } from \"@shadow-js/core\";\n *\n * type Action = \"inc\" | \"dec\";\n * const [count, dispatch] = useReducer((n: number, a: Action) => (a === \"inc\" ? n + 1 : n - 1), 0);\n * dispatch(\"inc\");\n * console.log(count()); // 1\n * @see useSignal\n * @since 1.0.0\n */\nexport function useReducer<S, A>(\n reducer: (state: S, action: A) => S,\n initialState: S,\n options?: ReducerOptions,\n): [Accessor<S>, (action: A) => void] {\n const [state, setState] = useSignal<S>(initialState, {\n equals: (options?.equals ?? Object.is) as Equals<S>,\n ...(options?.name === undefined ? {} : { name: options.name }),\n });\n const dispatch = (action: A): void => {\n setState((previous) => reducer(previous, action));\n };\n return [state, dispatch];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAwCA,SAAgB,WACd,SACA,cACA,SACoC;CACpC,MAAM,CAAC,OAAO,YAAY,UAAa,cAAc;EACnD,QAAS,SAAS,UAAU,OAAO;EACnC,GAAI,SAAS,SAAS,KAAA,IAAY,CAAC,IAAI,EAAE,MAAM,QAAQ,KAAK;CAC9D,CAAC;CACD,MAAM,YAAY,WAAoB;EACpC,UAAU,aAAa,QAAQ,UAAU,MAAM,CAAC;CAClD;CACA,OAAO,CAAC,OAAO,QAAQ;AACzB"}
|