@shadow-js/core 0.1.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/LICENSE +2 -2
- package/README.md +134 -331
- package/dist/dev/action.d.ts +36 -0
- package/dist/dev/action.js +90 -0
- package/dist/dev/action.js.map +1 -0
- package/dist/dev/cache.d.ts +35 -0
- package/dist/dev/cache.js +62 -0
- package/dist/dev/cache.js.map +1 -0
- package/dist/dev/context.d.ts +106 -0
- package/dist/dev/context.js +111 -0
- package/dist/dev/context.js.map +1 -0
- package/dist/dev/effect.d.ts +122 -0
- package/dist/dev/effect.js +111 -0
- package/dist/dev/effect.js.map +1 -0
- package/dist/dev/env.d.ts +29 -0
- package/dist/dev/env.js +31 -0
- package/dist/dev/env.js.map +1 -0
- package/dist/dev/events.d.ts +28 -0
- package/dist/dev/events.js +33 -0
- package/dist/dev/events.js.map +1 -0
- package/dist/dev/extend/index.d.ts +331 -0
- package/dist/dev/extend/index.js +317 -0
- package/dist/dev/extend/index.js.map +1 -0
- package/dist/dev/external-store.d.ts +60 -0
- package/dist/dev/external-store.js +60 -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 +213 -0
- package/dist/dev/internal/concurrency.js +632 -0
- package/dist/dev/internal/concurrency.js.map +1 -0
- package/dist/dev/internal/devtools.d.ts +71 -0
- package/dist/dev/internal/devtools.js +77 -0
- package/dist/dev/internal/devtools.js.map +1 -0
- package/dist/dev/internal/diagnostics.d.ts +90 -0
- package/dist/dev/internal/diagnostics.js +102 -0
- package/dist/dev/internal/diagnostics.js.map +1 -0
- package/dist/dev/internal/dispose.d.ts +28 -0
- package/dist/dev/internal/dispose.js +49 -0
- package/dist/dev/internal/dispose.js.map +1 -0
- package/dist/dev/internal/flags.js +11 -0
- package/dist/dev/internal/flags.js.map +1 -0
- package/dist/dev/internal/graph.d.ts +233 -0
- package/dist/dev/internal/graph.js +578 -0
- package/dist/dev/internal/graph.js.map +1 -0
- package/dist/dev/internal/hydration-flag.d.ts +33 -0
- package/dist/dev/internal/hydration-flag.js +46 -0
- package/dist/dev/internal/hydration-flag.js.map +1 -0
- package/dist/dev/internal/messages.d.ts +56 -0
- package/dist/dev/internal/messages.js +54 -0
- package/dist/dev/internal/messages.js.map +1 -0
- package/dist/dev/internal/owner.d.ts +142 -0
- package/dist/dev/internal/owner.js +290 -0
- package/dist/dev/internal/owner.js.map +1 -0
- package/dist/dev/internal/queue.js +46 -0
- package/dist/dev/internal/queue.js.map +1 -0
- package/dist/dev/internal/scheduler.d.ts +113 -0
- package/dist/dev/internal/scheduler.js +287 -0
- package/dist/dev/internal/scheduler.js.map +1 -0
- package/dist/dev/internal/state.d.ts +141 -0
- package/dist/dev/internal/state.js +28 -0
- package/dist/dev/internal/state.js.map +1 -0
- package/dist/dev/internal/suspension.d.ts +43 -0
- package/dist/dev/internal/suspension.js +45 -0
- package/dist/dev/internal/suspension.js.map +1 -0
- package/dist/dev/internal/types.d.ts +46 -0
- package/dist/dev/list.d.ts +93 -0
- package/dist/dev/list.js +282 -0
- package/dist/dev/list.js.map +1 -0
- package/dist/dev/memo.d.ts +60 -0
- package/dist/dev/memo.js +41 -0
- package/dist/dev/memo.js.map +1 -0
- package/dist/dev/optimistic.d.ts +26 -0
- package/dist/dev/optimistic.js +45 -0
- package/dist/dev/optimistic.js.map +1 -0
- package/dist/dev/ownership.d.ts +204 -0
- package/dist/dev/ownership.js +261 -0
- package/dist/dev/ownership.js.map +1 -0
- package/dist/dev/props.d.ts +134 -0
- package/dist/dev/props.js +282 -0
- package/dist/dev/props.js.map +1 -0
- package/dist/dev/reaction.d.ts +29 -0
- package/dist/dev/reaction.js +61 -0
- package/dist/dev/reaction.js.map +1 -0
- package/dist/dev/reducer.d.ts +41 -0
- package/dist/dev/reducer.js +39 -0
- package/dist/dev/reducer.js.map +1 -0
- package/dist/dev/refs.d.ts +52 -0
- package/dist/dev/refs.js +58 -0
- package/dist/dev/refs.js.map +1 -0
- package/dist/dev/resource.d.ts +135 -0
- package/dist/dev/resource.js +177 -0
- package/dist/dev/resource.js.map +1 -0
- package/dist/dev/selector.d.ts +34 -0
- package/dist/dev/selector.js +118 -0
- package/dist/dev/selector.js.map +1 -0
- package/dist/dev/signal.d.ts +42 -0
- package/dist/dev/signal.js +39 -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 +192 -0
- package/dist/dev/store/nodes.js.map +1 -0
- package/dist/dev/store/proxies.js +160 -0
- package/dist/dev/store/proxies.js.map +1 -0
- package/dist/dev/store/read.d.ts +39 -0
- package/dist/dev/store/read.js +72 -0
- package/dist/dev/store/read.js.map +1 -0
- package/dist/dev/store/reconcile.d.ts +73 -0
- package/dist/dev/store/reconcile.js +186 -0
- package/dist/dev/store/reconcile.js.map +1 -0
- package/dist/dev/store/types.d.ts +106 -0
- package/dist/dev/store/use-store.d.ts +60 -0
- package/dist/dev/store/use-store.js +168 -0
- package/dist/dev/store/use-store.js.map +1 -0
- package/dist/dev/suspense.d.ts +101 -0
- package/dist/dev/suspense.js +45 -0
- package/dist/dev/suspense.js.map +1 -0
- package/dist/dev/transitions.d.ts +77 -0
- package/dist/dev/transitions.js +101 -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 +90 -0
- package/dist/server-dev/action.js.map +1 -0
- package/dist/server-dev/cache.js +62 -0
- package/dist/server-dev/cache.js.map +1 -0
- package/dist/server-dev/context.js +108 -0
- package/dist/server-dev/context.js.map +1 -0
- package/dist/server-dev/effect.js +56 -0
- package/dist/server-dev/effect.js.map +1 -0
- package/dist/server-dev/env.js +31 -0
- package/dist/server-dev/env.js.map +1 -0
- package/dist/server-dev/events.js +33 -0
- package/dist/server-dev/events.js.map +1 -0
- package/dist/server-dev/extend/index.js +317 -0
- package/dist/server-dev/extend/index.js.map +1 -0
- package/dist/server-dev/external-store.js +46 -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 +633 -0
- package/dist/server-dev/internal/concurrency.js.map +1 -0
- package/dist/server-dev/internal/devtools.js +77 -0
- package/dist/server-dev/internal/devtools.js.map +1 -0
- package/dist/server-dev/internal/diagnostics.js +102 -0
- package/dist/server-dev/internal/diagnostics.js.map +1 -0
- package/dist/server-dev/internal/dispose.js +49 -0
- package/dist/server-dev/internal/dispose.js.map +1 -0
- package/dist/server-dev/internal/flags.js +11 -0
- package/dist/server-dev/internal/flags.js.map +1 -0
- package/dist/server-dev/internal/graph.js +578 -0
- package/dist/server-dev/internal/graph.js.map +1 -0
- package/dist/server-dev/internal/hydration-flag.js +46 -0
- package/dist/server-dev/internal/hydration-flag.js.map +1 -0
- package/dist/server-dev/internal/messages.js +54 -0
- package/dist/server-dev/internal/messages.js.map +1 -0
- package/dist/server-dev/internal/owner.js +290 -0
- package/dist/server-dev/internal/owner.js.map +1 -0
- package/dist/server-dev/internal/queue.js +46 -0
- package/dist/server-dev/internal/queue.js.map +1 -0
- package/dist/server-dev/internal/scheduler.js +287 -0
- package/dist/server-dev/internal/scheduler.js.map +1 -0
- package/dist/server-dev/internal/state.js +28 -0
- package/dist/server-dev/internal/state.js.map +1 -0
- package/dist/server-dev/internal/suspension.js +45 -0
- package/dist/server-dev/internal/suspension.js.map +1 -0
- package/dist/server-dev/list.js +282 -0
- package/dist/server-dev/list.js.map +1 -0
- package/dist/server-dev/memo.js +41 -0
- package/dist/server-dev/memo.js.map +1 -0
- package/dist/server-dev/optimistic.js +45 -0
- package/dist/server-dev/optimistic.js.map +1 -0
- package/dist/server-dev/ownership.js +261 -0
- package/dist/server-dev/ownership.js.map +1 -0
- package/dist/server-dev/props.js +282 -0
- package/dist/server-dev/props.js.map +1 -0
- package/dist/server-dev/reaction.js +61 -0
- package/dist/server-dev/reaction.js.map +1 -0
- package/dist/server-dev/reducer.js +39 -0
- package/dist/server-dev/reducer.js.map +1 -0
- package/dist/server-dev/refs.js +58 -0
- package/dist/server-dev/refs.js.map +1 -0
- package/dist/server-dev/resource.js +176 -0
- package/dist/server-dev/resource.js.map +1 -0
- package/dist/server-dev/selector.js +82 -0
- package/dist/server-dev/selector.js.map +1 -0
- package/dist/server-dev/signal.js +39 -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 +192 -0
- package/dist/server-dev/store/nodes.js.map +1 -0
- package/dist/server-dev/store/proxies.js +160 -0
- package/dist/server-dev/store/proxies.js.map +1 -0
- package/dist/server-dev/store/read.js +72 -0
- package/dist/server-dev/store/read.js.map +1 -0
- package/dist/server-dev/store/reconcile.js +186 -0
- package/dist/server-dev/store/reconcile.js.map +1 -0
- package/dist/server-dev/store/use-store.js +168 -0
- package/dist/server-dev/store/use-store.js.map +1 -0
- package/dist/server-dev/suspense.js +45 -0
- package/dist/server-dev/suspense.js.map +1 -0
- package/dist/server-dev/transitions.js +86 -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,60 @@
|
|
|
1
|
+
import { Accessor, Equals } from "./internal/types.js";
|
|
2
|
+
//#region src/memo.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* Options of {@link useMemo}.
|
|
5
|
+
* @since 1.0.0
|
|
6
|
+
*/
|
|
7
|
+
export interface MemoOptions<T> {
|
|
8
|
+
/**
|
|
9
|
+
* Decides whether a recomputation changed the value: readers are notified only when it returns
|
|
10
|
+
* `false`. `false` always propagates.
|
|
11
|
+
* @default Object.is
|
|
12
|
+
*/
|
|
13
|
+
equals?: Equals<T>;
|
|
14
|
+
/**
|
|
15
|
+
* Skip the initial computation until the first read. Without it the memo computes once when it
|
|
16
|
+
* is created.
|
|
17
|
+
* @default false
|
|
18
|
+
*/
|
|
19
|
+
lazy?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Label shown by devtools and diagnostics.
|
|
22
|
+
* @default undefined (the memo is unnamed)
|
|
23
|
+
*/
|
|
24
|
+
name?: string;
|
|
25
|
+
/**
|
|
26
|
+
* Turn off the development check that rejects signal writes while this memo computes. Set it for
|
|
27
|
+
* a memo that builds children (components, `ref` callbacks, directives), where the code that runs
|
|
28
|
+
* is setup and may write a counter or a registry; the control-flow components do. A memo that
|
|
29
|
+
* derives a value must leave it off.
|
|
30
|
+
* @default false
|
|
31
|
+
*/
|
|
32
|
+
allowWrites?: boolean;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Creates a cached derivation that recomputes only when a dependency actually changed.
|
|
36
|
+
* Dependencies are discovered on every run, so branches that are not taken are not tracked.
|
|
37
|
+
* A memo that throws inside an error boundary (an ancestor `onError` or `catchError`) reports the
|
|
38
|
+
* error there and keeps its previous value; without a boundary it stores the error and rethrows it
|
|
39
|
+
* to every reader.
|
|
40
|
+
*
|
|
41
|
+
* @param fn Computation; receives the previous value (`undefined` on the first run).
|
|
42
|
+
* @param options Equality override, `lazy`, `allowWrites` and a devtools name. Undefined uses the
|
|
43
|
+
* defaults.
|
|
44
|
+
* @returns An accessor returning the current, always fresh value.
|
|
45
|
+
* @throws ShadowError `CYCLE_DETECTED` from the accessor when the memo reads itself while it
|
|
46
|
+
* computes. Outside an error boundary the accessor also rethrows the error `fn` threw on its last
|
|
47
|
+
* run.
|
|
48
|
+
* @example
|
|
49
|
+
* import { useMemo, useSignal } from "@shadow-js/core";
|
|
50
|
+
*
|
|
51
|
+
* const [count, setCount] = useSignal(2);
|
|
52
|
+
* const doubled = useMemo(() => count() * 2);
|
|
53
|
+
* setCount(5);
|
|
54
|
+
* console.log(doubled()); // 10
|
|
55
|
+
* @see useSignal
|
|
56
|
+
* @since 1.0.0
|
|
57
|
+
*/
|
|
58
|
+
export declare function useMemo<T>(fn: (previous: T | undefined) => T, options?: MemoOptions<T>): Accessor<T>;
|
|
59
|
+
//#endregion
|
|
60
|
+
//# sourceMappingURL=memo.d.ts.map
|
package/dist/dev/memo.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { createComputation, readComputation } from "./extend/index.js";
|
|
2
|
+
//#region src/memo.ts
|
|
3
|
+
/**
|
|
4
|
+
* Creates a cached derivation that recomputes only when a dependency actually changed.
|
|
5
|
+
* Dependencies are discovered on every run, so branches that are not taken are not tracked.
|
|
6
|
+
* A memo that throws inside an error boundary (an ancestor `onError` or `catchError`) reports the
|
|
7
|
+
* error there and keeps its previous value; without a boundary it stores the error and rethrows it
|
|
8
|
+
* to every reader.
|
|
9
|
+
*
|
|
10
|
+
* @param fn Computation; receives the previous value (`undefined` on the first run).
|
|
11
|
+
* @param options Equality override, `lazy`, `allowWrites` and a devtools name. Undefined uses the
|
|
12
|
+
* defaults.
|
|
13
|
+
* @returns An accessor returning the current, always fresh value.
|
|
14
|
+
* @throws ShadowError `CYCLE_DETECTED` from the accessor when the memo reads itself while it
|
|
15
|
+
* computes. Outside an error boundary the accessor also rethrows the error `fn` threw on its last
|
|
16
|
+
* run.
|
|
17
|
+
* @example
|
|
18
|
+
* import { useMemo, useSignal } from "@shadow-js/core";
|
|
19
|
+
*
|
|
20
|
+
* const [count, setCount] = useSignal(2);
|
|
21
|
+
* const doubled = useMemo(() => count() * 2);
|
|
22
|
+
* setCount(5);
|
|
23
|
+
* console.log(doubled()); // 10
|
|
24
|
+
* @see useSignal
|
|
25
|
+
* @since 1.0.0
|
|
26
|
+
*/
|
|
27
|
+
function useMemo(fn, options) {
|
|
28
|
+
const node = createComputation({
|
|
29
|
+
kind: "memo",
|
|
30
|
+
fn,
|
|
31
|
+
equals: options?.equals === void 0 ? Object.is : options.equals,
|
|
32
|
+
lazy: options?.lazy === true,
|
|
33
|
+
allowWrites: options?.allowWrites === true,
|
|
34
|
+
...options?.name === void 0 ? {} : { name: options.name }
|
|
35
|
+
});
|
|
36
|
+
return () => readComputation(node);
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
39
|
+
export { useMemo };
|
|
40
|
+
|
|
41
|
+
//# sourceMappingURL=memo.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memo.js","names":[],"sources":["../../src/memo.ts"],"sourcesContent":["import { type Accessor, createComputation, type Equals, readComputation } from \"./extend\";\n\n/**\n * Options of {@link useMemo}.\n * @since 1.0.0\n */\nexport interface MemoOptions<T> {\n /**\n * Decides whether a recomputation changed the value: readers are notified only when it returns\n * `false`. `false` always propagates.\n * @default Object.is\n */\n equals?: Equals<T>;\n /**\n * Skip the initial computation until the first read. Without it the memo computes once when it\n * is created.\n * @default false\n */\n lazy?: boolean;\n /**\n * Label shown by devtools and diagnostics.\n * @default undefined (the memo is unnamed)\n */\n name?: string;\n /**\n * Turn off the development check that rejects signal writes while this memo computes. Set it for\n * a memo that builds children (components, `ref` callbacks, directives), where the code that runs\n * is setup and may write a counter or a registry; the control-flow components do. A memo that\n * derives a value must leave it off.\n * @default false\n */\n allowWrites?: boolean;\n}\n\n/**\n * Creates a cached derivation that recomputes only when a dependency actually changed.\n * Dependencies are discovered on every run, so branches that are not taken are not tracked.\n * A memo that throws inside an error boundary (an ancestor `onError` or `catchError`) reports the\n * error there and keeps its previous value; without a boundary it stores the error and rethrows it\n * to every reader.\n *\n * @param fn Computation; receives the previous value (`undefined` on the first run).\n * @param options Equality override, `lazy`, `allowWrites` and a devtools name. Undefined uses the\n * defaults.\n * @returns An accessor returning the current, always fresh value.\n * @throws ShadowError `CYCLE_DETECTED` from the accessor when the memo reads itself while it\n * computes. Outside an error boundary the accessor also rethrows the error `fn` threw on its last\n * run.\n * @example\n * import { useMemo, useSignal } from \"@shadow-js/core\";\n *\n * const [count, setCount] = useSignal(2);\n * const doubled = useMemo(() => count() * 2);\n * setCount(5);\n * console.log(doubled()); // 10\n * @see useSignal\n * @since 1.0.0\n */\nexport function useMemo<T>(\n fn: (previous: T | undefined) => T,\n options?: MemoOptions<T>,\n): Accessor<T> {\n const node = createComputation({\n kind: \"memo\",\n fn: fn as (previous: never) => unknown,\n equals: (options?.equals === undefined ? Object.is : options.equals) as Equals<unknown>,\n lazy: options?.lazy === true,\n allowWrites: options?.allowWrites === true,\n ...(options?.name === undefined ? {} : { name: options.name }),\n });\n return () => readComputation<T>(node);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AA0DA,SAAgB,QACd,IACA,SACa;CACb,MAAM,OAAO,kBAAkB;EAC7B,MAAM;EACF;EACJ,QAAS,SAAS,WAAW,KAAA,IAAY,OAAO,KAAK,QAAQ;EAC7D,MAAM,SAAS,SAAS;EACxB,aAAa,SAAS,gBAAgB;EACtC,GAAI,SAAS,SAAS,KAAA,IAAY,CAAC,IAAI,EAAE,MAAM,QAAQ,KAAK;CAC9D,CAAC;CACD,aAAa,gBAAmB,IAAI;AACtC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Accessor } from "./internal/types.js";
|
|
2
|
+
//#region src/optimistic.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* Optimistic state: shows `reducer` applied over the real value for every action whose
|
|
5
|
+
* `settled` promise is still pending. When the promise settles (either way) the action is
|
|
6
|
+
* dropped and the view falls back to the real source, which is where the confirmed result lives.
|
|
7
|
+
*
|
|
8
|
+
* @param source The authoritative value, tracked.
|
|
9
|
+
* @param reducer Pure function combining the current state with one pending action. It runs for
|
|
10
|
+
* every pending action, in the order they were applied, each time the state recomputes.
|
|
11
|
+
* @returns `[state, apply]`. `state` is a memo; `apply(action, settled)` adds the action at once
|
|
12
|
+
* and drops it when `settled` resolves or rejects. Pass the mutation's promise as `settled`.
|
|
13
|
+
* @example
|
|
14
|
+
* import { useOptimistic, useSignal } from "@shadow-js/core";
|
|
15
|
+
*
|
|
16
|
+
* const [todos, setTodos] = useSignal<string[]>([]);
|
|
17
|
+
* const [shown, addTodo] = useOptimistic(todos, (list, text: string) => [...list, text]);
|
|
18
|
+
* const save = (text: string) =>
|
|
19
|
+
* fetch("/api/todos", { method: "POST", body: text }).then(() => setTodos((l) => [...l, text]));
|
|
20
|
+
* addTodo("milk", save("milk")); // shown() includes "milk" at once
|
|
21
|
+
* @see useAction
|
|
22
|
+
* @since 1.0.0
|
|
23
|
+
*/
|
|
24
|
+
export declare function useOptimistic<T, A>(source: Accessor<T>, reducer: (state: T, action: A) => T): [Accessor<T>, (action: A, settled: Promise<unknown>) => void];
|
|
25
|
+
//#endregion
|
|
26
|
+
//# sourceMappingURL=optimistic.d.ts.map
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { useSignal } from "./signal.js";
|
|
2
|
+
import { useMemo } from "./memo.js";
|
|
3
|
+
//#region src/optimistic.ts
|
|
4
|
+
/**
|
|
5
|
+
* Optimistic state: shows `reducer` applied over the real value for every action whose
|
|
6
|
+
* `settled` promise is still pending. When the promise settles (either way) the action is
|
|
7
|
+
* dropped and the view falls back to the real source, which is where the confirmed result lives.
|
|
8
|
+
*
|
|
9
|
+
* @param source The authoritative value, tracked.
|
|
10
|
+
* @param reducer Pure function combining the current state with one pending action. It runs for
|
|
11
|
+
* every pending action, in the order they were applied, each time the state recomputes.
|
|
12
|
+
* @returns `[state, apply]`. `state` is a memo; `apply(action, settled)` adds the action at once
|
|
13
|
+
* and drops it when `settled` resolves or rejects. Pass the mutation's promise as `settled`.
|
|
14
|
+
* @example
|
|
15
|
+
* import { useOptimistic, useSignal } from "@shadow-js/core";
|
|
16
|
+
*
|
|
17
|
+
* const [todos, setTodos] = useSignal<string[]>([]);
|
|
18
|
+
* const [shown, addTodo] = useOptimistic(todos, (list, text: string) => [...list, text]);
|
|
19
|
+
* const save = (text: string) =>
|
|
20
|
+
* fetch("/api/todos", { method: "POST", body: text }).then(() => setTodos((l) => [...l, text]));
|
|
21
|
+
* addTodo("milk", save("milk")); // shown() includes "milk" at once
|
|
22
|
+
* @see useAction
|
|
23
|
+
* @since 1.0.0
|
|
24
|
+
*/
|
|
25
|
+
function useOptimistic(source, reducer) {
|
|
26
|
+
const [pending, setPending] = useSignal([]);
|
|
27
|
+
const state = useMemo(() => {
|
|
28
|
+
let current = source();
|
|
29
|
+
for (const entry of pending()) current = reducer(current, entry.action);
|
|
30
|
+
return current;
|
|
31
|
+
});
|
|
32
|
+
const apply = (action, settled) => {
|
|
33
|
+
const entry = { action };
|
|
34
|
+
setPending((list) => [...list, entry]);
|
|
35
|
+
const drop = () => {
|
|
36
|
+
setPending((list) => list.filter((item) => item !== entry));
|
|
37
|
+
};
|
|
38
|
+
settled.then(drop, drop);
|
|
39
|
+
};
|
|
40
|
+
return [state, apply];
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
export { useOptimistic };
|
|
44
|
+
|
|
45
|
+
//# sourceMappingURL=optimistic.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"optimistic.js","names":[],"sources":["../../src/optimistic.ts"],"sourcesContent":["import type { Accessor } from \"./extend\";\nimport { useMemo } from \"./memo\";\nimport { useSignal } from \"./signal\";\n\n/**\n * Optimistic state: shows `reducer` applied over the real value for every action whose\n * `settled` promise is still pending. When the promise settles (either way) the action is\n * dropped and the view falls back to the real source, which is where the confirmed result lives.\n *\n * @param source The authoritative value, tracked.\n * @param reducer Pure function combining the current state with one pending action. It runs for\n * every pending action, in the order they were applied, each time the state recomputes.\n * @returns `[state, apply]`. `state` is a memo; `apply(action, settled)` adds the action at once\n * and drops it when `settled` resolves or rejects. Pass the mutation's promise as `settled`.\n * @example\n * import { useOptimistic, useSignal } from \"@shadow-js/core\";\n *\n * const [todos, setTodos] = useSignal<string[]>([]);\n * const [shown, addTodo] = useOptimistic(todos, (list, text: string) => [...list, text]);\n * const save = (text: string) =>\n * fetch(\"/api/todos\", { method: \"POST\", body: text }).then(() => setTodos((l) => [...l, text]));\n * addTodo(\"milk\", save(\"milk\")); // shown() includes \"milk\" at once\n * @see useAction\n * @since 1.0.0\n */\nexport function useOptimistic<T, A>(\n source: Accessor<T>,\n reducer: (state: T, action: A) => T,\n): [Accessor<T>, (action: A, settled: Promise<unknown>) => void] {\n const [pending, setPending] = useSignal<ReadonlyArray<{ action: A }>>([]);\n const state = useMemo(() => {\n let current = source();\n for (const entry of pending()) current = reducer(current, entry.action);\n return current;\n });\n const apply = (action: A, settled: Promise<unknown>): void => {\n const entry = { action };\n setPending((list) => [...list, entry]);\n const drop = (): void => {\n setPending((list) => list.filter((item) => item !== entry));\n };\n settled.then(drop, drop);\n };\n return [state, apply];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAyBA,SAAgB,cACd,QACA,SAC+D;CAC/D,MAAM,CAAC,SAAS,cAAc,UAAwC,CAAC,CAAC;CACxE,MAAM,QAAQ,cAAc;EAC1B,IAAI,UAAU,OAAO;EACrB,KAAK,MAAM,SAAS,QAAQ,GAAG,UAAU,QAAQ,SAAS,MAAM,MAAM;EACtE,OAAO;CACT,CAAC;CACD,MAAM,SAAS,QAAW,YAAoC;EAC5D,MAAM,QAAQ,EAAE,OAAO;EACvB,YAAY,SAAS,CAAC,GAAG,MAAM,KAAK,CAAC;EACrC,MAAM,aAAmB;GACvB,YAAY,SAAS,KAAK,QAAQ,SAAS,SAAS,KAAK,CAAC;EAC5D;EACA,QAAQ,KAAK,MAAM,IAAI;CACzB;CACA,OAAO,CAAC,OAAO,KAAK;AACtB"}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { Cleanup, ErrorHandler } from "./internal/types.js";
|
|
2
|
+
import { Owner } from "./internal/owner.js";
|
|
3
|
+
//#region src/ownership.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Runs `fn` without subscribing the running computation to anything `fn` reads. Use it inside an
|
|
6
|
+
* effect or a memo to read a value without re-running when that value changes. The current owner
|
|
7
|
+
* is kept, so effects and cleanups created inside `fn` are still owned.
|
|
8
|
+
*
|
|
9
|
+
* @param fn The function to run untracked.
|
|
10
|
+
* @returns Whatever `fn` returns. An error thrown by `fn` propagates unchanged.
|
|
11
|
+
* @example
|
|
12
|
+
* import { untrack, useEffect, useSignal } from "@shadow-js/core";
|
|
13
|
+
*
|
|
14
|
+
* const [count, setCount] = useSignal(0);
|
|
15
|
+
* const [unit] = useSignal("clicks");
|
|
16
|
+
* useEffect(() => {
|
|
17
|
+
* // Re-runs when `count` changes, never when `unit` changes.
|
|
18
|
+
* console.log(`${count()} ${untrack(unit)}`);
|
|
19
|
+
* });
|
|
20
|
+
* setCount(1);
|
|
21
|
+
* @see useEvent
|
|
22
|
+
* @since 1.0.0
|
|
23
|
+
*/
|
|
24
|
+
export declare function untrack<T>(fn: () => T): T;
|
|
25
|
+
/**
|
|
26
|
+
* Options of {@link createRoot}.
|
|
27
|
+
* @since 1.0.0
|
|
28
|
+
*/
|
|
29
|
+
export interface RootOptions {
|
|
30
|
+
/**
|
|
31
|
+
* When true, the root is not attached to the current owner: it outlives that owner and is
|
|
32
|
+
* disposed only through the `dispose` function passed to the body. It still sees the context
|
|
33
|
+
* values provided above the current owner.
|
|
34
|
+
* @default false (the root is a child of the current owner and is disposed with it)
|
|
35
|
+
*/
|
|
36
|
+
detached?: boolean;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Creates an ownership scope. Everything created inside (effects, memos, cleanups, children) is
|
|
40
|
+
* disposed together when `dispose` is called or, for attached roots, when the parent disposes. Use
|
|
41
|
+
* it for code that runs outside a component, such as an app entry point, a test or a module that
|
|
42
|
+
* owns long-lived effects.
|
|
43
|
+
*
|
|
44
|
+
* @param fn Body; receives the `dispose` function. Runs untracked, with the new root as owner.
|
|
45
|
+
* @param options `detached` keeps the root out of the current owner. Undefined uses the default
|
|
46
|
+
* (attached).
|
|
47
|
+
* @returns Whatever `fn` returns.
|
|
48
|
+
* @throws Rethrows any error thrown by `fn`, after the root and everything created in it are
|
|
49
|
+
* disposed.
|
|
50
|
+
* @example
|
|
51
|
+
* import { createRoot, useEffect, useSignal } from "@shadow-js/core";
|
|
52
|
+
*
|
|
53
|
+
* const [count, setCount] = useSignal(0);
|
|
54
|
+
* const dispose = createRoot((dispose) => {
|
|
55
|
+
* useEffect(() => console.log(count()));
|
|
56
|
+
* return dispose;
|
|
57
|
+
* });
|
|
58
|
+
* setCount(1);
|
|
59
|
+
* dispose(); // the effect stops and its cleanups run
|
|
60
|
+
* @see getOwner
|
|
61
|
+
* @since 1.0.0
|
|
62
|
+
*/
|
|
63
|
+
export declare function createRoot<T>(fn: (dispose: () => void) => T, options?: RootOptions): T;
|
|
64
|
+
/**
|
|
65
|
+
* Returns the owner that currently adopts new effects, memos and cleanups. Capture it while a
|
|
66
|
+
* component or root runs, then hand it to {@link runWithOwner} from code that runs later.
|
|
67
|
+
*
|
|
68
|
+
* @returns The current owner, or `null` outside any owner (at module scope, or in a callback that
|
|
69
|
+
* runs after an `await` or a timer).
|
|
70
|
+
* @example
|
|
71
|
+
* import { getOwner, onCleanup, runWithOwner } from "@shadow-js/core";
|
|
72
|
+
*
|
|
73
|
+
* function Poller() {
|
|
74
|
+
* const owner = getOwner();
|
|
75
|
+
* setTimeout(() => {
|
|
76
|
+
* runWithOwner(owner, () => onCleanup(() => console.log("poller disposed")));
|
|
77
|
+
* }, 1000);
|
|
78
|
+
* return null;
|
|
79
|
+
* }
|
|
80
|
+
* @see runWithOwner
|
|
81
|
+
* @since 1.0.0
|
|
82
|
+
*/
|
|
83
|
+
export declare function getOwner(): Owner | null;
|
|
84
|
+
/**
|
|
85
|
+
* Runs `fn` with `owner` as the current owner, untracked. Use it to restore ownership after an
|
|
86
|
+
* `await` or inside callbacks that outlive the synchronous run, so that effects, cleanups and
|
|
87
|
+
* context reads inside `fn` attach to that owner. A disposed owner is ignored: `fn` then runs
|
|
88
|
+
* without an owner, and development builds warn `RUN_WITH_DISPOSED_OWNER`.
|
|
89
|
+
*
|
|
90
|
+
* @param owner The owner to make current, usually captured with {@link getOwner}. `null` runs `fn`
|
|
91
|
+
* without an owner.
|
|
92
|
+
* @param fn The function to run.
|
|
93
|
+
* @returns Whatever `fn` returns. An error thrown by `fn` propagates unchanged.
|
|
94
|
+
* @example
|
|
95
|
+
* import { createContext, getOwner, runWithOwner, useContext } from "@shadow-js/core";
|
|
96
|
+
*
|
|
97
|
+
* const Locale = createContext("en");
|
|
98
|
+
*
|
|
99
|
+
* function Clock() {
|
|
100
|
+
* const owner = getOwner();
|
|
101
|
+
* queueMicrotask(() => {
|
|
102
|
+
* const locale = runWithOwner(owner, () => useContext(Locale));
|
|
103
|
+
* console.log(new Date().toLocaleTimeString(locale));
|
|
104
|
+
* });
|
|
105
|
+
* return null;
|
|
106
|
+
* }
|
|
107
|
+
* @see getOwner
|
|
108
|
+
* @since 1.0.0
|
|
109
|
+
*/
|
|
110
|
+
export declare function runWithOwner<T>(owner: Owner | null, fn: () => T): T;
|
|
111
|
+
/**
|
|
112
|
+
* Registers `fn` to run when the current owner is disposed or, for an effect or memo, before it
|
|
113
|
+
* re-runs. Cleanups run newest first and untracked; one that throws does not stop the others, and
|
|
114
|
+
* its error goes to the nearest error handler ({@link onError}, {@link catchError}). Outside any owner the call does nothing, because
|
|
115
|
+
* nothing would ever run the cleanup; development builds warn `NO_OWNER_CLEANUP`.
|
|
116
|
+
*
|
|
117
|
+
* @param fn The cleanup to register.
|
|
118
|
+
* @example
|
|
119
|
+
* import { onCleanup } from "@shadow-js/core";
|
|
120
|
+
*
|
|
121
|
+
* function Ticker() {
|
|
122
|
+
* const timer = setInterval(() => console.log("tick"), 1000);
|
|
123
|
+
* onCleanup(() => clearInterval(timer));
|
|
124
|
+
* return null;
|
|
125
|
+
* }
|
|
126
|
+
* @see onMount
|
|
127
|
+
* @since 1.0.0
|
|
128
|
+
*/
|
|
129
|
+
export declare function onCleanup(fn: Cleanup): void;
|
|
130
|
+
/**
|
|
131
|
+
* Registers an error handler on the current owner. It receives errors thrown in this owner's scope
|
|
132
|
+
* (its effects, memos, cleanups and child owners) that no closer handler took. Handlers on the same
|
|
133
|
+
* owner are tried newest first; a handler that throws passes its own error to the next handler up.
|
|
134
|
+
* Outside any owner the call does nothing; development builds warn `NO_OWNER_ERROR`.
|
|
135
|
+
*
|
|
136
|
+
* @param handler Receives the error, untracked. Throw from it to pass an error on to the next handler.
|
|
137
|
+
* @example
|
|
138
|
+
* import { onError, useEffect } from "@shadow-js/core";
|
|
139
|
+
*
|
|
140
|
+
* function Dashboard() {
|
|
141
|
+
* onError((error) => console.error("dashboard failed:", error));
|
|
142
|
+
* useEffect(() => {
|
|
143
|
+
* throw new Error("widget crashed"); // reaches the handler above
|
|
144
|
+
* });
|
|
145
|
+
* return null;
|
|
146
|
+
* }
|
|
147
|
+
* @see catchError
|
|
148
|
+
* @since 1.0.0
|
|
149
|
+
*/
|
|
150
|
+
export declare function onError(handler: ErrorHandler): void;
|
|
151
|
+
/**
|
|
152
|
+
* Runs `fn` inside a new child scope whose errors go to `handler`: errors thrown synchronously by
|
|
153
|
+
* `fn`, and later errors of the effects, memos and cleanups it created. The scope is disposed with
|
|
154
|
+
* the current owner. Use it to contain the failures of one part of a component tree.
|
|
155
|
+
*
|
|
156
|
+
* @param fn The function to run; it runs untracked, with the new scope as owner.
|
|
157
|
+
* @param handler Receives each error, untracked. Throw from it to pass the error on to the next handler up.
|
|
158
|
+
* @returns Whatever `fn` returns, or `undefined` when `fn` threw and `handler` took the error.
|
|
159
|
+
* @throws Rethrows the error `handler` throws when no handler above takes it (outside a flush).
|
|
160
|
+
* @example
|
|
161
|
+
* import { catchError, useEffect } from "@shadow-js/core";
|
|
162
|
+
*
|
|
163
|
+
* function Widget() {
|
|
164
|
+
* return catchError(
|
|
165
|
+
* () => {
|
|
166
|
+
* useEffect(() => {
|
|
167
|
+
* throw new Error("network down");
|
|
168
|
+
* });
|
|
169
|
+
* return "ready";
|
|
170
|
+
* },
|
|
171
|
+
* (error) => console.error("widget failed:", error),
|
|
172
|
+
* );
|
|
173
|
+
* }
|
|
174
|
+
* @see onError
|
|
175
|
+
* @since 1.0.0
|
|
176
|
+
*/
|
|
177
|
+
export declare function catchError<T>(fn: () => T, handler: ErrorHandler): T | undefined;
|
|
178
|
+
/**
|
|
179
|
+
* Returns an id that is identical on server and client for the same position in the owner tree.
|
|
180
|
+
* The id uses only letters, digits, `-` and `_`, so it is valid in `id`, `for`, `aria-*` references
|
|
181
|
+
* and in `querySelector("#" + id)` without escaping. Use it to link a label to its input, or an
|
|
182
|
+
* element to its `aria-describedby` target, in markup that is rendered on the server and hydrated.
|
|
183
|
+
*
|
|
184
|
+
* @returns A new id, unique within the root; each call on the same owner returns the next one.
|
|
185
|
+
* @throws ShadowError `NO_OWNER_ID` when called outside any owner, because no stable id exists
|
|
186
|
+
* there.
|
|
187
|
+
* @example
|
|
188
|
+
* import { useId } from "@shadow-js/core";
|
|
189
|
+
*
|
|
190
|
+
* function EmailField() {
|
|
191
|
+
* const id = useId();
|
|
192
|
+
* return (
|
|
193
|
+
* <>
|
|
194
|
+
* <label for={id}>Email</label>
|
|
195
|
+
* <input id={id} type="email" />
|
|
196
|
+
* </>
|
|
197
|
+
* );
|
|
198
|
+
* }
|
|
199
|
+
* @since 1.0.0
|
|
200
|
+
*/
|
|
201
|
+
export declare function useId(): string;
|
|
202
|
+
//#endregion
|
|
203
|
+
export type { Owner };
|
|
204
|
+
//# sourceMappingURL=ownership.d.ts.map
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import { ShadowError, warn } from "./internal/diagnostics.js";
|
|
2
|
+
import { Owner, addCleanup, handleError } from "./internal/owner.js";
|
|
3
|
+
import { disposeTree } from "./internal/dispose.js";
|
|
4
|
+
import { currentOwner, runInScope } from "./extend/index.js";
|
|
5
|
+
//#region src/ownership.ts
|
|
6
|
+
/**
|
|
7
|
+
* Runs `fn` without subscribing the running computation to anything `fn` reads. Use it inside an
|
|
8
|
+
* effect or a memo to read a value without re-running when that value changes. The current owner
|
|
9
|
+
* is kept, so effects and cleanups created inside `fn` are still owned.
|
|
10
|
+
*
|
|
11
|
+
* @param fn The function to run untracked.
|
|
12
|
+
* @returns Whatever `fn` returns. An error thrown by `fn` propagates unchanged.
|
|
13
|
+
* @example
|
|
14
|
+
* import { untrack, useEffect, useSignal } from "@shadow-js/core";
|
|
15
|
+
*
|
|
16
|
+
* const [count, setCount] = useSignal(0);
|
|
17
|
+
* const [unit] = useSignal("clicks");
|
|
18
|
+
* useEffect(() => {
|
|
19
|
+
* // Re-runs when `count` changes, never when `unit` changes.
|
|
20
|
+
* console.log(`${count()} ${untrack(unit)}`);
|
|
21
|
+
* });
|
|
22
|
+
* setCount(1);
|
|
23
|
+
* @see useEvent
|
|
24
|
+
* @since 1.0.0
|
|
25
|
+
*/
|
|
26
|
+
function untrack(fn) {
|
|
27
|
+
return runInScope({ listener: null }, fn);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Creates an ownership scope. Everything created inside (effects, memos, cleanups, children) is
|
|
31
|
+
* disposed together when `dispose` is called or, for attached roots, when the parent disposes. Use
|
|
32
|
+
* it for code that runs outside a component, such as an app entry point, a test or a module that
|
|
33
|
+
* owns long-lived effects.
|
|
34
|
+
*
|
|
35
|
+
* @param fn Body; receives the `dispose` function. Runs untracked, with the new root as owner.
|
|
36
|
+
* @param options `detached` keeps the root out of the current owner. Undefined uses the default
|
|
37
|
+
* (attached).
|
|
38
|
+
* @returns Whatever `fn` returns.
|
|
39
|
+
* @throws Rethrows any error thrown by `fn`, after the root and everything created in it are
|
|
40
|
+
* disposed.
|
|
41
|
+
* @example
|
|
42
|
+
* import { createRoot, useEffect, useSignal } from "@shadow-js/core";
|
|
43
|
+
*
|
|
44
|
+
* const [count, setCount] = useSignal(0);
|
|
45
|
+
* const dispose = createRoot((dispose) => {
|
|
46
|
+
* useEffect(() => console.log(count()));
|
|
47
|
+
* return dispose;
|
|
48
|
+
* });
|
|
49
|
+
* setCount(1);
|
|
50
|
+
* dispose(); // the effect stops and its cleanups run
|
|
51
|
+
* @see getOwner
|
|
52
|
+
* @since 1.0.0
|
|
53
|
+
*/
|
|
54
|
+
function createRoot(fn, options) {
|
|
55
|
+
const detached = options?.detached === true;
|
|
56
|
+
const current = currentOwner();
|
|
57
|
+
const root = new Owner(detached ? null : current);
|
|
58
|
+
if (detached) root.context = current === null ? null : current.context;
|
|
59
|
+
try {
|
|
60
|
+
return runInScope({
|
|
61
|
+
owner: root,
|
|
62
|
+
listener: null
|
|
63
|
+
}, () => fn(() => disposeTree(root)));
|
|
64
|
+
} catch (error) {
|
|
65
|
+
disposeTree(root);
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Returns the owner that currently adopts new effects, memos and cleanups. Capture it while a
|
|
71
|
+
* component or root runs, then hand it to {@link runWithOwner} from code that runs later.
|
|
72
|
+
*
|
|
73
|
+
* @returns The current owner, or `null` outside any owner (at module scope, or in a callback that
|
|
74
|
+
* runs after an `await` or a timer).
|
|
75
|
+
* @example
|
|
76
|
+
* import { getOwner, onCleanup, runWithOwner } from "@shadow-js/core";
|
|
77
|
+
*
|
|
78
|
+
* function Poller() {
|
|
79
|
+
* const owner = getOwner();
|
|
80
|
+
* setTimeout(() => {
|
|
81
|
+
* runWithOwner(owner, () => onCleanup(() => console.log("poller disposed")));
|
|
82
|
+
* }, 1000);
|
|
83
|
+
* return null;
|
|
84
|
+
* }
|
|
85
|
+
* @see runWithOwner
|
|
86
|
+
* @since 1.0.0
|
|
87
|
+
*/
|
|
88
|
+
function getOwner() {
|
|
89
|
+
return currentOwner();
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Runs `fn` with `owner` as the current owner, untracked. Use it to restore ownership after an
|
|
93
|
+
* `await` or inside callbacks that outlive the synchronous run, so that effects, cleanups and
|
|
94
|
+
* context reads inside `fn` attach to that owner. A disposed owner is ignored: `fn` then runs
|
|
95
|
+
* without an owner, and development builds warn `RUN_WITH_DISPOSED_OWNER`.
|
|
96
|
+
*
|
|
97
|
+
* @param owner The owner to make current, usually captured with {@link getOwner}. `null` runs `fn`
|
|
98
|
+
* without an owner.
|
|
99
|
+
* @param fn The function to run.
|
|
100
|
+
* @returns Whatever `fn` returns. An error thrown by `fn` propagates unchanged.
|
|
101
|
+
* @example
|
|
102
|
+
* import { createContext, getOwner, runWithOwner, useContext } from "@shadow-js/core";
|
|
103
|
+
*
|
|
104
|
+
* const Locale = createContext("en");
|
|
105
|
+
*
|
|
106
|
+
* function Clock() {
|
|
107
|
+
* const owner = getOwner();
|
|
108
|
+
* queueMicrotask(() => {
|
|
109
|
+
* const locale = runWithOwner(owner, () => useContext(Locale));
|
|
110
|
+
* console.log(new Date().toLocaleTimeString(locale));
|
|
111
|
+
* });
|
|
112
|
+
* return null;
|
|
113
|
+
* }
|
|
114
|
+
* @see getOwner
|
|
115
|
+
* @since 1.0.0
|
|
116
|
+
*/
|
|
117
|
+
function runWithOwner(owner, fn) {
|
|
118
|
+
let target = owner;
|
|
119
|
+
if (target?.disposed) {
|
|
120
|
+
warn("RUN_WITH_DISPOSED_OWNER");
|
|
121
|
+
target = null;
|
|
122
|
+
}
|
|
123
|
+
return runInScope({
|
|
124
|
+
owner: target,
|
|
125
|
+
listener: null
|
|
126
|
+
}, fn);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Registers `fn` to run when the current owner is disposed or, for an effect or memo, before it
|
|
130
|
+
* re-runs. Cleanups run newest first and untracked; one that throws does not stop the others, and
|
|
131
|
+
* its error goes to the nearest error handler ({@link onError}, {@link catchError}). Outside any owner the call does nothing, because
|
|
132
|
+
* nothing would ever run the cleanup; development builds warn `NO_OWNER_CLEANUP`.
|
|
133
|
+
*
|
|
134
|
+
* @param fn The cleanup to register.
|
|
135
|
+
* @example
|
|
136
|
+
* import { onCleanup } from "@shadow-js/core";
|
|
137
|
+
*
|
|
138
|
+
* function Ticker() {
|
|
139
|
+
* const timer = setInterval(() => console.log("tick"), 1000);
|
|
140
|
+
* onCleanup(() => clearInterval(timer));
|
|
141
|
+
* return null;
|
|
142
|
+
* }
|
|
143
|
+
* @see onMount
|
|
144
|
+
* @since 1.0.0
|
|
145
|
+
*/
|
|
146
|
+
function onCleanup(fn) {
|
|
147
|
+
const owner = currentOwner();
|
|
148
|
+
if (owner === null) {
|
|
149
|
+
warn("NO_OWNER_CLEANUP");
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
addCleanup(owner, fn);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Registers an error handler on the current owner. It receives errors thrown in this owner's scope
|
|
156
|
+
* (its effects, memos, cleanups and child owners) that no closer handler took. Handlers on the same
|
|
157
|
+
* owner are tried newest first; a handler that throws passes its own error to the next handler up.
|
|
158
|
+
* Outside any owner the call does nothing; development builds warn `NO_OWNER_ERROR`.
|
|
159
|
+
*
|
|
160
|
+
* @param handler Receives the error, untracked. Throw from it to pass an error on to the next handler.
|
|
161
|
+
* @example
|
|
162
|
+
* import { onError, useEffect } from "@shadow-js/core";
|
|
163
|
+
*
|
|
164
|
+
* function Dashboard() {
|
|
165
|
+
* onError((error) => console.error("dashboard failed:", error));
|
|
166
|
+
* useEffect(() => {
|
|
167
|
+
* throw new Error("widget crashed"); // reaches the handler above
|
|
168
|
+
* });
|
|
169
|
+
* return null;
|
|
170
|
+
* }
|
|
171
|
+
* @see catchError
|
|
172
|
+
* @since 1.0.0
|
|
173
|
+
*/
|
|
174
|
+
function onError(handler) {
|
|
175
|
+
const owner = currentOwner();
|
|
176
|
+
if (owner === null) {
|
|
177
|
+
warn("NO_OWNER_ERROR");
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
if (owner.errorHandlers === null) owner.errorHandlers = [handler];
|
|
181
|
+
else owner.errorHandlers.push(handler);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Runs `fn` inside a new child scope whose errors go to `handler`: errors thrown synchronously by
|
|
185
|
+
* `fn`, and later errors of the effects, memos and cleanups it created. The scope is disposed with
|
|
186
|
+
* the current owner. Use it to contain the failures of one part of a component tree.
|
|
187
|
+
*
|
|
188
|
+
* @param fn The function to run; it runs untracked, with the new scope as owner.
|
|
189
|
+
* @param handler Receives each error, untracked. Throw from it to pass the error on to the next handler up.
|
|
190
|
+
* @returns Whatever `fn` returns, or `undefined` when `fn` threw and `handler` took the error.
|
|
191
|
+
* @throws Rethrows the error `handler` throws when no handler above takes it (outside a flush).
|
|
192
|
+
* @example
|
|
193
|
+
* import { catchError, useEffect } from "@shadow-js/core";
|
|
194
|
+
*
|
|
195
|
+
* function Widget() {
|
|
196
|
+
* return catchError(
|
|
197
|
+
* () => {
|
|
198
|
+
* useEffect(() => {
|
|
199
|
+
* throw new Error("network down");
|
|
200
|
+
* });
|
|
201
|
+
* return "ready";
|
|
202
|
+
* },
|
|
203
|
+
* (error) => console.error("widget failed:", error),
|
|
204
|
+
* );
|
|
205
|
+
* }
|
|
206
|
+
* @see onError
|
|
207
|
+
* @since 1.0.0
|
|
208
|
+
*/
|
|
209
|
+
function catchError(fn, handler) {
|
|
210
|
+
const scope = new Owner(currentOwner());
|
|
211
|
+
scope.errorHandlers = [handler];
|
|
212
|
+
try {
|
|
213
|
+
return runWithOwner(scope, fn);
|
|
214
|
+
} catch (error) {
|
|
215
|
+
handleError(error, scope);
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Returns an id that is identical on server and client for the same position in the owner tree.
|
|
221
|
+
* The id uses only letters, digits, `-` and `_`, so it is valid in `id`, `for`, `aria-*` references
|
|
222
|
+
* and in `querySelector("#" + id)` without escaping. Use it to link a label to its input, or an
|
|
223
|
+
* element to its `aria-describedby` target, in markup that is rendered on the server and hydrated.
|
|
224
|
+
*
|
|
225
|
+
* @returns A new id, unique within the root; each call on the same owner returns the next one.
|
|
226
|
+
* @throws ShadowError `NO_OWNER_ID` when called outside any owner, because no stable id exists
|
|
227
|
+
* there.
|
|
228
|
+
* @example
|
|
229
|
+
* import { useId } from "@shadow-js/core";
|
|
230
|
+
*
|
|
231
|
+
* function EmailField() {
|
|
232
|
+
* const id = useId();
|
|
233
|
+
* return (
|
|
234
|
+
* <>
|
|
235
|
+
* <label for={id}>Email</label>
|
|
236
|
+
* <input id={id} type="email" />
|
|
237
|
+
* </>
|
|
238
|
+
* );
|
|
239
|
+
* }
|
|
240
|
+
* @since 1.0.0
|
|
241
|
+
*/
|
|
242
|
+
function useId() {
|
|
243
|
+
const owner = currentOwner();
|
|
244
|
+
if (owner === null) throw new ShadowError("NO_OWNER_ID");
|
|
245
|
+
for (let host = owner; host !== null; host = host.parent) {
|
|
246
|
+
if (host.idPrefix === void 0) continue;
|
|
247
|
+
const hosted = host.idCount;
|
|
248
|
+
host.idCount += 1;
|
|
249
|
+
return `s${host.idPrefix}${hosted}`;
|
|
250
|
+
}
|
|
251
|
+
const path = [];
|
|
252
|
+
for (let node = owner; node !== null; node = node.parent) path.push(node.index);
|
|
253
|
+
path.reverse();
|
|
254
|
+
const sequence = owner.idCount;
|
|
255
|
+
owner.idCount += 1;
|
|
256
|
+
return `s${path.join("-")}_${sequence}`;
|
|
257
|
+
}
|
|
258
|
+
//#endregion
|
|
259
|
+
export { catchError, createRoot, getOwner, onCleanup, onError, runWithOwner, untrack, useId };
|
|
260
|
+
|
|
261
|
+
//# sourceMappingURL=ownership.js.map
|