@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/effects.js
ADDED
|
@@ -0,0 +1,719 @@
|
|
|
1
|
+
// React-style effect scheduling on top of signals.
|
|
2
|
+
//
|
|
3
|
+
// Krate components run once, so there is no render to hang effects on.
|
|
4
|
+
// Instead each lowered useEffect/useLayoutEffect/useInsertionEffect is a
|
|
5
|
+
// "hook effect": a tiny tracker effect re-reads the dependency thunk whenever a
|
|
6
|
+
// signal it reads changes and, when the dependency array differs (Object.is
|
|
7
|
+
// per element), marks the hook dirty. Dirty hooks run in a commit that follows
|
|
8
|
+
// the signal flush, after the DOM bindings have been updated:
|
|
9
|
+
//
|
|
10
|
+
// mutation per component (children before parents): insertion cleanup,
|
|
11
|
+
// insertion setup, layout cleanup
|
|
12
|
+
// layout layout setups, children before parents
|
|
13
|
+
// passive (a macrotask later, after paint) all passive cleanups, then all
|
|
14
|
+
// passive setups, children before parents
|
|
15
|
+
//
|
|
16
|
+
// A pending passive phase is always flushed before the next update is
|
|
17
|
+
// processed, as React does. Unmounting a component runs its cleanups parent before child:
|
|
18
|
+
// insertion and layout cleanups immediately, passive cleanups in the passive
|
|
19
|
+
// phase. Component boundaries come from owners: hydration scripts declare
|
|
20
|
+
// them with krateOwner and runtime-created components get one from h().
|
|
21
|
+
import { createEffect, krateGetOwner, krateSetOwner, krateOnAfterFlush, krateOnBeforeFlush, krateOnDispose, kratePendingFlush, runAfterFlush, untrack, krateCommitting, krateDisposeOwner, krateEpoch, kratePruneSnapshots, krateReadAt, krateTakeSyncLane, } from './signal.js';
|
|
22
|
+
const root = newOwner(null, null);
|
|
23
|
+
const byId = new Map();
|
|
24
|
+
// Last branch shown by each dynamic conditional slot, as reported by kbindCond.
|
|
25
|
+
const condShown = new Map();
|
|
26
|
+
// Content slots whose server-rendered content was replaced on the client.
|
|
27
|
+
const replacedSlots = new Set();
|
|
28
|
+
const dirty = new Set();
|
|
29
|
+
let seq = 0;
|
|
30
|
+
let commitQueued = false;
|
|
31
|
+
let passiveTimer = null;
|
|
32
|
+
// Passive work of the last commit, run in order by flushPassive.
|
|
33
|
+
let passiveCleanups = [];
|
|
34
|
+
let passiveSetups = [];
|
|
35
|
+
const records = [];
|
|
36
|
+
// Mounted hooks per snapshot epoch, so old snapshots can be pruned.
|
|
37
|
+
const liveEpochs = new Map();
|
|
38
|
+
const unmountHooks = [];
|
|
39
|
+
function newOwner(id, parent) {
|
|
40
|
+
return { id, parent, children: [], hooks: [], disposed: false, shown: true, conds: [], slot: null, record: null };
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Registers fn to run whenever a component owner unmounts (parent first),
|
|
44
|
+
* after its effect cleanups and before its bindings are disposed. Lets other
|
|
45
|
+
* lowerings (class component lifecycles) observe unmount and remount.
|
|
46
|
+
*/
|
|
47
|
+
export function krateOnUnmount(fn) {
|
|
48
|
+
unmountHooks.push(fn);
|
|
49
|
+
}
|
|
50
|
+
/** The id of a component owner (null for runtime-created components). */
|
|
51
|
+
export function krateOwnerId(owner) {
|
|
52
|
+
return ownerOf(owner).id;
|
|
53
|
+
}
|
|
54
|
+
function ownerOf(value) {
|
|
55
|
+
return value && typeof value === 'object' && 'hooks' in value ? value : root;
|
|
56
|
+
}
|
|
57
|
+
function isActive(owner) {
|
|
58
|
+
for (let o = owner; o; o = o.parent) {
|
|
59
|
+
if (o.disposed || !o.shown)
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
function condVisible(owner) {
|
|
65
|
+
for (const [id, branch] of owner.conds) {
|
|
66
|
+
const shown = condShown.get(id);
|
|
67
|
+
if (shown !== undefined && shown !== branch)
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Starts a hydrated component scope (emitted at the top of each component's
|
|
74
|
+
* hydration script). `parentId` names the enclosing component, `conds` the
|
|
75
|
+
* conditional branches `[slotId, "c" | "a", initiallyShown]` it renders
|
|
76
|
+
* inside, and `slot` the content slot (list) whose client-side replacement
|
|
77
|
+
* unmounts it. `krateOwner(null)` ends hydration scopes.
|
|
78
|
+
*/
|
|
79
|
+
export function krateOwner(id, parentId, conds, slot) {
|
|
80
|
+
if (id == null) {
|
|
81
|
+
krateSetOwner(null);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
let owner = byId.get(id);
|
|
85
|
+
if (!owner) {
|
|
86
|
+
const parent = (parentId != null && byId.get(parentId)) || root;
|
|
87
|
+
owner = newOwner(id, parent);
|
|
88
|
+
// A component inside an unmounted one is unmounted too.
|
|
89
|
+
for (let o = parent; o; o = o.parent) {
|
|
90
|
+
if (o.disposed)
|
|
91
|
+
owner.disposed = true;
|
|
92
|
+
}
|
|
93
|
+
parent.children.push(owner);
|
|
94
|
+
byId.set(id, owner);
|
|
95
|
+
for (const [cid, branch, initially] of conds || []) {
|
|
96
|
+
owner.conds.push([cid, branch]);
|
|
97
|
+
if (!condShown.has(cid)) {
|
|
98
|
+
// No binding reported this conditional yet: trust the server markup.
|
|
99
|
+
condShown.set(cid, initially ? branch : branch === 'c' ? 'a' : 'c');
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
owner.shown = condVisible(owner);
|
|
103
|
+
if (slot) {
|
|
104
|
+
owner.slot = slot;
|
|
105
|
+
// The list was already re-rendered by its binding: this server-rendered
|
|
106
|
+
// instance no longer exists, so its effects must never run.
|
|
107
|
+
if (replacedSlots.has(slot))
|
|
108
|
+
owner.disposed = true;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
krateSetOwner(owner);
|
|
112
|
+
}
|
|
113
|
+
/** Runs fn as the body of a new child component of the current owner. */
|
|
114
|
+
export function krateWithOwner(fn) {
|
|
115
|
+
const parent = ownerOf(krateGetOwner());
|
|
116
|
+
const owner = newOwner(null, parent);
|
|
117
|
+
parent.children.push(owner);
|
|
118
|
+
const prev = krateSetOwner(owner);
|
|
119
|
+
try {
|
|
120
|
+
return { value: fn(), owner };
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
krateSetOwner(prev);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Unmounts a component created by krateWithOwner/krateOwner: cleanups run
|
|
128
|
+
* parent before child (insertion and layout now, passive in the passive
|
|
129
|
+
* phase) and its hooks never run again.
|
|
130
|
+
*/
|
|
131
|
+
export function krateUnmount(value) {
|
|
132
|
+
const owner = ownerOf(value);
|
|
133
|
+
if (owner === root || owner.disposed)
|
|
134
|
+
return;
|
|
135
|
+
const wasActive = isActive(owner);
|
|
136
|
+
const passive = [];
|
|
137
|
+
disposeTree(owner, wasActive, passive);
|
|
138
|
+
if (owner.parent) {
|
|
139
|
+
const list = owner.parent.children;
|
|
140
|
+
const i = list.indexOf(owner);
|
|
141
|
+
if (i >= 0)
|
|
142
|
+
list.splice(i, 1);
|
|
143
|
+
}
|
|
144
|
+
if (passive.length) {
|
|
145
|
+
passiveCleanups.push(...passive);
|
|
146
|
+
schedulePassive();
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
function disposeTree(owner, active, passive) {
|
|
150
|
+
// Disposed owners stay in byId so hydration scopes declared later inside
|
|
151
|
+
// them resolve their (unmounted) parent instead of the root.
|
|
152
|
+
owner.disposed = true;
|
|
153
|
+
for (const hook of owner.hooks) {
|
|
154
|
+
hook.stop();
|
|
155
|
+
dirty.delete(hook);
|
|
156
|
+
if (!active) {
|
|
157
|
+
// Hidden, so its cleanup already ran (or never will): just release it.
|
|
158
|
+
if (hook.mounted) {
|
|
159
|
+
hook.mounted = false;
|
|
160
|
+
hook.cleanup = null;
|
|
161
|
+
releaseEpoch(hook.epoch);
|
|
162
|
+
}
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
if (hook.phase === 'passive') {
|
|
166
|
+
const c = takeCleanup(hook);
|
|
167
|
+
if (c)
|
|
168
|
+
passive.push(c);
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
runCleanup(hook);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
for (const fn of unmountHooks)
|
|
175
|
+
fn(owner, owner.id);
|
|
176
|
+
// Bindings, refs and top-level cleanups of the component go with it.
|
|
177
|
+
krateDisposeOwner(owner);
|
|
178
|
+
if (owner.record && owner.record.owner === owner)
|
|
179
|
+
owner.record.owner = null;
|
|
180
|
+
for (const child of owner.children.slice()) {
|
|
181
|
+
disposeTree(child, active && child.shown, passive);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Unmounts the hydrated component `id` and forgets it, so running its
|
|
186
|
+
* hydration scope again (a remount by the class/boundary owner tree in
|
|
187
|
+
* boundary.ts) creates a fresh owner whose effects mount again.
|
|
188
|
+
*/
|
|
189
|
+
export function krateForgetOwner(id) {
|
|
190
|
+
const owner = byId.get(id);
|
|
191
|
+
if (!owner)
|
|
192
|
+
return;
|
|
193
|
+
krateUnmount(owner);
|
|
194
|
+
byId.delete(id);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Declares a remountable hydrated component (emitted for components inside
|
|
198
|
+
* a conditional branch). `fn` is the component's hydration scope; it runs now
|
|
199
|
+
* when the branch is shown, and again, as a fresh instance, every time the
|
|
200
|
+
* branch is shown after being hidden. Hiding the branch unmounts it:
|
|
201
|
+
* cleanups run, refs and bindings are released and its state is discarded.
|
|
202
|
+
*/
|
|
203
|
+
export function krateMount(id, parentId, conds, slot, fn) {
|
|
204
|
+
const rec = { id, parentId, conds: [], slot: slot || null, fn, owner: null };
|
|
205
|
+
for (const [cid, branch, initially] of conds || []) {
|
|
206
|
+
rec.conds.push([cid, branch]);
|
|
207
|
+
if (!condShown.has(cid)) {
|
|
208
|
+
condShown.set(cid, initially ? branch : branch === 'c' ? 'a' : 'c');
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
records.push(rec);
|
|
212
|
+
if (recordVisible(rec))
|
|
213
|
+
mountRecord(rec);
|
|
214
|
+
}
|
|
215
|
+
function recordParent(rec) {
|
|
216
|
+
if (rec.parentId == null)
|
|
217
|
+
return root;
|
|
218
|
+
const p = byId.get(rec.parentId);
|
|
219
|
+
if (!p)
|
|
220
|
+
return root;
|
|
221
|
+
for (let o = p; o; o = o.parent) {
|
|
222
|
+
if (o.disposed)
|
|
223
|
+
return null;
|
|
224
|
+
}
|
|
225
|
+
return p;
|
|
226
|
+
}
|
|
227
|
+
function recordVisible(rec) {
|
|
228
|
+
if (rec.slot && replacedSlots.has(rec.slot))
|
|
229
|
+
return false;
|
|
230
|
+
for (const [cid, branch] of rec.conds) {
|
|
231
|
+
const shown = condShown.get(cid);
|
|
232
|
+
if (shown !== undefined && shown !== branch)
|
|
233
|
+
return false;
|
|
234
|
+
}
|
|
235
|
+
return recordParent(rec) !== null;
|
|
236
|
+
}
|
|
237
|
+
function mountRecord(rec) {
|
|
238
|
+
const parent = recordParent(rec) || root;
|
|
239
|
+
const owner = newOwner(rec.id, parent);
|
|
240
|
+
owner.record = rec;
|
|
241
|
+
owner.slot = rec.slot;
|
|
242
|
+
parent.children.push(owner);
|
|
243
|
+
byId.set(rec.id, owner);
|
|
244
|
+
rec.owner = owner;
|
|
245
|
+
const prev = krateSetOwner(owner);
|
|
246
|
+
try {
|
|
247
|
+
// The scope may run inside a binding (a conditional swap); its reads must
|
|
248
|
+
// not subscribe that binding.
|
|
249
|
+
untrack(rec.fn);
|
|
250
|
+
}
|
|
251
|
+
finally {
|
|
252
|
+
krateSetOwner(prev);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Reports that the hydrated content of slot `id` was replaced by its binding
|
|
257
|
+
* (a list re-render): hydrated components inside it are unmounted.
|
|
258
|
+
*/
|
|
259
|
+
export function krateSlotReplaced(id) {
|
|
260
|
+
if (replacedSlots.has(id))
|
|
261
|
+
return;
|
|
262
|
+
replacedSlots.add(id);
|
|
263
|
+
for (const owner of Array.from(byId.values())) {
|
|
264
|
+
if (owner.slot === id)
|
|
265
|
+
krateUnmount(owner);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Reports which branch ("c" consequent, "a" alternate) conditional slot `id`
|
|
270
|
+
* shows. Components in the hidden branch keep their state but have their
|
|
271
|
+
* effects cleaned up (React's Activity semantics); showing the branch again
|
|
272
|
+
* re-runs every effect setup in the next commit.
|
|
273
|
+
*/
|
|
274
|
+
export function krateCondActivity(id, branch) {
|
|
275
|
+
if (condShown.get(id) === branch)
|
|
276
|
+
return;
|
|
277
|
+
condShown.set(id, branch);
|
|
278
|
+
// Remountable components: unmount the ones that left (parents first), then
|
|
279
|
+
// mount fresh instances of the ones that entered (parents first).
|
|
280
|
+
for (const rec of records) {
|
|
281
|
+
if (rec.owner && !rec.owner.disposed && !recordVisible(rec))
|
|
282
|
+
krateUnmount(rec.owner);
|
|
283
|
+
}
|
|
284
|
+
for (const rec of records) {
|
|
285
|
+
if (!rec.owner && recordVisible(rec))
|
|
286
|
+
mountRecord(rec);
|
|
287
|
+
}
|
|
288
|
+
const affected = [];
|
|
289
|
+
for (const owner of byId.values()) {
|
|
290
|
+
if (!owner.record && owner.conds.some(([cid]) => cid === id))
|
|
291
|
+
affected.push(owner);
|
|
292
|
+
}
|
|
293
|
+
let passive = [];
|
|
294
|
+
for (const owner of affected) {
|
|
295
|
+
const visible = condVisible(owner);
|
|
296
|
+
if (visible === owner.shown)
|
|
297
|
+
continue;
|
|
298
|
+
const before = isActive(owner);
|
|
299
|
+
owner.shown = visible;
|
|
300
|
+
const after = isActive(owner);
|
|
301
|
+
if (before && !after)
|
|
302
|
+
hideTree(owner, passive);
|
|
303
|
+
else if (!before && after)
|
|
304
|
+
showTree(owner);
|
|
305
|
+
}
|
|
306
|
+
if (passive.length) {
|
|
307
|
+
passiveCleanups.push(...passive);
|
|
308
|
+
passive = [];
|
|
309
|
+
schedulePassive();
|
|
310
|
+
}
|
|
311
|
+
scheduleCommit();
|
|
312
|
+
}
|
|
313
|
+
function hideTree(owner, passive) {
|
|
314
|
+
for (const hook of owner.hooks) {
|
|
315
|
+
dirty.delete(hook);
|
|
316
|
+
if (hook.phase === 'passive') {
|
|
317
|
+
const c = takeCleanup(hook);
|
|
318
|
+
if (c)
|
|
319
|
+
passive.push(c);
|
|
320
|
+
}
|
|
321
|
+
else {
|
|
322
|
+
runCleanup(hook);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
for (const child of owner.children) {
|
|
326
|
+
if (child.shown && !child.disposed)
|
|
327
|
+
hideTree(child, passive);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
function showTree(owner) {
|
|
331
|
+
for (const hook of owner.hooks) {
|
|
332
|
+
markDirty(hook);
|
|
333
|
+
}
|
|
334
|
+
for (const child of owner.children) {
|
|
335
|
+
if (child.shown && !child.disposed)
|
|
336
|
+
showTree(child);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
function depsEqual(a, b) {
|
|
340
|
+
if (!a || !b || a.length !== b.length)
|
|
341
|
+
return false;
|
|
342
|
+
for (let i = 0; i < a.length; i++) {
|
|
343
|
+
if (!Object.is(a[i], b[i]))
|
|
344
|
+
return false;
|
|
345
|
+
}
|
|
346
|
+
return true;
|
|
347
|
+
}
|
|
348
|
+
function toDeps(value) {
|
|
349
|
+
if (value == null)
|
|
350
|
+
return undefined;
|
|
351
|
+
return Array.isArray(value) ? value : Array.from(value);
|
|
352
|
+
}
|
|
353
|
+
function markDirty(hook) {
|
|
354
|
+
hook.dirty = true;
|
|
355
|
+
dirty.add(hook);
|
|
356
|
+
scheduleCommit();
|
|
357
|
+
}
|
|
358
|
+
function scheduleCommit() {
|
|
359
|
+
if (commitQueued)
|
|
360
|
+
return;
|
|
361
|
+
commitQueued = true;
|
|
362
|
+
queueMicrotask(() => {
|
|
363
|
+
// A pending signal flush runs the commit itself once it settles.
|
|
364
|
+
if (!kratePendingFlush()) {
|
|
365
|
+
flushPassive();
|
|
366
|
+
runAfterFlush();
|
|
367
|
+
}
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Registers a React effect. `deps` returns the dependency array (read inside
|
|
372
|
+
* a tracking scope, so signal getters it calls are subscribed); the effect
|
|
373
|
+
* re-runs in the next commit when any element changed by Object.is. A null
|
|
374
|
+
* thunk means "no dependency array" and the effect runs only on mount here;
|
|
375
|
+
* the compiler synthesizes the component's state and props as dependencies
|
|
376
|
+
* for that case instead. `fn` may return a cleanup function.
|
|
377
|
+
*/
|
|
378
|
+
export function createEffectDeps(fn, deps, phase = 'passive') {
|
|
379
|
+
const owner = ownerOf(krateGetOwner());
|
|
380
|
+
const hook = {
|
|
381
|
+
owner,
|
|
382
|
+
phase,
|
|
383
|
+
fn,
|
|
384
|
+
deps: deps || null,
|
|
385
|
+
prev: undefined,
|
|
386
|
+
hasPrev: false,
|
|
387
|
+
cleanup: null,
|
|
388
|
+
mounted: false,
|
|
389
|
+
epoch: 0,
|
|
390
|
+
dirty: false,
|
|
391
|
+
seq: seq++,
|
|
392
|
+
stop: () => { },
|
|
393
|
+
};
|
|
394
|
+
owner.hooks.push(hook);
|
|
395
|
+
if (owner.disposed)
|
|
396
|
+
return;
|
|
397
|
+
hook.stop = createEffect(() => {
|
|
398
|
+
const next = hook.deps ? toDeps(hook.deps()) : [];
|
|
399
|
+
if (hook.hasPrev && depsEqual(hook.prev, next))
|
|
400
|
+
return;
|
|
401
|
+
hook.prev = next;
|
|
402
|
+
hook.hasPrev = true;
|
|
403
|
+
if (isActive(hook.owner))
|
|
404
|
+
markDirty(hook);
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
function runSetup(hook, at) {
|
|
408
|
+
if (hook.owner.disposed)
|
|
409
|
+
return;
|
|
410
|
+
let ret;
|
|
411
|
+
try {
|
|
412
|
+
// The setup sees the state of the commit it belongs to.
|
|
413
|
+
ret = krateReadAt(at, () => untrack(hook.fn));
|
|
414
|
+
}
|
|
415
|
+
catch (err) {
|
|
416
|
+
reportHookError(err, hook.owner);
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
hook.cleanup = typeof ret === 'function' ? ret : null;
|
|
420
|
+
hook.mounted = true;
|
|
421
|
+
hook.epoch = at;
|
|
422
|
+
liveEpochs.set(at, (liveEpochs.get(at) || 0) + 1);
|
|
423
|
+
}
|
|
424
|
+
// Error boundaries (boundary.ts) receive errors thrown by effect setups and
|
|
425
|
+
// cleanups, as React's do. Without a handler the error is rethrown.
|
|
426
|
+
let hookErrorHandler = null;
|
|
427
|
+
/** Installs the handler for errors thrown by hook effects (internal). */
|
|
428
|
+
export function krateOnHookError(fn) {
|
|
429
|
+
hookErrorHandler = fn;
|
|
430
|
+
}
|
|
431
|
+
function reportHookError(err, owner) {
|
|
432
|
+
let id = null;
|
|
433
|
+
for (let o = owner; o && id === null; o = o.parent)
|
|
434
|
+
id = o.id;
|
|
435
|
+
if (!(hookErrorHandler && hookErrorHandler(err, id)))
|
|
436
|
+
throw err;
|
|
437
|
+
}
|
|
438
|
+
function runGuarded(fn, owner) {
|
|
439
|
+
try {
|
|
440
|
+
untrack(fn);
|
|
441
|
+
}
|
|
442
|
+
catch (err) {
|
|
443
|
+
reportHookError(err, owner);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
// Detaches the cleanup of the last setup, bound to that setup's snapshot so
|
|
447
|
+
// it reads the values of the render it belongs to (React closure semantics).
|
|
448
|
+
function takeCleanup(hook) {
|
|
449
|
+
if (!hook.mounted)
|
|
450
|
+
return null;
|
|
451
|
+
hook.mounted = false;
|
|
452
|
+
const at = hook.epoch;
|
|
453
|
+
const c = hook.cleanup;
|
|
454
|
+
hook.cleanup = null;
|
|
455
|
+
if (!c) {
|
|
456
|
+
releaseEpoch(at);
|
|
457
|
+
return null;
|
|
458
|
+
}
|
|
459
|
+
// The snapshot stays live until the (possibly deferred) cleanup ran.
|
|
460
|
+
return () => {
|
|
461
|
+
try {
|
|
462
|
+
krateReadAt(at, () => untrack(c));
|
|
463
|
+
}
|
|
464
|
+
finally {
|
|
465
|
+
releaseEpoch(at);
|
|
466
|
+
}
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
function releaseEpoch(at) {
|
|
470
|
+
const n = (liveEpochs.get(at) || 1) - 1;
|
|
471
|
+
if (n > 0)
|
|
472
|
+
liveEpochs.set(at, n);
|
|
473
|
+
else
|
|
474
|
+
liveEpochs.delete(at);
|
|
475
|
+
}
|
|
476
|
+
function runCleanup(hook) {
|
|
477
|
+
const c = takeCleanup(hook);
|
|
478
|
+
if (c)
|
|
479
|
+
runGuarded(c, hook.owner);
|
|
480
|
+
}
|
|
481
|
+
function pruneSnapshots() {
|
|
482
|
+
let min = krateEpoch();
|
|
483
|
+
for (const e of liveEpochs.keys())
|
|
484
|
+
if (e < min)
|
|
485
|
+
min = e;
|
|
486
|
+
kratePruneSnapshots(min);
|
|
487
|
+
}
|
|
488
|
+
// Orders hooks children-before-parents (post-order over the owner tree),
|
|
489
|
+
// declaration order within a component.
|
|
490
|
+
function orderHooks(hooks) {
|
|
491
|
+
const rank = new Map();
|
|
492
|
+
let n = 0;
|
|
493
|
+
const visit = (o) => {
|
|
494
|
+
for (const c of o.children)
|
|
495
|
+
visit(c);
|
|
496
|
+
rank.set(o, n++);
|
|
497
|
+
};
|
|
498
|
+
for (const c of root.children)
|
|
499
|
+
visit(c);
|
|
500
|
+
rank.set(root, n++);
|
|
501
|
+
return Array.from(hooks).sort((a, b) => {
|
|
502
|
+
const ra = rank.get(a.owner) ?? n;
|
|
503
|
+
const rb = rank.get(b.owner) ?? n;
|
|
504
|
+
return ra !== rb ? ra - rb : a.seq - b.seq;
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
// Callbacks run in every commit's layout phase, after layout effects and
|
|
508
|
+
// before passive ones (class component lifecycles, see class-component.ts).
|
|
509
|
+
const layoutHooks = [];
|
|
510
|
+
/** Registers fn to run in the layout phase of every commit (internal). */
|
|
511
|
+
export function krateOnLayoutCommit(fn) {
|
|
512
|
+
layoutHooks.push(fn);
|
|
513
|
+
}
|
|
514
|
+
function runLayoutHooks() {
|
|
515
|
+
for (const fn of layoutHooks)
|
|
516
|
+
fn();
|
|
517
|
+
}
|
|
518
|
+
function commit() {
|
|
519
|
+
commitQueued = false;
|
|
520
|
+
// runAfterFlush recorded the React state this commit rendered.
|
|
521
|
+
const at = krateEpoch();
|
|
522
|
+
const sync = krateTakeSyncLane();
|
|
523
|
+
if (dirty.size === 0) {
|
|
524
|
+
runLayoutHooks();
|
|
525
|
+
if (sync)
|
|
526
|
+
flushPassive();
|
|
527
|
+
pruneSnapshots();
|
|
528
|
+
return;
|
|
529
|
+
}
|
|
530
|
+
const hooks = orderHooks(dirty).filter((h) => isActive(h.owner));
|
|
531
|
+
dirty.clear();
|
|
532
|
+
for (const h of hooks)
|
|
533
|
+
h.dirty = false;
|
|
534
|
+
krateCommitting(true);
|
|
535
|
+
try {
|
|
536
|
+
// Mutation phase, per component in order: insertion cleanup + setup,
|
|
537
|
+
// then layout cleanup.
|
|
538
|
+
let i = 0;
|
|
539
|
+
while (i < hooks.length) {
|
|
540
|
+
const owner = hooks[i].owner;
|
|
541
|
+
let j = i;
|
|
542
|
+
while (j < hooks.length && hooks[j].owner === owner)
|
|
543
|
+
j++;
|
|
544
|
+
for (let k = i; k < j; k++) {
|
|
545
|
+
if (hooks[k].phase === 'insertion') {
|
|
546
|
+
runCleanup(hooks[k]);
|
|
547
|
+
runSetup(hooks[k], at);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
for (let k = i; k < j; k++) {
|
|
551
|
+
if (hooks[k].phase === 'layout')
|
|
552
|
+
runCleanup(hooks[k]);
|
|
553
|
+
}
|
|
554
|
+
i = j;
|
|
555
|
+
}
|
|
556
|
+
// Layout phase.
|
|
557
|
+
for (const h of hooks) {
|
|
558
|
+
if (h.phase === 'layout')
|
|
559
|
+
runSetup(h, at);
|
|
560
|
+
}
|
|
561
|
+
runLayoutHooks();
|
|
562
|
+
// Passive phase, after paint.
|
|
563
|
+
let queued = false;
|
|
564
|
+
for (const h of hooks) {
|
|
565
|
+
if (h.phase !== 'passive')
|
|
566
|
+
continue;
|
|
567
|
+
const c = takeCleanup(h);
|
|
568
|
+
if (c)
|
|
569
|
+
passiveCleanups.push(c);
|
|
570
|
+
passiveSetups.push([h, at]);
|
|
571
|
+
queued = true;
|
|
572
|
+
}
|
|
573
|
+
if (queued)
|
|
574
|
+
schedulePassive();
|
|
575
|
+
// A discrete event's update runs its passive effects before the event
|
|
576
|
+
// returns (React's SyncLane), ahead of microtasks the handler queued.
|
|
577
|
+
if (sync)
|
|
578
|
+
flushPassive();
|
|
579
|
+
}
|
|
580
|
+
finally {
|
|
581
|
+
krateCommitting(false);
|
|
582
|
+
}
|
|
583
|
+
pruneSnapshots();
|
|
584
|
+
}
|
|
585
|
+
function schedulePassive() {
|
|
586
|
+
if (passiveTimer !== null)
|
|
587
|
+
return;
|
|
588
|
+
passiveTimer = setTimeout(flushPassive, 0);
|
|
589
|
+
}
|
|
590
|
+
/** Runs pending passive effect cleanups and setups now. */
|
|
591
|
+
export function flushPassive() {
|
|
592
|
+
if (passiveTimer !== null) {
|
|
593
|
+
clearTimeout(passiveTimer);
|
|
594
|
+
passiveTimer = null;
|
|
595
|
+
}
|
|
596
|
+
// Loop: a passive cleanup or setup may unmount components, queueing more.
|
|
597
|
+
while (passiveCleanups.length || passiveSetups.length) {
|
|
598
|
+
const cleanups = passiveCleanups;
|
|
599
|
+
const setups = passiveSetups;
|
|
600
|
+
passiveCleanups = [];
|
|
601
|
+
passiveSetups = [];
|
|
602
|
+
krateCommitting(true);
|
|
603
|
+
try {
|
|
604
|
+
for (const c of cleanups)
|
|
605
|
+
c();
|
|
606
|
+
for (const [h, at] of setups) {
|
|
607
|
+
if (isActive(h.owner))
|
|
608
|
+
runSetup(h, at);
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
finally {
|
|
612
|
+
krateCommitting(false);
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
/**
|
|
617
|
+
* React's useMemo with a dependency array: the getter recomputes
|
|
618
|
+
* synchronously, on read, when the dependency array changed (Object.is), so
|
|
619
|
+
* the value is current within the same update. Signals read by `fn` are not
|
|
620
|
+
* tracked; only the dependencies are.
|
|
621
|
+
*/
|
|
622
|
+
export function createMemoDeps(fn, deps) {
|
|
623
|
+
let prev;
|
|
624
|
+
let has = false;
|
|
625
|
+
let value;
|
|
626
|
+
return () => {
|
|
627
|
+
const next = toDeps(deps()) || [];
|
|
628
|
+
if (!has || !depsEqual(prev, next)) {
|
|
629
|
+
prev = next;
|
|
630
|
+
has = true;
|
|
631
|
+
value = untrack(fn);
|
|
632
|
+
}
|
|
633
|
+
return value;
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* React's useCallback with a dependency array: a getter for the callback
|
|
638
|
+
* whose identity only changes when the dependency array changes.
|
|
639
|
+
*/
|
|
640
|
+
export function createCallbackDeps(fn, deps) {
|
|
641
|
+
// The source closure is created once, so a new identity per dependency
|
|
642
|
+
// change is modelled by a fresh forwarding wrapper.
|
|
643
|
+
let first = true;
|
|
644
|
+
return createMemoDeps(() => {
|
|
645
|
+
if (first) {
|
|
646
|
+
first = false;
|
|
647
|
+
return fn;
|
|
648
|
+
}
|
|
649
|
+
const base = fn;
|
|
650
|
+
return function (...args) {
|
|
651
|
+
return base.apply(this, args);
|
|
652
|
+
};
|
|
653
|
+
}, deps);
|
|
654
|
+
}
|
|
655
|
+
/**
|
|
656
|
+
* Dependency values of a component parameter: every own property of a props
|
|
657
|
+
* object (read live, so prop getters subscribe) except children, or the
|
|
658
|
+
* value itself for anything else (a custom hook argument).
|
|
659
|
+
*/
|
|
660
|
+
export function krateDepsOf(value) {
|
|
661
|
+
if (value && typeof value === 'object') {
|
|
662
|
+
const proto = Object.getPrototypeOf(value);
|
|
663
|
+
if (proto === Object.prototype || proto === null) {
|
|
664
|
+
const out = [];
|
|
665
|
+
for (const k of Object.keys(value)) {
|
|
666
|
+
if (k !== 'children')
|
|
667
|
+
out.push(value[k]);
|
|
668
|
+
}
|
|
669
|
+
return out;
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
return [value];
|
|
673
|
+
}
|
|
674
|
+
function unmountAll() {
|
|
675
|
+
// Page teardown: everything unmounts now, layout before passive.
|
|
676
|
+
const passive = [];
|
|
677
|
+
for (const child of root.children.slice()) {
|
|
678
|
+
disposeTree(child, child.shown, passive);
|
|
679
|
+
}
|
|
680
|
+
for (const hook of root.hooks) {
|
|
681
|
+
hook.stop();
|
|
682
|
+
if (hook.phase === 'passive') {
|
|
683
|
+
const c = takeCleanup(hook);
|
|
684
|
+
if (c)
|
|
685
|
+
passive.push(c);
|
|
686
|
+
}
|
|
687
|
+
else {
|
|
688
|
+
runCleanup(hook);
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
root.children.length = 0;
|
|
692
|
+
root.hooks.length = 0;
|
|
693
|
+
byId.clear();
|
|
694
|
+
condShown.clear();
|
|
695
|
+
replacedSlots.clear();
|
|
696
|
+
dirty.clear();
|
|
697
|
+
passiveSetups = [];
|
|
698
|
+
const pendingCleanups = passiveCleanups.concat(passive);
|
|
699
|
+
passiveCleanups = [];
|
|
700
|
+
if (passiveTimer !== null) {
|
|
701
|
+
clearTimeout(passiveTimer);
|
|
702
|
+
passiveTimer = null;
|
|
703
|
+
}
|
|
704
|
+
for (const c of pendingCleanups) {
|
|
705
|
+
try {
|
|
706
|
+
untrack(c);
|
|
707
|
+
}
|
|
708
|
+
catch {
|
|
709
|
+
// Keep tearing down.
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
commitQueued = false;
|
|
713
|
+
krateSetOwner(null);
|
|
714
|
+
}
|
|
715
|
+
// React flushes the previous commit's passive effects before a new update
|
|
716
|
+
// is processed, so the flush that starts an update runs them first.
|
|
717
|
+
krateOnBeforeFlush(flushPassive);
|
|
718
|
+
krateOnAfterFlush(commit);
|
|
719
|
+
krateOnDispose(unmountAll);
|