@solidjs/signals 2.0.0-rc.2 → 2.0.0-rc.20
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/dist/dev-shared.js +6505 -0
- package/dist/dev.attribution.js +3484 -0
- package/dist/dev.js +10227 -9508
- package/dist/observe/affects-marks.js +86 -0
- package/dist/observe/affects.js +112 -0
- package/dist/observe/attribution.js +7 -0
- package/dist/observe/boundaries.js +696 -0
- package/dist/observe/core/action.js +182 -0
- package/dist/observe/core/async.js +824 -0
- package/dist/observe/core/attribution-costs.js +66 -0
- package/dist/observe/core/attribution-feedback.js +280 -0
- package/dist/observe/core/attribution-hooks.js +83 -0
- package/dist/observe/core/attribution-queries.js +43 -0
- package/dist/observe/core/attribution.js +3079 -0
- package/dist/observe/core/constants.js +269 -0
- package/dist/observe/core/context.js +63 -0
- package/dist/observe/core/copies.js +10 -0
- package/dist/observe/core/core.js +1707 -0
- package/dist/observe/core/dev.js +394 -0
- package/dist/observe/core/effect.js +193 -0
- package/dist/observe/core/error-hooks.js +71 -0
- package/dist/observe/core/error.js +82 -0
- package/dist/observe/core/external.js +97 -0
- package/dist/observe/core/graph.js +162 -0
- package/dist/observe/core/heap.js +124 -0
- package/dist/observe/core/identities.js +62 -0
- package/dist/observe/core/invariants.js +12 -0
- package/dist/observe/core/lanes.js +972 -0
- package/dist/observe/core/owner.js +371 -0
- package/dist/observe/core/scheduler.js +1157 -0
- package/dist/observe/core/truth.js +47 -0
- package/dist/observe/core/verdict.js +470 -0
- package/dist/observe/deferred.js +186 -0
- package/dist/observe/flatten.js +79 -0
- package/dist/observe/from.js +248 -0
- package/dist/observe/index.js +71 -0
- package/dist/observe/map.js +593 -0
- package/dist/observe/reveal.js +212 -0
- package/dist/observe/signals.js +717 -0
- package/dist/observe/store/affects.js +142 -0
- package/dist/observe/store/edges.js +39 -0
- package/dist/observe/store/hooks.js +16 -0
- package/dist/observe/store/index.js +47 -0
- package/dist/observe/store/optimistic.js +713 -0
- package/dist/observe/store/projection.js +345 -0
- package/dist/observe/store/reconcile.js +281 -0
- package/dist/observe/store/store.js +2127 -0
- package/dist/observe/store/storePath.js +103 -0
- package/dist/observe/store/target.js +46 -0
- package/dist/observe/store/types.js +83 -0
- package/dist/observe/store/utils.js +1063 -0
- package/dist/prod/affects-marks.js +86 -0
- package/dist/prod/affects.js +98 -112
- package/dist/prod/attribution.js +45 -0
- package/dist/prod/boundaries.js +578 -468
- package/dist/prod/core/action.js +92 -59
- package/dist/prod/core/async.js +522 -336
- package/dist/prod/core/attribution-hooks.js +3 -0
- package/dist/prod/core/constants.js +168 -44
- package/dist/prod/core/context.js +12 -16
- package/dist/prod/core/copies.js +10 -0
- package/dist/prod/core/core.js +1345 -627
- package/dist/prod/core/dev.js +123 -1
- package/dist/prod/core/effect.js +113 -80
- package/dist/prod/core/error-hooks.js +71 -0
- package/dist/prod/core/error.js +18 -4
- package/dist/prod/core/external.js +6 -7
- package/dist/prod/core/graph.js +119 -72
- package/dist/prod/core/heap.js +85 -109
- package/dist/prod/core/identities.js +62 -0
- package/dist/prod/core/invariants.js +1 -32
- package/dist/prod/core/lanes.js +931 -132
- package/dist/prod/core/owner.js +192 -129
- package/dist/prod/core/scheduler.js +948 -642
- package/dist/prod/core/truth.js +47 -0
- package/dist/prod/core/verdict.js +409 -420
- package/dist/prod/deferred.js +184 -0
- package/dist/prod/flatten.js +77 -0
- package/dist/prod/from.js +248 -0
- package/dist/prod/index.js +40 -16
- package/dist/prod/map.js +481 -211
- package/dist/prod/reveal.js +212 -0
- package/dist/prod/signals.js +401 -84
- package/dist/prod/store/affects.js +142 -0
- package/dist/prod/store/edges.js +39 -0
- package/dist/prod/store/hooks.js +16 -0
- package/dist/prod/store/index.js +27 -18
- package/dist/prod/store/optimistic.js +707 -0
- package/dist/prod/store/projection.js +331 -0
- package/dist/prod/store/reconcile.js +281 -0
- package/dist/prod/store/store.js +1952 -267
- package/dist/prod/store/storePath.js +1 -1
- package/dist/prod/store/target.js +43 -0
- package/dist/prod/store/types.js +83 -0
- package/dist/prod/store/utils.js +997 -138
- package/dist/types/affects-marks.d.ts +14 -0
- package/dist/types/affects.d.ts +12 -30
- package/dist/types/attribution.d.ts +19 -0
- package/dist/types/attribution.prod.d.ts +22 -0
- package/dist/types/boundaries.d.ts +99 -124
- package/dist/types/carved.d.ts +1 -0
- package/dist/types/core/action.d.ts +23 -9
- package/dist/types/core/async.d.ts +34 -6
- package/dist/types/core/attribution-costs.d.ts +35 -0
- package/dist/types/core/attribution-feedback.d.ts +133 -0
- package/dist/types/core/attribution-hooks.d.ts +307 -8
- package/dist/types/core/attribution-queries.d.ts +24 -0
- package/dist/types/core/attribution.d.ts +802 -72
- package/dist/types/core/constants.d.ts +195 -38
- package/dist/types/core/copies.d.ts +54 -0
- package/dist/types/core/core.d.ts +295 -65
- package/dist/types/core/dev.d.ts +509 -22
- package/dist/types/core/effect.d.ts +1 -1
- package/dist/types/core/error-hooks.d.ts +71 -0
- package/dist/types/core/error.d.ts +18 -1
- package/dist/types/core/graph.d.ts +40 -1
- package/dist/types/core/heap.d.ts +5 -5
- package/dist/types/core/identities.d.ts +17 -0
- package/dist/types/core/index.d.ts +11 -7
- package/dist/types/core/invariants.d.ts +1 -26
- package/dist/types/core/lanes.d.ts +74 -43
- package/dist/types/core/owner.d.ts +27 -9
- package/dist/types/core/scheduler.d.ts +353 -102
- package/dist/types/core/truth.d.ts +18 -0
- package/dist/types/core/types.d.ts +97 -71
- package/dist/types/core/verdict.d.ts +35 -0
- package/dist/types/deferred.d.ts +28 -0
- package/dist/types/flatten.d.ts +8 -0
- package/dist/types/from.d.ts +47 -0
- package/dist/types/index.d.ts +33 -7
- package/dist/types/map.d.ts +4 -2
- package/dist/types/reveal.d.ts +19 -0
- package/dist/types/signals.d.ts +196 -45
- package/dist/types/store/affects.d.ts +12 -0
- package/dist/types/store/edges.d.ts +9 -0
- package/dist/types/store/hooks.d.ts +62 -0
- package/dist/types/store/index.d.ts +42 -15
- package/dist/types/store/optimistic.d.ts +24 -45
- package/dist/types/store/projection.d.ts +17 -68
- package/dist/types/store/reconcile.d.ts +3 -46
- package/dist/types/store/store.d.ts +133 -136
- package/dist/types/store/storePath.d.ts +1 -1
- package/dist/types/store/target.d.ts +166 -0
- package/dist/types/store/types.d.ts +105 -0
- package/dist/types/store/utils.d.ts +188 -0
- package/package.json +31 -26
- package/dist/node.cjs +0 -8745
- package/dist/prod/core/optimistic.js +0 -265
- package/dist/prod/store/next/optimistic.js +0 -385
- package/dist/prod/store/next/projection.js +0 -172
- package/dist/prod/store/next/reconcile.js +0 -331
- package/dist/prod/store/next/store.js +0 -1499
- package/dist/prod/store/next/target.js +0 -20
- package/dist/types/core/optimistic.d.ts +0 -6
- package/dist/types/store/next/optimistic.d.ts +0 -20
- package/dist/types/store/next/projection.d.ts +0 -8
- package/dist/types/store/next/reconcile.d.ts +0 -3
- package/dist/types/store/next/store.d.ts +0 -77
- package/dist/types/store/next/target.d.ts +0 -117
- package/dist/types-cjs/affects.d.cts +0 -47
- package/dist/types-cjs/boundaries.d.cts +0 -175
- package/dist/types-cjs/core/action.d.cts +0 -64
- package/dist/types-cjs/core/async.d.cts +0 -21
- package/dist/types-cjs/core/attribution-hooks.d.cts +0 -52
- package/dist/types-cjs/core/attribution.d.cts +0 -186
- package/dist/types-cjs/core/constants.d.cts +0 -97
- package/dist/types-cjs/core/context.d.cts +0 -36
- package/dist/types-cjs/core/core.d.cts +0 -163
- package/dist/types-cjs/core/dev.d.cts +0 -81
- package/dist/types-cjs/core/effect.d.cts +0 -30
- package/dist/types-cjs/core/error.d.cts +0 -47
- package/dist/types-cjs/core/external.d.cts +0 -15
- package/dist/types-cjs/core/graph.d.cts +0 -6
- package/dist/types-cjs/core/heap.d.cts +0 -22
- package/dist/types-cjs/core/index.d.cts +0 -13
- package/dist/types-cjs/core/invariants.d.cts +0 -59
- package/dist/types-cjs/core/lanes.d.cts +0 -44
- package/dist/types-cjs/core/optimistic.d.cts +0 -6
- package/dist/types-cjs/core/owner.d.cts +0 -124
- package/dist/types-cjs/core/scheduler.d.cts +0 -180
- package/dist/types-cjs/core/types.d.cts +0 -192
- package/dist/types-cjs/core/verdict.d.cts +0 -2
- package/dist/types-cjs/index.d.cts +0 -10
- package/dist/types-cjs/map.d.cts +0 -69
- package/dist/types-cjs/package.json +0 -3
- package/dist/types-cjs/signals.d.cts +0 -624
- package/dist/types-cjs/store/index.d.cts +0 -19
- package/dist/types-cjs/store/next/optimistic.d.cts +0 -20
- package/dist/types-cjs/store/next/projection.d.cts +0 -8
- package/dist/types-cjs/store/next/reconcile.d.cts +0 -3
- package/dist/types-cjs/store/next/store.d.cts +0 -77
- package/dist/types-cjs/store/next/target.d.cts +0 -117
- package/dist/types-cjs/store/optimistic.d.cts +0 -45
- package/dist/types-cjs/store/projection.d.cts +0 -70
- package/dist/types-cjs/store/reconcile.d.cts +0 -46
- package/dist/types-cjs/store/store.d.cts +0 -138
- package/dist/types-cjs/store/storePath.d.cts +0 -58
- package/dist/types-cjs/store/utils.d.cts +0 -74
|
@@ -0,0 +1,717 @@
|
|
|
1
|
+
import { TimeoutError, NotReadyError, unwrapStatusError } from "./core/error.js";
|
|
2
|
+
|
|
3
|
+
import "./core/verdict.js";
|
|
4
|
+
|
|
5
|
+
import { CONFIG_CHILDREN_FORBIDDEN, EFFECT_USER, $REFRESH, CONFIG_AUTO_DISPOSE, CONFIG_AUTHORITATIVE, CONFIG_REDERIVE, REACTIVE_JOINED, CONFIG_FRESH_READ, CONFIG_GUESS, STATUS_PENDING, STATUS_ERROR } from "./core/constants.js";
|
|
6
|
+
|
|
7
|
+
import { computed, signal, runWithOwner, setMemo, setSignal, markRefresh, read, untrack } from "./core/core.js";
|
|
8
|
+
|
|
9
|
+
import { registerGraph } from "./core/dev.js";
|
|
10
|
+
|
|
11
|
+
import { schedule, globalQueue, flushTransaction, dirtyQueue, GlobalQueue, resolveTx, passTx, staleReader, joinFuture } from "./core/scheduler.js";
|
|
12
|
+
|
|
13
|
+
import { cleanup, createRoot, getOwner, dispose } from "./core/owner.js";
|
|
14
|
+
|
|
15
|
+
import { signalHook } from "./core/identities.js";
|
|
16
|
+
|
|
17
|
+
import { effect, trackedEffect } from "./core/effect.js";
|
|
18
|
+
|
|
19
|
+
import { optimisticWrite } from "./core/lanes.js";
|
|
20
|
+
|
|
21
|
+
import { truthRead, truthConfirmed } from "./core/truth.js";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Low-level reactive-cleanup primitive. Registers a callback that runs when
|
|
25
|
+
* the surrounding owner is disposed.
|
|
26
|
+
*
|
|
27
|
+
* **In 2.0 user code this is rare.** The two cases where you might reach for
|
|
28
|
+
* it have better-shaped tools:
|
|
29
|
+
*
|
|
30
|
+
* - **Component lifecycle (mount/unmount, listeners, intervals):** use
|
|
31
|
+
* {@link onSettled} and **return** a cleanup function. Setup and teardown
|
|
32
|
+
* stay paired in one block. This replaces the 1.x `onMount` + `onCleanup`
|
|
33
|
+
* pairing.
|
|
34
|
+
* - **Cleanup tied to an effect run:** `onCleanup` does not belong in
|
|
35
|
+
* `createEffect`'s apply phase. If a compute phase genuinely needs per-run
|
|
36
|
+
* teardown, that's usually a sign the work should be a memo/projection
|
|
37
|
+
* instead, or moved to `onSettled` if it's lifecycle-shaped.
|
|
38
|
+
*
|
|
39
|
+
* Where `onCleanup` is the right tool is **library / custom-primitive
|
|
40
|
+
* internals** — coordinating disposal inside a `createRoot` body, or wiring
|
|
41
|
+
* cleanup to a captured owner via `runWithOwner` from a custom factory.
|
|
42
|
+
* Application code rarely needs to write any of those shapes directly.
|
|
43
|
+
*
|
|
44
|
+
* Must be called inside an owner. Calling outside an owner is a no-op (with a
|
|
45
|
+
* dev-mode warning).
|
|
46
|
+
*
|
|
47
|
+
* Cannot be used inside `createTrackedEffect` or `onSettled` — return a
|
|
48
|
+
* cleanup function from the callback body instead.
|
|
49
|
+
*
|
|
50
|
+
* Cleanups run in unwind order: an owner's children are disposed before its
|
|
51
|
+
* own cleanups, and within one owner later registrations run before earlier
|
|
52
|
+
* ones. In production a component body shares its enclosing owner, so
|
|
53
|
+
* register cleanup before creating children when the order between them
|
|
54
|
+
* matters.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```ts
|
|
58
|
+
* // Library shape: thread a resource's disposal into a *captured* owner
|
|
59
|
+
* // from a factory that has no settle-phase setup of its own. `onSettled`
|
|
60
|
+
* // would queue a callback we don't need; `onCleanup` is the leaner
|
|
61
|
+
* // primitive when the only job is "register disposal on this owner".
|
|
62
|
+
* function bindToOwner<T extends { dispose(): void }>(owner: Owner, resource: T): T {
|
|
63
|
+
* runWithOwner(owner, () => onCleanup(() => resource.dispose()));
|
|
64
|
+
* return resource;
|
|
65
|
+
* }
|
|
66
|
+
* ```
|
|
67
|
+
*/ function onCleanup(e) {
|
|
68
|
+
return cleanup(e);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function accessor(e) {
|
|
72
|
+
const t = read.bind(null, e);
|
|
73
|
+
t[$REFRESH] = e;
|
|
74
|
+
return t;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function createSignal(e, t) {
|
|
78
|
+
if (typeof e === "function") {
|
|
79
|
+
const r = computed(e, t);
|
|
80
|
+
r.l &= ~CONFIG_AUTO_DISPOSE;
|
|
81
|
+
return [ accessor(r), setMemo.bind(null, r) ];
|
|
82
|
+
}
|
|
83
|
+
const r = signal(e, t);
|
|
84
|
+
registerGraph(r, getOwner());
|
|
85
|
+
if (signalHook !== null) signalHook(r);
|
|
86
|
+
return [ accessor(r), setSignal.bind(null, r) ];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function createOptimistic(e, t) {
|
|
90
|
+
if (typeof e === "function") {
|
|
91
|
+
const r = computed(e, t);
|
|
92
|
+
r.l &= ~CONFIG_AUTO_DISPOSE;
|
|
93
|
+
return [ accessor(r), optimisticWrite.bind(null, r) ];
|
|
94
|
+
}
|
|
95
|
+
const r = signal(e, t);
|
|
96
|
+
registerGraph(r, getOwner());
|
|
97
|
+
return [ accessor(r), optimisticWrite.bind(null, r) ];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function createMemo(e, t) {
|
|
101
|
+
return accessor(computed(e, t));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Creates a reactive effect with **separate compute and effect phases**.
|
|
106
|
+
*
|
|
107
|
+
* - `compute(prev)` runs reactively — *put all reactive reads here*. The
|
|
108
|
+
* returned value is passed to `effect` and is also the new "previous" value
|
|
109
|
+
* for the next run.
|
|
110
|
+
* - `effect(next, prev?)` runs imperatively (untracked) after the queue
|
|
111
|
+
* flushes. *Put DOM writes / fetch / logging / subscriptions here.* It may
|
|
112
|
+
* return a cleanup function which runs before the next effect or on
|
|
113
|
+
* disposal.
|
|
114
|
+
*
|
|
115
|
+
* Reactive reads inside `effect` will *not* re-trigger this effect — that's
|
|
116
|
+
* intentional. If you need a single-phase tracked effect, use
|
|
117
|
+
* `createTrackedEffect` (with the tradeoffs noted there).
|
|
118
|
+
*
|
|
119
|
+
* Pass an `EffectBundle` (`{ effect, error }`) instead of a plain function to
|
|
120
|
+
* intercept **compute-phase** errors — errors thrown by `compute` or arriving
|
|
121
|
+
* from upstream reactive sources (including async rejections), which your own
|
|
122
|
+
* code has no frame to `try/catch`. The `error` handler is the error arm of
|
|
123
|
+
* the effect phase: it runs on the same schedule and in the same imperative,
|
|
124
|
+
* writable scope as `effect` (setting error state via signals is fine), and
|
|
125
|
+
* only for *settled* errors — a transient error that recovers before the
|
|
126
|
+
* effect phase runs `effect` with the recovered value instead, and a held
|
|
127
|
+
* transition defers it exactly as it defers `effect`. Without an `error`
|
|
128
|
+
* handler a compute-phase error is logged and the effect simply skips that
|
|
129
|
+
* run — a non-render effect's reactivity failing does not crash the app.
|
|
130
|
+
* Rethrowing from `error` escalates it to the nearest error boundary
|
|
131
|
+
* (halting the system if none exists).
|
|
132
|
+
*
|
|
133
|
+
* The **effect phase is different**: it is your own imperative code, so handle
|
|
134
|
+
* failures with `try/catch` where they occur. An uncaught effect-phase throw
|
|
135
|
+
* is treated as an unhandled application error — caught by the nearest
|
|
136
|
+
* `createErrorBoundary`/`<Errored>`, and permanently halting the reactive
|
|
137
|
+
* system if there is none. It is *not* routed to the bundle's `error` handler.
|
|
138
|
+
*
|
|
139
|
+
* ```typescript
|
|
140
|
+
* createEffect<T>(compute, effectFn | { effect, error }, options?: EffectOptions);
|
|
141
|
+
* ```
|
|
142
|
+
* @param compute a function that receives its previous value and returns a new value used to react on a computation
|
|
143
|
+
* @param effectFn a function that receives the new value and is used to perform side effects (return a cleanup function), or an `EffectBundle` with `effect` and `error` handlers
|
|
144
|
+
* @param options `EffectOptions` -- name, defer, schedule, transparent
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```ts
|
|
148
|
+
* const [count, setCount] = createSignal(0);
|
|
149
|
+
*
|
|
150
|
+
* createEffect(
|
|
151
|
+
* () => count(), // compute: tracks `count`
|
|
152
|
+
* value => console.log(value) // effect: side effect
|
|
153
|
+
* );
|
|
154
|
+
*
|
|
155
|
+
* setCount(1); // logs 1 after the next flush
|
|
156
|
+
* ```
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```ts
|
|
160
|
+
* createEffect(
|
|
161
|
+
* () => userId(),
|
|
162
|
+
* id => {
|
|
163
|
+
* const ctrl = new AbortController();
|
|
164
|
+
* fetch(`/users/${id}`, { signal: ctrl.signal });
|
|
165
|
+
* return () => ctrl.abort(); // cleanup before next run / disposal
|
|
166
|
+
* }
|
|
167
|
+
* );
|
|
168
|
+
* ```
|
|
169
|
+
*
|
|
170
|
+
* @description https://v2.solidjs.com/reference/solid-js/reactivity/create-effect
|
|
171
|
+
*/ function createEffect(e, t, r) {
|
|
172
|
+
effect(e, t.effect || t, t.error, {
|
|
173
|
+
user: true,
|
|
174
|
+
...r
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Creates a reactive computation that runs during the render phase as DOM elements
|
|
180
|
+
* are created and updated but not necessarily connected.
|
|
181
|
+
*
|
|
182
|
+
* Same compute / effect split as `createEffect`, but scheduled inside the render
|
|
183
|
+
* queue rather than after it. Reach for this only when authoring renderer
|
|
184
|
+
* plumbing (custom DOM bindings, JSX-generated `insert()` / `spread()` calls).
|
|
185
|
+
* App code should use `createEffect`.
|
|
186
|
+
*
|
|
187
|
+
* ```typescript
|
|
188
|
+
* createRenderEffect<T>(compute, effectFn, options?: EffectOptions);
|
|
189
|
+
* ```
|
|
190
|
+
* @param compute a function that receives its previous value and returns a new value used to react on a computation
|
|
191
|
+
* @param effectFn a function that receives the new value and is used to perform side effects
|
|
192
|
+
* @param options `EffectOptions` -- name, defer, schedule, transparent
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* ```ts
|
|
196
|
+
* // Custom directive: bind an element's textContent to a reactive source.
|
|
197
|
+
* function bindText(el: HTMLElement, source: () => string) {
|
|
198
|
+
* createRenderEffect(
|
|
199
|
+
* () => source(),
|
|
200
|
+
* value => { el.textContent = value; }
|
|
201
|
+
* );
|
|
202
|
+
* }
|
|
203
|
+
* ```
|
|
204
|
+
*
|
|
205
|
+
* @description https://v2.solidjs.com/reference/solid-js/advanced/specialized-reactivity/create-render-effect
|
|
206
|
+
*/ function createRenderEffect(e, t, r) {
|
|
207
|
+
effect(e, t, undefined, r);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Creates a tracked reactive effect where dependency tracking and side effects happen
|
|
212
|
+
* in the same scope.
|
|
213
|
+
*
|
|
214
|
+
* @deprecated Do not use in new code. For a side effect that follows reactive
|
|
215
|
+
* state, use `createEffect(compute, effect)` — it separates tracking from the
|
|
216
|
+
* side effect, knows its dependencies before it runs, and participates in
|
|
217
|
+
* async and transitions. For one-time DOM work after render (measuring,
|
|
218
|
+
* attaching third-party widgets to a ref), use `onSettled`. Tracking from
|
|
219
|
+
* inside the effect phase — the only thing this primitive adds — is retained
|
|
220
|
+
* solely to ease 1.x migration: it runs beside user-effect callbacks after
|
|
221
|
+
* values commit, never holds a transition, and cannot observe a write staged
|
|
222
|
+
* earlier in the same flush by a signal it has not read yet.
|
|
223
|
+
*
|
|
224
|
+
* WARNING: Because tracking and effects happen in the same scope, this primitive
|
|
225
|
+
* may run multiple times for a single change or show tearing (reading inconsistent
|
|
226
|
+
* state). Use only when dynamic subscription patterns require same-scope tracking.
|
|
227
|
+
*
|
|
228
|
+
* The callback runs during the flush itself: writes made inside it are queued
|
|
229
|
+
* into the same flush's continuation and are never visible to the callback's
|
|
230
|
+
* own reads (reads return settled values, as in every effect-phase scope), and
|
|
231
|
+
* `flush()` cannot be called from inside it (dev throws; production is a
|
|
232
|
+
* no-op) — defer with `queueMicrotask(() => flush())` if needed.
|
|
233
|
+
*
|
|
234
|
+
* ```typescript
|
|
235
|
+
* createTrackedEffect(compute, options?: { name?: string });
|
|
236
|
+
* ```
|
|
237
|
+
* @param compute a function that contains reactive reads to track and returns an optional cleanup function to run on disposal or before next execution
|
|
238
|
+
* @param options -- name
|
|
239
|
+
*
|
|
240
|
+
* @example
|
|
241
|
+
* ```ts
|
|
242
|
+
* createTrackedEffect(() => {
|
|
243
|
+
* const target = focusedNode();
|
|
244
|
+
* if (!target) return;
|
|
245
|
+
*
|
|
246
|
+
* const handler = () => log(target.value());
|
|
247
|
+
* target.on("change", handler);
|
|
248
|
+
*
|
|
249
|
+
* return () => target.off("change", handler);
|
|
250
|
+
* });
|
|
251
|
+
* ```
|
|
252
|
+
*
|
|
253
|
+
* @description https://v2.solidjs.com/reference/solid-js/advanced/specialized-reactivity/create-tracked-effect
|
|
254
|
+
*/ function createTrackedEffect(e, t) {
|
|
255
|
+
trackedEffect(e, t);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Creates a reactive computation that runs after the render phase with flexible tracking.
|
|
260
|
+
*
|
|
261
|
+
* ```typescript
|
|
262
|
+
* const track = createReaction(effectFn, options?: EffectOptions);
|
|
263
|
+
* track(() => { // reactive reads });
|
|
264
|
+
* ```
|
|
265
|
+
* @param effectFn a function (or `EffectBundle`) that is called when tracked function is invalidated
|
|
266
|
+
* @param options `EffectOptions` -- name, defer
|
|
267
|
+
*
|
|
268
|
+
* @example
|
|
269
|
+
* ```ts
|
|
270
|
+
* const [count, setCount] = createSignal(0);
|
|
271
|
+
*
|
|
272
|
+
* const track = createReaction(() => {
|
|
273
|
+
* console.log("count changed once, re-arm to listen again");
|
|
274
|
+
* track(() => count()); // re-arm
|
|
275
|
+
* });
|
|
276
|
+
*
|
|
277
|
+
* track(() => count()); // initial arm
|
|
278
|
+
*
|
|
279
|
+
* setCount(1); // logs once, reaction re-armed for next change
|
|
280
|
+
* ```
|
|
281
|
+
*
|
|
282
|
+
* @description https://v2.solidjs.com/reference/solid-js/advanced/specialized-reactivity/create-reaction
|
|
283
|
+
*/ function createReaction(e, t) {
|
|
284
|
+
let r = undefined;
|
|
285
|
+
cleanup(() => r?.());
|
|
286
|
+
const n = getOwner();
|
|
287
|
+
// The currently armed effect node. `track()` replaces the previous
|
|
288
|
+
// subscription (1.x semantics): without disposing the superseded arm, its
|
|
289
|
+
// sources stayed live (firing the callback for replaced dependencies), each
|
|
290
|
+
// accumulated arm delivered its own fire, and un-fired arms leaked as live
|
|
291
|
+
// effect nodes until the owner disposed (#2861).
|
|
292
|
+
let o;
|
|
293
|
+
return c => {
|
|
294
|
+
if (o) {
|
|
295
|
+
dispose(o);
|
|
296
|
+
o = undefined;
|
|
297
|
+
}
|
|
298
|
+
runWithOwner(n, () => {
|
|
299
|
+
effect(() => {
|
|
300
|
+
const e = getOwner();
|
|
301
|
+
o = e;
|
|
302
|
+
c();
|
|
303
|
+
return e;
|
|
304
|
+
}, t => {
|
|
305
|
+
o = undefined;
|
|
306
|
+
r?.();
|
|
307
|
+
const n = (e.effect || e)?.();
|
|
308
|
+
if (false && n !== undefined && typeof n !== "function") ;
|
|
309
|
+
r = n;
|
|
310
|
+
dispose(t);
|
|
311
|
+
}, e.error, {
|
|
312
|
+
...t,
|
|
313
|
+
user: true,
|
|
314
|
+
defer: true
|
|
315
|
+
});
|
|
316
|
+
});
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/** A promise over a reactive expression (`resolve`, `until`): a pass that
|
|
321
|
+
* runs the expression and settles on a microtask. Not an effect — an
|
|
322
|
+
* effect's run is the flush's effect phase, and a flush parked by the
|
|
323
|
+
* action that is `yield`ing on this promise stashes its runs with the
|
|
324
|
+
* transaction (#2930); the heap runs a pass parked or not. A pending read
|
|
325
|
+
* re-runs it when the source lands (the pass is pending itself); the
|
|
326
|
+
* expression throwing settles the rejection. */
|
|
327
|
+
/** The promise-delivery readers' pass (`resolve`, `until`, `refresh`'s
|
|
328
|
+
* waiter): delivers what it read on a microtask. By posture (#3482, #3490):
|
|
329
|
+
* - the reader of the transaction it was created in (`own`: an action's
|
|
330
|
+
* body, a `refresh()` called there — the waiter reads its slice's frame on
|
|
331
|
+
* a later microtask) delivers that frame — the staged landing an action
|
|
332
|
+
* holds (the action awaits the promise that would otherwise wait on its
|
|
333
|
+
* own settle);
|
|
334
|
+
* - a mainline reader whose pass read a frame another transaction holds
|
|
335
|
+
* joined it (L2, born held — A29) and delivers at that commit: the
|
|
336
|
+
* committed view, never a foreign hold's unrevealed frame. */ function watch(e, t, r, n = 0, o, c = flushTransaction) {
|
|
337
|
+
if (n & CONFIG_AUTHORITATIVE) {
|
|
338
|
+
GlobalQueue.un = truthRead;
|
|
339
|
+
GlobalQueue.tn = truthConfirmed;
|
|
340
|
+
}
|
|
341
|
+
// `config` is on the node for its first pass too (the pass runs inside
|
|
342
|
+
// `computed()`): `until`'s predicate is authoritative from its first read,
|
|
343
|
+
// or an already-displayed guess would satisfy it in a later slice. (REDERIVE
|
|
344
|
+
// after: the first pass's own error is the promise's rejection.)
|
|
345
|
+
computed(() => {
|
|
346
|
+
let u, i = false;
|
|
347
|
+
try {
|
|
348
|
+
u = e();
|
|
349
|
+
} catch (e) {
|
|
350
|
+
if (e instanceof NotReadyError) throw e;
|
|
351
|
+
// An error is an answer (D-41): delivered by the same frame rule.
|
|
352
|
+
u = unwrapStatusError(e);
|
|
353
|
+
i = true;
|
|
354
|
+
}
|
|
355
|
+
const s = getOwner();
|
|
356
|
+
// A truth reader waits out a `createDeferred` flight its value derives
|
|
357
|
+
// from, which serves everyone else the stale answer (D-40).
|
|
358
|
+
const f = n & CONFIG_AUTHORITATIVE && GlobalQueue.er?.(s);
|
|
359
|
+
if (f) throw new NotReadyError(f);
|
|
360
|
+
// (Outside a flush a mainline pass joins through `passTx`.)
|
|
361
|
+
const a = flushTransaction ?? passTx;
|
|
362
|
+
if (s.R & REACTIVE_JOINED && a !== null && (c === null || resolveTx(c) !== resolveTx(a))) {
|
|
363
|
+
staleReader(s, resolveTx(a));
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
if (i) return void queueMicrotask(() => r(u));
|
|
367
|
+
o?.(u);
|
|
368
|
+
queueMicrotask(() => t(u));
|
|
369
|
+
}, {
|
|
370
|
+
ct: n
|
|
371
|
+
}).l |= CONFIG_REDERIVE;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Awaits a reactive expression and returns its first fully-settled value as a
|
|
376
|
+
* `Promise`. Pending async reads (`createMemo` returning a promise, etc.) are
|
|
377
|
+
* waited on; once the expression returns synchronously without `NotReadyError`
|
|
378
|
+
* the promise resolves with that value. If the expression settles with an
|
|
379
|
+
* error instead — including an async source that rejects — the promise
|
|
380
|
+
* rejects with it.
|
|
381
|
+
*
|
|
382
|
+
* Must be called *outside* a tracking scope — it doesn't subscribe, it just
|
|
383
|
+
* resolves the current value once.
|
|
384
|
+
*
|
|
385
|
+
* @example
|
|
386
|
+
* ```ts
|
|
387
|
+
* const user = createMemo(() => fetch(`/users/${id()}`).then(r => r.json()));
|
|
388
|
+
*
|
|
389
|
+
* // outside any reactive scope
|
|
390
|
+
* const initial = await resolve(() => user());
|
|
391
|
+
* ```
|
|
392
|
+
*
|
|
393
|
+
* @param fn a reactive expression to resolve
|
|
394
|
+
*/ function resolve(e) {
|
|
395
|
+
return new Promise((t, r) => {
|
|
396
|
+
createRoot(n => watch(e, e => (t(e), n()), e => (r(e), n())));
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Invalidates one reactive source, forcing it to re-execute even if its inputs
|
|
402
|
+
* haven't changed, and returns a promise for the target's NEXT QUIESCENT
|
|
403
|
+
* STATE — the re-ask (and anything that supersedes it) has settled.
|
|
404
|
+
*
|
|
405
|
+
* Pass either a Solid-created accessor or a projected store created from
|
|
406
|
+
* `createStore(fn, ...)` / `createProjection(...)`. `refresh()` is a
|
|
407
|
+
* write-like invalidation operation: it does not read the target's value, and
|
|
408
|
+
* refreshing a plain signal accessor is a no-op that resolves immediately.
|
|
409
|
+
*
|
|
410
|
+
* The returned promise is safe to ignore (fire-and-forget refresh is
|
|
411
|
+
* unchanged, and a failed refetch will not surface an unhandled rejection).
|
|
412
|
+
* Awaiting it gives imperative flows the settle point without a reactive
|
|
413
|
+
* read:
|
|
414
|
+
* - Accessor targets resolve with the settled value; store targets resolve
|
|
415
|
+
* with the store node passed (reads through it are fresh after the await).
|
|
416
|
+
* - A failed re-ask rejects with the error.
|
|
417
|
+
* - Semantics are quiescence, not flight identity: if another refresh (or
|
|
418
|
+
* any invalidation) supersedes this one mid-flight, the promise waits for
|
|
419
|
+
* — and delivers — whatever finally lands.
|
|
420
|
+
*
|
|
421
|
+
* @example
|
|
422
|
+
* ```ts
|
|
423
|
+
* const user = createMemo(async () => fetch(`/users/${id()}`).then(r => r.json()));
|
|
424
|
+
*
|
|
425
|
+
* // Fire-and-forget re-fetch
|
|
426
|
+
* <button onClick={() => refresh(user)}>Reload</button>;
|
|
427
|
+
*
|
|
428
|
+
* // Imperative settle point
|
|
429
|
+
* const fresh = await refresh(user);
|
|
430
|
+
* ```
|
|
431
|
+
*/ function refresh(e) {
|
|
432
|
+
const t = e?.[$REFRESH];
|
|
433
|
+
if (!t) {
|
|
434
|
+
return Promise.resolve(undefined);
|
|
435
|
+
}
|
|
436
|
+
// Mark now, watch on a microtask. The waiter is resolve()'s machinery with
|
|
437
|
+
// two extra reader bits, but it must NOT compute at call time (effects
|
|
438
|
+
// recompute eagerly on creation): same-tick refreshes coalesce into ONE
|
|
439
|
+
// re-ask only because every mark lands before anything pulls, and eager
|
|
440
|
+
// per-call pulls turned three refreshes into three fetches. Deferred, the
|
|
441
|
+
// waiter's first read sees the coalesced state: FRESH_READ pulls the node
|
|
442
|
+
// through recompute if it is still dirty (self-deduping — a clean node
|
|
443
|
+
// no-ops, so N waiters cost one pull; this also closes the race where a
|
|
444
|
+
// waiter reads the PRE-re-ask value as settled and delivers stale), after
|
|
445
|
+
// which the read either parks on the re-ask's pending window (async — the
|
|
446
|
+
// settle walk re-runs it on every landing, equal-value included, and a
|
|
447
|
+
// rejection arrives through the effect's error channel) or serves the sync
|
|
448
|
+
// answer. resolve()'s own eager compute is untouched: created after a
|
|
449
|
+
// refresh it still settles stale-while-revalidate (#2930) — its contract is
|
|
450
|
+
// "first settled value", not "next quiescent state".
|
|
451
|
+
markRefresh(t);
|
|
452
|
+
// The transaction `refresh()` is called in (an action's slice): the
|
|
453
|
+
// waiter, created on the microtask below, is its reader (`watch`).
|
|
454
|
+
const r = flushTransaction;
|
|
455
|
+
const n = new Promise((n, o) => {
|
|
456
|
+
queueMicrotask(() => {
|
|
457
|
+
// The root is load-bearing: an ownerless computed is auto-disposing,
|
|
458
|
+
// and the settle walk releases an unobserved one instead of re-running
|
|
459
|
+
// it, so the waiter would never see the refetch land (#3888). Settle
|
|
460
|
+
// disposes the waiter directly; the capture runs inside the watcher's
|
|
461
|
+
// own pass, where the ambient owner IS the watcher — exactly what
|
|
462
|
+
// dispose() takes.
|
|
463
|
+
let c = null;
|
|
464
|
+
const make = () => watch(() => {
|
|
465
|
+
if (c === null) c = getOwner();
|
|
466
|
+
// Authoritative: the truth beneath a guess (`until`'s view). A
|
|
467
|
+
// guessed node's own re-ask is not answered by it (#3895): park
|
|
468
|
+
// on that flight, or reject with its error.
|
|
469
|
+
const e = read(t);
|
|
470
|
+
if (t.l & CONFIG_GUESS && t.m & (STATUS_PENDING | STATUS_ERROR)) throw t.T?.U;
|
|
471
|
+
return e;
|
|
472
|
+
}, t => {
|
|
473
|
+
n(typeof e === "function" ? t : e);
|
|
474
|
+
dispose(c);
|
|
475
|
+
}, e => {
|
|
476
|
+
o(e);
|
|
477
|
+
dispose(c);
|
|
478
|
+
}, CONFIG_FRESH_READ | CONFIG_AUTHORITATIVE, undefined, r);
|
|
479
|
+
createRoot(make);
|
|
480
|
+
});
|
|
481
|
+
});
|
|
482
|
+
// Fire-and-forget refresh must not turn a failed refetch into an unhandled
|
|
483
|
+
// rejection; awaiting callers attach their own handlers to `promise`.
|
|
484
|
+
n.catch(() => {});
|
|
485
|
+
return n;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* Awaits a reactive predicate and resolves the first time it settles *truthy*,
|
|
490
|
+
* with that (narrowed) value. Falsy results and pending async reads both mean
|
|
491
|
+
* "not yet": the subscription stays live and re-evaluates as sources change.
|
|
492
|
+
* If the predicate settles with an error — a throw, or an async source that
|
|
493
|
+
* rejects — the promise rejects with it, as do timeout and abort.
|
|
494
|
+
*
|
|
495
|
+
* Where {@link resolve} answers "what is this value" (first settled value,
|
|
496
|
+
* whatever it is), `until` answers "when does the world confirm this
|
|
497
|
+
* condition".
|
|
498
|
+
*
|
|
499
|
+
* This is the acknowledgment mechanism for mutations confirmed on a live data
|
|
500
|
+
* channel (sockets, subscriptions, live queries) rather than by the mutation's
|
|
501
|
+
* own response: correlate by a client-generated id or version in the predicate,
|
|
502
|
+
* and let truth arrive however it arrives — push, refetch, or another tab.
|
|
503
|
+
*
|
|
504
|
+
* Must be called *outside* a tracking scope.
|
|
505
|
+
*
|
|
506
|
+
* @example
|
|
507
|
+
* ```ts
|
|
508
|
+
* const clientId = crypto.randomUUID();
|
|
509
|
+
* await socket.send({ clientId, text }); // fire-and-forget transport
|
|
510
|
+
* // Wait until the live source echoes the write:
|
|
511
|
+
* await until(() => messages.some(m => m.clientId === clientId), { timeout: 10_000 });
|
|
512
|
+
* ```
|
|
513
|
+
*
|
|
514
|
+
* @param fn a reactive predicate
|
|
515
|
+
* @param options optional `timeout` (ms) and abort `signal`
|
|
516
|
+
*/ function until(e, t) {
|
|
517
|
+
return new Promise((r, n) => {
|
|
518
|
+
const o = t?.signal;
|
|
519
|
+
if (o?.aborted) return n(o.reason);
|
|
520
|
+
// The awaiting action's transaction, if any (its body runs joined to
|
|
521
|
+
// it). `until()` is a declaration of relatedness (#3164 follow-up): the
|
|
522
|
+
// predicate names the condition that confirms the action, so the frame
|
|
523
|
+
// whose landing flips it truthy is the confirming event by the user's
|
|
524
|
+
// own definition — it reveals WITH the action's settle, not before
|
|
525
|
+
// (`saving=true` beside the confirmed `version=1` is a tear no timeline
|
|
526
|
+
// contains). Updates that do not flip the predicate reveal freely.
|
|
527
|
+
const c = flushTransaction;
|
|
528
|
+
createRoot(u => {
|
|
529
|
+
let i;
|
|
530
|
+
let s;
|
|
531
|
+
const settle = e => {
|
|
532
|
+
if (i !== undefined) clearTimeout(i);
|
|
533
|
+
if (s !== undefined) o.removeEventListener("abort", s);
|
|
534
|
+
e();
|
|
535
|
+
u();
|
|
536
|
+
};
|
|
537
|
+
// Authoritative: the predicate sees the truth, never the action's own
|
|
538
|
+
// optimistic guess — a guess cannot satisfy the wait for its own
|
|
539
|
+
// confirmation.
|
|
540
|
+
watch(e, e => {
|
|
541
|
+
// Falsy is "not yet": keep the subscription live and wait for the
|
|
542
|
+
// next evaluation. Only a truthy settled value resolves.
|
|
543
|
+
if (e) settle(() => r(e));
|
|
544
|
+
}, e => settle(() => n(e)), CONFIG_AUTHORITATIVE, e => {
|
|
545
|
+
// The flip, inside the pass of the flush carrying it: the flush
|
|
546
|
+
// joins the action's transaction, and what it staged is held and
|
|
547
|
+
// revealed with the action.
|
|
548
|
+
if (e && c !== null && globalQueue.Y && resolveTx(c).tt !== 0) joinFuture(c);
|
|
549
|
+
});
|
|
550
|
+
if (t?.timeout !== undefined) i = setTimeout(() => settle(() => n(new TimeoutError)), t.timeout);
|
|
551
|
+
if (o !== undefined) {
|
|
552
|
+
s = () => settle(() => n(o.reason));
|
|
553
|
+
o.addEventListener("abort", s, {
|
|
554
|
+
once: true
|
|
555
|
+
});
|
|
556
|
+
}
|
|
557
|
+
});
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
* Schedules `callback` to run **once** after the reactive graph has fully
|
|
563
|
+
* settled — i.e. once every pending async read inside the current owner has
|
|
564
|
+
* resolved and the queue has flushed. Each call registers a single fire; it
|
|
565
|
+
* does not create an ongoing subscription.
|
|
566
|
+
*
|
|
567
|
+
* The canonical lifecycle primitive in 2.0. Three main usages:
|
|
568
|
+
*
|
|
569
|
+
* - **Component-level setup-and-teardown** *(the most common shape)*: run
|
|
570
|
+
* setup after the component's first stable render and **return a cleanup
|
|
571
|
+
* function** to dispose it on owner disposal. This is the replacement for
|
|
572
|
+
* the 1.x `onMount` + `onCleanup` pairing — setup and teardown live in one
|
|
573
|
+
* block, and `onCleanup` is no longer the right tool for component
|
|
574
|
+
* bodies. (`onMount` no longer exists in 2.0.)
|
|
575
|
+
* - **Post-settle "ready" hook:** run once after a component's first stable
|
|
576
|
+
* render — analytics ping, focus, scroll-into-view, etc. No cleanup needed.
|
|
577
|
+
* - **Inside an event handler:** schedule work to run after the action /
|
|
578
|
+
* transition triggered by the event has completed.
|
|
579
|
+
*
|
|
580
|
+
* Reactive reads inside the callback are *not* tracked — to react to
|
|
581
|
+
* subsequent settles, register a new `onSettled` each time.
|
|
582
|
+
*
|
|
583
|
+
* The callback runs during the settle flush itself, which gives it the same
|
|
584
|
+
* write semantics as every other effect-phase scope (the effect half of
|
|
585
|
+
* `createEffect`, event handlers):
|
|
586
|
+
*
|
|
587
|
+
* - **Writes** are queued into the same flush's continuation — dependent memos
|
|
588
|
+
* and effects update before the flush returns — but reads inside the
|
|
589
|
+
* callback keep returning the settled (pre-write) values. A callback never
|
|
590
|
+
* observes its own unsettled write. Functional setters still compose:
|
|
591
|
+
* `set(v => v + 1)` twice increments twice.
|
|
592
|
+
* - **`flush()` cannot be called** from inside the callback — the flush is
|
|
593
|
+
* already running (dev throws; production is a no-op). To force a drain
|
|
594
|
+
* after this settle, defer it: `queueMicrotask(() => flush())`.
|
|
595
|
+
*
|
|
596
|
+
* `onCleanup` is **not** allowed inside the callback — return a cleanup
|
|
597
|
+
* function instead. The returned cleanup runs on owner disposal.
|
|
598
|
+
*
|
|
599
|
+
* A cleanup return is only honored when `onSettled` is called from an **owned**
|
|
600
|
+
* scope (e.g. a component body). When it fires out of band from an *unowned*
|
|
601
|
+
* scope — an event handler, a tracked effect, or another `onSettled` — there is
|
|
602
|
+
* no owner lifecycle to bind a cleanup to; returning one is a dev-mode error
|
|
603
|
+
* (and is dropped in production). Use the post-settle/event-handler forms below
|
|
604
|
+
* for one-shot work, and keep setup-with-teardown in an owned scope.
|
|
605
|
+
*
|
|
606
|
+
* @example
|
|
607
|
+
* ```tsx
|
|
608
|
+
* // Component-level setup + teardown — replaces onMount + onCleanup.
|
|
609
|
+
* // Subscribe to an external source on mount, unsubscribe on dispose.
|
|
610
|
+
* function useViewportWidth() {
|
|
611
|
+
* const [width, setWidth] = createSignal(window.innerWidth);
|
|
612
|
+
* onSettled(() => {
|
|
613
|
+
* const onResize = () => setWidth(window.innerWidth);
|
|
614
|
+
* window.addEventListener("resize", onResize);
|
|
615
|
+
* return () => window.removeEventListener("resize", onResize);
|
|
616
|
+
* });
|
|
617
|
+
* return width;
|
|
618
|
+
* }
|
|
619
|
+
* ```
|
|
620
|
+
*
|
|
621
|
+
* @example
|
|
622
|
+
* ```tsx
|
|
623
|
+
* // Post-settle "ready" hook — no cleanup needed.
|
|
624
|
+
* function Dashboard() {
|
|
625
|
+
* const data = createMemo(async () => fetchData());
|
|
626
|
+
*
|
|
627
|
+
* onSettled(() => {
|
|
628
|
+
* analytics.track("dashboard.ready");
|
|
629
|
+
* });
|
|
630
|
+
*
|
|
631
|
+
* return <Loading fallback={<Spinner />}><pre>{data()}</pre></Loading>;
|
|
632
|
+
* }
|
|
633
|
+
* ```
|
|
634
|
+
*
|
|
635
|
+
* @example
|
|
636
|
+
* ```tsx
|
|
637
|
+
* // Event-handler — runs after the action settles.
|
|
638
|
+
* function SaveButton() {
|
|
639
|
+
* const save = action(function* () {
|
|
640
|
+
* yield api.save();
|
|
641
|
+
* });
|
|
642
|
+
*
|
|
643
|
+
* const handleClick = () => {
|
|
644
|
+
* save();
|
|
645
|
+
* onSettled(() => toast("Saved!"));
|
|
646
|
+
* };
|
|
647
|
+
*
|
|
648
|
+
* return <button onClick={handleClick}>Save</button>;
|
|
649
|
+
* }
|
|
650
|
+
* ```
|
|
651
|
+
*
|
|
652
|
+
* @param callback Function to run; may return a cleanup function that fires
|
|
653
|
+
* on owner disposal
|
|
654
|
+
*/ function onSettled(e) {
|
|
655
|
+
const t = getOwner();
|
|
656
|
+
t && !(t.l & CONFIG_CHILDREN_FORBIDDEN) ? trackedEffect(() => untrack(e), {
|
|
657
|
+
name: "onSettled"
|
|
658
|
+
}) : globalQueue.enqueue(EFFECT_USER, function fire() {
|
|
659
|
+
// Settled means derived. A settle that reverts optimism (or replays
|
|
660
|
+
// gated reads) only enqueues the affected subscribers; the pass after
|
|
661
|
+
// the commit re-derives them. Fired in the commit pass, the callback
|
|
662
|
+
// read the optimistic source already reverted beside a sync memo of it
|
|
663
|
+
// still holding the optimistic value — reads do not pull (#3411). Fall
|
|
664
|
+
// to the next pass while the heap has work; `run` swapped the queue,
|
|
665
|
+
// so this lands there, and `enqueue` keeps the drain alive.
|
|
666
|
+
if (dirtyQueue.yt >= dirtyQueue.Et) return globalQueue.enqueue(EFFECT_USER, fire);
|
|
667
|
+
// Unowned, out-of-band fire (no owner, or a children-forbidden one this
|
|
668
|
+
// one-shot must not bind to): a returned cleanup has no lifecycle to
|
|
669
|
+
// attach to. Reject it in dev; in production the return is simply
|
|
670
|
+
// dropped — never bound to an unrelated owner or run eagerly.
|
|
671
|
+
e();
|
|
672
|
+
});
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
/**
|
|
676
|
+
* Schedules `callback` to run **once**, right after the pending flush
|
|
677
|
+
* finishes, in the same microtask checkpoint (before the browser paints).
|
|
678
|
+
* Unlike `onSettled`, it never waits on async work: if that flush is held by
|
|
679
|
+
* a pending transition, the callback still runs.
|
|
680
|
+
*
|
|
681
|
+
* Its main use is keeping a write out of another update's hold. Writes made
|
|
682
|
+
* in the same synchronous frame are applied together, so when one of them
|
|
683
|
+
* starts async work (a route change, a fetch), the others are held with it
|
|
684
|
+
* until it resolves. A write made in `onFlush` happens after that flush, so
|
|
685
|
+
* it is its own update: it commits on its own unless something it changes
|
|
686
|
+
* also derives from the held work (a memo or `createEffect` reading both),
|
|
687
|
+
* in which case it joins that hold as any later write would.
|
|
688
|
+
*
|
|
689
|
+
* Call it from event handlers and effect callbacks (including
|
|
690
|
+
* `createTrackedEffect` and `onSettled` bodies). In an owned scope such as a
|
|
691
|
+
* component body or a computation it warns in dev: use `onSettled` there.
|
|
692
|
+
*
|
|
693
|
+
* - A write in the callback lands after any write made inline in the same
|
|
694
|
+
* frame. Don't write the same signal both inside and outside it; use a
|
|
695
|
+
* functional setter if the order matters.
|
|
696
|
+
* - Several `onFlush` callbacks run in call order and batch into one flush.
|
|
697
|
+
* - `queueMicrotask` is not a substitute: called before the frame's first
|
|
698
|
+
* write, it runs before that flush and its write joins it.
|
|
699
|
+
*
|
|
700
|
+
* @example
|
|
701
|
+
* ```tsx
|
|
702
|
+
* // A menu closes now even when the consumer's onSelect starts async work.
|
|
703
|
+
* const select = (id: string) => {
|
|
704
|
+
* onFlush(() => setOpen(false));
|
|
705
|
+
* props.onSelect?.(id);
|
|
706
|
+
* };
|
|
707
|
+
* ```
|
|
708
|
+
*
|
|
709
|
+
* @param callback Function to run once after the pending flush
|
|
710
|
+
*/ function onFlush(e) {
|
|
711
|
+
// The frame's flush is queued (or already draining) ahead of this
|
|
712
|
+
// microtask, so the callback's writes start a flush of their own.
|
|
713
|
+
schedule();
|
|
714
|
+
queueMicrotask(e);
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
export { accessor, createEffect, createMemo, createOptimistic, createReaction, createRenderEffect, createSignal, createTrackedEffect, onCleanup, onFlush, onSettled, refresh, resolve, until };
|