@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/boundary.js
ADDED
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
// Owner tree and error boundaries.
|
|
2
|
+
//
|
|
3
|
+
// Krate components run once, so there is no reconciler to unwind when code
|
|
4
|
+
// throws. This module gives each hydrated component instance an Owner: a node
|
|
5
|
+
// in a tree that mirrors React's fiber parent chain. Effects, cleanups and
|
|
6
|
+
// mount callbacks created while an owner is current belong to it (see
|
|
7
|
+
// OwnerHooks in signal.ts), so unmounting an owner disposes its whole subtree,
|
|
8
|
+
// and values thrown by its code are routed up the tree to the nearest owner
|
|
9
|
+
// with a boundary handler. Error boundaries install a handler; Suspense can
|
|
10
|
+
// install one that intercepts thrown promises the same way.
|
|
11
|
+
//
|
|
12
|
+
// Owners also follow conditional branches: an owner registered inside a
|
|
13
|
+
// branch of a compiled conditional is mounted while that branch is shown and
|
|
14
|
+
// unmounted (its cleanups run) when it is swapped out, so componentDidMount /
|
|
15
|
+
// componentWillUnmount and state resets match React. A dormant owner keeps its
|
|
16
|
+
// setup function and is remounted by running it again.
|
|
17
|
+
import { setOwnerHooks, untrack, onCleanup, krateGetOwner, krateSetOwner } from './signal.js';
|
|
18
|
+
import { krateForgetOwner, krateOnHookError } from './effects.js';
|
|
19
|
+
let current = null;
|
|
20
|
+
let nextOrder = 0;
|
|
21
|
+
const byId = new Map();
|
|
22
|
+
const condState = new Map();
|
|
23
|
+
const condOwners = new Map();
|
|
24
|
+
let resetRegistered = false;
|
|
25
|
+
let mountDepth = 0;
|
|
26
|
+
const deferredMounts = [];
|
|
27
|
+
function makeOwner(id, name, parent, creator, cond, branch, setup) {
|
|
28
|
+
const o = {
|
|
29
|
+
id,
|
|
30
|
+
name,
|
|
31
|
+
parent,
|
|
32
|
+
creator,
|
|
33
|
+
children: [],
|
|
34
|
+
order: nextOrder++,
|
|
35
|
+
state: 'dormant',
|
|
36
|
+
boundary: null,
|
|
37
|
+
blocked: false,
|
|
38
|
+
inError: false,
|
|
39
|
+
cond,
|
|
40
|
+
branch,
|
|
41
|
+
setup,
|
|
42
|
+
data: {},
|
|
43
|
+
cleanups: [],
|
|
44
|
+
};
|
|
45
|
+
if (parent)
|
|
46
|
+
parent.children.push(o);
|
|
47
|
+
return o;
|
|
48
|
+
}
|
|
49
|
+
/** The owner whose code is running, or null. */
|
|
50
|
+
export function getOwner() {
|
|
51
|
+
return current;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Creates a mounted owner under parent (default: the current owner). Use it for
|
|
55
|
+
* regions created at runtime; run code in it with runWithOwner.
|
|
56
|
+
*/
|
|
57
|
+
export function createOwner(name, parent = current) {
|
|
58
|
+
const o = makeOwner('', name, parent || null, parent || null, '', '', null);
|
|
59
|
+
o.state = parent && parent.state !== 'mounted' ? 'dormant' : 'mounted';
|
|
60
|
+
if (!parent)
|
|
61
|
+
registerReset();
|
|
62
|
+
return o;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Runs fn with owner current: effects and cleanups it creates belong to the
|
|
66
|
+
* owner and errors it throws are routed to the owner's boundaries. Returns
|
|
67
|
+
* fn's value, or undefined when it threw or the owner is not mounted.
|
|
68
|
+
*/
|
|
69
|
+
export function runWithOwner(owner, fn) {
|
|
70
|
+
if (!owner)
|
|
71
|
+
return fn();
|
|
72
|
+
const o = owner;
|
|
73
|
+
if (o.state !== 'mounted')
|
|
74
|
+
return undefined;
|
|
75
|
+
const prev = current;
|
|
76
|
+
current = o;
|
|
77
|
+
try {
|
|
78
|
+
return untrack(fn);
|
|
79
|
+
}
|
|
80
|
+
catch (err) {
|
|
81
|
+
handleError(err, o);
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
finally {
|
|
85
|
+
current = prev;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/** Registers fn to run when owner unmounts (immediately if it already has). */
|
|
89
|
+
export function onOwnerCleanup(owner, fn) {
|
|
90
|
+
const o = owner;
|
|
91
|
+
if (o.state !== 'mounted') {
|
|
92
|
+
safely(fn, o);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
o.cleanups.push(fn);
|
|
96
|
+
}
|
|
97
|
+
/** Installs a boundary handler on owner (null removes it). */
|
|
98
|
+
export function setBoundary(owner, handler) {
|
|
99
|
+
owner.boundary = handler;
|
|
100
|
+
}
|
|
101
|
+
/** React-style component stack from source up to the root. */
|
|
102
|
+
export function componentStack(source) {
|
|
103
|
+
let out = '';
|
|
104
|
+
for (let o = source; o; o = o.parent) {
|
|
105
|
+
out += '\n at ' + o.name;
|
|
106
|
+
}
|
|
107
|
+
return out;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Routes a thrown value from source to the nearest mounted ancestor boundary
|
|
111
|
+
* that handles it. An owner never catches its own errors: a boundary's own
|
|
112
|
+
* render or lifecycle failure goes to the boundary above it, as in React.
|
|
113
|
+
* Unhandled errors are reported and the failing subtree is unmounted.
|
|
114
|
+
*/
|
|
115
|
+
export function handleError(thrown, source = current) {
|
|
116
|
+
if (!source)
|
|
117
|
+
throw thrown;
|
|
118
|
+
const info = { componentStack: componentStack(source) };
|
|
119
|
+
for (let b = source.parent; b; b = b.parent) {
|
|
120
|
+
if (b.state !== 'mounted' || !b.boundary)
|
|
121
|
+
continue;
|
|
122
|
+
let handled = false;
|
|
123
|
+
try {
|
|
124
|
+
handled = b.boundary(thrown, info, source);
|
|
125
|
+
}
|
|
126
|
+
catch (next) {
|
|
127
|
+
// The boundary itself failed (getDerivedStateFromError threw): the new
|
|
128
|
+
// error propagates from the boundary.
|
|
129
|
+
handleError(next, b);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
if (handled)
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
reportUncaught(thrown, info, source);
|
|
136
|
+
}
|
|
137
|
+
// Uncaught errors. React 18 unmounts the whole root; Krate keeps the page's
|
|
138
|
+
// server-rendered HTML and unmounts only the component that threw (and its
|
|
139
|
+
// descendants), logging the error like React does.
|
|
140
|
+
function reportUncaught(thrown, info, source) {
|
|
141
|
+
const g = globalThis;
|
|
142
|
+
try {
|
|
143
|
+
const msg = String(thrown?.stack || thrown?.message || thrown);
|
|
144
|
+
if (Array.isArray(g.__krateErrs))
|
|
145
|
+
g.__krateErrs.push(msg);
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
// Reporting must never throw.
|
|
149
|
+
}
|
|
150
|
+
if (typeof console !== 'undefined') {
|
|
151
|
+
console.error(thrown);
|
|
152
|
+
console.error('The above error occurred in the <' + source.name + '> component:' + info.componentStack +
|
|
153
|
+
'\n\nAdd an error boundary (a class component with static getDerivedStateFromError) to recover from it.');
|
|
154
|
+
}
|
|
155
|
+
source.blocked = true;
|
|
156
|
+
unmountOwner(source);
|
|
157
|
+
}
|
|
158
|
+
function safely(fn, owner) {
|
|
159
|
+
try {
|
|
160
|
+
fn();
|
|
161
|
+
}
|
|
162
|
+
catch (err) {
|
|
163
|
+
if (owner && owner.parent)
|
|
164
|
+
handleError(err, owner);
|
|
165
|
+
else
|
|
166
|
+
reportUncaughtLoose(err);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
function reportUncaughtLoose(err) {
|
|
170
|
+
if (typeof console !== 'undefined')
|
|
171
|
+
console.error(err);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Unmounts owner and its subtree: cleanups run parent-first (React calls
|
|
175
|
+
* componentWillUnmount top-down) and the owners become dormant, so they can
|
|
176
|
+
* be mounted again by running their setup.
|
|
177
|
+
*/
|
|
178
|
+
export function unmountOwner(owner) {
|
|
179
|
+
disposeTree(owner, 'dormant');
|
|
180
|
+
}
|
|
181
|
+
/** Mounts a dormant owner (and its mountable descendants). */
|
|
182
|
+
export function mountOwner(owner) {
|
|
183
|
+
const o = owner;
|
|
184
|
+
if (o.state !== 'dormant')
|
|
185
|
+
return;
|
|
186
|
+
o.blocked = false;
|
|
187
|
+
if (canMount(o))
|
|
188
|
+
mount(o);
|
|
189
|
+
}
|
|
190
|
+
/** Unmounts every child of owner (an error boundary dropping its subtree). */
|
|
191
|
+
export function unmountChildren(owner) {
|
|
192
|
+
for (const c of owner.children)
|
|
193
|
+
disposeTree(c, 'dormant');
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Mounts owner's dormant children that their conditional branch allows,
|
|
197
|
+
* clearing the block a caught error put on them.
|
|
198
|
+
*/
|
|
199
|
+
export function remountChildren(owner) {
|
|
200
|
+
if (owner.state !== 'mounted')
|
|
201
|
+
return;
|
|
202
|
+
for (const c of owner.children.slice()) {
|
|
203
|
+
c.blocked = false;
|
|
204
|
+
if (c.state === 'dormant' && canMount(c))
|
|
205
|
+
mount(c);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
function disposeTree(o, to) {
|
|
209
|
+
if (o.state === 'mounted') {
|
|
210
|
+
o.state = to;
|
|
211
|
+
const errors = [];
|
|
212
|
+
for (const fn of o.cleanups.splice(0)) {
|
|
213
|
+
try {
|
|
214
|
+
fn();
|
|
215
|
+
}
|
|
216
|
+
catch (err) {
|
|
217
|
+
errors.push(err);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
// React hook effects (effects.ts) of this component clean up too, and a
|
|
221
|
+
// remount starts them fresh.
|
|
222
|
+
if (o.id) {
|
|
223
|
+
try {
|
|
224
|
+
krateForgetOwner(o.id);
|
|
225
|
+
}
|
|
226
|
+
catch (err) {
|
|
227
|
+
errors.push(err);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
for (const c of o.children)
|
|
231
|
+
disposeTree(c, to);
|
|
232
|
+
// Errors thrown while unmounting go to the boundary above the owner.
|
|
233
|
+
for (const err of errors) {
|
|
234
|
+
if (o.parent)
|
|
235
|
+
handleError(err, o);
|
|
236
|
+
else
|
|
237
|
+
reportUncaughtLoose(err);
|
|
238
|
+
}
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
if (to === 'disposed' && o.state !== 'disposed') {
|
|
242
|
+
o.state = 'disposed';
|
|
243
|
+
for (const c of o.children)
|
|
244
|
+
disposeTree(c, to);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
function condShows(o) {
|
|
248
|
+
if (!o.cond)
|
|
249
|
+
return true;
|
|
250
|
+
const shown = condState.get(o.cond);
|
|
251
|
+
return shown === undefined || shown === o.branch;
|
|
252
|
+
}
|
|
253
|
+
function canMount(o) {
|
|
254
|
+
if (o.blocked || !condShows(o))
|
|
255
|
+
return false;
|
|
256
|
+
return !o.parent || o.parent.state === 'mounted';
|
|
257
|
+
}
|
|
258
|
+
function mount(o) {
|
|
259
|
+
if (o.state !== 'dormant')
|
|
260
|
+
return;
|
|
261
|
+
o.state = 'mounted';
|
|
262
|
+
mountDepth++;
|
|
263
|
+
// The scope declares its effects.ts owner itself; restore the caller's.
|
|
264
|
+
const prevEffectsOwner = krateGetOwner();
|
|
265
|
+
try {
|
|
266
|
+
if (o.setup)
|
|
267
|
+
runWithOwner(o, o.setup);
|
|
268
|
+
}
|
|
269
|
+
finally {
|
|
270
|
+
krateSetOwner(prevEffectsOwner);
|
|
271
|
+
mountDepth--;
|
|
272
|
+
}
|
|
273
|
+
if (o.state === 'mounted') {
|
|
274
|
+
for (const c of o.children.slice()) {
|
|
275
|
+
if (c.state === 'dormant' && canMount(c))
|
|
276
|
+
mount(c);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
if (mountDepth === 0 && deferredMounts.length > 0) {
|
|
280
|
+
const list = deferredMounts.splice(0).sort((a, b) => a.order - b.order);
|
|
281
|
+
for (const d of list) {
|
|
282
|
+
if (d.state === 'dormant' && canMount(d))
|
|
283
|
+
mount(d);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
function registerReset() {
|
|
288
|
+
if (resetRegistered)
|
|
289
|
+
return;
|
|
290
|
+
resetRegistered = true;
|
|
291
|
+
// Top-level registration: runs on disposeAll() (SPA navigation).
|
|
292
|
+
onCleanup(resetOwners);
|
|
293
|
+
}
|
|
294
|
+
function resetOwners() {
|
|
295
|
+
const roots = [];
|
|
296
|
+
for (const o of byId.values())
|
|
297
|
+
if (!o.parent)
|
|
298
|
+
roots.push(o);
|
|
299
|
+
for (const o of roots)
|
|
300
|
+
disposeTree(o, 'disposed');
|
|
301
|
+
byId.clear();
|
|
302
|
+
condState.clear();
|
|
303
|
+
condOwners.clear();
|
|
304
|
+
deferredMounts.length = 0;
|
|
305
|
+
resetRegistered = false;
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Hydration entry point: the compiler wraps each component's hydration scope
|
|
309
|
+
* in `__krateOwn(id, meta, setup)` on pages that use the owner tree. The setup
|
|
310
|
+
* runs now when the component is visible, or later when its branch is shown.
|
|
311
|
+
*/
|
|
312
|
+
export function __krateOwn(id, meta, setup) {
|
|
313
|
+
const m = meta || {};
|
|
314
|
+
const prior = byId.get(id);
|
|
315
|
+
if (prior)
|
|
316
|
+
disposeTree(prior, 'disposed');
|
|
317
|
+
const parent = (m.p && byId.get(m.p)) || null;
|
|
318
|
+
const creator = (m.k && byId.get(m.k)) || null;
|
|
319
|
+
const o = makeOwner(id, m.n || id, parent, creator, m.c || '', m.b || '', setup);
|
|
320
|
+
byId.set(id, o);
|
|
321
|
+
registerReset();
|
|
322
|
+
if (o.cond) {
|
|
323
|
+
let list = condOwners.get(o.cond);
|
|
324
|
+
if (!list) {
|
|
325
|
+
list = [];
|
|
326
|
+
condOwners.set(o.cond, list);
|
|
327
|
+
}
|
|
328
|
+
list.push(o);
|
|
329
|
+
}
|
|
330
|
+
// An owner rendered as part of a boundary's failed render stays down until
|
|
331
|
+
// the boundary renders again.
|
|
332
|
+
if (parent && parent.inError && condShows(o))
|
|
333
|
+
o.blocked = true;
|
|
334
|
+
if (canMount(o))
|
|
335
|
+
mount(o);
|
|
336
|
+
}
|
|
337
|
+
/** Owner registered under id, if any (tests, devtools). */
|
|
338
|
+
export function __krateOwner(id) {
|
|
339
|
+
return byId.get(id) || null;
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Called by the hydration bootstrap before a conditional swaps branches: the
|
|
343
|
+
* outgoing branch's owners unmount while their DOM is still attached.
|
|
344
|
+
*/
|
|
345
|
+
export function __krateCondPre(id, show) {
|
|
346
|
+
const prev = condState.get(id);
|
|
347
|
+
const want = show ? 'c' : 'a';
|
|
348
|
+
if (prev === undefined || prev === want)
|
|
349
|
+
return;
|
|
350
|
+
const list = condOwners.get(id);
|
|
351
|
+
if (!list)
|
|
352
|
+
return;
|
|
353
|
+
for (const o of list) {
|
|
354
|
+
if (o.branch === prev && o.state === 'mounted')
|
|
355
|
+
disposeTree(o, 'dormant');
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
/** Called after a conditional swapped: the incoming branch's owners mount. */
|
|
359
|
+
export function __krateCondPost(id, show) {
|
|
360
|
+
const want = show ? 'c' : 'a';
|
|
361
|
+
const prev = condState.get(id);
|
|
362
|
+
condState.set(id, want);
|
|
363
|
+
if (prev === want)
|
|
364
|
+
return;
|
|
365
|
+
const list = condOwners.get(id);
|
|
366
|
+
if (!list)
|
|
367
|
+
return;
|
|
368
|
+
for (const o of list) {
|
|
369
|
+
if (o.branch !== want || o.state !== 'dormant' || !canMount(o))
|
|
370
|
+
continue;
|
|
371
|
+
if (mountDepth > 0)
|
|
372
|
+
deferredMounts.push(o);
|
|
373
|
+
else
|
|
374
|
+
mount(o);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Called by the hydration bootstrap's __safe wrapper. Routes an error thrown
|
|
379
|
+
* while a component's hydration scope runs; nodes outside the document (a
|
|
380
|
+
* hidden conditional branch React would not have rendered) are ignored.
|
|
381
|
+
*/
|
|
382
|
+
export function __krateOnError(err, node) {
|
|
383
|
+
if (!current)
|
|
384
|
+
return false;
|
|
385
|
+
if (node && node.isConnected === false)
|
|
386
|
+
return false;
|
|
387
|
+
handleError(err, current);
|
|
388
|
+
return true;
|
|
389
|
+
}
|
|
390
|
+
// React hook effects (effects.ts) report errors to the component's owner.
|
|
391
|
+
krateOnHookError((err, id) => {
|
|
392
|
+
const o = id ? byId.get(id) : undefined;
|
|
393
|
+
if (!o)
|
|
394
|
+
return false;
|
|
395
|
+
if (o.state === 'mounted') {
|
|
396
|
+
commitPhaseDepth++;
|
|
397
|
+
try {
|
|
398
|
+
handleError(err, o);
|
|
399
|
+
}
|
|
400
|
+
finally {
|
|
401
|
+
commitPhaseDepth--;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
return true;
|
|
405
|
+
});
|
|
406
|
+
let commitPhaseDepth = 0;
|
|
407
|
+
/**
|
|
408
|
+
* Reports whether the error being handled was thrown in the commit phase
|
|
409
|
+
* (effects, lifecycles). React retries a failed render once, but not a
|
|
410
|
+
* failed commit.
|
|
411
|
+
*/
|
|
412
|
+
export function inCommitPhaseError() {
|
|
413
|
+
return commitPhaseDepth > 0;
|
|
414
|
+
}
|
|
415
|
+
setOwnerHooks({
|
|
416
|
+
current: () => current,
|
|
417
|
+
run: (owner, fn) => {
|
|
418
|
+
runWithOwner(owner, fn);
|
|
419
|
+
},
|
|
420
|
+
cleanup: (owner, fn) => onOwnerCleanup(owner, fn),
|
|
421
|
+
error: (owner, err) => {
|
|
422
|
+
const o = owner;
|
|
423
|
+
// Late errors from an unmounted subtree are dropped like React drops
|
|
424
|
+
// updates to unmounted components.
|
|
425
|
+
if (o.state === 'mounted')
|
|
426
|
+
handleError(err, o);
|
|
427
|
+
return true;
|
|
428
|
+
},
|
|
429
|
+
});
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { type ErrorInfo } from './boundary.js';
|
|
2
|
+
type Obj = Record<string, unknown>;
|
|
3
|
+
/** Lifecycle methods and statics of a lowered class, as closures. */
|
|
4
|
+
export interface ClassSpec {
|
|
5
|
+
name?: string;
|
|
6
|
+
/** PureComponent: shallow-compare props and state. */
|
|
7
|
+
pure?: boolean;
|
|
8
|
+
defaultProps?: Obj | null;
|
|
9
|
+
/** Reads the value of `static contextType`. */
|
|
10
|
+
context?: () => unknown;
|
|
11
|
+
/** Writes the committed context the lowered render reads. */
|
|
12
|
+
setContext?: (value: unknown) => void;
|
|
13
|
+
getDerivedStateFromProps?: (props: Obj, state: Obj) => Obj | null | undefined;
|
|
14
|
+
getDerivedStateFromError?: (error: unknown) => Obj | null | undefined;
|
|
15
|
+
shouldComponentUpdate?: (nextProps: Obj, nextState: Obj, nextContext: unknown) => unknown;
|
|
16
|
+
getSnapshotBeforeUpdate?: (prevProps: Obj, prevState: Obj) => unknown;
|
|
17
|
+
componentDidMount?: () => void;
|
|
18
|
+
componentDidUpdate?: (prevProps: Obj, prevState: Obj, snapshot: unknown) => void;
|
|
19
|
+
componentWillUnmount?: () => void;
|
|
20
|
+
componentDidCatch?: (error: unknown, info: ErrorInfo) => void;
|
|
21
|
+
componentWillReceiveProps?: (nextProps: Obj, nextContext: unknown) => void;
|
|
22
|
+
componentWillUpdate?: (nextProps: Obj, nextState: Obj, nextContext: unknown) => void;
|
|
23
|
+
/** Public methods to put on the instance (read once, at mount). */
|
|
24
|
+
expose?: () => Obj;
|
|
25
|
+
/** An error a descendant threw while the page was built (see build-time boundaries). */
|
|
26
|
+
buildError?: {
|
|
27
|
+
name?: string;
|
|
28
|
+
message?: string;
|
|
29
|
+
} | null;
|
|
30
|
+
}
|
|
31
|
+
/** The object lowered code uses as `this`. Instance fields are added freely. */
|
|
32
|
+
export interface ClassInstance {
|
|
33
|
+
readonly props: Obj;
|
|
34
|
+
state: Obj;
|
|
35
|
+
readonly context: unknown;
|
|
36
|
+
setState(update: Obj | ((state: Obj, props: Obj) => Obj | null | undefined) | null, callback?: () => void): void;
|
|
37
|
+
forceUpdate(callback?: () => void): void;
|
|
38
|
+
[field: string]: unknown;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Creates the instance behind a lowered class component. `props` is the
|
|
42
|
+
* component's props registry object, `getState`/`setState` the committed-state
|
|
43
|
+
* signal its render reads.
|
|
44
|
+
*/
|
|
45
|
+
export declare function __krateClass(props: Obj | null | undefined, getState: () => Obj, setState: (v: Obj) => void, spec: ClassSpec): ClassInstance;
|
|
46
|
+
/** Shape of a runtime (not compiler-lowered) class component instance. */
|
|
47
|
+
interface RuntimeClassInstance {
|
|
48
|
+
props?: Obj;
|
|
49
|
+
state?: Obj | null;
|
|
50
|
+
context?: unknown;
|
|
51
|
+
render(): unknown;
|
|
52
|
+
setState?: unknown;
|
|
53
|
+
forceUpdate?: unknown;
|
|
54
|
+
[method: string]: unknown;
|
|
55
|
+
}
|
|
56
|
+
type RuntimeClass = (new (props: Obj) => RuntimeClassInstance) & Record<string, unknown>;
|
|
57
|
+
/**
|
|
58
|
+
* Mounts a class component at runtime (React.createElement(MyClass) in code
|
|
59
|
+
* the compiler did not lower). The instance's setState/forceUpdate go through
|
|
60
|
+
* the same controller as lowered classes; each committed update re-runs
|
|
61
|
+
* render() and replaces the host's content.
|
|
62
|
+
*/
|
|
63
|
+
export declare function mountClassElement(Klass: RuntimeClass, props: Obj, toNode: (v: unknown) => Node): Node;
|
|
64
|
+
/** Reports an error from lowered class code to the nearest boundary. */
|
|
65
|
+
export declare function __krateClassError(err: unknown): void;
|
|
66
|
+
export {};
|