@solidjs/signals 2.0.0-beta.9 → 2.0.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -10
- package/dist/dev.js +6740 -2011
- package/dist/node.cjs +7927 -3340
- package/dist/prod/affects.js +126 -0
- package/dist/prod/boundaries.js +572 -0
- package/dist/prod/core/action.js +139 -0
- package/dist/prod/core/async.js +607 -0
- package/dist/prod/core/constants.js +92 -0
- package/dist/prod/core/context.js +67 -0
- package/dist/prod/core/core.js +828 -0
- package/dist/prod/core/dev.js +3 -0
- package/dist/prod/core/effect.js +145 -0
- package/dist/prod/core/error.js +68 -0
- package/dist/prod/core/external.js +98 -0
- package/dist/prod/core/graph.js +104 -0
- package/dist/prod/core/heap.js +140 -0
- package/dist/prod/core/invariants.js +42 -0
- package/dist/prod/core/lanes.js +140 -0
- package/dist/prod/core/optimistic.js +262 -0
- package/dist/prod/core/owner.js +302 -0
- package/dist/prod/core/scheduler.js +757 -0
- package/dist/prod/core/verdict.js +369 -0
- package/dist/prod/index.js +45 -0
- package/dist/prod/map.js +319 -0
- package/dist/prod/signals.js +394 -0
- package/dist/prod/store/index.js +36 -0
- package/dist/prod/store/next/optimistic.js +377 -0
- package/dist/prod/store/next/projection.js +172 -0
- package/dist/prod/store/next/reconcile.js +327 -0
- package/dist/prod/store/next/store.js +1232 -0
- package/dist/prod/store/next/target.js +20 -0
- package/dist/prod/store/store.js +325 -0
- package/dist/prod/store/storePath.js +103 -0
- package/dist/prod/store/utils.js +201 -0
- package/dist/types/affects.d.ts +47 -0
- package/dist/types/boundaries.d.ts +60 -13
- package/dist/types/core/action.d.ts +35 -6
- package/dist/types/core/async.d.ts +16 -1
- package/dist/types/core/constants.d.ts +35 -6
- package/dist/types/core/context.d.ts +10 -2
- package/dist/types/core/core.d.ts +66 -63
- package/dist/types/core/dev.d.ts +17 -2
- package/dist/types/core/effect.d.ts +1 -2
- package/dist/types/core/error.d.ts +33 -0
- package/dist/types/core/external.d.ts +0 -11
- package/dist/types/core/graph.d.ts +2 -1
- package/dist/types/core/heap.d.ts +8 -0
- package/dist/types/core/index.d.ts +3 -2
- package/dist/types/core/invariants.d.ts +59 -0
- package/dist/types/core/lanes.d.ts +2 -0
- package/dist/types/core/optimistic.d.ts +6 -0
- package/dist/types/core/owner.d.ts +50 -3
- package/dist/types/core/scheduler.d.ts +85 -11
- package/dist/types/core/types.d.ts +66 -1
- package/dist/types/core/verdict.d.ts +2 -0
- package/dist/types/index.d.ts +3 -2
- package/dist/types/map.d.ts +21 -5
- package/dist/types/signals.d.ts +183 -12
- package/dist/types/store/index.d.ts +16 -6
- package/dist/types/store/next/optimistic.d.ts +20 -0
- package/dist/types/store/next/projection.d.ts +24 -0
- package/dist/types/store/next/reconcile.d.ts +3 -0
- package/dist/types/store/next/store.d.ts +71 -0
- package/dist/types/store/next/target.d.ts +99 -0
- package/dist/types/store/optimistic.d.ts +3 -3
- package/dist/types/store/projection.d.ts +10 -6
- package/dist/types/store/reconcile.d.ts +31 -8
- package/dist/types/store/store.d.ts +91 -71
- package/dist/types/store/utils.d.ts +0 -40
- package/dist/types-cjs/affects.d.cts +47 -0
- package/dist/types-cjs/boundaries.d.cts +60 -13
- package/dist/types-cjs/core/action.d.cts +35 -6
- package/dist/types-cjs/core/async.d.cts +16 -1
- package/dist/types-cjs/core/constants.d.cts +35 -6
- package/dist/types-cjs/core/context.d.cts +10 -2
- package/dist/types-cjs/core/core.d.cts +66 -63
- package/dist/types-cjs/core/dev.d.cts +17 -2
- package/dist/types-cjs/core/effect.d.cts +1 -2
- package/dist/types-cjs/core/error.d.cts +33 -0
- package/dist/types-cjs/core/external.d.cts +0 -11
- package/dist/types-cjs/core/graph.d.cts +2 -1
- package/dist/types-cjs/core/heap.d.cts +8 -0
- package/dist/types-cjs/core/index.d.cts +3 -2
- package/dist/types-cjs/core/invariants.d.cts +59 -0
- package/dist/types-cjs/core/lanes.d.cts +2 -0
- package/dist/types-cjs/core/optimistic.d.cts +6 -0
- package/dist/types-cjs/core/owner.d.cts +50 -3
- package/dist/types-cjs/core/scheduler.d.cts +85 -11
- package/dist/types-cjs/core/types.d.cts +66 -1
- package/dist/types-cjs/core/verdict.d.cts +2 -0
- package/dist/types-cjs/index.d.cts +3 -2
- package/dist/types-cjs/map.d.cts +21 -5
- package/dist/types-cjs/signals.d.cts +183 -12
- package/dist/types-cjs/store/index.d.cts +16 -6
- package/dist/types-cjs/store/next/optimistic.d.cts +20 -0
- package/dist/types-cjs/store/next/projection.d.cts +24 -0
- package/dist/types-cjs/store/next/reconcile.d.cts +3 -0
- package/dist/types-cjs/store/next/store.d.cts +71 -0
- package/dist/types-cjs/store/next/target.d.cts +99 -0
- package/dist/types-cjs/store/optimistic.d.cts +3 -3
- package/dist/types-cjs/store/projection.d.cts +10 -6
- package/dist/types-cjs/store/reconcile.d.cts +31 -8
- package/dist/types-cjs/store/store.d.cts +91 -71
- package/dist/types-cjs/store/utils.d.cts +0 -40
- package/package.json +12 -9
- package/dist/prod.js +0 -3627
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
import { computed, optimisticComputed, setSignal, optimisticSignal, runWithOwner, setMemo, signal, read, untrack } from "./core/core.js";
|
|
2
|
+
|
|
3
|
+
import { cleanup, createRoot, getOwner, dispose } from "./core/owner.js";
|
|
4
|
+
|
|
5
|
+
import { globalQueue, Queue } from "./core/scheduler.js";
|
|
6
|
+
|
|
7
|
+
import { CONFIG_AUTO_DISPOSE, CONFIG_CHILDREN_FORBIDDEN, EFFECT_USER, $REFRESH } from "./core/constants.js";
|
|
8
|
+
|
|
9
|
+
import "./core/invariants.js";
|
|
10
|
+
|
|
11
|
+
import "./core/verdict.js";
|
|
12
|
+
|
|
13
|
+
import { effect, trackedEffect } from "./core/effect.js";
|
|
14
|
+
|
|
15
|
+
import { installOptimisticEngine } from "./core/optimistic.js";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Low-level reactive-cleanup primitive. Registers a callback that runs when
|
|
19
|
+
* the surrounding owner is disposed.
|
|
20
|
+
*
|
|
21
|
+
* **In 2.0 user code this is rare.** The two cases where you might reach for
|
|
22
|
+
* it have better-shaped tools:
|
|
23
|
+
*
|
|
24
|
+
* - **Component lifecycle (mount/unmount, listeners, intervals):** use
|
|
25
|
+
* {@link onSettled} and **return** a cleanup function. Setup and teardown
|
|
26
|
+
* stay paired in one block. This replaces the 1.x `onMount` + `onCleanup`
|
|
27
|
+
* pairing.
|
|
28
|
+
* - **Cleanup tied to an effect run:** `onCleanup` does not belong in
|
|
29
|
+
* `createEffect`'s apply phase. If a compute phase genuinely needs per-run
|
|
30
|
+
* teardown, that's usually a sign the work should be a memo/projection
|
|
31
|
+
* instead, or moved to `onSettled` if it's lifecycle-shaped.
|
|
32
|
+
*
|
|
33
|
+
* Where `onCleanup` is the right tool is **library / custom-primitive
|
|
34
|
+
* internals** — coordinating disposal inside a `createRoot` body, or wiring
|
|
35
|
+
* cleanup to a captured owner via `runWithOwner` from a custom factory.
|
|
36
|
+
* Application code rarely needs to write any of those shapes directly.
|
|
37
|
+
*
|
|
38
|
+
* Must be called inside an owner. Calling outside an owner is a no-op (with a
|
|
39
|
+
* dev-mode warning).
|
|
40
|
+
*
|
|
41
|
+
* Cannot be used inside `createTrackedEffect` or `onSettled` — return a
|
|
42
|
+
* cleanup function from the callback body instead.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* // Library shape: thread a resource's disposal into a *captured* owner
|
|
47
|
+
* // from a factory that has no settle-phase setup of its own. `onSettled`
|
|
48
|
+
* // would queue a callback we don't need; `onCleanup` is the leaner
|
|
49
|
+
* // primitive when the only job is "register disposal on this owner".
|
|
50
|
+
* function bindToOwner<T extends { dispose(): void }>(owner: Owner, resource: T): T {
|
|
51
|
+
* runWithOwner(owner, () => onCleanup(() => resource.dispose()));
|
|
52
|
+
* return resource;
|
|
53
|
+
* }
|
|
54
|
+
* ```
|
|
55
|
+
*/ function onCleanup(e) {
|
|
56
|
+
return cleanup(e);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function accessor(e) {
|
|
60
|
+
const t = read.bind(null, e);
|
|
61
|
+
t[$REFRESH] = e;
|
|
62
|
+
return t;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function createSignal(e, t) {
|
|
66
|
+
if (typeof e === "function") {
|
|
67
|
+
const n = computed(e, t);
|
|
68
|
+
n.T &= ~CONFIG_AUTO_DISPOSE;
|
|
69
|
+
return [ accessor(n), setMemo.bind(null, n) ];
|
|
70
|
+
}
|
|
71
|
+
const n = signal(e, t);
|
|
72
|
+
return [ accessor(n), setSignal.bind(null, n) ];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function createMemo(e, t) {
|
|
76
|
+
return accessor(computed(e, t));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function createEffect(e, t, n) {
|
|
80
|
+
effect(e, t.effect || t, t.error, {
|
|
81
|
+
user: true,
|
|
82
|
+
...n
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Creates a reactive computation that runs during the render phase as DOM elements
|
|
88
|
+
* are created and updated but not necessarily connected.
|
|
89
|
+
*
|
|
90
|
+
* Same compute / effect split as `createEffect`, but scheduled inside the render
|
|
91
|
+
* queue rather than after it. Reach for this only when authoring renderer
|
|
92
|
+
* plumbing (custom DOM bindings, JSX-generated `insert()` / `spread()` calls).
|
|
93
|
+
* App code should use `createEffect`.
|
|
94
|
+
*
|
|
95
|
+
* ```typescript
|
|
96
|
+
* createRenderEffect<T>(compute, effectFn, options?: EffectOptions);
|
|
97
|
+
* ```
|
|
98
|
+
* @param compute a function that receives its previous value and returns a new value used to react on a computation
|
|
99
|
+
* @param effectFn a function that receives the new value and is used to perform side effects
|
|
100
|
+
* @param options `EffectOptions` -- name, defer, schedule, transparent
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```ts
|
|
104
|
+
* // Custom directive: bind an element's textContent to a reactive source.
|
|
105
|
+
* function bindText(el: HTMLElement, source: () => string) {
|
|
106
|
+
* createRenderEffect(
|
|
107
|
+
* () => source(),
|
|
108
|
+
* value => { el.textContent = value; }
|
|
109
|
+
* );
|
|
110
|
+
* }
|
|
111
|
+
* ```
|
|
112
|
+
*
|
|
113
|
+
* @description https://docs.solidjs.com/reference/secondary-primitives/create-render-effect
|
|
114
|
+
*/ function createRenderEffect(e, t, n) {
|
|
115
|
+
effect(e, t, undefined, n);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Creates a tracked reactive effect where dependency tracking and side effects happen
|
|
120
|
+
* in the same scope.
|
|
121
|
+
*
|
|
122
|
+
* WARNING: Because tracking and effects happen in the same scope, this primitive
|
|
123
|
+
* may run multiple times for a single change or show tearing (reading inconsistent
|
|
124
|
+
* state). Use only when dynamic subscription patterns require same-scope tracking.
|
|
125
|
+
*
|
|
126
|
+
* The callback runs during the flush itself: writes made inside it are queued
|
|
127
|
+
* into the same flush's continuation and are never visible to the callback's
|
|
128
|
+
* own reads (reads return settled values, as in every effect-phase scope), and
|
|
129
|
+
* `flush()` cannot be called from inside it (dev throws; production is a
|
|
130
|
+
* no-op) — defer with `queueMicrotask(() => flush())` if needed.
|
|
131
|
+
*
|
|
132
|
+
* ```typescript
|
|
133
|
+
* createTrackedEffect(compute, options?: { name?: string });
|
|
134
|
+
* ```
|
|
135
|
+
* @param compute a function that contains reactive reads to track and returns an optional cleanup function to run on disposal or before next execution
|
|
136
|
+
* @param options -- name
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* ```ts
|
|
140
|
+
* createTrackedEffect(() => {
|
|
141
|
+
* const target = focusedNode();
|
|
142
|
+
* if (!target) return;
|
|
143
|
+
*
|
|
144
|
+
* const handler = () => log(target.value());
|
|
145
|
+
* target.on("change", handler);
|
|
146
|
+
*
|
|
147
|
+
* return () => target.off("change", handler);
|
|
148
|
+
* });
|
|
149
|
+
* ```
|
|
150
|
+
*
|
|
151
|
+
* @description https://docs.solidjs.com/reference/secondary-primitives/create-tracked-effect
|
|
152
|
+
*/ function createTrackedEffect(e, t) {
|
|
153
|
+
trackedEffect(e, t);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Creates a reactive computation that runs after the render phase with flexible tracking.
|
|
158
|
+
*
|
|
159
|
+
* ```typescript
|
|
160
|
+
* const track = createReaction(effectFn, options?: EffectOptions);
|
|
161
|
+
* track(() => { // reactive reads });
|
|
162
|
+
* ```
|
|
163
|
+
* @param effectFn a function (or `EffectBundle`) that is called when tracked function is invalidated
|
|
164
|
+
* @param options `EffectOptions` -- name, defer
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```ts
|
|
168
|
+
* const [count, setCount] = createSignal(0);
|
|
169
|
+
*
|
|
170
|
+
* const track = createReaction(() => {
|
|
171
|
+
* console.log("count changed once, re-arm to listen again");
|
|
172
|
+
* track(() => count()); // re-arm
|
|
173
|
+
* });
|
|
174
|
+
*
|
|
175
|
+
* track(() => count()); // initial arm
|
|
176
|
+
*
|
|
177
|
+
* setCount(1); // logs once, reaction re-armed for next change
|
|
178
|
+
* ```
|
|
179
|
+
*
|
|
180
|
+
* @description https://docs.solidjs.com/reference/secondary-primitives/create-reaction
|
|
181
|
+
*/ function createReaction(e, t) {
|
|
182
|
+
let n = undefined;
|
|
183
|
+
cleanup(() => n?.());
|
|
184
|
+
const c = getOwner();
|
|
185
|
+
// The currently armed effect node. `track()` replaces the previous
|
|
186
|
+
// subscription (1.x semantics): without disposing the superseded arm, its
|
|
187
|
+
// sources stayed live (firing the callback for replaced dependencies), each
|
|
188
|
+
// accumulated arm delivered its own fire, and un-fired arms leaked as live
|
|
189
|
+
// effect nodes until the owner disposed (#2861).
|
|
190
|
+
let r;
|
|
191
|
+
return o => {
|
|
192
|
+
if (r) {
|
|
193
|
+
dispose(r);
|
|
194
|
+
r = undefined;
|
|
195
|
+
}
|
|
196
|
+
runWithOwner(c, () => {
|
|
197
|
+
effect(() => (o(), r = getOwner()), t => {
|
|
198
|
+
r = undefined;
|
|
199
|
+
n?.();
|
|
200
|
+
const c = (e.effect || e)?.();
|
|
201
|
+
if (false && c !== undefined && typeof c !== "function") ;
|
|
202
|
+
n = c;
|
|
203
|
+
dispose(t);
|
|
204
|
+
}, e.error, {
|
|
205
|
+
...false ? {
|
|
206
|
+
...t,
|
|
207
|
+
name: t?.name ?? "effect"
|
|
208
|
+
} : t,
|
|
209
|
+
user: true,
|
|
210
|
+
defer: true
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Delivers effect applies on a microtask instead of queueing them (#2930). */ class MicrotaskQueue extends Queue {
|
|
217
|
+
enqueue(e, t) {
|
|
218
|
+
queueMicrotask(() => t(e));
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Awaits a reactive expression and returns its first fully-settled value as a
|
|
224
|
+
* `Promise`. Pending async reads (`createMemo` returning a promise, etc.) are
|
|
225
|
+
* waited on; once the expression returns synchronously without `NotReadyError`
|
|
226
|
+
* the promise resolves with that value. If the expression settles with an
|
|
227
|
+
* error instead — including an async source that rejects — the promise
|
|
228
|
+
* rejects with it.
|
|
229
|
+
*
|
|
230
|
+
* Must be called *outside* a tracking scope — it doesn't subscribe, it just
|
|
231
|
+
* resolves the current value once.
|
|
232
|
+
*
|
|
233
|
+
* @example
|
|
234
|
+
* ```ts
|
|
235
|
+
* const user = createMemo(() => fetch(`/users/${id()}`).then(r => r.json()));
|
|
236
|
+
*
|
|
237
|
+
* // outside any reactive scope
|
|
238
|
+
* const initial = await resolve(() => user());
|
|
239
|
+
* ```
|
|
240
|
+
*
|
|
241
|
+
* @param fn a reactive expression to resolve
|
|
242
|
+
*/ function resolve(e) {
|
|
243
|
+
return new Promise((t, n) => {
|
|
244
|
+
createRoot(c => {
|
|
245
|
+
// Deliver effect applies on a microtask instead of the owner queue: an
|
|
246
|
+
// incomplete transition stashes its effect queues until it settles, but
|
|
247
|
+
// an action yielding this promise is itself what keeps the transition
|
|
248
|
+
// open — the stashed res() deadlocked the action (#2930). The compute
|
|
249
|
+
// still runs in place (under the transaction's view when created inside
|
|
250
|
+
// an action step), and status/boundary notifications keep their normal
|
|
251
|
+
// route through the inherited queue.
|
|
252
|
+
const r = getOwner();
|
|
253
|
+
const o = new MicrotaskQueue;
|
|
254
|
+
o.Fe = r.C;
|
|
255
|
+
// notify() forwards up the normal chain
|
|
256
|
+
r.C = o;
|
|
257
|
+
// A user effect rather than a bare computed: computeds are pull-based and
|
|
258
|
+
// are only re-enqueued when a pending source *resolves* — a rejection just
|
|
259
|
+
// marks them errored, so nothing would re-run and the promise would never
|
|
260
|
+
// settle (#2842). The effect's error channel is notified on rejection.
|
|
261
|
+
effect(e, e => {
|
|
262
|
+
t(e);
|
|
263
|
+
c();
|
|
264
|
+
}, e => {
|
|
265
|
+
// The error arm already unwraps StatusError (#2840) — `err` is the
|
|
266
|
+
// user's original error, matching what error boundaries expose.
|
|
267
|
+
n(e);
|
|
268
|
+
c();
|
|
269
|
+
}, {
|
|
270
|
+
user: true
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function createOptimistic(e, t) {
|
|
277
|
+
// Install before the node exists: only engine-installed programs can carry
|
|
278
|
+
// an _overrideValue slot (same runtime-install pattern as
|
|
279
|
+
// GlobalQueue._clearOptimisticStore in createOptimisticStore).
|
|
280
|
+
installOptimisticEngine();
|
|
281
|
+
if (typeof e === "function") {
|
|
282
|
+
const n = optimisticComputed(e, t);
|
|
283
|
+
n.T &= ~CONFIG_AUTO_DISPOSE;
|
|
284
|
+
return [ accessor(n), setSignal.bind(null, n) ];
|
|
285
|
+
}
|
|
286
|
+
const n = optimisticSignal(e, t);
|
|
287
|
+
return [ accessor(n), setSignal.bind(null, n) ];
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Schedules `callback` to run **once** after the reactive graph has fully
|
|
292
|
+
* settled — i.e. once every pending async read inside the current owner has
|
|
293
|
+
* resolved and the queue has flushed. Each call registers a single fire; it
|
|
294
|
+
* does not create an ongoing subscription.
|
|
295
|
+
*
|
|
296
|
+
* The canonical lifecycle primitive in 2.0. Three main usages:
|
|
297
|
+
*
|
|
298
|
+
* - **Component-level setup-and-teardown** *(the most common shape)*: run
|
|
299
|
+
* setup after the component's first stable render and **return a cleanup
|
|
300
|
+
* function** to dispose it on owner disposal. This is the replacement for
|
|
301
|
+
* the 1.x `onMount` + `onCleanup` pairing — setup and teardown live in one
|
|
302
|
+
* block, and `onCleanup` is no longer the right tool for component
|
|
303
|
+
* bodies. (`onMount` no longer exists in 2.0.)
|
|
304
|
+
* - **Post-settle "ready" hook:** run once after a component's first stable
|
|
305
|
+
* render — analytics ping, focus, scroll-into-view, etc. No cleanup needed.
|
|
306
|
+
* - **Inside an event handler:** schedule work to run after the action /
|
|
307
|
+
* transition triggered by the event has completed.
|
|
308
|
+
*
|
|
309
|
+
* Reactive reads inside the callback are *not* tracked — to react to
|
|
310
|
+
* subsequent settles, register a new `onSettled` each time.
|
|
311
|
+
*
|
|
312
|
+
* The callback runs during the settle flush itself, which gives it the same
|
|
313
|
+
* write semantics as every other effect-phase scope (the effect half of
|
|
314
|
+
* `createEffect`, event handlers):
|
|
315
|
+
*
|
|
316
|
+
* - **Writes** are queued into the same flush's continuation — dependent memos
|
|
317
|
+
* and effects update before the flush returns — but reads inside the
|
|
318
|
+
* callback keep returning the settled (pre-write) values. A callback never
|
|
319
|
+
* observes its own unsettled write. Functional setters still compose:
|
|
320
|
+
* `set(v => v + 1)` twice increments twice.
|
|
321
|
+
* - **`flush()` cannot be called** from inside the callback — the flush is
|
|
322
|
+
* already running (dev throws; production is a no-op). To force a drain
|
|
323
|
+
* after this settle, defer it: `queueMicrotask(() => flush())`.
|
|
324
|
+
*
|
|
325
|
+
* `onCleanup` is **not** allowed inside the callback — return a cleanup
|
|
326
|
+
* function instead. The returned cleanup runs on owner disposal.
|
|
327
|
+
*
|
|
328
|
+
* A cleanup return is only honored when `onSettled` is called from an **owned**
|
|
329
|
+
* scope (e.g. a component body). When it fires out of band from an *unowned*
|
|
330
|
+
* scope — an event handler, a tracked effect, or another `onSettled` — there is
|
|
331
|
+
* no owner lifecycle to bind a cleanup to; returning one is a dev-mode error
|
|
332
|
+
* (and is dropped in production). Use the post-settle/event-handler forms below
|
|
333
|
+
* for one-shot work, and keep setup-with-teardown in an owned scope.
|
|
334
|
+
*
|
|
335
|
+
* @example
|
|
336
|
+
* ```tsx
|
|
337
|
+
* // Component-level setup + teardown — replaces onMount + onCleanup.
|
|
338
|
+
* // Subscribe to an external source on mount, unsubscribe on dispose.
|
|
339
|
+
* function useViewportWidth() {
|
|
340
|
+
* const [width, setWidth] = createSignal(window.innerWidth);
|
|
341
|
+
* onSettled(() => {
|
|
342
|
+
* const onResize = () => setWidth(window.innerWidth);
|
|
343
|
+
* window.addEventListener("resize", onResize);
|
|
344
|
+
* return () => window.removeEventListener("resize", onResize);
|
|
345
|
+
* });
|
|
346
|
+
* return width;
|
|
347
|
+
* }
|
|
348
|
+
* ```
|
|
349
|
+
*
|
|
350
|
+
* @example
|
|
351
|
+
* ```tsx
|
|
352
|
+
* // Post-settle "ready" hook — no cleanup needed.
|
|
353
|
+
* function Dashboard() {
|
|
354
|
+
* const data = createMemo(async () => fetchData());
|
|
355
|
+
*
|
|
356
|
+
* onSettled(() => {
|
|
357
|
+
* analytics.track("dashboard.ready");
|
|
358
|
+
* });
|
|
359
|
+
*
|
|
360
|
+
* return <Loading fallback={<Spinner />}><pre>{data()}</pre></Loading>;
|
|
361
|
+
* }
|
|
362
|
+
* ```
|
|
363
|
+
*
|
|
364
|
+
* @example
|
|
365
|
+
* ```tsx
|
|
366
|
+
* // Event-handler — runs after the action settles.
|
|
367
|
+
* function SaveButton() {
|
|
368
|
+
* const save = action(function* () {
|
|
369
|
+
* yield api.save();
|
|
370
|
+
* });
|
|
371
|
+
*
|
|
372
|
+
* const handleClick = () => {
|
|
373
|
+
* save();
|
|
374
|
+
* onSettled(() => toast("Saved!"));
|
|
375
|
+
* };
|
|
376
|
+
*
|
|
377
|
+
* return <button onClick={handleClick}>Save</button>;
|
|
378
|
+
* }
|
|
379
|
+
* ```
|
|
380
|
+
*
|
|
381
|
+
* @param callback Function to run; may return a cleanup function that fires
|
|
382
|
+
* on owner disposal
|
|
383
|
+
*/ function onSettled(e) {
|
|
384
|
+
const t = getOwner();
|
|
385
|
+
t && !(t.T & CONFIG_CHILDREN_FORBIDDEN) ? createTrackedEffect(() => untrack(e), undefined) : globalQueue.enqueue(EFFECT_USER, () => {
|
|
386
|
+
// Unowned, out-of-band fire (no owner, or a children-forbidden one this
|
|
387
|
+
// one-shot must not bind to): a returned cleanup has no lifecycle to
|
|
388
|
+
// attach to. Reject it in dev; in production the return is simply
|
|
389
|
+
// dropped — never bound to an unrelated owner or run eagerly.
|
|
390
|
+
e();
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
export { accessor, createEffect, createMemo, createOptimistic, createReaction, createRenderEffect, createSignal, createTrackedEffect, onCleanup, onSettled, resolve };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import "../core/core.js";
|
|
2
|
+
|
|
3
|
+
import "../core/scheduler.js";
|
|
4
|
+
|
|
5
|
+
import "../core/invariants.js";
|
|
6
|
+
|
|
7
|
+
import "../core/verdict.js";
|
|
8
|
+
|
|
9
|
+
import "../core/effect.js";
|
|
10
|
+
|
|
11
|
+
import { createStoreNext, deepNext, snapshotNext } from "./next/store.js";
|
|
12
|
+
|
|
13
|
+
import { reconcileNextState } from "./next/reconcile.js";
|
|
14
|
+
|
|
15
|
+
import { createStoreDerivedNext } from "./next/projection.js";
|
|
16
|
+
|
|
17
|
+
export { createProjectionNext as createProjection } from "./next/projection.js";
|
|
18
|
+
|
|
19
|
+
function createStore(e, t, r) {
|
|
20
|
+
if (typeof e === "function") return createStoreDerivedNext(e, t, r);
|
|
21
|
+
return createStoreNext(e, !!t?.shallow);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function reconcile(e, t = "id") {
|
|
25
|
+
return r => reconcileNextState(e, r, t);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function snapshot(e) {
|
|
29
|
+
return snapshotNext(e);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function deep(e) {
|
|
33
|
+
return deepNext(e);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export { createStore, deep, reconcile, snapshot };
|