@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,31 @@
|
|
|
1
|
+
//#region src/env.ts
|
|
2
|
+
/**
|
|
3
|
+
* True in the server condition build, where effects never run and rendering produces strings. Use
|
|
4
|
+
* it to skip browser-only work. The value is fixed per build: the package export conditions
|
|
5
|
+
* (`node`, `worker`, `browser`, ...) choose the build, not a runtime check.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* import { isServer } from "@shadow-js/core";
|
|
9
|
+
*
|
|
10
|
+
* const width = isServer ? 1024 : window.innerWidth;
|
|
11
|
+
* @see isDev
|
|
12
|
+
* @since 1.0.0
|
|
13
|
+
*/
|
|
14
|
+
const isServer = true;
|
|
15
|
+
/**
|
|
16
|
+
* True in development builds; false in production builds where `__DEV__` is compiled away. The
|
|
17
|
+
* `development` export condition selects the development build. Use it to guard checks and logging
|
|
18
|
+
* meant for development only.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* import { isDev } from "@shadow-js/core";
|
|
22
|
+
*
|
|
23
|
+
* if (isDev) console.info("debug panel enabled");
|
|
24
|
+
* @see isServer
|
|
25
|
+
* @since 1.0.0
|
|
26
|
+
*/
|
|
27
|
+
const isDev = true;
|
|
28
|
+
//#endregion
|
|
29
|
+
export { isDev, isServer };
|
|
30
|
+
|
|
31
|
+
//# sourceMappingURL=env.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env.js","names":[],"sources":["../../src/env.ts"],"sourcesContent":["/**\n * True in the server condition build, where effects never run and rendering produces strings. Use\n * it to skip browser-only work. The value is fixed per build: the package export conditions\n * (`node`, `worker`, `browser`, ...) choose the build, not a runtime check.\n *\n * @example\n * import { isServer } from \"@shadow-js/core\";\n *\n * const width = isServer ? 1024 : window.innerWidth;\n * @see isDev\n * @since 1.0.0\n */\nexport const isServer: boolean = __SERVER__;\n\n/**\n * True in development builds; false in production builds where `__DEV__` is compiled away. The\n * `development` export condition selects the development build. Use it to guard checks and logging\n * meant for development only.\n *\n * @example\n * import { isDev } from \"@shadow-js/core\";\n *\n * if (isDev) console.info(\"debug panel enabled\");\n * @see isServer\n * @since 1.0.0\n */\nexport const isDev: boolean = __DEV__;\n"],"mappings":";;;;;;;;;;;;;AAYA,MAAa,WAAA;;;;;;;;;;;;;AAcb,MAAa,QAAA"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { untrack } from "./ownership.js";
|
|
2
|
+
//#region src/events.ts
|
|
3
|
+
/**
|
|
4
|
+
* A stable callback that always runs its handler untracked. The ShadowJS form of React's
|
|
5
|
+
* `useEffectEvent`: components run once and props are getters, so the handler already sees the
|
|
6
|
+
* latest values. What it adds is the untracked call: an effect that calls the event does not
|
|
7
|
+
* subscribe to the signals the handler reads.
|
|
8
|
+
*
|
|
9
|
+
* @param handler The handler. Its arguments and result are passed through.
|
|
10
|
+
* @returns A new function with the same signature that calls `handler` untracked. Create it once,
|
|
11
|
+
* in the component body, and keep the reference.
|
|
12
|
+
* @example
|
|
13
|
+
* import { useEffect, useEvent, useSignal } from "@shadow-js/core";
|
|
14
|
+
*
|
|
15
|
+
* function Ticker() {
|
|
16
|
+
* const [count] = useSignal(0);
|
|
17
|
+
* const onTick = useEvent(() => console.log(count())); // count() is not tracked by the effect
|
|
18
|
+
* useEffect(() => {
|
|
19
|
+
* const id = setInterval(() => onTick(), 1000);
|
|
20
|
+
* return () => clearInterval(id);
|
|
21
|
+
* });
|
|
22
|
+
* return null;
|
|
23
|
+
* }
|
|
24
|
+
* @see untrack
|
|
25
|
+
* @since 1.0.0
|
|
26
|
+
*/
|
|
27
|
+
function useEvent(handler) {
|
|
28
|
+
return (...args) => untrack(() => handler(...args));
|
|
29
|
+
}
|
|
30
|
+
//#endregion
|
|
31
|
+
export { useEvent };
|
|
32
|
+
|
|
33
|
+
//# sourceMappingURL=events.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events.js","names":[],"sources":["../../src/events.ts"],"sourcesContent":["import { untrack } from \"./ownership\";\n\n/**\n * A stable callback that always runs its handler untracked. The ShadowJS form of React's\n * `useEffectEvent`: components run once and props are getters, so the handler already sees the\n * latest values. What it adds is the untracked call: an effect that calls the event does not\n * subscribe to the signals the handler reads.\n *\n * @param handler The handler. Its arguments and result are passed through.\n * @returns A new function with the same signature that calls `handler` untracked. Create it once,\n * in the component body, and keep the reference.\n * @example\n * import { useEffect, useEvent, useSignal } from \"@shadow-js/core\";\n *\n * function Ticker() {\n * const [count] = useSignal(0);\n * const onTick = useEvent(() => console.log(count())); // count() is not tracked by the effect\n * useEffect(() => {\n * const id = setInterval(() => onTick(), 1000);\n * return () => clearInterval(id);\n * });\n * return null;\n * }\n * @see untrack\n * @since 1.0.0\n */\nexport function useEvent<A extends unknown[], R>(handler: (...args: A) => R): (...args: A) => R {\n return (...args: A): R => untrack(() => handler(...args));\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AA0BA,SAAgB,SAAiC,SAA+C;CAC9F,QAAQ,GAAG,SAAe,cAAc,QAAQ,GAAG,IAAI,CAAC;AAC1D"}
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
import { DEVTOOLS_EVENT, report } from "../internal/devtools.js";
|
|
2
|
+
import { MESSAGES, formatMessage } from "../internal/messages.js";
|
|
3
|
+
import { ShadowError, resetWarningDedupe, setWarningListener, warn } from "../internal/diagnostics.js";
|
|
4
|
+
import { rt } from "../internal/state.js";
|
|
5
|
+
import { Owner, addCleanup, handleError } from "../internal/owner.js";
|
|
6
|
+
import { enqueue } from "../internal/queue.js";
|
|
7
|
+
import { Computation, Signal, hasObservers, markObservers, observersOf, readMemo, readSignal, recompute, sourcesOf, track, updateIfNecessary } from "../internal/graph.js";
|
|
8
|
+
import { batch, flush, flushSync, group, schedule } from "../internal/scheduler.js";
|
|
9
|
+
import { runWithContext } from "../context.js";
|
|
10
|
+
import { disposeTree } from "../internal/dispose.js";
|
|
11
|
+
import { SUSPENSION, isSuspension } from "../internal/suspension.js";
|
|
12
|
+
import { beginHold, createTransitionHold, endHold, holdReader, inTransition, pauseOwner, registerTransitionLane, resumeOwner, startTransition, transitionHooks, wakeSuspended } from "../internal/concurrency.js";
|
|
13
|
+
import { isHydrating, withHydration } from "../internal/hydration-flag.js";
|
|
14
|
+
//#region src/extend.ts
|
|
15
|
+
/**
|
|
16
|
+
* The extension API: everything the built-in hooks (`useSignal`, `useMemo`, `useEffect`,
|
|
17
|
+
* `useStore`, `useResource`, `mapArray`, `useSelector`, contexts, ...) are built from. They import
|
|
18
|
+
* ONLY this module, never the engine internals, so a third-party package can create hooks with the
|
|
19
|
+
* same behavior, performance and diagnostics. A test (`public-api.test.ts`) enforces that.
|
|
20
|
+
*
|
|
21
|
+
* Import from `@shadow-js/core/extend`. These functions are stable but low level: prefer the hooks
|
|
22
|
+
* unless you are building a new primitive.
|
|
23
|
+
*
|
|
24
|
+
* @packageDocumentation
|
|
25
|
+
*/
|
|
26
|
+
/**
|
|
27
|
+
* Returns the owner that adopts nodes created right now. The extension-API form of `getOwner`: use
|
|
28
|
+
* it in a primitive that attaches a node or a cleanup to its caller's owner.
|
|
29
|
+
*
|
|
30
|
+
* @returns The current owner, or `null` outside any owner.
|
|
31
|
+
* @example
|
|
32
|
+
* import { addCleanup, currentOwner } from "@shadow-js/core/extend";
|
|
33
|
+
*
|
|
34
|
+
* function useInterval(fn: () => void, ms: number): void {
|
|
35
|
+
* const timer = setInterval(fn, ms);
|
|
36
|
+
* const owner = currentOwner();
|
|
37
|
+
* if (owner !== null) addCleanup(owner, () => clearInterval(timer));
|
|
38
|
+
* }
|
|
39
|
+
* @see createOwner
|
|
40
|
+
* @since 1.0.0
|
|
41
|
+
*/
|
|
42
|
+
function currentOwner() {
|
|
43
|
+
return rt.owner;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Returns the computation that records reads right now. Use it in a primitive whose read behaves
|
|
47
|
+
* differently for a tracked reader, for example one that holds a boundary only for a listening
|
|
48
|
+
* memo or effect.
|
|
49
|
+
*
|
|
50
|
+
* @returns The listening memo or effect, or `null` when reads are untracked.
|
|
51
|
+
* @example
|
|
52
|
+
* import { currentListener } from "@shadow-js/core/extend";
|
|
53
|
+
*
|
|
54
|
+
* function readWithStats<T>(read: () => T, stats: { tracked: number }): T {
|
|
55
|
+
* if (currentListener() !== null) stats.tracked += 1;
|
|
56
|
+
* return read();
|
|
57
|
+
* }
|
|
58
|
+
* @see isTracking
|
|
59
|
+
* @since 1.0.0
|
|
60
|
+
*/
|
|
61
|
+
function currentListener() {
|
|
62
|
+
return rt.listener;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Says whether reads are being recorded as dependencies right now. Use it to skip subscription
|
|
66
|
+
* work, or to warn, when a primitive is read outside an effect or a memo.
|
|
67
|
+
*
|
|
68
|
+
* @returns `true` while an effect or a memo runs tracked, `false` otherwise (module scope,
|
|
69
|
+
* `untrack`, event handlers).
|
|
70
|
+
* @example
|
|
71
|
+
* import { untrack, useMemo } from "@shadow-js/core";
|
|
72
|
+
* import { isTracking } from "@shadow-js/core/extend";
|
|
73
|
+
*
|
|
74
|
+
* const inMemo = useMemo(() => isTracking());
|
|
75
|
+
* console.log(inMemo(), untrack(() => isTracking())); // true false
|
|
76
|
+
* @see currentListener
|
|
77
|
+
* @since 1.0.0
|
|
78
|
+
*/
|
|
79
|
+
function isTracking() {
|
|
80
|
+
return rt.listener !== null;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Says whether a memo (not an effect) is computing right now: writing signals is a mistake there.
|
|
84
|
+
* Cleanups that run while a memo tears down rows or children may write, so a disposal in progress
|
|
85
|
+
* is not a violation, and neither is a memo created with `allowWrites`. Use it to guard a custom
|
|
86
|
+
* write path the way {@link writeSignal} does.
|
|
87
|
+
*
|
|
88
|
+
* @returns `true` while a memo without `allowWrites` computes and no disposal is running.
|
|
89
|
+
* @example
|
|
90
|
+
* import { isComputingMemo, ShadowError } from "@shadow-js/core/extend";
|
|
91
|
+
*
|
|
92
|
+
* function appendLog(log: string[], entry: string): void {
|
|
93
|
+
* if (isComputingMemo()) throw new ShadowError("WRITE_IN_MEMO");
|
|
94
|
+
* log.push(entry);
|
|
95
|
+
* }
|
|
96
|
+
* @since 1.0.0
|
|
97
|
+
*/
|
|
98
|
+
function isComputingMemo() {
|
|
99
|
+
return rt.disposing === 0 && rt.running !== null && rt.running.kind === 1 && (rt.running.flags & 256) === 0;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Runs `fn` with the given owner and listener in effect, and restores both afterward, even when
|
|
103
|
+
* `fn` throws. The low-level form of `runWithOwner` and `untrack`; unlike `runWithOwner` it does
|
|
104
|
+
* not check whether the owner is disposed.
|
|
105
|
+
*
|
|
106
|
+
* @param scope `owner` and `listener` to install. For each, `undefined` (or absent) keeps the
|
|
107
|
+
* current one and `null` clears it.
|
|
108
|
+
* Getters are evaluated once; a failed getter restores the original scope.
|
|
109
|
+
* @param fn The function to run.
|
|
110
|
+
* @returns Whatever `fn` returns. An error thrown by `fn` propagates after the registers are
|
|
111
|
+
* restored.
|
|
112
|
+
* @example
|
|
113
|
+
* import { onCleanup } from "@shadow-js/core";
|
|
114
|
+
* import { createOwner, disposeOwner, runInScope } from "@shadow-js/core/extend";
|
|
115
|
+
*
|
|
116
|
+
* const owner = createOwner(null);
|
|
117
|
+
* runInScope({ owner, listener: null }, () => {
|
|
118
|
+
* onCleanup(() => console.log("released"));
|
|
119
|
+
* });
|
|
120
|
+
* disposeOwner(owner); // logs "released"
|
|
121
|
+
* @see createOwner
|
|
122
|
+
* @since 1.0.0
|
|
123
|
+
*/
|
|
124
|
+
function runInScope(scope, fn) {
|
|
125
|
+
const owner = rt.owner;
|
|
126
|
+
const listener = rt.listener;
|
|
127
|
+
try {
|
|
128
|
+
const nextOwner = scope.owner;
|
|
129
|
+
if (nextOwner !== void 0) rt.owner = nextOwner;
|
|
130
|
+
const nextListener = scope.listener;
|
|
131
|
+
if (nextListener !== void 0) rt.listener = nextListener;
|
|
132
|
+
return fn();
|
|
133
|
+
} finally {
|
|
134
|
+
rt.owner = owner;
|
|
135
|
+
rt.listener = listener;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Creates the storage of a signal: its value, its equality and the observers that read it. Read it
|
|
140
|
+
* with `readSignal` and write it with {@link writeSignal}; `useSignal` is built this way.
|
|
141
|
+
*
|
|
142
|
+
* @param value Initial value.
|
|
143
|
+
* @param equals Decides whether a write is a change: readers are notified only when it returns
|
|
144
|
+
* `false`. `false` notifies on every write.
|
|
145
|
+
* @param name Label shown by devtools. Undefined leaves the signal unnamed.
|
|
146
|
+
* @returns The signal node. It has no owner: it lives as long as something references it.
|
|
147
|
+
* @example
|
|
148
|
+
* import { createSignalNode, readSignal, writeSignal } from "@shadow-js/core/extend";
|
|
149
|
+
*
|
|
150
|
+
* const temperature = createSignalNode<number>(20, Object.is, "temperature");
|
|
151
|
+
* const read = () => readSignal(temperature);
|
|
152
|
+
* writeSignal(temperature, 21);
|
|
153
|
+
* console.log(read()); // 21
|
|
154
|
+
* @see writeSignal
|
|
155
|
+
* @since 1.0.0
|
|
156
|
+
*/
|
|
157
|
+
function createSignalNode(value, equals, name) {
|
|
158
|
+
return new Signal(value, equals, name);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Stores a new value in a signal node and notifies its readers when the value differs by the
|
|
162
|
+
* signal's equality. Effects that read it run on the next flush; memos recompute on their next
|
|
163
|
+
* read.
|
|
164
|
+
*
|
|
165
|
+
* @param signal The node, from {@link createSignalNode}.
|
|
166
|
+
* @param next The new value. A function is stored as is: there is no updater form.
|
|
167
|
+
* @param options `allowInMemo` permits the write while a memo computes. Undefined uses the default.
|
|
168
|
+
* @returns `true` when the value changed and was stored, `false` when the equality kept the old
|
|
169
|
+
* value (nobody is notified then).
|
|
170
|
+
* @throws ShadowError `WRITE_IN_MEMO`, in development builds only, when a memo is computing and
|
|
171
|
+
* `allowInMemo` is not set.
|
|
172
|
+
* @example
|
|
173
|
+
* import { createSignalNode, writeSignal } from "@shadow-js/core/extend";
|
|
174
|
+
*
|
|
175
|
+
* const status = createSignalNode<string>("idle", Object.is);
|
|
176
|
+
* console.log(writeSignal(status, "loading")); // true
|
|
177
|
+
* console.log(writeSignal(status, "loading")); // false: an equal value notifies nobody
|
|
178
|
+
* @see notify
|
|
179
|
+
* @since 1.0.0
|
|
180
|
+
*/
|
|
181
|
+
function writeSignal(signal, next, options) {
|
|
182
|
+
if (options?.allowInMemo !== true && isComputingMemo()) throw new ShadowError("WRITE_IN_MEMO");
|
|
183
|
+
const equals = signal.equals;
|
|
184
|
+
if (equals !== false && equals(signal.value, next)) return false;
|
|
185
|
+
signal.value = next;
|
|
186
|
+
report(DEVTOOLS_EVENT.SIGNAL_WRITTEN, signal, next, rt.running, observersOf(signal));
|
|
187
|
+
notify(signal);
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Marks the observers of `source` stale and schedules a flush. Call it after changing a source's
|
|
192
|
+
* value in place (for example after mutating an array a signal node holds), where
|
|
193
|
+
* {@link writeSignal} cannot see the change.
|
|
194
|
+
*
|
|
195
|
+
* @param source A signal or memo node. A source nobody observes is left alone.
|
|
196
|
+
* @example
|
|
197
|
+
* import { createSignalNode, notify } from "@shadow-js/core/extend";
|
|
198
|
+
*
|
|
199
|
+
* const list = createSignalNode<string[]>([], false);
|
|
200
|
+
* list.value.push("milk"); // an in-place change: no equality can see it
|
|
201
|
+
* notify(list); // the readers of `list` run again
|
|
202
|
+
* @see writeSignal
|
|
203
|
+
* @since 1.0.0
|
|
204
|
+
*/
|
|
205
|
+
function notify(source) {
|
|
206
|
+
if (!hasObservers(source)) return;
|
|
207
|
+
markObservers(source);
|
|
208
|
+
schedule();
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Creates a memo or effect node: the building block of `useMemo`, `useEffect` and the render
|
|
212
|
+
* effects of a renderer. Memos compute immediately unless `lazy`; render effects run immediately;
|
|
213
|
+
* user effects are queued for the next flush. The node is owned and disposed with its owner.
|
|
214
|
+
* Errors of a memo are stored and rethrown to its readers; errors of an effect go to the owner's
|
|
215
|
+
* error handler.
|
|
216
|
+
*
|
|
217
|
+
* @param options What to create, how it runs and where it is owned.
|
|
218
|
+
* @returns The node. Read a memo with {@link readComputation}; an effect needs no handle.
|
|
219
|
+
* @throws The error of a render effect's first run when no error handler takes it and no flush is
|
|
220
|
+
* running.
|
|
221
|
+
* @example
|
|
222
|
+
* import { createComputation, createSignalNode, readComputation, readSignal, writeSignal } from "@shadow-js/core/extend";
|
|
223
|
+
*
|
|
224
|
+
* const price = createSignalNode<number>(10, Object.is);
|
|
225
|
+
* const withTax = createComputation({ kind: "memo", fn: () => readSignal(price) * 1.2 });
|
|
226
|
+
* createComputation({
|
|
227
|
+
* kind: "effect",
|
|
228
|
+
* fn: () => console.log(readComputation<number>(withTax)),
|
|
229
|
+
* });
|
|
230
|
+
* writeSignal(price, 20); // the effect logs 24 on the next flush
|
|
231
|
+
* @see readComputation
|
|
232
|
+
* @since 1.0.0
|
|
233
|
+
*/
|
|
234
|
+
function createComputation(options) {
|
|
235
|
+
const owner = options.owner === void 0 ? rt.owner : options.owner;
|
|
236
|
+
if (options.kind === "memo") {
|
|
237
|
+
const node = new Computation(owner, 1, 32 | (options.allowWrites === true ? 256 : 0), options.fn, options.equals ?? Object.is, options.name);
|
|
238
|
+
if (options.lazy !== true) updateIfNecessary(node);
|
|
239
|
+
return node;
|
|
240
|
+
}
|
|
241
|
+
const flags = (options.render === true ? 16 : 0) | (options.defer === true ? 64 : 0) | (options.lane === true ? 512 : 0);
|
|
242
|
+
const node = new Computation(owner, 2, flags, options.fn, false, options.name);
|
|
243
|
+
node.apply = options.apply ?? null;
|
|
244
|
+
if (options.render === true) recompute(node);
|
|
245
|
+
else {
|
|
246
|
+
enqueue(node);
|
|
247
|
+
schedule();
|
|
248
|
+
}
|
|
249
|
+
return node;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Reads a memo node: brings it up to date if a source changed, subscribes the running computation
|
|
253
|
+
* to it, and rethrows the error its last run stored. The accessor `useMemo` returns is this call.
|
|
254
|
+
*
|
|
255
|
+
* @param node A memo node from {@link createComputation}.
|
|
256
|
+
* @returns The memo's current value.
|
|
257
|
+
* @throws The error the memo's last computation threw, when no error handler took it. ShadowError
|
|
258
|
+
* `CYCLE_DETECTED` when the memo is read while it computes.
|
|
259
|
+
* @example
|
|
260
|
+
* import { createComputation, createSignalNode, readComputation, readSignal } from "@shadow-js/core/extend";
|
|
261
|
+
*
|
|
262
|
+
* const first = createSignalNode<string>("Ada", Object.is);
|
|
263
|
+
* const greeting = createComputation({ kind: "memo", fn: () => `Hello, ${readSignal(first)}` });
|
|
264
|
+
* console.log(readComputation<string>(greeting)); // "Hello, Ada"
|
|
265
|
+
* @see createComputation
|
|
266
|
+
* @since 1.0.0
|
|
267
|
+
*/
|
|
268
|
+
function readComputation(node) {
|
|
269
|
+
return readMemo(node);
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Brings a memo or effect node up to date if one of its sources changed, without tracking it. Use
|
|
273
|
+
* it to settle a node before reading its fields directly.
|
|
274
|
+
*
|
|
275
|
+
* @param node A node from {@link createComputation}. A node that is up to date is left as is.
|
|
276
|
+
* @throws ShadowError `CYCLE_DETECTED` when a dependency is still computing (a cycle). An effect
|
|
277
|
+
* that re-runs routes its errors to its owner's error handler.
|
|
278
|
+
* @example
|
|
279
|
+
* import { createComputation, createSignalNode, readSignal, refresh, writeSignal } from "@shadow-js/core/extend";
|
|
280
|
+
*
|
|
281
|
+
* const count = createSignalNode<number>(1, Object.is);
|
|
282
|
+
* const doubled = createComputation({ kind: "memo", fn: () => readSignal(count) * 2 });
|
|
283
|
+
* writeSignal(count, 2);
|
|
284
|
+
* refresh(doubled);
|
|
285
|
+
* console.log(doubled.value); // 4
|
|
286
|
+
* @see readComputation
|
|
287
|
+
* @since 1.0.0
|
|
288
|
+
*/
|
|
289
|
+
function refresh(node) {
|
|
290
|
+
updateIfNecessary(node);
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Creates a plain owner: a scope that adopts the nodes and cleanups created under it and disposes
|
|
294
|
+
* them together. The building block of `createRoot` and of control-flow branches.
|
|
295
|
+
*
|
|
296
|
+
* @param parent The owner to attach to and inherit context from. Undefined (or omitted) uses the
|
|
297
|
+
* current owner; `null` creates a root with no context that only an explicit dispose ends.
|
|
298
|
+
* @returns The new owner. Dispose it with `disposeOwner`.
|
|
299
|
+
* @example
|
|
300
|
+
* import { useEffect } from "@shadow-js/core";
|
|
301
|
+
* import { createOwner, disposeOwner, runInScope } from "@shadow-js/core/extend";
|
|
302
|
+
*
|
|
303
|
+
* const branch = createOwner();
|
|
304
|
+
* runInScope({ owner: branch, listener: null }, () => {
|
|
305
|
+
* useEffect(() => console.log("branch effect"));
|
|
306
|
+
* });
|
|
307
|
+
* disposeOwner(branch); // the effect and its cleanups are gone
|
|
308
|
+
* @see runInScope
|
|
309
|
+
* @since 1.0.0
|
|
310
|
+
*/
|
|
311
|
+
function createOwner(parent) {
|
|
312
|
+
return new Owner(parent === void 0 ? rt.owner : parent);
|
|
313
|
+
}
|
|
314
|
+
//#endregion
|
|
315
|
+
export { Computation as ComputationNode, DEVTOOLS_EVENT, MESSAGES, Owner, SUSPENSION, ShadowError, Signal as SignalNode, addCleanup, batch, beginHold, createComputation, createOwner, createSignalNode, createTransitionHold, currentListener, currentOwner, report as devtoolsReport, disposeTree as disposeOwner, endHold, flush, flushSync, formatMessage, group, handleError, holdReader, inTransition, isComputingMemo, isHydrating, isSuspension, isTracking, markObservers, notify, observersOf, pauseOwner, readComputation, readSignal, refresh, registerTransitionLane, resetWarningDedupe, resumeOwner, runInScope, runWithContext, schedule, setWarningListener, sourcesOf, startTransition, track, transitionHooks, wakeSuspended, warn, withHydration, writeSignal };
|
|
316
|
+
|
|
317
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["OwnerClass"],"sources":["../../../src/extend.ts"],"sourcesContent":["/**\n * The extension API: everything the built-in hooks (`useSignal`, `useMemo`, `useEffect`,\n * `useStore`, `useResource`, `mapArray`, `useSelector`, contexts, ...) are built from. They import\n * ONLY this module, never the engine internals, so a third-party package can create hooks with the\n * same behavior, performance and diagnostics. A test (`public-api.test.ts`) enforces that.\n *\n * Import from `@shadow-js/core/extend`. These functions are stable but low level: prefer the hooks\n * unless you are building a new primitive.\n *\n * @packageDocumentation\n */\n\nimport { DEVTOOLS_EVENT, report } from \"./internal/devtools\";\nimport { ShadowError } from \"./internal/diagnostics\";\nimport {\n FLAG_ALLOW_WRITES,\n FLAG_DEFER,\n FLAG_LANE,\n FLAG_RENDER,\n FLAG_UNINIT,\n KIND_EFFECT,\n KIND_MEMO,\n} from \"./internal/flags\";\nimport {\n Computation,\n hasObservers,\n markObservers,\n observersOf,\n readMemo,\n readSignal,\n recompute,\n Signal,\n type Source,\n sourcesOf,\n track,\n updateIfNecessary,\n} from \"./internal/graph\";\nimport { type Owner, Owner as OwnerClass } from \"./internal/owner\";\nimport { enqueue } from \"./internal/queue\";\nimport { schedule } from \"./internal/scheduler\";\nimport { rt } from \"./internal/state\";\nimport type { Equals } from \"./internal/types\";\n\nexport { runWithContext } from \"./context\";\nexport {\n beginHold,\n createTransitionHold,\n endHold,\n holdReader,\n inTransition,\n pauseOwner,\n registerTransitionLane,\n resumeOwner,\n startTransition,\n transitionHooks,\n wakeSuspended,\n} from \"./internal/concurrency\";\nexport { DEVTOOLS_EVENT, report as devtoolsReport } from \"./internal/devtools\";\nexport type { WarningListener } from \"./internal/diagnostics\";\nexport { resetWarningDedupe, ShadowError, setWarningListener, warn } from \"./internal/diagnostics\";\nexport { disposeTree as disposeOwner } from \"./internal/dispose\";\nexport { isHydrating, withHydration } from \"./internal/hydration-flag\";\nexport type { MessageCode } from \"./internal/messages\";\nexport { formatMessage, MESSAGES } from \"./internal/messages\";\nexport type { ContextMap } from \"./internal/owner\";\nexport { addCleanup, handleError, Owner } from \"./internal/owner\";\nexport { batch, flush, flushSync, group } from \"./internal/scheduler\";\nexport type {\n BoundaryHold,\n SlotBranch,\n SlotChoice,\n SlotOwned,\n SlotState,\n SlotSwap,\n TransitionHold,\n} from \"./internal/state\";\nexport { isSuspension, SUSPENSION } from \"./internal/suspension\";\nexport type {\n Accessor,\n AnyFunction,\n Cleanup,\n Equals,\n ErrorHandler,\n Setter,\n} from \"./internal/types\";\nexport type { Source as SourceNode };\nexport { Computation as ComputationNode, Signal as SignalNode };\n\n// ---------- registers ----------\n\n/**\n * Returns the owner that adopts nodes created right now. The extension-API form of `getOwner`: use\n * it in a primitive that attaches a node or a cleanup to its caller's owner.\n *\n * @returns The current owner, or `null` outside any owner.\n * @example\n * import { addCleanup, currentOwner } from \"@shadow-js/core/extend\";\n *\n * function useInterval(fn: () => void, ms: number): void {\n * const timer = setInterval(fn, ms);\n * const owner = currentOwner();\n * if (owner !== null) addCleanup(owner, () => clearInterval(timer));\n * }\n * @see createOwner\n * @since 1.0.0\n */\nexport function currentOwner(): Owner | null {\n return rt.owner;\n}\n\n/**\n * Returns the computation that records reads right now. Use it in a primitive whose read behaves\n * differently for a tracked reader, for example one that holds a boundary only for a listening\n * memo or effect.\n *\n * @returns The listening memo or effect, or `null` when reads are untracked.\n * @example\n * import { currentListener } from \"@shadow-js/core/extend\";\n *\n * function readWithStats<T>(read: () => T, stats: { tracked: number }): T {\n * if (currentListener() !== null) stats.tracked += 1;\n * return read();\n * }\n * @see isTracking\n * @since 1.0.0\n */\nexport function currentListener(): Computation | null {\n return rt.listener;\n}\n\n/**\n * Says whether reads are being recorded as dependencies right now. Use it to skip subscription\n * work, or to warn, when a primitive is read outside an effect or a memo.\n *\n * @returns `true` while an effect or a memo runs tracked, `false` otherwise (module scope,\n * `untrack`, event handlers).\n * @example\n * import { untrack, useMemo } from \"@shadow-js/core\";\n * import { isTracking } from \"@shadow-js/core/extend\";\n *\n * const inMemo = useMemo(() => isTracking());\n * console.log(inMemo(), untrack(() => isTracking())); // true false\n * @see currentListener\n * @since 1.0.0\n */\nexport function isTracking(): boolean {\n return rt.listener !== null;\n}\n\n/**\n * Says whether a memo (not an effect) is computing right now: writing signals is a mistake there.\n * Cleanups that run while a memo tears down rows or children may write, so a disposal in progress\n * is not a violation, and neither is a memo created with `allowWrites`. Use it to guard a custom\n * write path the way {@link writeSignal} does.\n *\n * @returns `true` while a memo without `allowWrites` computes and no disposal is running.\n * @example\n * import { isComputingMemo, ShadowError } from \"@shadow-js/core/extend\";\n *\n * function appendLog(log: string[], entry: string): void {\n * if (isComputingMemo()) throw new ShadowError(\"WRITE_IN_MEMO\");\n * log.push(entry);\n * }\n * @since 1.0.0\n */\nexport function isComputingMemo(): boolean {\n return (\n rt.disposing === 0 &&\n rt.running !== null &&\n rt.running.kind === KIND_MEMO &&\n (rt.running.flags & FLAG_ALLOW_WRITES) === 0\n );\n}\n\n/**\n * Runs `fn` with the given owner and listener in effect, and restores both afterward, even when\n * `fn` throws. The low-level form of `runWithOwner` and `untrack`; unlike `runWithOwner` it does\n * not check whether the owner is disposed.\n *\n * @param scope `owner` and `listener` to install. For each, `undefined` (or absent) keeps the\n * current one and `null` clears it.\n * Getters are evaluated once; a failed getter restores the original scope.\n * @param fn The function to run.\n * @returns Whatever `fn` returns. An error thrown by `fn` propagates after the registers are\n * restored.\n * @example\n * import { onCleanup } from \"@shadow-js/core\";\n * import { createOwner, disposeOwner, runInScope } from \"@shadow-js/core/extend\";\n *\n * const owner = createOwner(null);\n * runInScope({ owner, listener: null }, () => {\n * onCleanup(() => console.log(\"released\"));\n * });\n * disposeOwner(owner); // logs \"released\"\n * @see createOwner\n * @since 1.0.0\n */\nexport function runInScope<T>(\n scope: { owner?: Owner | null; listener?: Computation | null },\n fn: () => T,\n): T {\n const owner = rt.owner;\n const listener = rt.listener;\n try {\n const nextOwner = scope.owner;\n if (nextOwner !== undefined) rt.owner = nextOwner;\n const nextListener = scope.listener;\n if (nextListener !== undefined) rt.listener = nextListener;\n return fn();\n } finally {\n rt.owner = owner;\n rt.listener = listener;\n }\n}\n\n// ---------- reactive cells ----------\n\n/**\n * Creates the storage of a signal: its value, its equality and the observers that read it. Read it\n * with `readSignal` and write it with {@link writeSignal}; `useSignal` is built this way.\n *\n * @param value Initial value.\n * @param equals Decides whether a write is a change: readers are notified only when it returns\n * `false`. `false` notifies on every write.\n * @param name Label shown by devtools. Undefined leaves the signal unnamed.\n * @returns The signal node. It has no owner: it lives as long as something references it.\n * @example\n * import { createSignalNode, readSignal, writeSignal } from \"@shadow-js/core/extend\";\n *\n * const temperature = createSignalNode<number>(20, Object.is, \"temperature\");\n * const read = () => readSignal(temperature);\n * writeSignal(temperature, 21);\n * console.log(read()); // 21\n * @see writeSignal\n * @since 1.0.0\n */\nexport function createSignalNode<T>(value: T, equals: Equals<T>, name?: string): Signal<T> {\n return new Signal<T>(value, equals, name);\n}\n\n/**\n * Options of {@link writeSignal}.\n * @since 1.0.0\n */\nexport interface WriteOptions {\n /**\n * Permit the write while a memo computes, for bookkeeping signals owned by a primitive (a\n * Suspense counter, a resource's state). Turns the development `WRITE_IN_MEMO` check off for\n * this write.\n * @default false\n */\n allowInMemo?: boolean;\n}\n\n/**\n * Stores a new value in a signal node and notifies its readers when the value differs by the\n * signal's equality. Effects that read it run on the next flush; memos recompute on their next\n * read.\n *\n * @param signal The node, from {@link createSignalNode}.\n * @param next The new value. A function is stored as is: there is no updater form.\n * @param options `allowInMemo` permits the write while a memo computes. Undefined uses the default.\n * @returns `true` when the value changed and was stored, `false` when the equality kept the old\n * value (nobody is notified then).\n * @throws ShadowError `WRITE_IN_MEMO`, in development builds only, when a memo is computing and\n * `allowInMemo` is not set.\n * @example\n * import { createSignalNode, writeSignal } from \"@shadow-js/core/extend\";\n *\n * const status = createSignalNode<string>(\"idle\", Object.is);\n * console.log(writeSignal(status, \"loading\")); // true\n * console.log(writeSignal(status, \"loading\")); // false: an equal value notifies nobody\n * @see notify\n * @since 1.0.0\n */\nexport function writeSignal<T>(signal: Signal<T>, next: T, options?: WriteOptions): boolean {\n if (__DEV__ && options?.allowInMemo !== true && isComputingMemo()) {\n throw new ShadowError(\"WRITE_IN_MEMO\");\n }\n const equals = signal.equals;\n if (equals !== false && equals(signal.value, next)) return false;\n signal.value = next;\n if (__DEV__) {\n report(DEVTOOLS_EVENT.SIGNAL_WRITTEN, signal, next, rt.running, observersOf(signal));\n }\n notify(signal);\n return true;\n}\n\n/**\n * Marks the observers of `source` stale and schedules a flush. Call it after changing a source's\n * value in place (for example after mutating an array a signal node holds), where\n * {@link writeSignal} cannot see the change.\n *\n * @param source A signal or memo node. A source nobody observes is left alone.\n * @example\n * import { createSignalNode, notify } from \"@shadow-js/core/extend\";\n *\n * const list = createSignalNode<string[]>([], false);\n * list.value.push(\"milk\"); // an in-place change: no equality can see it\n * notify(list); // the readers of `list` run again\n * @see writeSignal\n * @since 1.0.0\n */\nexport function notify(source: Source): void {\n if (!hasObservers(source)) return;\n markObservers(source);\n schedule();\n}\n\nexport { markObservers, observersOf, readSignal, schedule, sourcesOf, track };\n\n// ---------- computations (memos and effects) ----------\n\n/**\n * Options of {@link createComputation}.\n * @since 1.0.0\n */\nexport interface ComputationOptions {\n /**\n * `\"memo\"` creates a cached value that readers pull with {@link readComputation}; `\"effect\"`\n * creates a side effect that the scheduler runs.\n */\n kind: \"memo\" | \"effect\";\n /**\n * The computation. It is tracked: every source it reads becomes a dependency. It receives the\n * previous value (a memo's last value, or an effect's last computed value when `apply` is set;\n * `undefined` otherwise). A memo stores its result; an effect without `apply` may return a\n * cleanup function.\n */\n fn: (previous: never) => unknown;\n /**\n * Memos: decides whether a recomputation changed the value; readers are notified only when it\n * returns `false`, and `false` always propagates. Ignored for effects.\n * @default Object.is\n */\n equals?: Equals<unknown>;\n /**\n * Label shown by devtools and diagnostics.\n * @default undefined (the node is unnamed)\n */\n name?: string;\n /**\n * Effects: run in the render phase, synchronously at creation and before user effects in each\n * flush. Ignored for memos.\n * @default false\n */\n render?: boolean;\n /**\n * Effects with `apply`: skip the first call of `apply`; `fn` still runs and tracks.\n * @default false\n */\n defer?: boolean;\n /**\n * Memos: do not compute until the first read.\n * @default false (the memo computes once at creation)\n */\n lazy?: boolean;\n /**\n * Memos that run component setup (such as a hot-reload wrapper): writing signals inside is\n * legitimate there, so the dev-mode write guard stays off.\n * @default false\n */\n allowWrites?: boolean;\n /**\n * Effects: second stage, run untracked with the value `fn` computed and the previous one. It may\n * return a cleanup function.\n * @default undefined (`fn` is the whole effect)\n */\n apply?: (value: never, previous: never) => unknown;\n /**\n * Owner to attach to. `null` creates a node that no owner disposes.\n * @default the current owner\n */\n owner?: Owner | null;\n /**\n * Effects: always run in the transition lane (the watcher behind `useDeferredValue`).\n * @default false\n */\n lane?: boolean;\n}\n\n/**\n * Creates a memo or effect node: the building block of `useMemo`, `useEffect` and the render\n * effects of a renderer. Memos compute immediately unless `lazy`; render effects run immediately;\n * user effects are queued for the next flush. The node is owned and disposed with its owner.\n * Errors of a memo are stored and rethrown to its readers; errors of an effect go to the owner's\n * error handler.\n *\n * @param options What to create, how it runs and where it is owned.\n * @returns The node. Read a memo with {@link readComputation}; an effect needs no handle.\n * @throws The error of a render effect's first run when no error handler takes it and no flush is\n * running.\n * @example\n * import { createComputation, createSignalNode, readComputation, readSignal, writeSignal } from \"@shadow-js/core/extend\";\n *\n * const price = createSignalNode<number>(10, Object.is);\n * const withTax = createComputation({ kind: \"memo\", fn: () => readSignal(price) * 1.2 });\n * createComputation({\n * kind: \"effect\",\n * fn: () => console.log(readComputation<number>(withTax)),\n * });\n * writeSignal(price, 20); // the effect logs 24 on the next flush\n * @see readComputation\n * @since 1.0.0\n */\nexport function createComputation(options: ComputationOptions): Computation {\n const owner = options.owner === undefined ? rt.owner : options.owner;\n if (options.kind === \"memo\") {\n const node = new Computation(\n owner,\n KIND_MEMO,\n FLAG_UNINIT | (options.allowWrites === true ? FLAG_ALLOW_WRITES : 0),\n options.fn,\n options.equals ?? Object.is,\n options.name,\n );\n if (options.lazy !== true) updateIfNecessary(node);\n return node;\n }\n const flags =\n (options.render === true ? FLAG_RENDER : 0) |\n (options.defer === true ? FLAG_DEFER : 0) |\n (options.lane === true ? FLAG_LANE : 0);\n const node = new Computation(owner, KIND_EFFECT, flags, options.fn, false, options.name);\n node.apply = options.apply ?? null;\n if (options.render === true) {\n // A suspended render effect keeps its DOM and waits on its own: its creator completes normally\n // (the branch that holds it decides whether to commit; see createBranches).\n recompute(node);\n } else {\n enqueue(node);\n schedule();\n }\n return node;\n}\n\n/**\n * Reads a memo node: brings it up to date if a source changed, subscribes the running computation\n * to it, and rethrows the error its last run stored. The accessor `useMemo` returns is this call.\n *\n * @param node A memo node from {@link createComputation}.\n * @returns The memo's current value.\n * @throws The error the memo's last computation threw, when no error handler took it. ShadowError\n * `CYCLE_DETECTED` when the memo is read while it computes.\n * @example\n * import { createComputation, createSignalNode, readComputation, readSignal } from \"@shadow-js/core/extend\";\n *\n * const first = createSignalNode<string>(\"Ada\", Object.is);\n * const greeting = createComputation({ kind: \"memo\", fn: () => `Hello, ${readSignal(first)}` });\n * console.log(readComputation<string>(greeting)); // \"Hello, Ada\"\n * @see createComputation\n * @since 1.0.0\n */\nexport function readComputation<T>(node: Computation): T {\n return readMemo<T>(node);\n}\n\n/**\n * Brings a memo or effect node up to date if one of its sources changed, without tracking it. Use\n * it to settle a node before reading its fields directly.\n *\n * @param node A node from {@link createComputation}. A node that is up to date is left as is.\n * @throws ShadowError `CYCLE_DETECTED` when a dependency is still computing (a cycle). An effect\n * that re-runs routes its errors to its owner's error handler.\n * @example\n * import { createComputation, createSignalNode, readSignal, refresh, writeSignal } from \"@shadow-js/core/extend\";\n *\n * const count = createSignalNode<number>(1, Object.is);\n * const doubled = createComputation({ kind: \"memo\", fn: () => readSignal(count) * 2 });\n * writeSignal(count, 2);\n * refresh(doubled);\n * console.log(doubled.value); // 4\n * @see readComputation\n * @since 1.0.0\n */\nexport function refresh(node: Computation): void {\n updateIfNecessary(node);\n}\n\n/**\n * Creates a plain owner: a scope that adopts the nodes and cleanups created under it and disposes\n * them together. The building block of `createRoot` and of control-flow branches.\n *\n * @param parent The owner to attach to and inherit context from. Undefined (or omitted) uses the\n * current owner; `null` creates a root with no context that only an explicit dispose ends.\n * @returns The new owner. Dispose it with `disposeOwner`.\n * @example\n * import { useEffect } from \"@shadow-js/core\";\n * import { createOwner, disposeOwner, runInScope } from \"@shadow-js/core/extend\";\n *\n * const branch = createOwner();\n * runInScope({ owner: branch, listener: null }, () => {\n * useEffect(() => console.log(\"branch effect\"));\n * });\n * disposeOwner(branch); // the effect and its cleanups are gone\n * @see runInScope\n * @since 1.0.0\n */\nexport function createOwner(parent?: Owner | null): Owner {\n return new OwnerClass(parent === undefined ? rt.owner : parent);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0GA,SAAgB,eAA6B;CAC3C,OAAO,GAAG;AACZ;;;;;;;;;;;;;;;;;AAkBA,SAAgB,kBAAsC;CACpD,OAAO,GAAG;AACZ;;;;;;;;;;;;;;;;AAiBA,SAAgB,aAAsB;CACpC,OAAO,GAAG,aAAa;AACzB;;;;;;;;;;;;;;;;;AAkBA,SAAgB,kBAA2B;CACzC,OACE,GAAG,cAAc,KACjB,GAAG,YAAY,QACf,GAAG,QAAQ,SAAA,MACV,GAAG,QAAQ,QAAA,SAA+B;AAE/C;;;;;;;;;;;;;;;;;;;;;;;;AAyBA,SAAgB,WACd,OACA,IACG;CACH,MAAM,QAAQ,GAAG;CACjB,MAAM,WAAW,GAAG;CACpB,IAAI;EACF,MAAM,YAAY,MAAM;EACxB,IAAI,cAAc,KAAA,GAAW,GAAG,QAAQ;EACxC,MAAM,eAAe,MAAM;EAC3B,IAAI,iBAAiB,KAAA,GAAW,GAAG,WAAW;EAC9C,OAAO,GAAG;CACZ,UAAU;EACR,GAAG,QAAQ;EACX,GAAG,WAAW;CAChB;AACF;;;;;;;;;;;;;;;;;;;;AAuBA,SAAgB,iBAAoB,OAAU,QAAmB,MAA0B;CACzF,OAAO,IAAI,OAAU,OAAO,QAAQ,IAAI;AAC1C;;;;;;;;;;;;;;;;;;;;;;AAqCA,SAAgB,YAAe,QAAmB,MAAS,SAAiC;CAC1F,IAAe,SAAS,gBAAgB,QAAQ,gBAAgB,GAC9D,MAAM,IAAI,YAAY,eAAe;CAEvC,MAAM,SAAS,OAAO;CACtB,IAAI,WAAW,SAAS,OAAO,OAAO,OAAO,IAAI,GAAG,OAAO;CAC3D,OAAO,QAAQ;CAEb,OAAO,eAAe,gBAAgB,QAAQ,MAAM,GAAG,SAAS,YAAY,MAAM,CAAC;CAErF,OAAO,MAAM;CACb,OAAO;AACT;;;;;;;;;;;;;;;;AAiBA,SAAgB,OAAO,QAAsB;CAC3C,IAAI,CAAC,aAAa,MAAM,GAAG;CAC3B,cAAc,MAAM;CACpB,SAAS;AACX;;;;;;;;;;;;;;;;;;;;;;;;;AAkGA,SAAgB,kBAAkB,SAA0C;CAC1E,MAAM,QAAQ,QAAQ,UAAU,KAAA,IAAY,GAAG,QAAQ,QAAQ;CAC/D,IAAI,QAAQ,SAAS,QAAQ;EAC3B,MAAM,OAAO,IAAI,YACf,OAAA,GAAA,MAEe,QAAQ,gBAAgB,OAAA,MAA2B,IAClE,QAAQ,IACR,QAAQ,UAAU,OAAO,IACzB,QAAQ,IACV;EACA,IAAI,QAAQ,SAAS,MAAM,kBAAkB,IAAI;EACjD,OAAO;CACT;CACA,MAAM,SACH,QAAQ,WAAW,OAAA,KAAqB,MACxC,QAAQ,UAAU,OAAA,KAAoB,MACtC,QAAQ,SAAS,OAAA,MAAmB;CACvC,MAAM,OAAO,IAAI,YAAY,OAAA,GAAoB,OAAO,QAAQ,IAAI,OAAO,QAAQ,IAAI;CACvF,KAAK,QAAQ,QAAQ,SAAS;CAC9B,IAAI,QAAQ,WAAW,MAGrB,UAAU,IAAI;MACT;EACL,QAAQ,IAAI;EACZ,SAAS;CACX;CACA,OAAO;AACT;;;;;;;;;;;;;;;;;;AAmBA,SAAgB,gBAAmB,MAAsB;CACvD,OAAO,SAAY,IAAI;AACzB;;;;;;;;;;;;;;;;;;;AAoBA,SAAgB,QAAQ,MAAyB;CAC/C,kBAAkB,IAAI;AACxB;;;;;;;;;;;;;;;;;;;;AAqBA,SAAgB,YAAY,QAA8B;CACxD,OAAO,IAAIA,MAAW,WAAW,KAAA,IAAY,GAAG,QAAQ,MAAM;AAChE"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { untrack } from "./ownership.js";
|
|
2
|
+
//#region src/external-store.ts
|
|
3
|
+
/**
|
|
4
|
+
* Reads a value that lives outside ShadowJS (a store, a browser API, an observable) as a reactive
|
|
5
|
+
* accessor. The one bridge for every external source, and the ShadowJS form of React's
|
|
6
|
+
* `useSyncExternalStore` and Solid's `from`.
|
|
7
|
+
*
|
|
8
|
+
* `subscribe(notify)` is called in an effect after the owner is created and returns the
|
|
9
|
+
* unsubscribe function, which runs when the owner is disposed. `getSnapshot()` is read untracked
|
|
10
|
+
* at creation, on every notification, and once after subscribing (so a change made between the
|
|
11
|
+
* first read and the subscription is not lost). The accessor updates only when the snapshot differs
|
|
12
|
+
* by `equals`. On the server nothing is subscribed: the accessor returns `getServerSnapshot()`.
|
|
13
|
+
*
|
|
14
|
+
* @param subscribe Registers `notify`; returns the unsubscribe function.
|
|
15
|
+
* @param getSnapshot Returns the current value. Reads inside it are not tracked.
|
|
16
|
+
* @param options Server snapshot, equality and a devtools name. Undefined uses the defaults.
|
|
17
|
+
* @returns An accessor of the current snapshot.
|
|
18
|
+
* @example
|
|
19
|
+
* import { useExternalStore } from "@shadow-js/core";
|
|
20
|
+
*
|
|
21
|
+
* function subscribe(notify: () => void): () => void {
|
|
22
|
+
* window.addEventListener("online", notify);
|
|
23
|
+
* window.addEventListener("offline", notify);
|
|
24
|
+
* return () => {
|
|
25
|
+
* window.removeEventListener("online", notify);
|
|
26
|
+
* window.removeEventListener("offline", notify);
|
|
27
|
+
* };
|
|
28
|
+
* }
|
|
29
|
+
*
|
|
30
|
+
* function OnlineBadge() {
|
|
31
|
+
* const online = useExternalStore(subscribe, () => navigator.onLine, { getServerSnapshot: () => true });
|
|
32
|
+
* return <span>{online() ? "online" : "offline"}</span>;
|
|
33
|
+
* }
|
|
34
|
+
* @see useSignal
|
|
35
|
+
* @since 1.0.0
|
|
36
|
+
*/
|
|
37
|
+
function useExternalStore(subscribe, getSnapshot, options) {
|
|
38
|
+
{
|
|
39
|
+
const snapshot = untrack(options?.getServerSnapshot ?? getSnapshot);
|
|
40
|
+
return () => snapshot;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
export { useExternalStore };
|
|
45
|
+
|
|
46
|
+
//# sourceMappingURL=external-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"external-store.js","names":[],"sources":["../../src/external-store.ts"],"sourcesContent":["import { useEffect } from \"./effect\";\nimport type { Accessor, Equals } from \"./extend\";\nimport { isHydrating } from \"./extend\";\nimport { untrack } from \"./ownership\";\nimport { useSignal } from \"./signal\";\n\n/** Options of {@link useExternalStore}. @since 1.0.0 */\nexport interface ExternalStoreOptions<T> {\n /**\n * The snapshot the server renders (there is no subscription there). Without it the server reads\n * `getSnapshot` once. Keep it equal to what the browser reads on its first render: a difference is\n * corrected only after subscribing, and no hydration test covers that case yet. While hydrating,\n * the browser's first render also uses it.\n * @default undefined (the server, and hydration, read `getSnapshot`)\n */\n getServerSnapshot?: () => T;\n /**\n * Decides whether a new snapshot is a change: the accessor updates only when it returns `false`.\n * @default Object.is\n */\n equals?: Equals<T>;\n /**\n * Label shown by devtools and diagnostics.\n * @default undefined (the store is unnamed)\n */\n name?: string;\n}\n\n/**\n * Reads a value that lives outside ShadowJS (a store, a browser API, an observable) as a reactive\n * accessor. The one bridge for every external source, and the ShadowJS form of React's\n * `useSyncExternalStore` and Solid's `from`.\n *\n * `subscribe(notify)` is called in an effect after the owner is created and returns the\n * unsubscribe function, which runs when the owner is disposed. `getSnapshot()` is read untracked\n * at creation, on every notification, and once after subscribing (so a change made between the\n * first read and the subscription is not lost). The accessor updates only when the snapshot differs\n * by `equals`. On the server nothing is subscribed: the accessor returns `getServerSnapshot()`.\n *\n * @param subscribe Registers `notify`; returns the unsubscribe function.\n * @param getSnapshot Returns the current value. Reads inside it are not tracked.\n * @param options Server snapshot, equality and a devtools name. Undefined uses the defaults.\n * @returns An accessor of the current snapshot.\n * @example\n * import { useExternalStore } from \"@shadow-js/core\";\n *\n * function subscribe(notify: () => void): () => void {\n * window.addEventListener(\"online\", notify);\n * window.addEventListener(\"offline\", notify);\n * return () => {\n * window.removeEventListener(\"online\", notify);\n * window.removeEventListener(\"offline\", notify);\n * };\n * }\n *\n * function OnlineBadge() {\n * const online = useExternalStore(subscribe, () => navigator.onLine, { getServerSnapshot: () => true });\n * return <span>{online() ? \"online\" : \"offline\"}</span>;\n * }\n * @see useSignal\n * @since 1.0.0\n */\nexport function useExternalStore<T>(\n subscribe: (notify: () => void) => () => void,\n getSnapshot: () => T,\n options?: ExternalStoreOptions<T>,\n): Accessor<T> {\n if (__SERVER__) {\n const snapshot = untrack(options?.getServerSnapshot ?? getSnapshot);\n return () => snapshot;\n }\n // While hydrating, the first render must equal the server markup: the server snapshot, when given.\n // The browser's snapshot is applied by the subscription below.\n const first =\n isHydrating() && options?.getServerSnapshot !== undefined\n ? options.getServerSnapshot\n : getSnapshot;\n const [value, setValue] = useSignal<T>(untrack(first), {\n equals: options?.equals ?? Object.is,\n ...(options?.name === undefined ? {} : { name: options.name }),\n });\n const sync = (): void => {\n const next = untrack(getSnapshot);\n setValue(() => next);\n };\n useEffect(() => {\n const unsubscribe = subscribe(sync);\n // Catch up with changes made between the first read and the subscription.\n sync();\n return unsubscribe;\n });\n return value;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8DA,SAAgB,iBACd,WACA,aACA,SACa;CACG;EACd,MAAM,WAAW,QAAQ,SAAS,qBAAqB,WAAW;EAClE,aAAa;CACf;AAsBF"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { batch, flushSync } from "./internal/scheduler.js";
|
|
2
|
+
import { catchError, createRoot, getOwner, onCleanup, onError, runWithOwner, untrack, useId } from "./ownership.js";
|
|
3
|
+
import { onMount, useEffect, useRenderEffect } from "./effect.js";
|
|
4
|
+
import { useSignal } from "./signal.js";
|
|
5
|
+
import { createContext, useContext } from "./context.js";
|
|
6
|
+
import { ResourceCacheContext, SuspenseContext } from "./suspense.js";
|
|
7
|
+
import { startTransition } from "./internal/concurrency.js";
|
|
8
|
+
import { useAction } from "./action.js";
|
|
9
|
+
import { cache } from "./cache.js";
|
|
10
|
+
import { isDev, isServer } from "./env.js";
|
|
11
|
+
import { useEvent } from "./events.js";
|
|
12
|
+
import { useExternalStore } from "./external-store.js";
|
|
13
|
+
import { useMemo } from "./memo.js";
|
|
14
|
+
import { indexArray, mapArray } from "./list.js";
|
|
15
|
+
import { useOptimistic } from "./optimistic.js";
|
|
16
|
+
import { children, mergeProps, splitProps } from "./props.js";
|
|
17
|
+
import { useReaction } from "./reaction.js";
|
|
18
|
+
import { useReducer } from "./reducer.js";
|
|
19
|
+
import { createRef, useImperativeHandle } from "./refs.js";
|
|
20
|
+
import { useResource } from "./resource.js";
|
|
21
|
+
import { useSelector } from "./selector.js";
|
|
22
|
+
import { snapshot, unwrap } from "./store/read.js";
|
|
23
|
+
import { produce, reconcile } from "./store/reconcile.js";
|
|
24
|
+
import { useStore } from "./store/use-store.js";
|
|
25
|
+
import { useDeferredValue, useTransition } from "./transitions.js";
|
|
26
|
+
export { ResourceCacheContext, SuspenseContext, batch, cache, catchError, children, createContext, createRef, createRoot, flushSync, getOwner, indexArray, isDev, isServer, mapArray, mergeProps, onCleanup, onError, onMount, produce, reconcile, runWithOwner, snapshot, splitProps, startTransition, untrack, unwrap, useAction, useContext, useDeferredValue, useEffect, useEvent, useExternalStore, useId, useImperativeHandle, useMemo, useOptimistic, useReaction, useReducer, useRenderEffect, useResource, useSelector, useSignal, useStore, useTransition };
|