@krate/runtime 0.5.0-alpha.1 → 0.5.0-alpha.3
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/action-context.d.ts +49 -0
- package/dist/action-context.js +37 -0
- package/dist/action-context.test.d.ts +1 -0
- package/dist/action-context.test.js +47 -0
- package/dist/actions.d.ts +120 -0
- package/dist/actions.js +533 -0
- package/dist/actions.test.d.ts +1 -0
- package/dist/actions.test.js +391 -0
- package/dist/boundary.d.ts +121 -0
- package/dist/boundary.js +429 -0
- package/dist/class-component.d.ts +66 -0
- package/dist/class-component.js +633 -0
- package/dist/class-component.test.d.ts +1 -0
- package/dist/class-component.test.js +411 -0
- package/dist/context.d.ts +13 -0
- package/dist/context.js +58 -32
- package/dist/context.test.d.ts +1 -0
- package/dist/context.test.js +57 -0
- package/dist/derived.test.d.ts +1 -0
- package/dist/derived.test.js +140 -0
- package/dist/discrete.test.d.ts +1 -0
- package/dist/discrete.test.js +100 -0
- package/dist/dom.d.ts +55 -0
- package/dist/dom.js +414 -63
- package/dist/effects.d.ts +88 -0
- package/dist/effects.js +719 -0
- package/dist/effects.test.d.ts +1 -0
- package/dist/effects.test.js +485 -0
- package/dist/events.d.ts +58 -0
- package/dist/events.js +503 -0
- package/dist/events.test.d.ts +1 -0
- package/dist/events.test.js +447 -0
- package/dist/index.d.ts +12 -2
- package/dist/index.js +8 -2
- package/dist/jsx-types.d.ts +2 -0
- package/dist/krate-hydrate-lite.js +8 -0
- package/dist/krate-hydrate.js +7 -1
- package/dist/krate-runtime.js +7 -1
- package/dist/krate-suspense.js +2 -0
- package/dist/lanes.test.d.ts +1 -0
- package/dist/lanes.test.js +59 -0
- package/dist/list.d.ts +20 -0
- package/dist/list.js +466 -0
- package/dist/list.test.d.ts +1 -0
- package/dist/list.test.js +344 -0
- package/dist/optimistic-lanes.test.d.ts +1 -0
- package/dist/optimistic-lanes.test.js +45 -0
- package/dist/portal.d.ts +43 -0
- package/dist/portal.js +178 -0
- package/dist/portal.test.d.ts +1 -0
- package/dist/portal.test.js +99 -0
- package/dist/react-effects.test.d.ts +1 -0
- package/dist/react-effects.test.js +96 -0
- package/dist/react-suspense.test.d.ts +1 -0
- package/dist/react-suspense.test.js +194 -0
- package/dist/react.d.ts +132 -32
- package/dist/react.js +335 -81
- package/dist/react.test.js +12 -2
- package/dist/reconcile.js +13 -0
- package/dist/refs.d.ts +33 -0
- package/dist/refs.js +142 -0
- package/dist/refs.test.d.ts +1 -0
- package/dist/refs.test.js +257 -0
- package/dist/resource.js +20 -1
- package/dist/router-lite.d.ts +8 -0
- package/dist/router-lite.js +11 -0
- package/dist/server-renderer.js +39 -1
- package/dist/server.d.ts +4 -1
- package/dist/server.js +30 -1
- package/dist/server.test.js +20 -0
- package/dist/signal-lanes.test.d.ts +1 -0
- package/dist/signal-lanes.test.js +32 -0
- package/dist/signal.d.ts +150 -1
- package/dist/signal.js +750 -65
- package/dist/slots.d.ts +18 -0
- package/dist/slots.js +108 -0
- package/dist/slots.test.d.ts +1 -0
- package/dist/slots.test.js +169 -0
- package/dist/snapshots.test.d.ts +1 -0
- package/dist/snapshots.test.js +208 -0
- package/dist/suspense-actions.test.d.ts +1 -0
- package/dist/suspense-actions.test.js +72 -0
- package/dist/suspense.d.ts +220 -0
- package/dist/suspense.js +901 -0
- package/dist/suspense.test.d.ts +1 -0
- package/dist/suspense.test.js +518 -0
- package/package.json +1 -1
package/dist/signal.js
CHANGED
|
@@ -1,8 +1,23 @@
|
|
|
1
1
|
import { resetContexts } from './context.js';
|
|
2
2
|
import { resolvePersist, readDurableState, readPersisted, writePersisted, subscribePersist, } from './storage.js';
|
|
3
|
+
/** Signal options that give a signal React state semantics. */
|
|
4
|
+
export const krateHookState = { react: true };
|
|
5
|
+
// A null entry is an untracked scope (see untrack): reads inside it subscribe
|
|
6
|
+
// nothing.
|
|
3
7
|
const context = [];
|
|
8
|
+
// Each queued mount callback remembers the owner (effect or root) that was
|
|
9
|
+
// current when it was registered, so an onCleanup inside the callback belongs
|
|
10
|
+
// to that owner instead of the page root, and the error-boundary owner its
|
|
11
|
+
// errors are routed to.
|
|
4
12
|
const mountQueue = [];
|
|
5
13
|
let mountScheduled = false;
|
|
14
|
+
let ownerHooks = null;
|
|
15
|
+
/** Installs (or with null removes) the owner integration hooks. */
|
|
16
|
+
export function setOwnerHooks(hooks) {
|
|
17
|
+
ownerHooks = hooks;
|
|
18
|
+
}
|
|
19
|
+
// Callbacks that run once the effect queue has fully drained (see afterFlush).
|
|
20
|
+
const afterQueue = [];
|
|
6
21
|
const allEffects = new Set();
|
|
7
22
|
// Top-level onCleanup registrations (called outside an effect body) are scoped
|
|
8
23
|
// to the page/root and run when disposeAll() executes on route change. This
|
|
@@ -15,11 +30,263 @@ const rootCleanups = [];
|
|
|
15
30
|
const pending = [];
|
|
16
31
|
let flushScheduled = false;
|
|
17
32
|
function newEffectState(fn) {
|
|
18
|
-
|
|
33
|
+
const effect = { fn, cleanups: [], disposed: false, sources: new Set(), running: false, rerun: false, queued: false, owner: currentOwner, boundaryOwner: ownerHooks ? ownerHooks.current() : null };
|
|
34
|
+
if (currentOwner !== null) {
|
|
35
|
+
let set = ownerEffects.get(currentOwner);
|
|
36
|
+
if (!set)
|
|
37
|
+
ownerEffects.set(currentOwner, (set = new Set()));
|
|
38
|
+
set.add(effect);
|
|
39
|
+
}
|
|
40
|
+
return effect;
|
|
41
|
+
}
|
|
42
|
+
// Effects and top-level cleanups created while an owner was current, so the
|
|
43
|
+
// owner can be unmounted on its own (a component removed by a conditional).
|
|
44
|
+
const ownerEffects = new Map();
|
|
45
|
+
const ownerCleanups = new Map();
|
|
46
|
+
/**
|
|
47
|
+
* Disposes every effect created under `owner` and runs its top-level
|
|
48
|
+
* onCleanup callbacks (internal, used when a component unmounts).
|
|
49
|
+
*/
|
|
50
|
+
export function krateDisposeOwner(owner) {
|
|
51
|
+
const effects = ownerEffects.get(owner);
|
|
52
|
+
ownerEffects.delete(owner);
|
|
53
|
+
if (effects) {
|
|
54
|
+
for (const effect of effects) {
|
|
55
|
+
disposeEffect(effect);
|
|
56
|
+
allEffects.delete(effect);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const cleanups = ownerCleanups.get(owner);
|
|
60
|
+
ownerCleanups.delete(owner);
|
|
61
|
+
if (cleanups) {
|
|
62
|
+
for (const c of cleanups) {
|
|
63
|
+
try {
|
|
64
|
+
c();
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
// One failing cleanup must not keep the rest from running.
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// Render snapshots for React state (see SignalOptions.react).
|
|
73
|
+
//
|
|
74
|
+
// Krate's render phase is any tracked effect run (DOM bindings, dependency
|
|
75
|
+
// trackers, memo computations read by them): reads there see the latest
|
|
76
|
+
// value. Everywhere else a React state read returns the value as of the last
|
|
77
|
+
// commit, or as of `snapshotEpoch` while an effect setup/cleanup runs for the
|
|
78
|
+
// commit it belongs to.
|
|
79
|
+
let renderDepth = 0;
|
|
80
|
+
let snapshotEpoch = null;
|
|
81
|
+
let epoch = 0;
|
|
82
|
+
const changedCells = new Set();
|
|
83
|
+
const historyCells = new Set();
|
|
84
|
+
/**
|
|
85
|
+
* Runs fn as render work: React state reads see the latest values rather than
|
|
86
|
+
* the last commit's (internal; the class component render phase).
|
|
87
|
+
*/
|
|
88
|
+
export function krateRenderPhase(fn) {
|
|
89
|
+
renderDepth++;
|
|
90
|
+
const prevEpoch = snapshotEpoch;
|
|
91
|
+
snapshotEpoch = null;
|
|
92
|
+
try {
|
|
93
|
+
return fn();
|
|
94
|
+
}
|
|
95
|
+
finally {
|
|
96
|
+
snapshotEpoch = prevEpoch;
|
|
97
|
+
renderDepth--;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/** Records the current React state values as commit `epoch` (internal). */
|
|
101
|
+
export function krateCommitSnapshot() {
|
|
102
|
+
epoch++;
|
|
103
|
+
for (const cell of changedCells)
|
|
104
|
+
cell.commit(epoch);
|
|
105
|
+
changedCells.clear();
|
|
106
|
+
return epoch;
|
|
107
|
+
}
|
|
108
|
+
/** The epoch of the last commit snapshot (internal). */
|
|
109
|
+
export function krateEpoch() {
|
|
110
|
+
return epoch;
|
|
111
|
+
}
|
|
112
|
+
/** Runs fn reading React state as of commit `at` (internal). */
|
|
113
|
+
export function krateReadAt(at, fn) {
|
|
114
|
+
const prev = snapshotEpoch;
|
|
115
|
+
snapshotEpoch = at;
|
|
116
|
+
try {
|
|
117
|
+
return fn();
|
|
118
|
+
}
|
|
119
|
+
finally {
|
|
120
|
+
snapshotEpoch = prev;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/** Drops snapshot history older than commit `min` (internal). */
|
|
124
|
+
export function kratePruneSnapshots(min) {
|
|
125
|
+
for (const cell of historyCells) {
|
|
126
|
+
if (!cell.prune(min))
|
|
127
|
+
historyCells.delete(cell);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
// Update lanes. A write during a discrete event (click, key, input...) or a
|
|
131
|
+
// commit flushes in a microtask, and a discrete update also runs its passive
|
|
132
|
+
// effects at the end of its commit, like React's SyncLane. A React state
|
|
133
|
+
// write anywhere else (timeouts, promises) flushes in a later task, after
|
|
134
|
+
// the microtasks that code queued, like React's DefaultLane.
|
|
135
|
+
const DISCRETE = new Set(('auxclick beforeinput blur cancel change click close compositionend compositionstart compositionupdate ' +
|
|
136
|
+
'contextmenu copy cut dblclick dragend dragstart drop focusin focusout input invalid keydown keypress keyup ' +
|
|
137
|
+
'mousedown mouseup paste pause play pointercancel pointerdown pointerup ratechange reset resize seeked ' +
|
|
138
|
+
'select submit touchcancel touchend touchstart volumechange focus').split(' '));
|
|
139
|
+
let committing = 0;
|
|
140
|
+
let syncLane = false;
|
|
141
|
+
// Set while the event runtime dispatches a discrete native event.
|
|
142
|
+
let discreteDispatch = 0;
|
|
143
|
+
function inDiscreteEvent() {
|
|
144
|
+
if (discreteDispatch > 0)
|
|
145
|
+
return true;
|
|
146
|
+
const ev = typeof window !== 'undefined' ? window.event : undefined;
|
|
147
|
+
return !!ev && DISCRETE.has(ev.type);
|
|
148
|
+
}
|
|
149
|
+
let transitionWrites = 0;
|
|
150
|
+
/**
|
|
151
|
+
* Runs fn with its signal writes in the transition lane: they commit in a
|
|
152
|
+
* later task, like React's transition and default updates, even for plain
|
|
153
|
+
* signals. Used for the updates an async action makes when it settles
|
|
154
|
+
* (pending flags, results), so the pending render stays on screen for at
|
|
155
|
+
* least one task, as it does in React.
|
|
156
|
+
*/
|
|
157
|
+
export function krateTransitionWrites(fn) {
|
|
158
|
+
transitionWrites++;
|
|
159
|
+
try {
|
|
160
|
+
return fn();
|
|
161
|
+
}
|
|
162
|
+
finally {
|
|
163
|
+
transitionWrites--;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/** Reports whether a native event type is discrete (React's SyncLane). */
|
|
167
|
+
export function krateIsDiscrete(type) {
|
|
168
|
+
return DISCRETE.has(type);
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Runs the handlers of one native event dispatch. For a discrete event
|
|
172
|
+
* (click, input, keydown, submit, change, focus, ...) the updates they made
|
|
173
|
+
* are flushed synchronously when `flush` is set, as React does at the end of
|
|
174
|
+
* a discrete event: render, commit, layout and passive effects all run before
|
|
175
|
+
* the browser event returns. Continuous events keep the normal batching.
|
|
176
|
+
*/
|
|
177
|
+
export function krateRunEvent(type, flush, fn) {
|
|
178
|
+
const discrete = DISCRETE.has(type);
|
|
179
|
+
if (!discrete) {
|
|
180
|
+
fn();
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
discreteDispatch++;
|
|
184
|
+
try {
|
|
185
|
+
fn();
|
|
186
|
+
}
|
|
187
|
+
finally {
|
|
188
|
+
discreteDispatch--;
|
|
189
|
+
// An event dispatched by the flush itself (a blur fired by removing the
|
|
190
|
+
// focused node) joins the running flush instead of committing on its own.
|
|
191
|
+
if (flush && discreteDispatch === 0 && flushDepth === 0 && committing === 0 && (pending.length > 0 || syncLane)) {
|
|
192
|
+
krateFlushSync();
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
/** Marks a commit in progress (internal): writes there flush in a microtask. */
|
|
197
|
+
export function krateCommitting(on) {
|
|
198
|
+
committing += on ? 1 : -1;
|
|
199
|
+
}
|
|
200
|
+
/** Reports and clears whether the pending commit came from a discrete event. */
|
|
201
|
+
export function krateTakeSyncLane() {
|
|
202
|
+
const v = syncLane;
|
|
203
|
+
syncLane = false;
|
|
204
|
+
return v;
|
|
205
|
+
}
|
|
206
|
+
/** The effect currently collecting dependencies, ignoring untracked frames. */
|
|
207
|
+
function currentEffect() {
|
|
208
|
+
return context[context.length - 1] || null;
|
|
209
|
+
}
|
|
210
|
+
// The component owner whose body (or binding effect) is currently running.
|
|
211
|
+
// Opaque here; effects.ts gives it meaning (effect ordering and unmounting).
|
|
212
|
+
// Effects capture it at creation and restore it while they re-run, so
|
|
213
|
+
// components created by a binding re-run are parented correctly.
|
|
214
|
+
let currentOwner = null;
|
|
215
|
+
/** Returns the current component owner (internal, used by effects.ts). */
|
|
216
|
+
export function krateGetOwner() {
|
|
217
|
+
return currentOwner;
|
|
218
|
+
}
|
|
219
|
+
/** Sets the current component owner and returns the previous one (internal). */
|
|
220
|
+
export function krateSetOwner(owner) {
|
|
221
|
+
const prev = currentOwner;
|
|
222
|
+
currentOwner = owner;
|
|
223
|
+
return prev;
|
|
224
|
+
}
|
|
225
|
+
// Hooks run after a flush settles (no pending effects) and when the page is
|
|
226
|
+
// torn down. effects.ts registers its commit and unmount here; the API is
|
|
227
|
+
// additive so other schedulers can hook in too.
|
|
228
|
+
const beforeFlushHooks = [];
|
|
229
|
+
const afterFlushHooks = [];
|
|
230
|
+
const disposeHooks = [];
|
|
231
|
+
/** Registers fn to run before every effect flush starts (internal). */
|
|
232
|
+
export function krateOnBeforeFlush(fn) {
|
|
233
|
+
beforeFlushHooks.push(fn);
|
|
234
|
+
}
|
|
235
|
+
/** Registers fn to run after every settled effect flush (internal). */
|
|
236
|
+
export function krateOnAfterFlush(fn) {
|
|
237
|
+
afterFlushHooks.push(fn);
|
|
238
|
+
}
|
|
239
|
+
/** Registers fn to run at the start of disposeAll (internal). */
|
|
240
|
+
export function krateOnDispose(fn) {
|
|
241
|
+
disposeHooks.push(fn);
|
|
242
|
+
}
|
|
243
|
+
/** Reports whether signal writes are waiting for the next flush. */
|
|
244
|
+
export function kratePendingFlush() {
|
|
245
|
+
return pending.length > 0;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Runs fn without subscribing the surrounding effect to the signals it reads.
|
|
249
|
+
*/
|
|
250
|
+
export function untrack(fn) {
|
|
251
|
+
context.push(null);
|
|
252
|
+
try {
|
|
253
|
+
return fn();
|
|
254
|
+
}
|
|
255
|
+
finally {
|
|
256
|
+
context.pop();
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Runs fn once after the pending effect queue has fully drained (including
|
|
261
|
+
* effects scheduled while it drained) and the after-flush hooks ran, so the
|
|
262
|
+
* DOM reflects every write made so far. Schedules a flush when none is
|
|
263
|
+
* pending. Used by the class component commit phase.
|
|
264
|
+
*/
|
|
265
|
+
export function afterFlush(fn) {
|
|
266
|
+
afterQueue.push(fn);
|
|
267
|
+
scheduleFlush();
|
|
268
|
+
}
|
|
269
|
+
function drainAfterQueue() {
|
|
270
|
+
if (afterQueue.length === 0)
|
|
271
|
+
return;
|
|
272
|
+
const after = afterQueue.splice(0);
|
|
273
|
+
for (const fn of after) {
|
|
274
|
+
fn();
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Same-value equality used for every React-facing comparison (state
|
|
279
|
+
* bail-out, dependency arrays): Object.is, so NaN equals NaN and +0 differs
|
|
280
|
+
* from -0.
|
|
281
|
+
*/
|
|
282
|
+
export function sameValue(a, b) {
|
|
283
|
+
return Object.is(a, b);
|
|
19
284
|
}
|
|
20
285
|
function disposeEffect(effect) {
|
|
21
286
|
if (!effect.disposed) {
|
|
22
287
|
effect.disposed = true;
|
|
288
|
+
if (effect.owner !== null)
|
|
289
|
+
ownerEffects.get(effect.owner)?.delete(effect);
|
|
23
290
|
// Detach from every signal subscriber Set so a long-lived signal never
|
|
24
291
|
// retains a disposed effect (and its closure graph).
|
|
25
292
|
for (const set of effect.sources) {
|
|
@@ -33,6 +300,14 @@ function disposeEffect(effect) {
|
|
|
33
300
|
}
|
|
34
301
|
}
|
|
35
302
|
export function disposeAll() {
|
|
303
|
+
for (const hook of disposeHooks) {
|
|
304
|
+
try {
|
|
305
|
+
hook();
|
|
306
|
+
}
|
|
307
|
+
catch {
|
|
308
|
+
// Unmount errors must not prevent the rest of the teardown.
|
|
309
|
+
}
|
|
310
|
+
}
|
|
36
311
|
for (const effect of allEffects) {
|
|
37
312
|
disposeEffect(effect);
|
|
38
313
|
}
|
|
@@ -46,6 +321,13 @@ export function disposeAll() {
|
|
|
46
321
|
}
|
|
47
322
|
}
|
|
48
323
|
rootCleanups.length = 0;
|
|
324
|
+
for (const owner of Array.from(ownerCleanups.keys())) {
|
|
325
|
+
krateDisposeOwner(owner);
|
|
326
|
+
}
|
|
327
|
+
ownerEffects.clear();
|
|
328
|
+
changedCells.clear();
|
|
329
|
+
historyCells.clear();
|
|
330
|
+
snapshotEpoch = null;
|
|
49
331
|
// Unmount external Preact islands mounted by the page before the DOM swap.
|
|
50
332
|
const g = globalThis;
|
|
51
333
|
const islands = g.__krate_island_cleanups;
|
|
@@ -61,7 +343,11 @@ export function disposeAll() {
|
|
|
61
343
|
}
|
|
62
344
|
}
|
|
63
345
|
pending.length = 0;
|
|
346
|
+
afterQueue.length = 0;
|
|
64
347
|
flushScheduled = false;
|
|
348
|
+
flushMode = 0;
|
|
349
|
+
flushToken++;
|
|
350
|
+
syncLane = false;
|
|
65
351
|
// Reset every context stack to its default so provider values from the page
|
|
66
352
|
// being torn down cannot leak into the next SPA navigation.
|
|
67
353
|
resetContexts();
|
|
@@ -165,44 +451,70 @@ export function createSignal(initial, options) {
|
|
|
165
451
|
}
|
|
166
452
|
}
|
|
167
453
|
const subs = new Set();
|
|
454
|
+
// React state: the committed value and its per-commit history.
|
|
455
|
+
const react = !!options?.react;
|
|
456
|
+
let committed = value;
|
|
457
|
+
let history = null;
|
|
458
|
+
const cell = react
|
|
459
|
+
? {
|
|
460
|
+
commit(e) {
|
|
461
|
+
if (Object.is(committed, value))
|
|
462
|
+
return;
|
|
463
|
+
if (!history)
|
|
464
|
+
history = [[0, committed]];
|
|
465
|
+
history.push([e, value]);
|
|
466
|
+
committed = value;
|
|
467
|
+
historyCells.add(cell);
|
|
468
|
+
},
|
|
469
|
+
prune(min) {
|
|
470
|
+
if (!history)
|
|
471
|
+
return false;
|
|
472
|
+
while (history.length > 1 && history[1][0] <= min)
|
|
473
|
+
history.shift();
|
|
474
|
+
if (history.length === 1) {
|
|
475
|
+
history = null;
|
|
476
|
+
return false;
|
|
477
|
+
}
|
|
478
|
+
return true;
|
|
479
|
+
},
|
|
480
|
+
}
|
|
481
|
+
: null;
|
|
482
|
+
const valueAt = (at) => {
|
|
483
|
+
if (!history)
|
|
484
|
+
return committed;
|
|
485
|
+
for (let i = history.length - 1; i >= 0; i--) {
|
|
486
|
+
if (history[i][0] <= at)
|
|
487
|
+
return history[i][1];
|
|
488
|
+
}
|
|
489
|
+
return history[0][1];
|
|
490
|
+
};
|
|
168
491
|
const read = () => {
|
|
169
492
|
const current = context[context.length - 1];
|
|
170
493
|
if (current && !current.disposed) {
|
|
171
494
|
subs.add(current);
|
|
172
495
|
current.sources.add(subs);
|
|
173
496
|
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
if (effect.disposed) {
|
|
180
|
-
// Lazy cleanup for effects that were disposed without unsubscribing.
|
|
181
|
-
subs.delete(effect);
|
|
182
|
-
continue;
|
|
183
|
-
}
|
|
184
|
-
if (effect.running) {
|
|
185
|
-
// A feedback loop that writes its own dependencies gets one
|
|
186
|
-
// re-run after the current run; further iterations are dropped to
|
|
187
|
-
// guarantee termination.
|
|
188
|
-
effect.rerun = true;
|
|
189
|
-
continue;
|
|
190
|
-
}
|
|
191
|
-
if (!effect.queued) {
|
|
192
|
-
effect.queued = true;
|
|
193
|
-
pending.push(effect);
|
|
194
|
-
}
|
|
497
|
+
if (react) {
|
|
498
|
+
if (snapshotEpoch !== null)
|
|
499
|
+
return valueAt(snapshotEpoch);
|
|
500
|
+
if (renderDepth === 0)
|
|
501
|
+
return committed;
|
|
195
502
|
}
|
|
196
|
-
|
|
503
|
+
return value;
|
|
197
504
|
};
|
|
505
|
+
const notify = (reactWrite = false) => notifySubs(subs, reactWrite);
|
|
198
506
|
const write = (next) => {
|
|
199
507
|
const resolved = typeof next === 'function' ? next(value) : next;
|
|
200
|
-
|
|
508
|
+
// Object.is bail-out (React semantics): writing the same value notifies
|
|
509
|
+
// nothing, and NaN written over NaN is "the same value".
|
|
510
|
+
if (!Object.is(resolved, value)) {
|
|
201
511
|
value = resolved;
|
|
202
512
|
if (persist) {
|
|
203
513
|
writePersisted(persist, value);
|
|
204
514
|
}
|
|
205
|
-
|
|
515
|
+
if (cell)
|
|
516
|
+
changedCells.add(cell);
|
|
517
|
+
notify(react);
|
|
206
518
|
}
|
|
207
519
|
};
|
|
208
520
|
// Cross-tab synchronization: another tab changing the same key updates this
|
|
@@ -214,7 +526,7 @@ export function createSignal(initial, options) {
|
|
|
214
526
|
return;
|
|
215
527
|
try {
|
|
216
528
|
const incoming = p.deserialize(raw);
|
|
217
|
-
if (incoming
|
|
529
|
+
if (!Object.is(incoming, value)) {
|
|
218
530
|
value = incoming;
|
|
219
531
|
notify();
|
|
220
532
|
}
|
|
@@ -225,39 +537,294 @@ export function createSignal(initial, options) {
|
|
|
225
537
|
});
|
|
226
538
|
onCleanup(unsubscribe);
|
|
227
539
|
}
|
|
540
|
+
signalGetters.add(read);
|
|
228
541
|
return [read, write];
|
|
229
542
|
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
543
|
+
// notifySubs schedules every subscriber of a changed source. Derived-value
|
|
544
|
+
// observers are marked stale synchronously (and forward the notification to
|
|
545
|
+
// their own subscribers), so anything that re-runs in the flush reads a
|
|
546
|
+
// recomputed value.
|
|
547
|
+
function notifySubs(subs, reactWrite = false) {
|
|
548
|
+
const list = Array.from(subs);
|
|
549
|
+
for (const effect of list) {
|
|
550
|
+
if (effect.disposed) {
|
|
551
|
+
// Lazy cleanup for effects that were disposed without unsubscribing.
|
|
552
|
+
subs.delete(effect);
|
|
553
|
+
continue;
|
|
554
|
+
}
|
|
555
|
+
if (effect.mark) {
|
|
556
|
+
effect.mark(reactWrite);
|
|
557
|
+
continue;
|
|
558
|
+
}
|
|
559
|
+
if (effect.running) {
|
|
560
|
+
// A feedback loop that writes its own dependencies gets one
|
|
561
|
+
// re-run after the current run; further iterations are dropped to
|
|
562
|
+
// guarantee termination.
|
|
563
|
+
effect.rerun = true;
|
|
564
|
+
continue;
|
|
565
|
+
}
|
|
566
|
+
if (!effect.queued) {
|
|
567
|
+
effect.queued = true;
|
|
568
|
+
pending.push(effect);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
scheduleFlush(reactWrite);
|
|
572
|
+
}
|
|
573
|
+
const derivedGetters = new WeakSet();
|
|
574
|
+
/**
|
|
575
|
+
* A lazily evaluated derived value: the compile target of a React render-body
|
|
576
|
+
* value (`const total = rows.reduce(...)`). The getter recomputes on read when
|
|
577
|
+
* a signal (or another derived value) it read last time has changed since, so
|
|
578
|
+
* every reader in a flush sees the same current value. Nothing is computed
|
|
579
|
+
* until the first read, and a write only marks it stale. triggers, when
|
|
580
|
+
* given, is read for its subscriptions only: an object or array literal built
|
|
581
|
+
* in render gets a new identity whenever the component would re-render.
|
|
582
|
+
*/
|
|
583
|
+
export function krateDerived(fn, triggers) {
|
|
584
|
+
let value;
|
|
585
|
+
let stale = true;
|
|
586
|
+
const subs = new Set();
|
|
587
|
+
const node = newEffectState(() => { });
|
|
588
|
+
node.mark = (reactWrite) => {
|
|
589
|
+
if (!stale) {
|
|
590
|
+
stale = true;
|
|
591
|
+
notifySubs(subs, reactWrite);
|
|
592
|
+
}
|
|
593
|
+
};
|
|
594
|
+
allEffects.add(node);
|
|
595
|
+
const read = () => {
|
|
596
|
+
const current = context[context.length - 1];
|
|
597
|
+
if (current && !current.disposed) {
|
|
598
|
+
subs.add(current);
|
|
599
|
+
current.sources.add(subs);
|
|
600
|
+
}
|
|
601
|
+
// Outside render (an event handler, an effect setup) React state reads
|
|
602
|
+
// see a committed snapshot. When that snapshot can differ from the latest
|
|
603
|
+
// values, compute from it without touching the cache, which always holds
|
|
604
|
+
// the latest render value.
|
|
605
|
+
if ((renderDepth === 0 || snapshotEpoch !== null) &&
|
|
606
|
+
(changedCells.size > 0 || (snapshotEpoch !== null && historyCells.size > 0))) {
|
|
607
|
+
return untrack(fn);
|
|
608
|
+
}
|
|
609
|
+
if (stale && !node.disposed) {
|
|
610
|
+
for (const set of node.sources) {
|
|
611
|
+
set.delete(node);
|
|
612
|
+
}
|
|
613
|
+
node.sources.clear();
|
|
614
|
+
// Cleared before computing so a source written during the computation
|
|
615
|
+
// marks it stale again instead of being lost.
|
|
616
|
+
stale = false;
|
|
617
|
+
context.push(node);
|
|
618
|
+
try {
|
|
619
|
+
// Extra sources (a render literal's state and props) only subscribe.
|
|
620
|
+
if (triggers)
|
|
621
|
+
triggers();
|
|
622
|
+
value = fn();
|
|
623
|
+
}
|
|
624
|
+
catch (err) {
|
|
625
|
+
stale = true;
|
|
626
|
+
throw err;
|
|
627
|
+
}
|
|
628
|
+
finally {
|
|
629
|
+
context.pop();
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
else if (node.disposed) {
|
|
633
|
+
return untrack(fn);
|
|
634
|
+
}
|
|
635
|
+
return value;
|
|
636
|
+
};
|
|
637
|
+
// Disposed with the binding (or component) that created it.
|
|
638
|
+
const parent = context[context.length - 1];
|
|
639
|
+
if (parent) {
|
|
640
|
+
parent.cleanups.push(() => {
|
|
641
|
+
disposeEffect(node);
|
|
642
|
+
allEffects.delete(node);
|
|
643
|
+
});
|
|
644
|
+
}
|
|
645
|
+
derivedGetters.add(read);
|
|
646
|
+
signalGetters.add(read);
|
|
647
|
+
return read;
|
|
648
|
+
}
|
|
649
|
+
/** Reports whether fn is a getter returned by krateDerived. */
|
|
650
|
+
export function isDerivedGetter(fn) {
|
|
651
|
+
return typeof fn === 'function' && derivedGetters.has(fn);
|
|
652
|
+
}
|
|
653
|
+
/**
|
|
654
|
+
* Normalizes a custom hook's return value to a getter. A hook compiled from
|
|
655
|
+
* React source returns a derived getter for a render-dependent result; any
|
|
656
|
+
* other value (a hook from a plain module, a constant) is wrapped so the call
|
|
657
|
+
* site can read every hook result the same way.
|
|
658
|
+
*/
|
|
659
|
+
export function krateHook(value) {
|
|
660
|
+
if (isDerivedGetter(value))
|
|
661
|
+
return value;
|
|
662
|
+
return () => value;
|
|
663
|
+
}
|
|
664
|
+
// Getters returned by createSignal, so the React compatibility hooks can read
|
|
665
|
+
// a dependency that is a signal getter instead of comparing the function.
|
|
666
|
+
const signalGetters = new WeakSet();
|
|
667
|
+
/** Reports whether fn is a getter returned by createSignal. */
|
|
668
|
+
export function isSignalGetter(fn) {
|
|
669
|
+
return typeof fn === 'function' && signalGetters.has(fn);
|
|
670
|
+
}
|
|
671
|
+
// 0: none, 1: microtask, 2: later task.
|
|
672
|
+
let flushMode = 0;
|
|
673
|
+
let flushToken = 0;
|
|
674
|
+
function scheduleFlush(reactWrite = false) {
|
|
675
|
+
// Transition writes never take the sync lane, even when the microtask that
|
|
676
|
+
// makes them still sees the discrete event that started the action.
|
|
677
|
+
const discrete = transitionWrites === 0 && inDiscreteEvent();
|
|
678
|
+
if (discrete)
|
|
679
|
+
syncLane = true;
|
|
680
|
+
const later = reactWrite || transitionWrites > 0;
|
|
681
|
+
const micro = !later || discrete || committing > 0 || renderDepth > 0 || typeof setTimeout !== 'function';
|
|
682
|
+
if (flushMode === 1 || (!micro && flushMode === 2))
|
|
683
|
+
return;
|
|
684
|
+
flushScheduled = true;
|
|
685
|
+
const token = ++flushToken;
|
|
686
|
+
const run = () => {
|
|
687
|
+
if (token === flushToken)
|
|
688
|
+
flushEffects();
|
|
689
|
+
};
|
|
690
|
+
if (micro) {
|
|
691
|
+
flushMode = 1;
|
|
692
|
+
queueMicrotask(run);
|
|
693
|
+
}
|
|
694
|
+
else {
|
|
695
|
+
flushMode = 2;
|
|
696
|
+
setTimeout(run, 0);
|
|
234
697
|
}
|
|
235
698
|
}
|
|
699
|
+
let flushDepth = 0;
|
|
236
700
|
function flushEffects() {
|
|
701
|
+
flushDepth++;
|
|
702
|
+
try {
|
|
703
|
+
flushEffectsInner();
|
|
704
|
+
}
|
|
705
|
+
finally {
|
|
706
|
+
flushDepth--;
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
function flushEffectsInner() {
|
|
237
710
|
flushScheduled = false;
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
711
|
+
flushMode = 0;
|
|
712
|
+
flushToken++;
|
|
713
|
+
for (const hook of beforeFlushHooks) {
|
|
714
|
+
hook();
|
|
715
|
+
}
|
|
716
|
+
// Effects re-queued by writes during a pass run in further passes of this
|
|
717
|
+
// same flush, so the cascade settles and commits before any other task or
|
|
718
|
+
// microtask can run. Otherwise code running between passes (an action's
|
|
719
|
+
// settle callback) would read React state that is written but not yet
|
|
720
|
+
// committed.
|
|
721
|
+
for (let pass = 0; pending.length > 0 && pass < 100; pass++) {
|
|
722
|
+
const effects = pending.splice(0);
|
|
723
|
+
for (const effect of effects) {
|
|
724
|
+
if (effect.disposed) {
|
|
725
|
+
continue;
|
|
726
|
+
}
|
|
727
|
+
effect.queued = false;
|
|
247
728
|
effect.rerun = false;
|
|
248
729
|
runEffect(effect);
|
|
730
|
+
if (effect.rerun && !effect.disposed) {
|
|
731
|
+
effect.rerun = false;
|
|
732
|
+
runEffect(effect);
|
|
733
|
+
}
|
|
249
734
|
}
|
|
250
735
|
}
|
|
736
|
+
if (pending.length === 0) {
|
|
737
|
+
// The cascade is done: a flush the passes scheduled has nothing to do.
|
|
738
|
+
flushMode = 0;
|
|
739
|
+
flushScheduled = false;
|
|
740
|
+
flushToken++;
|
|
741
|
+
}
|
|
742
|
+
// A cascade longer than the pass cap continues in the scheduled flush; the
|
|
743
|
+
// commit runs once it settles, so it sees the final DOM.
|
|
744
|
+
if (pending.length === 0) {
|
|
745
|
+
// Settle hooks (the class component render phase) may write signals;
|
|
746
|
+
// flush those in this same pass so the commit sees every DOM write.
|
|
747
|
+
for (const hook of settleHooks) {
|
|
748
|
+
hook();
|
|
749
|
+
}
|
|
750
|
+
if (pending.length > 0 && settleDepth < 50) {
|
|
751
|
+
settleDepth++;
|
|
752
|
+
try {
|
|
753
|
+
flushEffects();
|
|
754
|
+
}
|
|
755
|
+
finally {
|
|
756
|
+
settleDepth--;
|
|
757
|
+
}
|
|
758
|
+
return;
|
|
759
|
+
}
|
|
760
|
+
runAfterFlush();
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
const settleHooks = [];
|
|
764
|
+
let settleDepth = 0;
|
|
765
|
+
/**
|
|
766
|
+
* Registers fn to run when a flush has no pending effects, before the
|
|
767
|
+
* after-flush hooks. Signal writes it makes are flushed first (internal).
|
|
768
|
+
*/
|
|
769
|
+
export function krateOnSettle(fn) {
|
|
770
|
+
settleHooks.push(fn);
|
|
771
|
+
}
|
|
772
|
+
/** Ensures a flush runs soon even when no signal was written (internal). */
|
|
773
|
+
export function krateRequestFlush() {
|
|
774
|
+
scheduleFlush();
|
|
775
|
+
}
|
|
776
|
+
/**
|
|
777
|
+
* Runs the after-flush hooks now. Used when no signal flush is pending (for
|
|
778
|
+
* example, right after hydration registered effects).
|
|
779
|
+
*/
|
|
780
|
+
export function runAfterFlush() {
|
|
781
|
+
// Every settled flush is a commit: record React state here, so committed
|
|
782
|
+
// values advance even when no effect scheduler is loaded.
|
|
783
|
+
if (changedCells.size > 0)
|
|
784
|
+
krateCommitSnapshot();
|
|
785
|
+
// React's commit reads document.activeElement (to restore focus and
|
|
786
|
+
// selection). In Firefox that read applies the focus fixup rule, so a
|
|
787
|
+
// focused button the commit disabled loses focus; reading it here gives
|
|
788
|
+
// each browser the same focus outcome as under React.
|
|
789
|
+
if (typeof document !== 'undefined')
|
|
790
|
+
void document.activeElement;
|
|
791
|
+
for (const hook of afterFlushHooks) {
|
|
792
|
+
hook();
|
|
793
|
+
}
|
|
794
|
+
drainAfterQueue();
|
|
795
|
+
}
|
|
796
|
+
/**
|
|
797
|
+
* Synchronously flushes pending signal effects and the commit that follows
|
|
798
|
+
* them (the building block for flushSync and tests).
|
|
799
|
+
*/
|
|
800
|
+
export function krateFlushSync() {
|
|
801
|
+
if (pending.length === 0) {
|
|
802
|
+
runAfterFlush();
|
|
803
|
+
return;
|
|
804
|
+
}
|
|
805
|
+
// flushEffects runs the after-flush hooks itself once nothing is pending.
|
|
806
|
+
let guard = 0;
|
|
807
|
+
while (pending.length > 0 && guard++ < 100) {
|
|
808
|
+
flushEffects();
|
|
809
|
+
}
|
|
251
810
|
}
|
|
252
811
|
function runEffect(effect) {
|
|
253
812
|
effect.running = true;
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
813
|
+
let pushed = false;
|
|
814
|
+
const prevOwner = currentOwner;
|
|
815
|
+
currentOwner = effect.owner;
|
|
816
|
+
renderDepth++;
|
|
817
|
+
// A tracked run is render work: it sees the latest values even when it is
|
|
818
|
+
// started from inside an effect setup or cleanup.
|
|
819
|
+
const prevEpoch = snapshotEpoch;
|
|
820
|
+
snapshotEpoch = null;
|
|
260
821
|
try {
|
|
822
|
+
// Run cleanups from previous execution
|
|
823
|
+
for (const cleanup of effect.cleanups.splice(0)) {
|
|
824
|
+
cleanup();
|
|
825
|
+
}
|
|
826
|
+
context.push(effect);
|
|
827
|
+
pushed = true;
|
|
261
828
|
// Support Solid-style `return () => {...}` cleanup functions. Many
|
|
262
829
|
// components (Slider, Dropdown, Dialog, Tooltip) register their document
|
|
263
830
|
// listeners in an effect and return a cleanup that removes them; without
|
|
@@ -268,33 +835,112 @@ function runEffect(effect) {
|
|
|
268
835
|
effect.cleanups.push(ret);
|
|
269
836
|
}
|
|
270
837
|
}
|
|
838
|
+
catch (err) {
|
|
839
|
+
// An owned effect reports to its owner (error boundaries); unowned
|
|
840
|
+
// effects keep the previous behavior and throw.
|
|
841
|
+
if (!(effect.boundaryOwner && ownerHooks && ownerHooks.error(effect.boundaryOwner, err))) {
|
|
842
|
+
throw err;
|
|
843
|
+
}
|
|
844
|
+
}
|
|
271
845
|
finally {
|
|
272
|
-
|
|
846
|
+
snapshotEpoch = prevEpoch;
|
|
847
|
+
renderDepth--;
|
|
848
|
+
currentOwner = prevOwner;
|
|
849
|
+
if (pushed)
|
|
850
|
+
context.pop();
|
|
273
851
|
effect.running = false;
|
|
274
852
|
}
|
|
275
853
|
}
|
|
276
854
|
export function createEffect(fn) {
|
|
277
855
|
const effect = newEffectState(fn);
|
|
278
856
|
allEffects.add(effect);
|
|
279
|
-
|
|
280
|
-
|
|
857
|
+
// An effect created while an ownership root is current is disposed with it,
|
|
858
|
+
// and an effect created under a component owner is disposed when that
|
|
859
|
+
// component unmounts.
|
|
860
|
+
const owner = context[context.length - 1];
|
|
861
|
+
const dispose = () => {
|
|
281
862
|
disposeEffect(effect);
|
|
282
863
|
allEffects.delete(effect);
|
|
283
864
|
};
|
|
865
|
+
if (effect.boundaryOwner && ownerHooks) {
|
|
866
|
+
ownerHooks.cleanup(effect.boundaryOwner, dispose);
|
|
867
|
+
}
|
|
868
|
+
runEffect(effect);
|
|
869
|
+
if (owner && owner.root)
|
|
870
|
+
addOwnerCleanup(owner, dispose);
|
|
871
|
+
return dispose;
|
|
284
872
|
}
|
|
285
873
|
export function onCleanup(fn) {
|
|
286
|
-
const current =
|
|
874
|
+
const current = currentEffect();
|
|
287
875
|
if (current) {
|
|
288
|
-
current
|
|
876
|
+
addOwnerCleanup(current, fn);
|
|
877
|
+
return;
|
|
878
|
+
}
|
|
879
|
+
const owner = ownerHooks ? ownerHooks.current() : null;
|
|
880
|
+
if (owner && ownerHooks) {
|
|
881
|
+
// Component-level cleanup: runs when the owning component unmounts.
|
|
882
|
+
ownerHooks.cleanup(owner, fn);
|
|
289
883
|
}
|
|
290
884
|
else {
|
|
291
|
-
// No active effect:
|
|
292
|
-
// disposeAll() (route change / page teardown).
|
|
293
|
-
|
|
885
|
+
// No active effect: a component-level cleanup. It runs when its owner
|
|
886
|
+
// unmounts, or on disposeAll() (route change / page teardown).
|
|
887
|
+
if (currentOwner !== null) {
|
|
888
|
+
let list = ownerCleanups.get(currentOwner);
|
|
889
|
+
if (!list)
|
|
890
|
+
ownerCleanups.set(currentOwner, (list = []));
|
|
891
|
+
list.push(fn);
|
|
892
|
+
}
|
|
893
|
+
else {
|
|
894
|
+
rootCleanups.push(fn);
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
function addOwnerCleanup(owner, fn) {
|
|
899
|
+
if (owner.dead) {
|
|
900
|
+
// The root is already gone (a late mount callback): clean up at once.
|
|
901
|
+
fn();
|
|
902
|
+
return;
|
|
903
|
+
}
|
|
904
|
+
owner.cleanups.push(fn);
|
|
905
|
+
}
|
|
906
|
+
/**
|
|
907
|
+
* Run `fn` in a new ownership root. Effects, memos, onCleanup callbacks and
|
|
908
|
+
* onMount callbacks registered while it runs (and the cleanups those mount
|
|
909
|
+
* callbacks register) belong to the root and are torn down, in registration
|
|
910
|
+
* order, when the returned `dispose` is called. Signal reads inside `fn` are
|
|
911
|
+
* not tracked by any enclosing effect. Keyed list rows and keyed remounts use
|
|
912
|
+
* one root per row so a removed row runs its cleanups without touching its
|
|
913
|
+
* siblings.
|
|
914
|
+
*/
|
|
915
|
+
export function createRoot(fn) {
|
|
916
|
+
const root = newEffectState(() => { });
|
|
917
|
+
// `disposed` keeps signal reads inside the root untracked.
|
|
918
|
+
root.disposed = true;
|
|
919
|
+
root.root = true;
|
|
920
|
+
const dispose = () => {
|
|
921
|
+
if (root.dead)
|
|
922
|
+
return;
|
|
923
|
+
root.dead = true;
|
|
924
|
+
const cleanups = root.cleanups.splice(0);
|
|
925
|
+
for (const cleanup of cleanups) {
|
|
926
|
+
try {
|
|
927
|
+
cleanup();
|
|
928
|
+
}
|
|
929
|
+
catch (err) {
|
|
930
|
+
console.error(err);
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
};
|
|
934
|
+
context.push(root);
|
|
935
|
+
try {
|
|
936
|
+
return fn(dispose);
|
|
937
|
+
}
|
|
938
|
+
finally {
|
|
939
|
+
context.pop();
|
|
294
940
|
}
|
|
295
941
|
}
|
|
296
942
|
export function onMount(fn) {
|
|
297
|
-
mountQueue.push(fn);
|
|
943
|
+
mountQueue.push({ fn, owner: context[context.length - 1], boundary: ownerHooks ? ownerHooks.current() : null });
|
|
298
944
|
if (!mountScheduled) {
|
|
299
945
|
mountScheduled = true;
|
|
300
946
|
queueMicrotask(flushMounts);
|
|
@@ -303,8 +949,33 @@ export function onMount(fn) {
|
|
|
303
949
|
function flushMounts() {
|
|
304
950
|
mountScheduled = false;
|
|
305
951
|
const queue = mountQueue.splice(0);
|
|
306
|
-
for (const fn of queue) {
|
|
307
|
-
|
|
952
|
+
for (const { fn, owner, boundary } of queue) {
|
|
953
|
+
const run = () => {
|
|
954
|
+
if (!owner) {
|
|
955
|
+
fn();
|
|
956
|
+
return;
|
|
957
|
+
}
|
|
958
|
+
// The owner unmounted before its mount effects ran: skip them, as React
|
|
959
|
+
// does for a component that never committed.
|
|
960
|
+
if (owner.dead || (!owner.root && owner.disposed))
|
|
961
|
+
return;
|
|
962
|
+
// Collect cleanups registered by the callback without tracking its reads.
|
|
963
|
+
const collector = newEffectState(() => { });
|
|
964
|
+
collector.disposed = true;
|
|
965
|
+
context.push(collector);
|
|
966
|
+
try {
|
|
967
|
+
fn();
|
|
968
|
+
}
|
|
969
|
+
finally {
|
|
970
|
+
context.pop();
|
|
971
|
+
}
|
|
972
|
+
for (const cleanup of collector.cleanups)
|
|
973
|
+
addOwnerCleanup(owner, cleanup);
|
|
974
|
+
};
|
|
975
|
+
if (boundary && ownerHooks)
|
|
976
|
+
ownerHooks.run(boundary, run);
|
|
977
|
+
else
|
|
978
|
+
run();
|
|
308
979
|
}
|
|
309
980
|
}
|
|
310
981
|
// createMemo computes a derived value. Its computation effect is registered
|
|
@@ -322,14 +993,18 @@ export function createMemo(fn) {
|
|
|
322
993
|
runEffect(effect);
|
|
323
994
|
// Register cleanup on the parent context so this memo effect is disposed
|
|
324
995
|
// when the parent re-runs or is disposed.
|
|
325
|
-
const parent =
|
|
996
|
+
const parent = currentEffect();
|
|
997
|
+
const disposeMemo = () => {
|
|
998
|
+
if (!effect.disposed) {
|
|
999
|
+
disposeEffect(effect);
|
|
1000
|
+
allEffects.delete(effect);
|
|
1001
|
+
}
|
|
1002
|
+
};
|
|
326
1003
|
if (parent) {
|
|
327
|
-
parent.cleanups.push(
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
}
|
|
332
|
-
});
|
|
1004
|
+
parent.cleanups.push(disposeMemo);
|
|
1005
|
+
}
|
|
1006
|
+
else if (effect.boundaryOwner && ownerHooks) {
|
|
1007
|
+
ownerHooks.cleanup(effect.boundaryOwner, disposeMemo);
|
|
333
1008
|
}
|
|
334
1009
|
return get;
|
|
335
1010
|
}
|
|
@@ -340,8 +1015,18 @@ export function createMemo(fn) {
|
|
|
340
1015
|
* other signal. Used both directly and as the lowering target for React's
|
|
341
1016
|
* `useReducer`.
|
|
342
1017
|
*/
|
|
343
|
-
export function createReducer(reducer, initial) {
|
|
344
|
-
|
|
1018
|
+
export function createReducer(reducer, initial, init, options) {
|
|
1019
|
+
// React's lazy initializer: useReducer(reducer, arg, init) starts at init(arg).
|
|
1020
|
+
// An options object may take init's place (createReducer(r, s, opts)).
|
|
1021
|
+
if (init && typeof init === 'object') {
|
|
1022
|
+
options = init;
|
|
1023
|
+
init = undefined;
|
|
1024
|
+
}
|
|
1025
|
+
const start = (typeof init === 'function' ? init(initial) : initial);
|
|
1026
|
+
const [get, set] = createSignal(start, options);
|
|
1027
|
+
// dispatch is created once, so its identity is stable for the component's
|
|
1028
|
+
// lifetime. The signal's Object.is bail-out skips updates when the reducer
|
|
1029
|
+
// returns the current state.
|
|
345
1030
|
const dispatch = (action) => set((prev) => reducer(prev, action));
|
|
346
1031
|
return [get, dispatch];
|
|
347
1032
|
}
|