@shadow-js/core 0.1.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/LICENSE +2 -2
- package/README.md +134 -331
- package/dist/dev/action.d.ts +34 -0
- package/dist/dev/action.js +89 -0
- package/dist/dev/action.js.map +1 -0
- package/dist/dev/cache.d.ts +33 -0
- package/dist/dev/cache.js +61 -0
- package/dist/dev/cache.js.map +1 -0
- package/dist/dev/context.d.ts +104 -0
- package/dist/dev/context.js +110 -0
- package/dist/dev/context.js.map +1 -0
- package/dist/dev/effect.d.ts +120 -0
- package/dist/dev/effect.js +110 -0
- package/dist/dev/effect.js.map +1 -0
- package/dist/dev/env.d.ts +27 -0
- package/dist/dev/env.js +30 -0
- package/dist/dev/env.js.map +1 -0
- package/dist/dev/events.d.ts +26 -0
- package/dist/dev/events.js +32 -0
- package/dist/dev/events.js.map +1 -0
- package/dist/dev/extend/index.d.ts +329 -0
- package/dist/dev/extend/index.js +316 -0
- package/dist/dev/extend/index.js.map +1 -0
- package/dist/dev/external-store.d.ts +58 -0
- package/dist/dev/external-store.js +59 -0
- package/dist/dev/external-store.js.map +1 -0
- package/dist/dev/index.d.ts +29 -0
- package/dist/dev/index.js +26 -0
- package/dist/dev/internal/concurrency.d.ts +211 -0
- package/dist/dev/internal/concurrency.js +631 -0
- package/dist/dev/internal/concurrency.js.map +1 -0
- package/dist/dev/internal/devtools.d.ts +69 -0
- package/dist/dev/internal/devtools.js +76 -0
- package/dist/dev/internal/devtools.js.map +1 -0
- package/dist/dev/internal/diagnostics.d.ts +88 -0
- package/dist/dev/internal/diagnostics.js +101 -0
- package/dist/dev/internal/diagnostics.js.map +1 -0
- package/dist/dev/internal/dispose.d.ts +26 -0
- package/dist/dev/internal/dispose.js +48 -0
- package/dist/dev/internal/dispose.js.map +1 -0
- package/dist/dev/internal/flags.js +10 -0
- package/dist/dev/internal/flags.js.map +1 -0
- package/dist/dev/internal/graph.d.ts +231 -0
- package/dist/dev/internal/graph.js +577 -0
- package/dist/dev/internal/graph.js.map +1 -0
- package/dist/dev/internal/hydration-flag.d.ts +31 -0
- package/dist/dev/internal/hydration-flag.js +45 -0
- package/dist/dev/internal/hydration-flag.js.map +1 -0
- package/dist/dev/internal/messages.d.ts +54 -0
- package/dist/dev/internal/messages.js +53 -0
- package/dist/dev/internal/messages.js.map +1 -0
- package/dist/dev/internal/owner.d.ts +140 -0
- package/dist/dev/internal/owner.js +289 -0
- package/dist/dev/internal/owner.js.map +1 -0
- package/dist/dev/internal/queue.js +45 -0
- package/dist/dev/internal/queue.js.map +1 -0
- package/dist/dev/internal/scheduler.d.ts +111 -0
- package/dist/dev/internal/scheduler.js +286 -0
- package/dist/dev/internal/scheduler.js.map +1 -0
- package/dist/dev/internal/state.d.ts +139 -0
- package/dist/dev/internal/state.js +27 -0
- package/dist/dev/internal/state.js.map +1 -0
- package/dist/dev/internal/suspension.d.ts +41 -0
- package/dist/dev/internal/suspension.js +44 -0
- package/dist/dev/internal/suspension.js.map +1 -0
- package/dist/dev/internal/types.d.ts +44 -0
- package/dist/dev/list.d.ts +91 -0
- package/dist/dev/list.js +281 -0
- package/dist/dev/list.js.map +1 -0
- package/dist/dev/memo.d.ts +58 -0
- package/dist/dev/memo.js +40 -0
- package/dist/dev/memo.js.map +1 -0
- package/dist/dev/optimistic.d.ts +24 -0
- package/dist/dev/optimistic.js +44 -0
- package/dist/dev/optimistic.js.map +1 -0
- package/dist/dev/ownership.d.ts +202 -0
- package/dist/dev/ownership.js +260 -0
- package/dist/dev/ownership.js.map +1 -0
- package/dist/dev/props.d.ts +132 -0
- package/dist/dev/props.js +281 -0
- package/dist/dev/props.js.map +1 -0
- package/dist/dev/reaction.d.ts +27 -0
- package/dist/dev/reaction.js +60 -0
- package/dist/dev/reaction.js.map +1 -0
- package/dist/dev/reducer.d.ts +39 -0
- package/dist/dev/reducer.js +38 -0
- package/dist/dev/reducer.js.map +1 -0
- package/dist/dev/refs.d.ts +50 -0
- package/dist/dev/refs.js +57 -0
- package/dist/dev/refs.js.map +1 -0
- package/dist/dev/resource.d.ts +133 -0
- package/dist/dev/resource.js +176 -0
- package/dist/dev/resource.js.map +1 -0
- package/dist/dev/selector.d.ts +32 -0
- package/dist/dev/selector.js +117 -0
- package/dist/dev/selector.js.map +1 -0
- package/dist/dev/signal.d.ts +40 -0
- package/dist/dev/signal.js +38 -0
- package/dist/dev/signal.js.map +1 -0
- package/dist/dev/store/index.d.ts +5 -0
- package/dist/dev/store/index.js +4 -0
- package/dist/dev/store/nodes.js +191 -0
- package/dist/dev/store/nodes.js.map +1 -0
- package/dist/dev/store/proxies.js +159 -0
- package/dist/dev/store/proxies.js.map +1 -0
- package/dist/dev/store/read.d.ts +37 -0
- package/dist/dev/store/read.js +71 -0
- package/dist/dev/store/read.js.map +1 -0
- package/dist/dev/store/reconcile.d.ts +71 -0
- package/dist/dev/store/reconcile.js +185 -0
- package/dist/dev/store/reconcile.js.map +1 -0
- package/dist/dev/store/types.d.ts +104 -0
- package/dist/dev/store/use-store.d.ts +58 -0
- package/dist/dev/store/use-store.js +167 -0
- package/dist/dev/store/use-store.js.map +1 -0
- package/dist/dev/suspense.d.ts +99 -0
- package/dist/dev/suspense.js +44 -0
- package/dist/dev/suspense.js.map +1 -0
- package/dist/dev/transitions.d.ts +75 -0
- package/dist/dev/transitions.js +100 -0
- package/dist/dev/transitions.js.map +1 -0
- package/dist/prod/action.js +2 -0
- package/dist/prod/action.js.map +1 -0
- package/dist/prod/cache.js +2 -0
- package/dist/prod/cache.js.map +1 -0
- package/dist/prod/context.js +2 -0
- package/dist/prod/context.js.map +1 -0
- package/dist/prod/effect.js +2 -0
- package/dist/prod/effect.js.map +1 -0
- package/dist/prod/env.js +2 -0
- package/dist/prod/env.js.map +1 -0
- package/dist/prod/events.js +2 -0
- package/dist/prod/events.js.map +1 -0
- package/dist/prod/extend/index.js +2 -0
- package/dist/prod/extend/index.js.map +1 -0
- package/dist/prod/external-store.js +2 -0
- package/dist/prod/external-store.js.map +1 -0
- package/dist/prod/index.js +1 -0
- package/dist/prod/internal/concurrency.js +2 -0
- package/dist/prod/internal/concurrency.js.map +1 -0
- package/dist/prod/internal/devtools.js +2 -0
- package/dist/prod/internal/devtools.js.map +1 -0
- package/dist/prod/internal/diagnostics.js +2 -0
- package/dist/prod/internal/diagnostics.js.map +1 -0
- package/dist/prod/internal/dispose.js +2 -0
- package/dist/prod/internal/dispose.js.map +1 -0
- package/dist/prod/internal/flags.js +2 -0
- package/dist/prod/internal/flags.js.map +1 -0
- package/dist/prod/internal/graph.js +2 -0
- package/dist/prod/internal/graph.js.map +1 -0
- package/dist/prod/internal/hydration-flag.js +2 -0
- package/dist/prod/internal/hydration-flag.js.map +1 -0
- package/dist/prod/internal/messages.js +2 -0
- package/dist/prod/internal/messages.js.map +1 -0
- package/dist/prod/internal/owner.js +2 -0
- package/dist/prod/internal/owner.js.map +1 -0
- package/dist/prod/internal/queue.js +2 -0
- package/dist/prod/internal/queue.js.map +1 -0
- package/dist/prod/internal/scheduler.js +2 -0
- package/dist/prod/internal/scheduler.js.map +1 -0
- package/dist/prod/internal/state.js +2 -0
- package/dist/prod/internal/state.js.map +1 -0
- package/dist/prod/internal/suspension.js +2 -0
- package/dist/prod/internal/suspension.js.map +1 -0
- package/dist/prod/list.js +2 -0
- package/dist/prod/list.js.map +1 -0
- package/dist/prod/memo.js +2 -0
- package/dist/prod/memo.js.map +1 -0
- package/dist/prod/optimistic.js +2 -0
- package/dist/prod/optimistic.js.map +1 -0
- package/dist/prod/ownership.js +2 -0
- package/dist/prod/ownership.js.map +1 -0
- package/dist/prod/props.js +2 -0
- package/dist/prod/props.js.map +1 -0
- package/dist/prod/reaction.js +2 -0
- package/dist/prod/reaction.js.map +1 -0
- package/dist/prod/reducer.js +2 -0
- package/dist/prod/reducer.js.map +1 -0
- package/dist/prod/refs.js +2 -0
- package/dist/prod/refs.js.map +1 -0
- package/dist/prod/resource.js +2 -0
- package/dist/prod/resource.js.map +1 -0
- package/dist/prod/selector.js +2 -0
- package/dist/prod/selector.js.map +1 -0
- package/dist/prod/signal.js +2 -0
- package/dist/prod/signal.js.map +1 -0
- package/dist/prod/store/index.js +1 -0
- package/dist/prod/store/nodes.js +2 -0
- package/dist/prod/store/nodes.js.map +1 -0
- package/dist/prod/store/proxies.js +2 -0
- package/dist/prod/store/proxies.js.map +1 -0
- package/dist/prod/store/read.js +2 -0
- package/dist/prod/store/read.js.map +1 -0
- package/dist/prod/store/reconcile.js +2 -0
- package/dist/prod/store/reconcile.js.map +1 -0
- package/dist/prod/store/use-store.js +2 -0
- package/dist/prod/store/use-store.js.map +1 -0
- package/dist/prod/suspense.js +2 -0
- package/dist/prod/suspense.js.map +1 -0
- package/dist/prod/transitions.js +2 -0
- package/dist/prod/transitions.js.map +1 -0
- package/dist/server/action.js +2 -0
- package/dist/server/action.js.map +1 -0
- package/dist/server/cache.js +2 -0
- package/dist/server/cache.js.map +1 -0
- package/dist/server/context.js +2 -0
- package/dist/server/context.js.map +1 -0
- package/dist/server/effect.js +2 -0
- package/dist/server/effect.js.map +1 -0
- package/dist/server/env.js +2 -0
- package/dist/server/env.js.map +1 -0
- package/dist/server/events.js +2 -0
- package/dist/server/events.js.map +1 -0
- package/dist/server/extend/index.js +2 -0
- package/dist/server/extend/index.js.map +1 -0
- package/dist/server/external-store.js +2 -0
- package/dist/server/external-store.js.map +1 -0
- package/dist/server/index.js +1 -0
- package/dist/server/internal/concurrency.js +2 -0
- package/dist/server/internal/concurrency.js.map +1 -0
- package/dist/server/internal/devtools.js +2 -0
- package/dist/server/internal/devtools.js.map +1 -0
- package/dist/server/internal/diagnostics.js +2 -0
- package/dist/server/internal/diagnostics.js.map +1 -0
- package/dist/server/internal/dispose.js +2 -0
- package/dist/server/internal/dispose.js.map +1 -0
- package/dist/server/internal/flags.js +2 -0
- package/dist/server/internal/flags.js.map +1 -0
- package/dist/server/internal/graph.js +2 -0
- package/dist/server/internal/graph.js.map +1 -0
- package/dist/server/internal/hydration-flag.js +2 -0
- package/dist/server/internal/hydration-flag.js.map +1 -0
- package/dist/server/internal/messages.js +2 -0
- package/dist/server/internal/messages.js.map +1 -0
- package/dist/server/internal/owner.js +2 -0
- package/dist/server/internal/owner.js.map +1 -0
- package/dist/server/internal/queue.js +2 -0
- package/dist/server/internal/queue.js.map +1 -0
- package/dist/server/internal/scheduler.js +2 -0
- package/dist/server/internal/scheduler.js.map +1 -0
- package/dist/server/internal/state.js +2 -0
- package/dist/server/internal/state.js.map +1 -0
- package/dist/server/internal/suspension.js +2 -0
- package/dist/server/internal/suspension.js.map +1 -0
- package/dist/server/list.js +2 -0
- package/dist/server/list.js.map +1 -0
- package/dist/server/memo.js +2 -0
- package/dist/server/memo.js.map +1 -0
- package/dist/server/optimistic.js +2 -0
- package/dist/server/optimistic.js.map +1 -0
- package/dist/server/ownership.js +2 -0
- package/dist/server/ownership.js.map +1 -0
- package/dist/server/props.js +2 -0
- package/dist/server/props.js.map +1 -0
- package/dist/server/reaction.js +2 -0
- package/dist/server/reaction.js.map +1 -0
- package/dist/server/reducer.js +2 -0
- package/dist/server/reducer.js.map +1 -0
- package/dist/server/refs.js +2 -0
- package/dist/server/refs.js.map +1 -0
- package/dist/server/resource.js +2 -0
- package/dist/server/resource.js.map +1 -0
- package/dist/server/selector.js +2 -0
- package/dist/server/selector.js.map +1 -0
- package/dist/server/signal.js +2 -0
- package/dist/server/signal.js.map +1 -0
- package/dist/server/store/index.js +1 -0
- package/dist/server/store/nodes.js +2 -0
- package/dist/server/store/nodes.js.map +1 -0
- package/dist/server/store/proxies.js +2 -0
- package/dist/server/store/proxies.js.map +1 -0
- package/dist/server/store/read.js +2 -0
- package/dist/server/store/read.js.map +1 -0
- package/dist/server/store/reconcile.js +2 -0
- package/dist/server/store/reconcile.js.map +1 -0
- package/dist/server/store/use-store.js +2 -0
- package/dist/server/store/use-store.js.map +1 -0
- package/dist/server/suspense.js +2 -0
- package/dist/server/suspense.js.map +1 -0
- package/dist/server/transitions.js +2 -0
- package/dist/server/transitions.js.map +1 -0
- package/dist/server-dev/action.js +89 -0
- package/dist/server-dev/action.js.map +1 -0
- package/dist/server-dev/cache.js +61 -0
- package/dist/server-dev/cache.js.map +1 -0
- package/dist/server-dev/context.js +107 -0
- package/dist/server-dev/context.js.map +1 -0
- package/dist/server-dev/effect.js +55 -0
- package/dist/server-dev/effect.js.map +1 -0
- package/dist/server-dev/env.js +30 -0
- package/dist/server-dev/env.js.map +1 -0
- package/dist/server-dev/events.js +32 -0
- package/dist/server-dev/events.js.map +1 -0
- package/dist/server-dev/extend/index.js +316 -0
- package/dist/server-dev/extend/index.js.map +1 -0
- package/dist/server-dev/external-store.js +45 -0
- package/dist/server-dev/external-store.js.map +1 -0
- package/dist/server-dev/index.js +26 -0
- package/dist/server-dev/internal/concurrency.js +632 -0
- package/dist/server-dev/internal/concurrency.js.map +1 -0
- package/dist/server-dev/internal/devtools.js +76 -0
- package/dist/server-dev/internal/devtools.js.map +1 -0
- package/dist/server-dev/internal/diagnostics.js +101 -0
- package/dist/server-dev/internal/diagnostics.js.map +1 -0
- package/dist/server-dev/internal/dispose.js +48 -0
- package/dist/server-dev/internal/dispose.js.map +1 -0
- package/dist/server-dev/internal/flags.js +10 -0
- package/dist/server-dev/internal/flags.js.map +1 -0
- package/dist/server-dev/internal/graph.js +577 -0
- package/dist/server-dev/internal/graph.js.map +1 -0
- package/dist/server-dev/internal/hydration-flag.js +45 -0
- package/dist/server-dev/internal/hydration-flag.js.map +1 -0
- package/dist/server-dev/internal/messages.js +53 -0
- package/dist/server-dev/internal/messages.js.map +1 -0
- package/dist/server-dev/internal/owner.js +289 -0
- package/dist/server-dev/internal/owner.js.map +1 -0
- package/dist/server-dev/internal/queue.js +45 -0
- package/dist/server-dev/internal/queue.js.map +1 -0
- package/dist/server-dev/internal/scheduler.js +286 -0
- package/dist/server-dev/internal/scheduler.js.map +1 -0
- package/dist/server-dev/internal/state.js +27 -0
- package/dist/server-dev/internal/state.js.map +1 -0
- package/dist/server-dev/internal/suspension.js +44 -0
- package/dist/server-dev/internal/suspension.js.map +1 -0
- package/dist/server-dev/list.js +281 -0
- package/dist/server-dev/list.js.map +1 -0
- package/dist/server-dev/memo.js +40 -0
- package/dist/server-dev/memo.js.map +1 -0
- package/dist/server-dev/optimistic.js +44 -0
- package/dist/server-dev/optimistic.js.map +1 -0
- package/dist/server-dev/ownership.js +260 -0
- package/dist/server-dev/ownership.js.map +1 -0
- package/dist/server-dev/props.js +281 -0
- package/dist/server-dev/props.js.map +1 -0
- package/dist/server-dev/reaction.js +60 -0
- package/dist/server-dev/reaction.js.map +1 -0
- package/dist/server-dev/reducer.js +38 -0
- package/dist/server-dev/reducer.js.map +1 -0
- package/dist/server-dev/refs.js +57 -0
- package/dist/server-dev/refs.js.map +1 -0
- package/dist/server-dev/resource.js +175 -0
- package/dist/server-dev/resource.js.map +1 -0
- package/dist/server-dev/selector.js +81 -0
- package/dist/server-dev/selector.js.map +1 -0
- package/dist/server-dev/signal.js +38 -0
- package/dist/server-dev/signal.js.map +1 -0
- package/dist/server-dev/store/index.js +4 -0
- package/dist/server-dev/store/nodes.js +191 -0
- package/dist/server-dev/store/nodes.js.map +1 -0
- package/dist/server-dev/store/proxies.js +159 -0
- package/dist/server-dev/store/proxies.js.map +1 -0
- package/dist/server-dev/store/read.js +71 -0
- package/dist/server-dev/store/read.js.map +1 -0
- package/dist/server-dev/store/reconcile.js +185 -0
- package/dist/server-dev/store/reconcile.js.map +1 -0
- package/dist/server-dev/store/use-store.js +167 -0
- package/dist/server-dev/store/use-store.js.map +1 -0
- package/dist/server-dev/suspense.js +44 -0
- package/dist/server-dev/suspense.js.map +1 -0
- package/dist/server-dev/transitions.js +85 -0
- package/dist/server-dev/transitions.js.map +1 -0
- package/package.json +111 -50
- package/dist/chunk-6Y3CCWOY.js +0 -887
- package/dist/chunk-6Y3CCWOY.js.map +0 -1
- package/dist/chunk-B5FTYDUG.js +0 -3
- package/dist/chunk-B5FTYDUG.js.map +0 -1
- package/dist/index.cjs +0 -2000
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -609
- package/dist/index.d.ts +0 -609
- package/dist/index.js +0 -1094
- package/dist/index.js.map +0 -1
- package/dist/jsx-Bugil6CJ.d.cts +0 -151
- package/dist/jsx-Bugil6CJ.d.ts +0 -151
- package/dist/jsx-dev-runtime.cjs +0 -742
- package/dist/jsx-dev-runtime.cjs.map +0 -1
- package/dist/jsx-dev-runtime.d.cts +0 -2
- package/dist/jsx-dev-runtime.d.ts +0 -2
- package/dist/jsx-dev-runtime.js +0 -4
- package/dist/jsx-dev-runtime.js.map +0 -1
- package/dist/jsx-runtime.cjs +0 -742
- package/dist/jsx-runtime.cjs.map +0 -1
- package/dist/jsx-runtime.d.cts +0 -36
- package/dist/jsx-runtime.d.ts +0 -36
- package/dist/jsx-runtime.js +0 -4
- package/dist/jsx-runtime.js.map +0 -1
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { FLAG_PARKED, FLAG_TRANSITION } from "./flags.js";
|
|
2
|
+
import { rt } from "./state.js";
|
|
3
|
+
// src/internal/queue.ts
|
|
4
|
+
/** Urgent effects waiting for the next flush, split by phase (render effects run first). */
|
|
5
|
+
const renderQueue = [];
|
|
6
|
+
const userQueue = [];
|
|
7
|
+
/**
|
|
8
|
+
* Puts an effect where it belongs. `lane` says the write that marked it was a transition write;
|
|
9
|
+
* effects with FLAG_LANE always go to the lane. Lane and paused effects are handed to the transition
|
|
10
|
+
* hooks (their queues live there); everything else is queued here, with no hook involved.
|
|
11
|
+
*/
|
|
12
|
+
function place(effect, lane) {
|
|
13
|
+
if ((effect.flags & 2) !== 0) return;
|
|
14
|
+
const inLane = lane || (effect.flags & 512) !== 0;
|
|
15
|
+
const hooks = rt.hooks;
|
|
16
|
+
if (hooks !== null && (inLane || effect.pauses > 0)) {
|
|
17
|
+
hooks.place(effect, inLane);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
effect.flags = effect.flags & ~(FLAG_PARKED | FLAG_TRANSITION) | 2;
|
|
21
|
+
if ((effect.flags & 16) !== 0) renderQueue.push(effect);
|
|
22
|
+
else userQueue.push(effect);
|
|
23
|
+
}
|
|
24
|
+
/** Queues an effect for the current write: a transition write joins the lane. */
|
|
25
|
+
function enqueue(effect) {
|
|
26
|
+
place(effect, rt.inTransition);
|
|
27
|
+
}
|
|
28
|
+
/** True when an urgent effect is waiting. */
|
|
29
|
+
function hasPending() {
|
|
30
|
+
return renderQueue.length > 0 || userQueue.length > 0;
|
|
31
|
+
}
|
|
32
|
+
/** Drops everything queued (used when a flush is aborted by the loop guard). */
|
|
33
|
+
function clearQueues() {
|
|
34
|
+
for (const queue of [renderQueue, userQueue]) {
|
|
35
|
+
for (const effect of queue) {
|
|
36
|
+
effect.flags &= ~(2 | FLAG_TRANSITION);
|
|
37
|
+
effect.state = 0;
|
|
38
|
+
}
|
|
39
|
+
queue.length = 0;
|
|
40
|
+
}
|
|
41
|
+
rt.hooks?.clear();
|
|
42
|
+
}
|
|
43
|
+
export { clearQueues, enqueue, hasPending, place, renderQueue, userQueue };
|
|
44
|
+
|
|
45
|
+
//# sourceMappingURL=queue.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"queue.js","names":[],"sources":["../../../src/internal/queue.ts"],"sourcesContent":["import { CLEAN, FLAG_LANE, FLAG_PARKED, FLAG_QUEUED, FLAG_RENDER, FLAG_TRANSITION } from \"./flags\";\nimport type { Computation } from \"./graph\";\nimport { rt } from \"./state\";\n\n/** Urgent effects waiting for the next flush, split by phase (render effects run first). */\nexport const renderQueue: Computation[] = [];\nexport const userQueue: Computation[] = [];\n\n/**\n * Puts an effect where it belongs. `lane` says the write that marked it was a transition write;\n * effects with FLAG_LANE always go to the lane. Lane and paused effects are handed to the transition\n * hooks (their queues live there); everything else is queued here, with no hook involved.\n */\nexport function place(effect: Computation, lane: boolean): void {\n if ((effect.flags & FLAG_QUEUED) !== 0) return;\n const inLane = lane || (effect.flags & FLAG_LANE) !== 0;\n const hooks = rt.hooks;\n if (hooks !== null && (inLane || effect.pauses > 0)) {\n hooks.place(effect, inLane);\n return;\n }\n effect.flags = (effect.flags & ~(FLAG_PARKED | FLAG_TRANSITION)) | FLAG_QUEUED;\n if ((effect.flags & FLAG_RENDER) !== 0) renderQueue.push(effect);\n else userQueue.push(effect);\n}\n\n/** Queues an effect for the current write: a transition write joins the lane. */\nexport function enqueue(effect: Computation): void {\n place(effect, rt.inTransition);\n}\n\n/** True when an urgent effect is waiting. */\nexport function hasPending(): boolean {\n return renderQueue.length > 0 || userQueue.length > 0;\n}\n\n/** Drops everything queued (used when a flush is aborted by the loop guard). */\nexport function clearQueues(): void {\n for (const queue of [renderQueue, userQueue]) {\n for (const effect of queue) {\n effect.flags &= ~(FLAG_QUEUED | FLAG_TRANSITION);\n // Back to CLEAN so the next write can notify (and re-queue) this effect again.\n effect.state = CLEAN;\n }\n queue.length = 0;\n }\n rt.hooks?.clear();\n}\n"],"mappings":";;;;AAKA,MAAa,cAA6B,CAAC;AAC3C,MAAa,YAA2B,CAAC;;;;;;AAOzC,SAAgB,MAAM,QAAqB,MAAqB;CAC9D,KAAK,OAAO,QAAA,OAAyB,GAAG;CACxC,MAAM,SAAS,SAAS,OAAO,QAAA,SAAuB;CACtD,MAAM,QAAQ,GAAG;CACjB,IAAI,UAAU,SAAS,UAAU,OAAO,SAAS,IAAI;EACnD,MAAM,MAAM,QAAQ,MAAM;EAC1B;CACF;CACA,OAAO,QAAS,OAAO,QAAQ,EAAE,cAAc,mBAAA;CAC/C,KAAK,OAAO,QAAA,QAAyB,GAAG,YAAY,KAAK,MAAM;MAC1D,UAAU,KAAK,MAAM;AAC5B;;AAGA,SAAgB,QAAQ,QAA2B;CACjD,MAAM,QAAQ,GAAG,YAAY;AAC/B;;AAGA,SAAgB,aAAsB;CACpC,OAAO,YAAY,SAAS,KAAK,UAAU,SAAS;AACtD;;AAGA,SAAgB,cAAoB;CAClC,KAAK,MAAM,SAAS,CAAC,aAAa,SAAS,GAAG;EAC5C,KAAK,MAAM,UAAU,OAAO;GAC1B,OAAO,SAAS,EAAA,IAAgB;GAEhC,OAAO,QAAA;EACT;EACA,MAAM,SAAS;CACjB;CACA,GAAG,OAAO,MAAM;AAClB"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runs every pending effect now: what the scheduled microtask does. Drains the queues until nothing
|
|
3
|
+
* is pending; render effects always run before user effects, parents before children, siblings by
|
|
4
|
+
* creation, and effects woken by a write in the flush run in a following pass. Errors nobody handled
|
|
5
|
+
* are collected and thrown once the queues are empty, so one failing effect never wedges the rest.
|
|
6
|
+
* Does nothing when a flush is already running. Transition-lane effects are not run here: they wait
|
|
7
|
+
* for the lane. Prefer {@link flushSync} in application code.
|
|
8
|
+
*
|
|
9
|
+
* @throws The error of an effect that no error handler caught; several are thrown together as an
|
|
10
|
+
* `AggregateError`.
|
|
11
|
+
* @throws ShadowError `INFINITE_LOOP` when effects keep waking each other for more than 1000 passes:
|
|
12
|
+
* the queued effects are dropped and the flush stops.
|
|
13
|
+
* @example
|
|
14
|
+
* import { flush, writeSignal, type SignalNode } from "@shadow-js/core/extend";
|
|
15
|
+
*
|
|
16
|
+
* export function setNow(signal: SignalNode<number>, value: number): void {
|
|
17
|
+
* writeSignal(signal, value);
|
|
18
|
+
* flush(); // the effects that read the signal have run
|
|
19
|
+
* }
|
|
20
|
+
* @see flushSync
|
|
21
|
+
* @since 1.0.0
|
|
22
|
+
*/
|
|
23
|
+
export declare function flush(): void;
|
|
24
|
+
/**
|
|
25
|
+
* Requests a flush in a microtask: call it after marking observers stale by hand, as
|
|
26
|
+
* {@link markObservers} does not. Does nothing when a microtask flush is already due, inside a
|
|
27
|
+
* batch or group (which flushes or schedules on exit), during a flush (which picks the work up), or
|
|
28
|
+
* during a transition (its effects wait for the lane). An error thrown by that flush has no caller
|
|
29
|
+
* to reach: it goes to the global `reportError`, or becomes an unhandled promise rejection.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* import { markObservers, schedule, type SourceNode } from "@shadow-js/core/extend";
|
|
33
|
+
*
|
|
34
|
+
* export function touch(source: SourceNode): void {
|
|
35
|
+
* markObservers(source);
|
|
36
|
+
* schedule(); // the effects that read `source` run in a microtask
|
|
37
|
+
* }
|
|
38
|
+
* @see notify
|
|
39
|
+
* @since 1.0.0
|
|
40
|
+
*/
|
|
41
|
+
export declare function schedule(): void;
|
|
42
|
+
/**
|
|
43
|
+
* Groups writes like {@link batch} but never flushes synchronously: when the outermost group ends,
|
|
44
|
+
* the effects wait for the microtask (or an enclosing batch / `flushSync`), exactly as after a
|
|
45
|
+
* single signal write. Store setters use it so that one `setStore(...)` behaves like one `set()`;
|
|
46
|
+
* use it for a primitive whose one call makes several writes.
|
|
47
|
+
*
|
|
48
|
+
* @param fn The writes to group. Its throw is rethrown after the group ends.
|
|
49
|
+
* @returns What `fn` returns.
|
|
50
|
+
* @example
|
|
51
|
+
* import { useSignal } from "@shadow-js/core";
|
|
52
|
+
* import { group } from "@shadow-js/core/extend";
|
|
53
|
+
*
|
|
54
|
+
* const [first, setFirst] = useSignal("Ada");
|
|
55
|
+
* const [last, setLast] = useSignal("Lovelace");
|
|
56
|
+
* group(() => {
|
|
57
|
+
* setFirst("Grace");
|
|
58
|
+
* setLast("Hopper");
|
|
59
|
+
* }); // effects reading both run once, in the next microtask
|
|
60
|
+
* @see batch
|
|
61
|
+
* @since 1.0.0
|
|
62
|
+
*/
|
|
63
|
+
export declare function group<T>(fn: () => T): T;
|
|
64
|
+
/**
|
|
65
|
+
* Runs `fn` with effects deferred until the outermost batch ends, then runs them synchronously, so
|
|
66
|
+
* several writes wake each effect once. Reads inside the batch always see fresh values (G-6). Inside
|
|
67
|
+
* a running flush the effects are left to that flush.
|
|
68
|
+
*
|
|
69
|
+
* @param fn The writes to batch.
|
|
70
|
+
* @returns What `fn` returns.
|
|
71
|
+
* @throws What `fn` throws, after the flush. An error of the flush is thrown as {@link flush} throws
|
|
72
|
+
* it (including ShadowError `INFINITE_LOOP`); when `fn` threw as well, both are thrown together as an
|
|
73
|
+
* `AggregateError`.
|
|
74
|
+
* @example
|
|
75
|
+
* import { batch, useSignal } from "@shadow-js/core";
|
|
76
|
+
*
|
|
77
|
+
* const [first, setFirst] = useSignal("Ada");
|
|
78
|
+
* const [last, setLast] = useSignal("Lovelace");
|
|
79
|
+
* batch(() => {
|
|
80
|
+
* setFirst("Grace");
|
|
81
|
+
* setLast("Hopper");
|
|
82
|
+
* }); // effects reading both have run once, here
|
|
83
|
+
* @see flushSync
|
|
84
|
+
* @since 1.0.0
|
|
85
|
+
*/
|
|
86
|
+
export declare function batch<T>(fn: () => T): T;
|
|
87
|
+
/**
|
|
88
|
+
* Runs `fn` (optional) in a batch and runs every pending effect before returning, so the DOM and
|
|
89
|
+
* other effects are up to date on the next line. Use it in tests and before reading layout. Called
|
|
90
|
+
* while a flush is running (from inside an effect), it only runs `fn` and leaves the effects to that
|
|
91
|
+
* flush; development builds warn `FLUSH_IN_EFFECT`.
|
|
92
|
+
*
|
|
93
|
+
* @param fn The writes to make first. Undefined only flushes.
|
|
94
|
+
* @returns What `fn` returns, or undefined without `fn`.
|
|
95
|
+
* @throws What `fn` throws, after the flush. An error of the flush is thrown as {@link flush} throws
|
|
96
|
+
* it (including ShadowError `INFINITE_LOOP`); when `fn` threw as well, both are thrown together as an
|
|
97
|
+
* `AggregateError`.
|
|
98
|
+
* @example
|
|
99
|
+
* import { createRoot, flushSync, useEffect, useSignal } from "@shadow-js/core";
|
|
100
|
+
*
|
|
101
|
+
* createRoot(() => {
|
|
102
|
+
* const [count, setCount] = useSignal(0);
|
|
103
|
+
* useEffect(() => console.log(count()));
|
|
104
|
+
* flushSync(); // logs 0
|
|
105
|
+
* flushSync(() => setCount(1)); // logs 1 before this line ends
|
|
106
|
+
* });
|
|
107
|
+
* @see batch
|
|
108
|
+
* @since 1.0.0
|
|
109
|
+
*/
|
|
110
|
+
export declare function flushSync<T = void>(fn?: () => T): T;
|
|
111
|
+
//# sourceMappingURL=scheduler.d.ts.map
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { DEVTOOLS_EVENT, report } from "./devtools.js";
|
|
2
|
+
import { ShadowError, warn } from "./diagnostics.js";
|
|
3
|
+
import { FLAG_PARKED, FLAG_TRANSITION } from "./flags.js";
|
|
4
|
+
import { rt } from "./state.js";
|
|
5
|
+
import { handleError } from "./owner.js";
|
|
6
|
+
import { clearQueues, hasPending, renderQueue, userQueue } from "./queue.js";
|
|
7
|
+
import { recompute, updateIfNecessary } from "./graph.js";
|
|
8
|
+
// src/internal/scheduler.ts
|
|
9
|
+
/**
|
|
10
|
+
* Flush passes allowed before the loop guard aborts (R-CODE-12).
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
const MAX_FLUSH_PASSES = 1e3;
|
|
14
|
+
let microtaskScheduled = false;
|
|
15
|
+
/**
|
|
16
|
+
* Sort order of queued effects: parents before children (by depth), siblings by creation (G-16).
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
function byCreation(a, b) {
|
|
20
|
+
return a.depth - b.depth || a.seq - b.seq;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Effects mostly arrive in creation order already; checking is O(n), sorting is not.
|
|
24
|
+
* @internal
|
|
25
|
+
*/
|
|
26
|
+
function isOrdered(list) {
|
|
27
|
+
for (let i = 1; i < list.length; i += 1) {
|
|
28
|
+
const previous = list[i - 1];
|
|
29
|
+
const current = list[i];
|
|
30
|
+
if (previous !== void 0 && current !== void 0 && byCreation(previous, current) > 0) return false;
|
|
31
|
+
}
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
/** Runs every queued effect of one phase, parents before children, siblings by creation. */
|
|
35
|
+
function drain(queue) {
|
|
36
|
+
const batch = queue.splice(0, queue.length);
|
|
37
|
+
if (!isOrdered(batch)) batch.sort(byCreation);
|
|
38
|
+
for (const effect of batch) {
|
|
39
|
+
effect.flags &= -3;
|
|
40
|
+
if ((effect.flags & 4) !== 0) continue;
|
|
41
|
+
if (effect.pauses > 0) {
|
|
42
|
+
effect.flags = effect.flags & ~FLAG_TRANSITION | FLAG_PARKED;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
try {
|
|
46
|
+
if (effect.state === 2) recompute(effect);
|
|
47
|
+
else updateIfNecessary(effect);
|
|
48
|
+
} catch (error) {
|
|
49
|
+
handleError(error, effect);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return batch.length;
|
|
53
|
+
}
|
|
54
|
+
function raise(errors) {
|
|
55
|
+
const [first] = errors;
|
|
56
|
+
if (errors.length === 1) throw first;
|
|
57
|
+
throw new AggregateError(errors, "Several errors were thrown while flushing effects.");
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Runs every pending effect now: what the scheduled microtask does. Drains the queues until nothing
|
|
61
|
+
* is pending; render effects always run before user effects, parents before children, siblings by
|
|
62
|
+
* creation, and effects woken by a write in the flush run in a following pass. Errors nobody handled
|
|
63
|
+
* are collected and thrown once the queues are empty, so one failing effect never wedges the rest.
|
|
64
|
+
* Does nothing when a flush is already running. Transition-lane effects are not run here: they wait
|
|
65
|
+
* for the lane. Prefer {@link flushSync} in application code.
|
|
66
|
+
*
|
|
67
|
+
* @throws The error of an effect that no error handler caught; several are thrown together as an
|
|
68
|
+
* `AggregateError`.
|
|
69
|
+
* @throws ShadowError `INFINITE_LOOP` when effects keep waking each other for more than 1000 passes:
|
|
70
|
+
* the queued effects are dropped and the flush stops.
|
|
71
|
+
* @example
|
|
72
|
+
* import { flush, writeSignal, type SignalNode } from "@shadow-js/core/extend";
|
|
73
|
+
*
|
|
74
|
+
* export function setNow(signal: SignalNode<number>, value: number): void {
|
|
75
|
+
* writeSignal(signal, value);
|
|
76
|
+
* flush(); // the effects that read the signal have run
|
|
77
|
+
* }
|
|
78
|
+
* @see flushSync
|
|
79
|
+
* @since 1.0.0
|
|
80
|
+
*/
|
|
81
|
+
function flush() {
|
|
82
|
+
if (rt.flushing) return;
|
|
83
|
+
rt.flushing = true;
|
|
84
|
+
let passes = 0;
|
|
85
|
+
let runs = 0;
|
|
86
|
+
report(DEVTOOLS_EVENT.FLUSH_START, renderQueue.length, userQueue.length);
|
|
87
|
+
try {
|
|
88
|
+
while (hasPending()) {
|
|
89
|
+
let aborted = false;
|
|
90
|
+
while (renderQueue.length > 0) {
|
|
91
|
+
passes += 1;
|
|
92
|
+
if (passes > 1e3) {
|
|
93
|
+
aborted = true;
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
runs += drain(renderQueue);
|
|
97
|
+
}
|
|
98
|
+
if (!aborted && userQueue.length > 0) {
|
|
99
|
+
passes += 1;
|
|
100
|
+
if (passes > 1e3) aborted = true;
|
|
101
|
+
else runs += drain(userQueue);
|
|
102
|
+
}
|
|
103
|
+
if (aborted) {
|
|
104
|
+
clearQueues();
|
|
105
|
+
rt.uncaught.push(new ShadowError("INFINITE_LOOP"));
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
} finally {
|
|
110
|
+
rt.flushing = false;
|
|
111
|
+
report(DEVTOOLS_EVENT.FLUSH_END, runs);
|
|
112
|
+
}
|
|
113
|
+
const errors = rt.uncaught;
|
|
114
|
+
if (errors.length > 0) {
|
|
115
|
+
rt.uncaught = [];
|
|
116
|
+
raise(errors);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Surfaces an error from the microtask flush where nothing can catch it.
|
|
121
|
+
* @internal
|
|
122
|
+
*/
|
|
123
|
+
function reportUncaught(error) {
|
|
124
|
+
if (typeof reportError === "function") reportError(error);
|
|
125
|
+
else queueMicrotask(() => Promise.reject(error));
|
|
126
|
+
}
|
|
127
|
+
function runScheduled() {
|
|
128
|
+
microtaskScheduled = false;
|
|
129
|
+
if (!hasPending()) return;
|
|
130
|
+
try {
|
|
131
|
+
flush();
|
|
132
|
+
} catch (error) {
|
|
133
|
+
reportUncaught(error);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Requests a flush in a microtask: call it after marking observers stale by hand, as
|
|
138
|
+
* {@link markObservers} does not. Does nothing when a microtask flush is already due, inside a
|
|
139
|
+
* batch or group (which flushes or schedules on exit), during a flush (which picks the work up), or
|
|
140
|
+
* during a transition (its effects wait for the lane). An error thrown by that flush has no caller
|
|
141
|
+
* to reach: it goes to the global `reportError`, or becomes an unhandled promise rejection.
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* import { markObservers, schedule, type SourceNode } from "@shadow-js/core/extend";
|
|
145
|
+
*
|
|
146
|
+
* export function touch(source: SourceNode): void {
|
|
147
|
+
* markObservers(source);
|
|
148
|
+
* schedule(); // the effects that read `source` run in a microtask
|
|
149
|
+
* }
|
|
150
|
+
* @see notify
|
|
151
|
+
* @since 1.0.0
|
|
152
|
+
*/
|
|
153
|
+
function schedule() {
|
|
154
|
+
if (rt.inTransition || rt.batchDepth > 0 || rt.flushing || microtaskScheduled) return;
|
|
155
|
+
microtaskScheduled = true;
|
|
156
|
+
queueMicrotask(runScheduled);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Groups writes like {@link batch} but never flushes synchronously: when the outermost group ends,
|
|
160
|
+
* the effects wait for the microtask (or an enclosing batch / `flushSync`), exactly as after a
|
|
161
|
+
* single signal write. Store setters use it so that one `setStore(...)` behaves like one `set()`;
|
|
162
|
+
* use it for a primitive whose one call makes several writes.
|
|
163
|
+
*
|
|
164
|
+
* @param fn The writes to group. Its throw is rethrown after the group ends.
|
|
165
|
+
* @returns What `fn` returns.
|
|
166
|
+
* @example
|
|
167
|
+
* import { useSignal } from "@shadow-js/core";
|
|
168
|
+
* import { group } from "@shadow-js/core/extend";
|
|
169
|
+
*
|
|
170
|
+
* const [first, setFirst] = useSignal("Ada");
|
|
171
|
+
* const [last, setLast] = useSignal("Lovelace");
|
|
172
|
+
* group(() => {
|
|
173
|
+
* setFirst("Grace");
|
|
174
|
+
* setLast("Hopper");
|
|
175
|
+
* }); // effects reading both run once, in the next microtask
|
|
176
|
+
* @see batch
|
|
177
|
+
* @since 1.0.0
|
|
178
|
+
*/
|
|
179
|
+
function group(fn) {
|
|
180
|
+
rt.batchDepth += 1;
|
|
181
|
+
try {
|
|
182
|
+
return fn();
|
|
183
|
+
} finally {
|
|
184
|
+
rt.batchDepth -= 1;
|
|
185
|
+
if (rt.batchDepth === 0 && hasPending()) schedule();
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
function combine(primary, secondary) {
|
|
189
|
+
return new AggregateError([primary, secondary], "Both the callback and the flush threw.");
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Runs `fn` with effects deferred until the outermost batch ends, then runs them synchronously, so
|
|
193
|
+
* several writes wake each effect once. Reads inside the batch always see fresh values (G-6). Inside
|
|
194
|
+
* a running flush the effects are left to that flush.
|
|
195
|
+
*
|
|
196
|
+
* @param fn The writes to batch.
|
|
197
|
+
* @returns What `fn` returns.
|
|
198
|
+
* @throws What `fn` throws, after the flush. An error of the flush is thrown as {@link flush} throws
|
|
199
|
+
* it (including ShadowError `INFINITE_LOOP`); when `fn` threw as well, both are thrown together as an
|
|
200
|
+
* `AggregateError`.
|
|
201
|
+
* @example
|
|
202
|
+
* import { batch, useSignal } from "@shadow-js/core";
|
|
203
|
+
*
|
|
204
|
+
* const [first, setFirst] = useSignal("Ada");
|
|
205
|
+
* const [last, setLast] = useSignal("Lovelace");
|
|
206
|
+
* batch(() => {
|
|
207
|
+
* setFirst("Grace");
|
|
208
|
+
* setLast("Hopper");
|
|
209
|
+
* }); // effects reading both have run once, here
|
|
210
|
+
* @see flushSync
|
|
211
|
+
* @since 1.0.0
|
|
212
|
+
*/
|
|
213
|
+
function batch(fn) {
|
|
214
|
+
rt.batchDepth += 1;
|
|
215
|
+
let result;
|
|
216
|
+
let failure;
|
|
217
|
+
let failed = false;
|
|
218
|
+
try {
|
|
219
|
+
result = fn();
|
|
220
|
+
} catch (error) {
|
|
221
|
+
failed = true;
|
|
222
|
+
failure = error;
|
|
223
|
+
} finally {
|
|
224
|
+
rt.batchDepth -= 1;
|
|
225
|
+
}
|
|
226
|
+
if (rt.batchDepth === 0 && !rt.flushing && hasPending()) try {
|
|
227
|
+
flush();
|
|
228
|
+
} catch (error) {
|
|
229
|
+
if (failed) throw combine(failure, error);
|
|
230
|
+
throw error;
|
|
231
|
+
}
|
|
232
|
+
if (failed) throw failure;
|
|
233
|
+
return result;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Runs `fn` (optional) in a batch and runs every pending effect before returning, so the DOM and
|
|
237
|
+
* other effects are up to date on the next line. Use it in tests and before reading layout. Called
|
|
238
|
+
* while a flush is running (from inside an effect), it only runs `fn` and leaves the effects to that
|
|
239
|
+
* flush; development builds warn `FLUSH_IN_EFFECT`.
|
|
240
|
+
*
|
|
241
|
+
* @param fn The writes to make first. Undefined only flushes.
|
|
242
|
+
* @returns What `fn` returns, or undefined without `fn`.
|
|
243
|
+
* @throws What `fn` throws, after the flush. An error of the flush is thrown as {@link flush} throws
|
|
244
|
+
* it (including ShadowError `INFINITE_LOOP`); when `fn` threw as well, both are thrown together as an
|
|
245
|
+
* `AggregateError`.
|
|
246
|
+
* @example
|
|
247
|
+
* import { createRoot, flushSync, useEffect, useSignal } from "@shadow-js/core";
|
|
248
|
+
*
|
|
249
|
+
* createRoot(() => {
|
|
250
|
+
* const [count, setCount] = useSignal(0);
|
|
251
|
+
* useEffect(() => console.log(count()));
|
|
252
|
+
* flushSync(); // logs 0
|
|
253
|
+
* flushSync(() => setCount(1)); // logs 1 before this line ends
|
|
254
|
+
* });
|
|
255
|
+
* @see batch
|
|
256
|
+
* @since 1.0.0
|
|
257
|
+
*/
|
|
258
|
+
function flushSync(fn) {
|
|
259
|
+
if (rt.flushing) {
|
|
260
|
+
warn("FLUSH_IN_EFFECT");
|
|
261
|
+
return fn === void 0 ? void 0 : fn();
|
|
262
|
+
}
|
|
263
|
+
rt.batchDepth += 1;
|
|
264
|
+
let result;
|
|
265
|
+
let failure;
|
|
266
|
+
let failed = false;
|
|
267
|
+
try {
|
|
268
|
+
result = fn === void 0 ? void 0 : fn();
|
|
269
|
+
} catch (error) {
|
|
270
|
+
failed = true;
|
|
271
|
+
failure = error;
|
|
272
|
+
} finally {
|
|
273
|
+
rt.batchDepth -= 1;
|
|
274
|
+
}
|
|
275
|
+
try {
|
|
276
|
+
flush();
|
|
277
|
+
} catch (error) {
|
|
278
|
+
if (failed) throw combine(failure, error);
|
|
279
|
+
throw error;
|
|
280
|
+
}
|
|
281
|
+
if (failed) throw failure;
|
|
282
|
+
return result;
|
|
283
|
+
}
|
|
284
|
+
export { MAX_FLUSH_PASSES, batch, byCreation, flush, flushSync, group, isOrdered, reportUncaught, schedule };
|
|
285
|
+
|
|
286
|
+
//# sourceMappingURL=scheduler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scheduler.js","names":[],"sources":["../../../src/internal/scheduler.ts"],"sourcesContent":["import { DEVTOOLS_EVENT, report } from \"./devtools\";\nimport { ShadowError, warn } from \"./diagnostics\";\nimport { DIRTY, FLAG_DISPOSED, FLAG_PARKED, FLAG_QUEUED, FLAG_TRANSITION } from \"./flags\";\nimport type { Computation } from \"./graph\";\nimport { recompute, updateIfNecessary } from \"./graph\";\nimport { handleError } from \"./owner\";\nimport { clearQueues, hasPending, renderQueue, userQueue } from \"./queue\";\nimport { rt } from \"./state\";\n\n/**\n * Flush passes allowed before the loop guard aborts (R-CODE-12).\n * @internal\n */\nexport const MAX_FLUSH_PASSES = 1000;\n\nlet microtaskScheduled = false;\n\n/**\n * Sort order of queued effects: parents before children (by depth), siblings by creation (G-16).\n * @internal\n */\nexport function byCreation(a: Computation, b: Computation): number {\n return a.depth - b.depth || a.seq - b.seq;\n}\n\n/**\n * Effects mostly arrive in creation order already; checking is O(n), sorting is not.\n * @internal\n */\nexport function isOrdered(list: readonly Computation[]): boolean {\n for (let i = 1; i < list.length; i += 1) {\n const previous = list[i - 1];\n const current = list[i];\n if (previous !== undefined && current !== undefined && byCreation(previous, current) > 0) {\n return false;\n }\n }\n return true;\n}\n\n/** Runs every queued effect of one phase, parents before children, siblings by creation. */\nfunction drain(queue: Computation[]): number {\n const batch = queue.splice(0, queue.length);\n if (!isOrdered(batch)) batch.sort(byCreation);\n for (const effect of batch) {\n effect.flags &= ~FLAG_QUEUED;\n if ((effect.flags & FLAG_DISPOSED) !== 0) continue;\n if (effect.pauses > 0) {\n // Paused after it was queued: wait (unqueued) until the owner resumes.\n effect.flags = (effect.flags & ~FLAG_TRANSITION) | FLAG_PARKED;\n continue;\n }\n try {\n // A directly dirtied effect needs no source verification: skip the frame machinery.\n if (effect.state === DIRTY) recompute(effect);\n else updateIfNecessary(effect);\n } catch (error) {\n handleError(error, effect);\n }\n }\n return batch.length;\n}\n\nfunction raise(errors: unknown[]): void {\n const [first] = errors;\n if (errors.length === 1) throw first;\n throw new AggregateError(\n errors,\n __DEV__ ? \"Several errors were thrown while flushing effects.\" : \"SHADOW_FLUSH_ERRORS\",\n );\n}\n\n/**\n * Runs every pending effect now: what the scheduled microtask does. Drains the queues until nothing\n * is pending; render effects always run before user effects, parents before children, siblings by\n * creation, and effects woken by a write in the flush run in a following pass. Errors nobody handled\n * are collected and thrown once the queues are empty, so one failing effect never wedges the rest.\n * Does nothing when a flush is already running. Transition-lane effects are not run here: they wait\n * for the lane. Prefer {@link flushSync} in application code.\n *\n * @throws The error of an effect that no error handler caught; several are thrown together as an\n * `AggregateError`.\n * @throws ShadowError `INFINITE_LOOP` when effects keep waking each other for more than 1000 passes:\n * the queued effects are dropped and the flush stops.\n * @example\n * import { flush, writeSignal, type SignalNode } from \"@shadow-js/core/extend\";\n *\n * export function setNow(signal: SignalNode<number>, value: number): void {\n * writeSignal(signal, value);\n * flush(); // the effects that read the signal have run\n * }\n * @see flushSync\n * @since 1.0.0\n */\nexport function flush(): void {\n if (rt.flushing) return;\n rt.flushing = true;\n let passes = 0;\n let runs = 0;\n if (__DEV__) report(DEVTOOLS_EVENT.FLUSH_START, renderQueue.length, userQueue.length);\n try {\n while (hasPending()) {\n let aborted = false;\n // Render passes count too: a render effect that keeps writing its own source must abort.\n while (renderQueue.length > 0) {\n passes += 1;\n if (passes > MAX_FLUSH_PASSES) {\n aborted = true;\n break;\n }\n runs += drain(renderQueue);\n }\n if (!aborted && userQueue.length > 0) {\n passes += 1;\n if (passes > MAX_FLUSH_PASSES) aborted = true;\n else runs += drain(userQueue);\n }\n if (aborted) {\n clearQueues();\n rt.uncaught.push(new ShadowError(\"INFINITE_LOOP\"));\n break;\n }\n }\n } finally {\n rt.flushing = false;\n if (__DEV__) report(DEVTOOLS_EVENT.FLUSH_END, runs);\n }\n const errors = rt.uncaught;\n if (errors.length > 0) {\n rt.uncaught = [];\n raise(errors);\n }\n}\n\n/**\n * Surfaces an error from the microtask flush where nothing can catch it.\n * @internal\n */\nexport function reportUncaught(error: unknown): void {\n if (typeof reportError === \"function\") reportError(error);\n else queueMicrotask(() => Promise.reject(error));\n}\n\nfunction runScheduled(): void {\n microtaskScheduled = false;\n if (!hasPending()) return;\n try {\n flush();\n } catch (error) {\n reportUncaught(error);\n }\n}\n\n/**\n * Requests a flush in a microtask: call it after marking observers stale by hand, as\n * {@link markObservers} does not. Does nothing when a microtask flush is already due, inside a\n * batch or group (which flushes or schedules on exit), during a flush (which picks the work up), or\n * during a transition (its effects wait for the lane). An error thrown by that flush has no caller\n * to reach: it goes to the global `reportError`, or becomes an unhandled promise rejection.\n *\n * @example\n * import { markObservers, schedule, type SourceNode } from \"@shadow-js/core/extend\";\n *\n * export function touch(source: SourceNode): void {\n * markObservers(source);\n * schedule(); // the effects that read `source` run in a microtask\n * }\n * @see notify\n * @since 1.0.0\n */\nexport function schedule(): void {\n // A transition write waits for the lane (scheduled when the effect is queued), never for a microtask.\n if (rt.inTransition || rt.batchDepth > 0 || rt.flushing || microtaskScheduled) return;\n microtaskScheduled = true;\n queueMicrotask(runScheduled);\n}\n\n/**\n * Groups writes like {@link batch} but never flushes synchronously: when the outermost group ends,\n * the effects wait for the microtask (or an enclosing batch / `flushSync`), exactly as after a\n * single signal write. Store setters use it so that one `setStore(...)` behaves like one `set()`;\n * use it for a primitive whose one call makes several writes.\n *\n * @param fn The writes to group. Its throw is rethrown after the group ends.\n * @returns What `fn` returns.\n * @example\n * import { useSignal } from \"@shadow-js/core\";\n * import { group } from \"@shadow-js/core/extend\";\n *\n * const [first, setFirst] = useSignal(\"Ada\");\n * const [last, setLast] = useSignal(\"Lovelace\");\n * group(() => {\n * setFirst(\"Grace\");\n * setLast(\"Hopper\");\n * }); // effects reading both run once, in the next microtask\n * @see batch\n * @since 1.0.0\n */\nexport function group<T>(fn: () => T): T {\n rt.batchDepth += 1;\n try {\n return fn();\n } finally {\n rt.batchDepth -= 1;\n if (rt.batchDepth === 0 && hasPending()) schedule();\n }\n}\n\nfunction combine(primary: unknown, secondary: unknown): unknown {\n return new AggregateError(\n [primary, secondary],\n __DEV__ ? \"Both the callback and the flush threw.\" : \"SHADOW_BATCH_ERRORS\",\n );\n}\n\n/**\n * Runs `fn` with effects deferred until the outermost batch ends, then runs them synchronously, so\n * several writes wake each effect once. Reads inside the batch always see fresh values (G-6). Inside\n * a running flush the effects are left to that flush.\n *\n * @param fn The writes to batch.\n * @returns What `fn` returns.\n * @throws What `fn` throws, after the flush. An error of the flush is thrown as {@link flush} throws\n * it (including ShadowError `INFINITE_LOOP`); when `fn` threw as well, both are thrown together as an\n * `AggregateError`.\n * @example\n * import { batch, useSignal } from \"@shadow-js/core\";\n *\n * const [first, setFirst] = useSignal(\"Ada\");\n * const [last, setLast] = useSignal(\"Lovelace\");\n * batch(() => {\n * setFirst(\"Grace\");\n * setLast(\"Hopper\");\n * }); // effects reading both have run once, here\n * @see flushSync\n * @since 1.0.0\n */\nexport function batch<T>(fn: () => T): T {\n rt.batchDepth += 1;\n let result: T | undefined;\n let failure: unknown;\n let failed = false;\n try {\n result = fn();\n } catch (error) {\n failed = true;\n failure = error;\n } finally {\n rt.batchDepth -= 1;\n }\n if (rt.batchDepth === 0 && !rt.flushing && hasPending()) {\n try {\n flush();\n } catch (error) {\n if (failed) throw combine(failure, error);\n throw error;\n }\n }\n if (failed) throw failure;\n return result as T;\n}\n\n/**\n * Runs `fn` (optional) in a batch and runs every pending effect before returning, so the DOM and\n * other effects are up to date on the next line. Use it in tests and before reading layout. Called\n * while a flush is running (from inside an effect), it only runs `fn` and leaves the effects to that\n * flush; development builds warn `FLUSH_IN_EFFECT`.\n *\n * @param fn The writes to make first. Undefined only flushes.\n * @returns What `fn` returns, or undefined without `fn`.\n * @throws What `fn` throws, after the flush. An error of the flush is thrown as {@link flush} throws\n * it (including ShadowError `INFINITE_LOOP`); when `fn` threw as well, both are thrown together as an\n * `AggregateError`.\n * @example\n * import { createRoot, flushSync, useEffect, useSignal } from \"@shadow-js/core\";\n *\n * createRoot(() => {\n * const [count, setCount] = useSignal(0);\n * useEffect(() => console.log(count()));\n * flushSync(); // logs 0\n * flushSync(() => setCount(1)); // logs 1 before this line ends\n * });\n * @see batch\n * @since 1.0.0\n */\nexport function flushSync<T = void>(fn?: () => T): T {\n if (rt.flushing) {\n warn(\"FLUSH_IN_EFFECT\");\n return (fn === undefined ? undefined : fn()) as T;\n }\n rt.batchDepth += 1;\n let result: T | undefined;\n let failure: unknown;\n let failed = false;\n try {\n result = fn === undefined ? undefined : fn();\n } catch (error) {\n failed = true;\n failure = error;\n } finally {\n rt.batchDepth -= 1;\n }\n try {\n flush();\n } catch (error) {\n if (failed) throw combine(failure, error);\n throw error;\n }\n if (failed) throw failure;\n return result as T;\n}\n"],"mappings":";;;;;;;;;;;;AAaA,MAAa,mBAAmB;AAEhC,IAAI,qBAAqB;;;;;AAMzB,SAAgB,WAAW,GAAgB,GAAwB;CACjE,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE;AACxC;;;;;AAMA,SAAgB,UAAU,MAAuC;CAC/D,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK,GAAG;EACvC,MAAM,WAAW,KAAK,IAAI;EAC1B,MAAM,UAAU,KAAK;EACrB,IAAI,aAAa,KAAA,KAAa,YAAY,KAAA,KAAa,WAAW,UAAU,OAAO,IAAI,GACrF,OAAO;CAEX;CACA,OAAO;AACT;;AAGA,SAAS,MAAM,OAA8B;CAC3C,MAAM,QAAQ,MAAM,OAAO,GAAG,MAAM,MAAM;CAC1C,IAAI,CAAC,UAAU,KAAK,GAAG,MAAM,KAAK,UAAU;CAC5C,KAAK,MAAM,UAAU,OAAO;EAC1B,OAAO,SAAS;EAChB,KAAK,OAAO,QAAA,OAA2B,GAAG;EAC1C,IAAI,OAAO,SAAS,GAAG;GAErB,OAAO,QAAS,OAAO,QAAQ,CAAC,kBAAmB;GACnD;EACF;EACA,IAAI;GAEF,IAAI,OAAO,UAAA,GAAiB,UAAU,MAAM;QACvC,kBAAkB,MAAM;EAC/B,SAAS,OAAO;GACd,YAAY,OAAO,MAAM;EAC3B;CACF;CACA,OAAO,MAAM;AACf;AAEA,SAAS,MAAM,QAAyB;CACtC,MAAM,CAAC,SAAS;CAChB,IAAI,OAAO,WAAW,GAAG,MAAM;CAC/B,MAAM,IAAI,eACR,QACU,oDACZ;AACF;;;;;;;;;;;;;;;;;;;;;;;AAwBA,SAAgB,QAAc;CAC5B,IAAI,GAAG,UAAU;CACjB,GAAG,WAAW;CACd,IAAI,SAAS;CACb,IAAI,OAAO;CACE,OAAO,eAAe,aAAa,YAAY,QAAQ,UAAU,MAAM;CACpF,IAAI;EACF,OAAO,WAAW,GAAG;GACnB,IAAI,UAAU;GAEd,OAAO,YAAY,SAAS,GAAG;IAC7B,UAAU;IACV,IAAI,SAAA,KAA2B;KAC7B,UAAU;KACV;IACF;IACA,QAAQ,MAAM,WAAW;GAC3B;GACA,IAAI,CAAC,WAAW,UAAU,SAAS,GAAG;IACpC,UAAU;IACV,IAAI,SAAA,KAA2B,UAAU;SACpC,QAAQ,MAAM,SAAS;GAC9B;GACA,IAAI,SAAS;IACX,YAAY;IACZ,GAAG,SAAS,KAAK,IAAI,YAAY,eAAe,CAAC;IACjD;GACF;EACF;CACF,UAAU;EACR,GAAG,WAAW;EACD,OAAO,eAAe,WAAW,IAAI;CACpD;CACA,MAAM,SAAS,GAAG;CAClB,IAAI,OAAO,SAAS,GAAG;EACrB,GAAG,WAAW,CAAC;EACf,MAAM,MAAM;CACd;AACF;;;;;AAMA,SAAgB,eAAe,OAAsB;CACnD,IAAI,OAAO,gBAAgB,YAAY,YAAY,KAAK;MACnD,qBAAqB,QAAQ,OAAO,KAAK,CAAC;AACjD;AAEA,SAAS,eAAqB;CAC5B,qBAAqB;CACrB,IAAI,CAAC,WAAW,GAAG;CACnB,IAAI;EACF,MAAM;CACR,SAAS,OAAO;EACd,eAAe,KAAK;CACtB;AACF;;;;;;;;;;;;;;;;;;AAmBA,SAAgB,WAAiB;CAE/B,IAAI,GAAG,gBAAgB,GAAG,aAAa,KAAK,GAAG,YAAY,oBAAoB;CAC/E,qBAAqB;CACrB,eAAe,YAAY;AAC7B;;;;;;;;;;;;;;;;;;;;;;AAuBA,SAAgB,MAAS,IAAgB;CACvC,GAAG,cAAc;CACjB,IAAI;EACF,OAAO,GAAG;CACZ,UAAU;EACR,GAAG,cAAc;EACjB,IAAI,GAAG,eAAe,KAAK,WAAW,GAAG,SAAS;CACpD;AACF;AAEA,SAAS,QAAQ,SAAkB,WAA6B;CAC9D,OAAO,IAAI,eACT,CAAC,SAAS,SAAS,GACT,wCACZ;AACF;;;;;;;;;;;;;;;;;;;;;;;AAwBA,SAAgB,MAAS,IAAgB;CACvC,GAAG,cAAc;CACjB,IAAI;CACJ,IAAI;CACJ,IAAI,SAAS;CACb,IAAI;EACF,SAAS,GAAG;CACd,SAAS,OAAO;EACd,SAAS;EACT,UAAU;CACZ,UAAU;EACR,GAAG,cAAc;CACnB;CACA,IAAI,GAAG,eAAe,KAAK,CAAC,GAAG,YAAY,WAAW,GACpD,IAAI;EACF,MAAM;CACR,SAAS,OAAO;EACd,IAAI,QAAQ,MAAM,QAAQ,SAAS,KAAK;EACxC,MAAM;CACR;CAEF,IAAI,QAAQ,MAAM;CAClB,OAAO;AACT;;;;;;;;;;;;;;;;;;;;;;;;AAyBA,SAAgB,UAAoB,IAAiB;CACnD,IAAI,GAAG,UAAU;EACf,KAAK,iBAAiB;EACtB,OAAQ,OAAO,KAAA,IAAY,KAAA,IAAY,GAAG;CAC5C;CACA,GAAG,cAAc;CACjB,IAAI;CACJ,IAAI;CACJ,IAAI,SAAS;CACb,IAAI;EACF,SAAS,OAAO,KAAA,IAAY,KAAA,IAAY,GAAG;CAC7C,SAAS,OAAO;EACd,SAAS;EACT,UAAU;CACZ,UAAU;EACR,GAAG,cAAc;CACnB;CACA,IAAI;EACF,MAAM;CACR,SAAS,OAAO;EACd,IAAI,QAAQ,MAAM,QAAQ,SAAS,KAAK;EACxC,MAAM;CACR;CACA,IAAI,QAAQ,MAAM;CAClB,OAAO;AACT"}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { Owner } from "./owner.js";
|
|
2
|
+
import { Computation } from "./graph.js";
|
|
3
|
+
/**
|
|
4
|
+
* A hold for a value a transition builds while its reads wait for data (a `Show` branch built during a
|
|
5
|
+
* transition): the reads that wait count against this hold, not against the Suspense boundary around
|
|
6
|
+
* the value, so the shown value stays until the hold settles. Created by `createTransitionHold`.
|
|
7
|
+
* @since 1.0.0
|
|
8
|
+
*/
|
|
9
|
+
export interface TransitionHold {
|
|
10
|
+
/** Runs `fn` with this hold as the Suspense boundary of the reads inside it. */
|
|
11
|
+
run<T>(fn: () => T): T;
|
|
12
|
+
/** Reads still waiting for data. */
|
|
13
|
+
count(): number;
|
|
14
|
+
/** True once a read in the hold was held (the value is then pending, not ready). */
|
|
15
|
+
engaged(): boolean;
|
|
16
|
+
/** Ends the hold without waking anything (the value is being dropped). */
|
|
17
|
+
drop(): void;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The choice a branch slot (Show, Switch) shows: its index, and what decides a rebuild. Renderers
|
|
21
|
+
* extend it with what they need to build the branch.
|
|
22
|
+
* @since 1.0.0
|
|
23
|
+
*/
|
|
24
|
+
export interface SlotChoice {
|
|
25
|
+
/** Which branch is chosen: its position among the slot's branches, or the renderer's fallback index. */
|
|
26
|
+
readonly index: number;
|
|
27
|
+
/** True when the branch is rebuilt whenever `when` changes identity; false when only `index` counts. */
|
|
28
|
+
readonly keyed: boolean;
|
|
29
|
+
/** The condition value that selected the branch. */
|
|
30
|
+
readonly when: unknown;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The record of a branch's owner that its accessors read: the value and whether it is being torn down.
|
|
34
|
+
* @since 1.0.0
|
|
35
|
+
*/
|
|
36
|
+
export interface SlotOwned {
|
|
37
|
+
/** The index of the branch this record belongs to (see {@link SlotChoice.index}). */
|
|
38
|
+
index: number;
|
|
39
|
+
/** The latest condition value while the branch is shown; what its accessor returns during teardown. */
|
|
40
|
+
value: unknown;
|
|
41
|
+
/** True while the branch is torn down: its accessor then returns `value` instead of throwing. */
|
|
42
|
+
disposing: boolean;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* A branch a slot shows, or builds during a transition. A branch without `dispose` is torn down by its memo.
|
|
46
|
+
* @since 1.0.0
|
|
47
|
+
*/
|
|
48
|
+
export interface SlotBranch<C extends SlotChoice> {
|
|
49
|
+
/** The choice the branch was built for. */
|
|
50
|
+
readonly choice: C;
|
|
51
|
+
/** The record its accessors read. */
|
|
52
|
+
readonly owned: SlotOwned;
|
|
53
|
+
/** What the branch produced: the value the slot's output returns while the branch is shown. */
|
|
54
|
+
readonly result: unknown;
|
|
55
|
+
/**
|
|
56
|
+
* Tears the branch down. Set for the branches a transition built: they live under the holder, not
|
|
57
|
+
* under the memo.
|
|
58
|
+
* @default undefined (the slot's memo tears the branch down)
|
|
59
|
+
*/
|
|
60
|
+
readonly dispose?: () => void;
|
|
61
|
+
/**
|
|
62
|
+
* The hold a transition built the branch under; null when none of its reads was held.
|
|
63
|
+
* @default undefined (a memo-owned branch has no hold)
|
|
64
|
+
*/
|
|
65
|
+
readonly hold?: TransitionHold | null;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* What a slot's output memo keeps: the branch on screen (its result is the value the output returns).
|
|
69
|
+
* @since 1.0.0
|
|
70
|
+
*/
|
|
71
|
+
export interface SlotState<C extends SlotChoice> {
|
|
72
|
+
/** The branch on screen, or undefined before the first build and while the memo rebuilds it. */
|
|
73
|
+
live: SlotBranch<C> | undefined;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The transition layer of one slot, from `transitionHooks()?.slotSwap(...)`: it keeps the shown
|
|
77
|
+
* branch while a transition builds the next one (see `slotSwap` in concurrency.ts).
|
|
78
|
+
* @since 1.0.0
|
|
79
|
+
*/
|
|
80
|
+
export interface SlotSwap<C extends SlotChoice> {
|
|
81
|
+
/**
|
|
82
|
+
* Runs one pass of the slot's memo for `choice`. True when a transition keeps the shown value. False
|
|
83
|
+
* when the usual path follows: the branch on screen has been retired and `state.live` is cleared.
|
|
84
|
+
*/
|
|
85
|
+
step(choice: C): boolean;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* The hold of one Suspense boundary: engaged by a transition read, released when its data arrived.
|
|
89
|
+
* From `transitionHooks()?.boundaryHold()`.
|
|
90
|
+
* @since 1.0.0
|
|
91
|
+
*/
|
|
92
|
+
export interface BoundaryHold {
|
|
93
|
+
/** Engages the hold (a second call changes nothing) so the boundary keeps its content; returns true. */
|
|
94
|
+
engage(): boolean;
|
|
95
|
+
/** When engaged: wakes the readers that waited, then ends the hold. Otherwise does nothing. */
|
|
96
|
+
release(): void;
|
|
97
|
+
/** True while the hold is engaged. */
|
|
98
|
+
holding(): boolean;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* The transition machinery the engine calls through `rt.hooks`: transition lanes, pauses (Activity)
|
|
102
|
+
* and suspensions (Suspense holds). It is created by the first call of an API that needs it (see
|
|
103
|
+
* concurrency.ts), so an application that never calls one of those APIs carries none of their code.
|
|
104
|
+
* Renderers reach it through `transitionHooks()`.
|
|
105
|
+
* @since 1.0.0
|
|
106
|
+
*/
|
|
107
|
+
export interface Hooks {
|
|
108
|
+
/** The transition layer of one branch slot (see `slotSwap` in concurrency.ts). */
|
|
109
|
+
slotSwap<C extends SlotChoice>(state: SlotState<C>, build: (choice: C, owned: SlotOwned, eager: boolean) => unknown, same: (a: C, b: C) => boolean, parent: Owner | null): SlotSwap<C>;
|
|
110
|
+
/** A Suspense boundary's hold (see `boundaryHold` in concurrency.ts). */
|
|
111
|
+
boundaryHold(): BoundaryHold;
|
|
112
|
+
/** Queues an effect that is due in the transition lane, or parks it while its owner is paused. */
|
|
113
|
+
place(effect: Computation, inLane: boolean): void;
|
|
114
|
+
/** An urgent write reached an effect that waits in the lane or is parked: it moves to the urgent queue. */
|
|
115
|
+
promote(effect: Computation): void;
|
|
116
|
+
/** Forgets the lane queues (the flush loop guard). */
|
|
117
|
+
clear(): void;
|
|
118
|
+
/** Keeps `node` for a transition when `error` is a suspension; returns false for any other error. */
|
|
119
|
+
suspend(node: Computation, error: unknown): boolean;
|
|
120
|
+
/** The engine side of `holdReader`: throws the suspension sentinel when `tracker.hold()` is true. */
|
|
121
|
+
holdReader(tracker: {
|
|
122
|
+
hold?(): boolean;
|
|
123
|
+
}): void;
|
|
124
|
+
/** The engine side of `startTransition` in the browser. */
|
|
125
|
+
startTransition(fn: () => void): Promise<void>;
|
|
126
|
+
/** The engine side of `beginHold`: counts one more Suspense hold. */
|
|
127
|
+
beginHold(): void;
|
|
128
|
+
/** The engine side of `endHold`: counts one hold less and settles the lane. */
|
|
129
|
+
endHold(): void;
|
|
130
|
+
/** The engine side of `wakeSuspended`. */
|
|
131
|
+
wakeSuspended(): void;
|
|
132
|
+
/** The engine side of `pauseOwner`. */
|
|
133
|
+
pause(owner: Owner): void;
|
|
134
|
+
/** The engine side of `resumeOwner`. */
|
|
135
|
+
resume(owner: Owner): void;
|
|
136
|
+
/** The engine side of `createTransitionHold`. */
|
|
137
|
+
createHold(onSettled: () => void): TransitionHold;
|
|
138
|
+
}
|
|
139
|
+
//# sourceMappingURL=state.d.ts.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// src/internal/state.ts
|
|
2
|
+
/**
|
|
3
|
+
* The runtime registers (R-CODE-4): the only module-level mutable state of core besides
|
|
4
|
+
* monotonic counters. Every write is paired with a `finally` restore (R-CODE-5).
|
|
5
|
+
*/
|
|
6
|
+
const rt = {
|
|
7
|
+
owner: null,
|
|
8
|
+
listener: null,
|
|
9
|
+
running: null,
|
|
10
|
+
batchDepth: 0,
|
|
11
|
+
flushing: false,
|
|
12
|
+
recomputeDepth: 0,
|
|
13
|
+
disposing: 0,
|
|
14
|
+
uncaught: [],
|
|
15
|
+
inTransition: false,
|
|
16
|
+
hydrating: false,
|
|
17
|
+
hooks: null
|
|
18
|
+
};
|
|
19
|
+
let counter = 0;
|
|
20
|
+
/** Monotonic creation sequence; orders sibling effects (G-16). */
|
|
21
|
+
function nextSequence() {
|
|
22
|
+
counter += 1;
|
|
23
|
+
return counter;
|
|
24
|
+
}
|
|
25
|
+
export { nextSequence, rt };
|
|
26
|
+
|
|
27
|
+
//# sourceMappingURL=state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state.js","names":[],"sources":["../../../src/internal/state.ts"],"sourcesContent":["import type { Computation } from \"./graph\";\nimport type { Owner } from \"./owner\";\n\n/**\n * A hold for a value a transition builds while its reads wait for data (a `Show` branch built during a\n * transition): the reads that wait count against this hold, not against the Suspense boundary around\n * the value, so the shown value stays until the hold settles. Created by `createTransitionHold`.\n * @since 1.0.0\n */\nexport interface TransitionHold {\n /** Runs `fn` with this hold as the Suspense boundary of the reads inside it. */\n run<T>(fn: () => T): T;\n /** Reads still waiting for data. */\n count(): number;\n /** True once a read in the hold was held (the value is then pending, not ready). */\n engaged(): boolean;\n /** Ends the hold without waking anything (the value is being dropped). */\n drop(): void;\n}\n\n/**\n * The choice a branch slot (Show, Switch) shows: its index, and what decides a rebuild. Renderers\n * extend it with what they need to build the branch.\n * @since 1.0.0\n */\nexport interface SlotChoice {\n /** Which branch is chosen: its position among the slot's branches, or the renderer's fallback index. */\n readonly index: number;\n /** True when the branch is rebuilt whenever `when` changes identity; false when only `index` counts. */\n readonly keyed: boolean;\n /** The condition value that selected the branch. */\n readonly when: unknown;\n}\n\n/**\n * The record of a branch's owner that its accessors read: the value and whether it is being torn down.\n * @since 1.0.0\n */\nexport interface SlotOwned {\n /** The index of the branch this record belongs to (see {@link SlotChoice.index}). */\n index: number;\n /** The latest condition value while the branch is shown; what its accessor returns during teardown. */\n value: unknown;\n /** True while the branch is torn down: its accessor then returns `value` instead of throwing. */\n disposing: boolean;\n}\n\n/**\n * A branch a slot shows, or builds during a transition. A branch without `dispose` is torn down by its memo.\n * @since 1.0.0\n */\nexport interface SlotBranch<C extends SlotChoice> {\n /** The choice the branch was built for. */\n readonly choice: C;\n /** The record its accessors read. */\n readonly owned: SlotOwned;\n /** What the branch produced: the value the slot's output returns while the branch is shown. */\n readonly result: unknown;\n /**\n * Tears the branch down. Set for the branches a transition built: they live under the holder, not\n * under the memo.\n * @default undefined (the slot's memo tears the branch down)\n */\n readonly dispose?: () => void;\n /**\n * The hold a transition built the branch under; null when none of its reads was held.\n * @default undefined (a memo-owned branch has no hold)\n */\n readonly hold?: TransitionHold | null;\n}\n\n/**\n * What a slot's output memo keeps: the branch on screen (its result is the value the output returns).\n * @since 1.0.0\n */\nexport interface SlotState<C extends SlotChoice> {\n /** The branch on screen, or undefined before the first build and while the memo rebuilds it. */\n live: SlotBranch<C> | undefined;\n}\n\n/**\n * The transition layer of one slot, from `transitionHooks()?.slotSwap(...)`: it keeps the shown\n * branch while a transition builds the next one (see `slotSwap` in concurrency.ts).\n * @since 1.0.0\n */\nexport interface SlotSwap<C extends SlotChoice> {\n /**\n * Runs one pass of the slot's memo for `choice`. True when a transition keeps the shown value. False\n * when the usual path follows: the branch on screen has been retired and `state.live` is cleared.\n */\n step(choice: C): boolean;\n}\n\n/**\n * The hold of one Suspense boundary: engaged by a transition read, released when its data arrived.\n * From `transitionHooks()?.boundaryHold()`.\n * @since 1.0.0\n */\nexport interface BoundaryHold {\n /** Engages the hold (a second call changes nothing) so the boundary keeps its content; returns true. */\n engage(): boolean;\n /** When engaged: wakes the readers that waited, then ends the hold. Otherwise does nothing. */\n release(): void;\n /** True while the hold is engaged. */\n holding(): boolean;\n}\n\n/**\n * The transition machinery the engine calls through `rt.hooks`: transition lanes, pauses (Activity)\n * and suspensions (Suspense holds). It is created by the first call of an API that needs it (see\n * concurrency.ts), so an application that never calls one of those APIs carries none of their code.\n * Renderers reach it through `transitionHooks()`.\n * @since 1.0.0\n */\nexport interface Hooks {\n /** The transition layer of one branch slot (see `slotSwap` in concurrency.ts). */\n slotSwap<C extends SlotChoice>(\n state: SlotState<C>,\n build: (choice: C, owned: SlotOwned, eager: boolean) => unknown,\n same: (a: C, b: C) => boolean,\n parent: Owner | null,\n ): SlotSwap<C>;\n /** A Suspense boundary's hold (see `boundaryHold` in concurrency.ts). */\n boundaryHold(): BoundaryHold;\n /** Queues an effect that is due in the transition lane, or parks it while its owner is paused. */\n place(effect: Computation, inLane: boolean): void;\n /** An urgent write reached an effect that waits in the lane or is parked: it moves to the urgent queue. */\n promote(effect: Computation): void;\n /** Forgets the lane queues (the flush loop guard). */\n clear(): void;\n /** Keeps `node` for a transition when `error` is a suspension; returns false for any other error. */\n suspend(node: Computation, error: unknown): boolean;\n /** The engine side of `holdReader`: throws the suspension sentinel when `tracker.hold()` is true. */\n holdReader(tracker: { hold?(): boolean }): void;\n /** The engine side of `startTransition` in the browser. */\n startTransition(fn: () => void): Promise<void>;\n /** The engine side of `beginHold`: counts one more Suspense hold. */\n beginHold(): void;\n /** The engine side of `endHold`: counts one hold less and settles the lane. */\n endHold(): void;\n /** The engine side of `wakeSuspended`. */\n wakeSuspended(): void;\n /** The engine side of `pauseOwner`. */\n pause(owner: Owner): void;\n /** The engine side of `resumeOwner`. */\n resume(owner: Owner): void;\n /** The engine side of `createTransitionHold`. */\n createHold(onSettled: () => void): TransitionHold;\n}\n\n/**\n * The runtime registers (R-CODE-4): the only module-level mutable state of core besides\n * monotonic counters. Every write is paired with a `finally` restore (R-CODE-5).\n */\nexport const rt: {\n /** Owner that adopts newly created nodes. */\n owner: Owner | null;\n /** Computation that records the signals it reads; null while untracked. */\n listener: Computation | null;\n /** Computation currently executing, tracked or not (used by the write-in-memo guard). */\n running: Computation | null;\n /** Nesting depth of batch()/flushSync() calls. */\n batchDepth: number;\n /** True while the flush loop is draining the effect queues. */\n flushing: boolean;\n /** Nesting depth of memo recomputation, used to switch to iterative pulls. */\n recomputeDepth: number;\n /** Nesting depth of disposals in progress; errors raised meanwhile are collected, not thrown. */\n disposing: number;\n /** Errors nobody handled during the current flush or disposal; rethrown once it completes. */\n uncaught: unknown[];\n /** True while a transition callback or a transition-lane run is active: writes made now join the lane. */\n inTransition: boolean;\n /** True while `hydrate()` claims server markup (useExternalStore reads its server snapshot). */\n hydrating: boolean;\n /** The transition machinery, or null until an API that needs it is first called. */\n hooks: Hooks | null;\n} = {\n owner: null,\n listener: null,\n running: null,\n batchDepth: 0,\n flushing: false,\n recomputeDepth: 0,\n disposing: 0,\n uncaught: [],\n inTransition: false,\n hydrating: false,\n hooks: null,\n};\n\nlet counter = 0;\n/** Monotonic creation sequence; orders sibling effects (G-16). */\nexport function nextSequence(): number {\n counter += 1;\n return counter;\n}\n"],"mappings":";;;;;AA0JA,MAAa,KAuBT;CACF,OAAO;CACP,UAAU;CACV,SAAS;CACT,YAAY;CACZ,UAAU;CACV,gBAAgB;CAChB,WAAW;CACX,UAAU,CAAC;CACX,cAAc;CACd,WAAW;CACX,OAAO;AACT;AAEA,IAAI,UAAU;;AAEd,SAAgB,eAAuB;CACrC,WAAW;CACX,OAAO;AACT"}
|