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