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