@krate/runtime 0.5.0-alpha.2 → 0.5.0-alpha.4

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.
Files changed (105) hide show
  1. package/dist/action-context.d.ts +56 -0
  2. package/dist/action-context.js +73 -0
  3. package/dist/action-context.test.d.ts +1 -0
  4. package/dist/action-context.test.js +47 -0
  5. package/dist/actions.d.ts +120 -0
  6. package/dist/actions.js +550 -0
  7. package/dist/actions.test.d.ts +1 -0
  8. package/dist/actions.test.js +417 -0
  9. package/dist/boundary.d.ts +121 -0
  10. package/dist/boundary.js +429 -0
  11. package/dist/cache-scope.d.ts +118 -0
  12. package/dist/cache-scope.js +311 -0
  13. package/dist/cache.d.ts +55 -0
  14. package/dist/cache.js +155 -0
  15. package/dist/cache.test.d.ts +1 -0
  16. package/dist/cache.test.js +217 -0
  17. package/dist/class-component.d.ts +66 -0
  18. package/dist/class-component.js +633 -0
  19. package/dist/class-component.test.d.ts +1 -0
  20. package/dist/class-component.test.js +411 -0
  21. package/dist/context.d.ts +13 -0
  22. package/dist/context.js +58 -32
  23. package/dist/context.test.d.ts +1 -0
  24. package/dist/context.test.js +57 -0
  25. package/dist/derived.test.d.ts +1 -0
  26. package/dist/derived.test.js +140 -0
  27. package/dist/discrete.test.d.ts +1 -0
  28. package/dist/discrete.test.js +100 -0
  29. package/dist/dom.d.ts +55 -0
  30. package/dist/dom.js +414 -63
  31. package/dist/effects.d.ts +88 -0
  32. package/dist/effects.js +790 -0
  33. package/dist/effects.test.d.ts +1 -0
  34. package/dist/effects.test.js +485 -0
  35. package/dist/events.d.ts +58 -0
  36. package/dist/events.js +503 -0
  37. package/dist/events.test.d.ts +1 -0
  38. package/dist/events.test.js +447 -0
  39. package/dist/index.d.ts +14 -4
  40. package/dist/index.js +9 -3
  41. package/dist/jsx-types.d.ts +2 -0
  42. package/dist/krate-hydrate-lite.js +7 -1
  43. package/dist/krate-hydrate.js +7 -1
  44. package/dist/krate-runtime.js +7 -1
  45. package/dist/krate-suspense.js +2 -0
  46. package/dist/lanes.test.d.ts +1 -0
  47. package/dist/lanes.test.js +59 -0
  48. package/dist/list.d.ts +20 -0
  49. package/dist/list.js +466 -0
  50. package/dist/list.test.d.ts +1 -0
  51. package/dist/list.test.js +344 -0
  52. package/dist/optimistic-lanes.test.d.ts +1 -0
  53. package/dist/optimistic-lanes.test.js +45 -0
  54. package/dist/portal.d.ts +47 -0
  55. package/dist/portal.js +189 -0
  56. package/dist/portal.test.d.ts +1 -0
  57. package/dist/portal.test.js +99 -0
  58. package/dist/react-effects.test.d.ts +1 -0
  59. package/dist/react-effects.test.js +96 -0
  60. package/dist/react-suspense.test.d.ts +1 -0
  61. package/dist/react-suspense.test.js +194 -0
  62. package/dist/react.d.ts +132 -32
  63. package/dist/react.js +335 -81
  64. package/dist/react.test.js +12 -2
  65. package/dist/reconcile.js +13 -0
  66. package/dist/refs.d.ts +33 -0
  67. package/dist/refs.js +142 -0
  68. package/dist/refs.test.d.ts +1 -0
  69. package/dist/refs.test.js +257 -0
  70. package/dist/request-context.d.ts +62 -0
  71. package/dist/request-context.js +107 -0
  72. package/dist/resource.js +20 -1
  73. package/dist/router-layout.test.d.ts +1 -0
  74. package/dist/router-layout.test.js +179 -0
  75. package/dist/router-lite.d.ts +25 -0
  76. package/dist/router-lite.js +73 -0
  77. package/dist/router-nav.test.d.ts +1 -0
  78. package/dist/router-nav.test.js +399 -0
  79. package/dist/router-slots.test.d.ts +1 -0
  80. package/dist/router-slots.test.js +144 -0
  81. package/dist/router.d.ts +66 -2
  82. package/dist/router.js +1535 -650
  83. package/dist/server-async.test.d.ts +1 -0
  84. package/dist/server-async.test.js +174 -0
  85. package/dist/server-renderer.js +494 -119
  86. package/dist/server.d.ts +27 -1
  87. package/dist/server.js +187 -1
  88. package/dist/server.test.js +20 -0
  89. package/dist/signal-lanes.test.d.ts +1 -0
  90. package/dist/signal-lanes.test.js +32 -0
  91. package/dist/signal.d.ts +186 -1
  92. package/dist/signal.js +896 -66
  93. package/dist/slots.d.ts +22 -0
  94. package/dist/slots.js +134 -0
  95. package/dist/slots.test.d.ts +1 -0
  96. package/dist/slots.test.js +169 -0
  97. package/dist/snapshots.test.d.ts +1 -0
  98. package/dist/snapshots.test.js +208 -0
  99. package/dist/suspense-actions.test.d.ts +1 -0
  100. package/dist/suspense-actions.test.js +72 -0
  101. package/dist/suspense.d.ts +220 -0
  102. package/dist/suspense.js +901 -0
  103. package/dist/suspense.test.d.ts +1 -0
  104. package/dist/suspense.test.js +518 -0
  105. package/package.json +5 -1
@@ -0,0 +1,633 @@
1
+ // Runtime for lowered React class components.
2
+ //
3
+ // The compiler lowers `class X extends Component` into a function component
4
+ // whose render reads a committed-state signal and a gated props object. This
5
+ // module supplies the instance behind `this`: batched setState with callbacks,
6
+ // forceUpdate, getDerivedStateFromProps, shouldComponentUpdate /
7
+ // PureComponent, getSnapshotBeforeUpdate, the mount/update/unmount
8
+ // lifecycles, legacy UNSAFE_ hooks and error boundaries.
9
+ //
10
+ // Updates run in two phases, like React's render and commit:
11
+ //
12
+ // render (after the current effect flush): every dirty instance, parents
13
+ // first, computes its next props/state, consults
14
+ // shouldComponentUpdate, takes the snapshot, and writes the
15
+ // committed signals the DOM bindings read;
16
+ // commit (after those DOM writes flush): componentDidMount /
17
+ // componentDidUpdate / componentDidCatch and setState callbacks,
18
+ // children before parents.
19
+ import { createEffect, createSignal, krateOnSettle, krateRenderPhase, krateRequestFlush, untrack } from './signal.js';
20
+ import { krateSetRef } from './refs.js';
21
+ import { krateOnLayoutCommit } from './effects.js';
22
+ import { getOwner, handleError, inCommitPhaseError, mountOwner, unmountOwner, onOwnerCleanup, remountChildren, runWithOwner, setBoundary, unmountChildren, } from './boundary.js';
23
+ let seq = 0;
24
+ // Where the parent's original prop getters are kept on a gated props object.
25
+ const RAW_GETTERS = Symbol.for('krate.class.rawProps');
26
+ class Controller {
27
+ constructor(raw, getState, setState, spec) {
28
+ this.seq = seq++;
29
+ this.rawGetters = [];
30
+ this.queue = [];
31
+ this.forced = false;
32
+ this.parentRender = false;
33
+ this.mounted = false;
34
+ this.disposed = false;
35
+ this.catching = false;
36
+ this.detachRef = null;
37
+ this.errorCommitPending = false;
38
+ this.spec = spec || {};
39
+ this.owner = getOwner();
40
+ this.raw = raw && typeof raw === 'object' ? raw : {};
41
+ this.getState = getState;
42
+ this.setStateSignal = setState;
43
+ // A remounted instance reuses the registry object an earlier instance
44
+ // gated; read the parent's original getters from where it saved them.
45
+ const holder = this.raw;
46
+ let saved = holder[RAW_GETTERS];
47
+ if (!saved) {
48
+ saved = [];
49
+ for (const key of Object.keys(this.raw)) {
50
+ const desc = Object.getOwnPropertyDescriptor(this.raw, key);
51
+ if (desc && desc.get) {
52
+ const get = desc.get;
53
+ const obj = this.raw;
54
+ saved.push([key, () => get.call(obj)]);
55
+ }
56
+ }
57
+ try {
58
+ Object.defineProperty(this.raw, RAW_GETTERS, { value: saved, enumerable: false, configurable: true });
59
+ }
60
+ catch {
61
+ // A frozen registry cannot be gated either.
62
+ }
63
+ }
64
+ this.rawGetters.push(...saved);
65
+ this.props = this.readProps();
66
+ this.key = this.readKey();
67
+ this.state = untrack(getState) || {};
68
+ this.context = this.readContext();
69
+ const [rendered, setRendered] = createSignal(this.props);
70
+ this.setRendered = setRendered;
71
+ this.gateProps(rendered);
72
+ const self = this;
73
+ const inst = {
74
+ get props() {
75
+ return self.props;
76
+ },
77
+ get state() {
78
+ return self.state;
79
+ },
80
+ set state(v) {
81
+ // `this.state = ...` outside the constructor replaces instance state
82
+ // without rendering, as in React.
83
+ self.state = v;
84
+ },
85
+ get context() {
86
+ return self.context;
87
+ },
88
+ setState(update, callback) {
89
+ self.enqueue({ partial: update, callback });
90
+ },
91
+ forceUpdate(callback) {
92
+ self.forced = true;
93
+ self.enqueue({ partial: null, callback });
94
+ },
95
+ };
96
+ this.inst = inst;
97
+ if (this.owner) {
98
+ this.owner.data.classController = this;
99
+ onOwnerCleanup(this.owner, () => this.unmount());
100
+ if (this.spec.getDerivedStateFromError || this.spec.componentDidCatch) {
101
+ setBoundary(this.owner, (err, info) => this.catchError(err, info));
102
+ }
103
+ }
104
+ // Track the parent's prop getters: a change schedules an update.
105
+ if (this.spec.setContext)
106
+ this.spec.setContext(this.context);
107
+ if (this.rawGetters.length > 0 || this.spec.context) {
108
+ let first = true;
109
+ createEffect(() => {
110
+ for (const [, get] of this.rawGetters)
111
+ get();
112
+ this.trackContext();
113
+ if (first) {
114
+ first = false;
115
+ return;
116
+ }
117
+ if (!this.disposed) {
118
+ renderQueue.add(this);
119
+ schedule();
120
+ }
121
+ });
122
+ }
123
+ commit(this, () => {
124
+ // Public methods live as locals in the lowered scope; put them on the
125
+ // instance so refs (`ref.current.focus()`) can call them.
126
+ const exposed = this.spec.expose ? untrack(this.spec.expose) : null;
127
+ if (exposed) {
128
+ for (const key of Object.keys(exposed)) {
129
+ if (!(key in this.inst))
130
+ this.inst[key] = exposed[key];
131
+ }
132
+ }
133
+ this.mounted = true;
134
+ if (this.spec.componentDidMount)
135
+ this.spec.componentDidMount.call(this.inst);
136
+ // `<MyClass ref={r}/>` receives the instance after componentDidMount,
137
+ // before the parent's componentDidMount (children commit first).
138
+ const ref = untrack(() => this.raw.ref);
139
+ if (ref)
140
+ this.detachRef = krateSetRef(ref, this.inst);
141
+ });
142
+ const buildError = this.spec.buildError;
143
+ if (buildError && this.spec.componentDidCatch) {
144
+ const err = new Error(buildError.message || 'Error');
145
+ if (buildError.name)
146
+ err.name = buildError.name;
147
+ if (this.owner)
148
+ this.owner.inError = true;
149
+ commit(this, () => {
150
+ this.spec.componentDidCatch.call(this.inst, err, { componentStack: '\n at ' + (this.spec.name || 'Component') + ' (build)' });
151
+ });
152
+ }
153
+ else if (buildError && this.owner) {
154
+ this.owner.inError = true;
155
+ }
156
+ }
157
+ readProps() {
158
+ const out = {};
159
+ untrack(() => {
160
+ for (const key of Object.keys(this.raw)) {
161
+ // React keeps `ref` (and `key`) out of a class's props.
162
+ if (key.startsWith('__krate') || key === 'ref' || key === 'key')
163
+ continue;
164
+ const getter = this.rawGetters.find((g) => g[0] === key);
165
+ out[key] = getter ? getter[1]() : this.raw[key];
166
+ }
167
+ });
168
+ const defaults = this.spec.defaultProps;
169
+ if (defaults) {
170
+ for (const key of Object.keys(defaults)) {
171
+ if (out[key] === undefined)
172
+ out[key] = defaults[key];
173
+ }
174
+ }
175
+ return out;
176
+ }
177
+ readContext() {
178
+ return untrack(() => this.trackContext());
179
+ }
180
+ // A dynamic provider value reaches the component as a thunk prop
181
+ // (`__krate_ctx_<Name>`); call it to get the value (tracked when called
182
+ // from the props-tracking effect).
183
+ trackContext() {
184
+ const read = this.spec.context;
185
+ if (!read)
186
+ return undefined;
187
+ const v = read();
188
+ if (typeof v === 'function') {
189
+ for (const key of Object.keys(this.raw)) {
190
+ if (key.startsWith('__krate_ctx_') && this.raw[key] === v)
191
+ return v();
192
+ }
193
+ }
194
+ return v;
195
+ }
196
+ // Render bindings read props through the registry object. Redefine its
197
+ // getters to read the committed props, so a skipped update
198
+ // (shouldComponentUpdate false) leaves the DOM unchanged and defaultProps
199
+ // apply at runtime.
200
+ gateProps(rendered) {
201
+ const keys = new Set();
202
+ for (const [key] of this.rawGetters)
203
+ keys.add(key);
204
+ const defaults = this.spec.defaultProps;
205
+ if (defaults) {
206
+ for (const key of Object.keys(defaults)) {
207
+ if (this.raw[key] === undefined)
208
+ keys.add(key);
209
+ }
210
+ }
211
+ for (const key of keys) {
212
+ if (key.startsWith('__krate') || key === 'ref' || key === 'key')
213
+ continue;
214
+ try {
215
+ Object.defineProperty(this.raw, key, {
216
+ get: () => rendered()[key],
217
+ enumerable: true,
218
+ configurable: true,
219
+ });
220
+ }
221
+ catch {
222
+ // A frozen registry keeps live props.
223
+ }
224
+ }
225
+ }
226
+ enqueue(update) {
227
+ if (this.disposed)
228
+ return;
229
+ this.queue.push(update);
230
+ renderQueue.add(this);
231
+ schedule();
232
+ }
233
+ /** Marks the instance for a re-render because its creator re-rendered. */
234
+ markParentRender() {
235
+ if (this.disposed || !this.mounted)
236
+ return;
237
+ this.parentRender = true;
238
+ renderQueue.add(this);
239
+ }
240
+ performUpdate() {
241
+ if (this.disposed || (this.owner && this.owner.state !== 'mounted'))
242
+ return;
243
+ if (this.owner)
244
+ runWithOwner(this.owner, () => this.update());
245
+ else {
246
+ try {
247
+ this.update();
248
+ }
249
+ catch (err) {
250
+ console.error(err);
251
+ }
252
+ }
253
+ }
254
+ readKey() {
255
+ const getter = this.rawGetters.find((g) => g[0] === '__krate_key');
256
+ return untrack(() => (getter ? getter[1]() : this.raw.__krate_key));
257
+ }
258
+ update() {
259
+ const spec = this.spec;
260
+ const inst = this.inst;
261
+ // A new key is a new component: unmount this instance and mount a fresh
262
+ // one (the owner's setup runs again).
263
+ if (!Object.is(this.readKey(), this.key) && this.owner) {
264
+ const owner = this.owner;
265
+ unmountOwner(owner);
266
+ owner.inError = false;
267
+ mountOwner(owner);
268
+ remountChildren(owner);
269
+ return;
270
+ }
271
+ const nextProps = this.readProps();
272
+ const nextContext = this.readContext();
273
+ const propsChanged = !shallowEqual(nextProps, this.props) || nextContext !== this.context;
274
+ const queue = this.queue;
275
+ this.queue = [];
276
+ const forced = this.forced;
277
+ this.forced = false;
278
+ const parentRender = this.parentRender;
279
+ this.parentRender = false;
280
+ if (queue.length === 0 && !forced && !propsChanged && !parentRender)
281
+ return;
282
+ const legacy = !spec.getDerivedStateFromProps && !spec.getSnapshotBeforeUpdate;
283
+ if (legacy && (propsChanged || parentRender) && spec.componentWillReceiveProps) {
284
+ spec.componentWillReceiveProps.call(inst, nextProps, nextContext);
285
+ // setState inside componentWillReceiveProps joins this update.
286
+ queue.push(...this.queue);
287
+ this.queue = [];
288
+ }
289
+ const prevProps = this.props;
290
+ const prevState = this.state;
291
+ let nextState = prevState;
292
+ const callbacks = [];
293
+ for (const u of queue) {
294
+ const partial = typeof u.partial === 'function'
295
+ ? u.partial.call(inst, nextState, nextProps)
296
+ : u.partial;
297
+ if (partial != null && typeof partial === 'object')
298
+ nextState = { ...nextState, ...partial };
299
+ if (typeof u.callback === 'function')
300
+ callbacks.push(u.callback);
301
+ }
302
+ if (spec.getDerivedStateFromProps) {
303
+ const derived = spec.getDerivedStateFromProps(nextProps, nextState);
304
+ if (derived != null)
305
+ nextState = { ...nextState, ...derived };
306
+ }
307
+ let shouldUpdate = true;
308
+ if (!forced) {
309
+ if (spec.shouldComponentUpdate) {
310
+ shouldUpdate = !!spec.shouldComponentUpdate.call(inst, nextProps, nextState, nextContext);
311
+ }
312
+ else if (spec.pure) {
313
+ shouldUpdate = !shallowEqual(prevProps, nextProps) || !shallowEqual(prevState, nextState);
314
+ }
315
+ }
316
+ if (shouldUpdate && legacy && spec.componentWillUpdate) {
317
+ spec.componentWillUpdate.call(inst, nextProps, nextState, nextContext);
318
+ }
319
+ // React updates the instance even when the render is skipped.
320
+ this.props = nextProps;
321
+ this.state = nextState;
322
+ this.context = nextContext;
323
+ if (!shouldUpdate) {
324
+ for (const cb of callbacks)
325
+ commit(this, () => cb.call(inst));
326
+ return;
327
+ }
328
+ const snapshot = spec.getSnapshotBeforeUpdate
329
+ ? spec.getSnapshotBeforeUpdate.call(inst, prevProps, prevState)
330
+ : undefined;
331
+ this.setRendered(nextProps);
332
+ if (spec.setContext)
333
+ spec.setContext(nextContext);
334
+ // A forced update re-runs every binding, even when state is unchanged.
335
+ this.setStateSignal(forced && nextState === prevState ? { ...nextState } : nextState);
336
+ if (forced && nextState === prevState)
337
+ this.state = untrack(this.getState);
338
+ const recovering = this.owner && this.owner.inError && !this.errorCommitPending;
339
+ if (recovering && this.owner) {
340
+ this.owner.inError = false;
341
+ remountQueue.add(this.owner);
342
+ }
343
+ commit(this, () => {
344
+ if (spec.componentDidUpdate)
345
+ spec.componentDidUpdate.call(inst, prevProps, prevState, snapshot);
346
+ });
347
+ for (const cb of callbacks)
348
+ commit(this, () => cb.call(inst));
349
+ this.cascade();
350
+ }
351
+ // A re-render re-renders the class components this one's render created
352
+ // (directly or through function components), unless they bail out.
353
+ cascade() {
354
+ const owner = this.owner;
355
+ if (!owner)
356
+ return;
357
+ const reaches = (o) => {
358
+ for (let c = o ? o.creator : null; c; c = c.creator) {
359
+ if (c === owner)
360
+ return true;
361
+ if (c.data.classController)
362
+ return false;
363
+ }
364
+ return false;
365
+ };
366
+ const walk = (o) => {
367
+ for (const child of o.children) {
368
+ if (child.state !== 'mounted')
369
+ continue;
370
+ const ctrl = child.data.classController;
371
+ if (ctrl && reaches(child))
372
+ ctrl.markParentRender();
373
+ walk(child);
374
+ }
375
+ };
376
+ walk(owner);
377
+ }
378
+ catchError(err, info) {
379
+ const spec = this.spec;
380
+ const owner = this.owner;
381
+ if (this.disposed || !owner || owner.state !== 'mounted')
382
+ return false;
383
+ if (this.catching)
384
+ return true;
385
+ this.catching = true;
386
+ try {
387
+ // React logs every error a boundary catches.
388
+ if (typeof console !== 'undefined') {
389
+ console.error(err);
390
+ console.error('The above error occurred in the <' + (info.componentStack.match(/at (\S+)/)?.[1] || 'Unknown') +
391
+ '> component:' + info.componentStack +
392
+ '\n\nReact will try to recreate this component tree from scratch using the error boundary you provided, ' +
393
+ (spec.name || 'Component') + '.');
394
+ }
395
+ owner.inError = true;
396
+ // The children that were rendered when the error happened stay down
397
+ // until the boundary renders again; dormant ones (a hidden fallback
398
+ // branch) may still mount.
399
+ const failed = owner.children.filter((c) => c.state === 'mounted');
400
+ unmountChildren(owner);
401
+ for (const child of failed)
402
+ child.blocked = true;
403
+ let next = this.state;
404
+ if (spec.getDerivedStateFromError) {
405
+ // React 18 retries a render that threw during an update once before
406
+ // committing the fallback, so getDerivedStateFromError runs twice.
407
+ if (this.mounted && !inCommitPhase && !inCommitPhaseError())
408
+ spec.getDerivedStateFromError(err);
409
+ const partial = spec.getDerivedStateFromError(err);
410
+ if (partial != null)
411
+ next = { ...next, ...partial };
412
+ }
413
+ else if (typeof console !== 'undefined') {
414
+ console.error((spec.name || 'Component') +
415
+ ': Error boundaries should implement getDerivedStateFromError(). ' +
416
+ 'In that method, return a state update to display an error message or fallback UI.');
417
+ }
418
+ const prevProps = this.props;
419
+ const prevState = this.state;
420
+ const wasMounted = this.mounted;
421
+ this.state = next;
422
+ this.errorCommitPending = true;
423
+ this.setStateSignal(next);
424
+ commit(this, () => {
425
+ this.errorCommitPending = false;
426
+ if (wasMounted && next !== prevState && spec.componentDidUpdate) {
427
+ spec.componentDidUpdate.call(this.inst, prevProps, prevState, undefined);
428
+ }
429
+ if (spec.componentDidCatch)
430
+ spec.componentDidCatch.call(this.inst, err, info);
431
+ });
432
+ }
433
+ finally {
434
+ this.catching = false;
435
+ }
436
+ return true;
437
+ }
438
+ unmount() {
439
+ if (this.disposed)
440
+ return;
441
+ this.disposed = true;
442
+ renderQueue.delete(this);
443
+ // React detaches the ref before componentWillUnmount.
444
+ const detach = this.detachRef;
445
+ this.detachRef = null;
446
+ if (detach)
447
+ detach();
448
+ if (this.mounted && this.spec.componentWillUnmount) {
449
+ this.spec.componentWillUnmount.call(this.inst);
450
+ }
451
+ this.mounted = false;
452
+ }
453
+ runLifecycle(fn) {
454
+ if (this.disposed)
455
+ return;
456
+ if (this.owner)
457
+ runWithOwner(this.owner, fn);
458
+ else {
459
+ try {
460
+ fn();
461
+ }
462
+ catch (err) {
463
+ console.error(err);
464
+ }
465
+ }
466
+ }
467
+ }
468
+ function shallowEqual(a, b) {
469
+ if (a === b)
470
+ return true;
471
+ if (!a || !b)
472
+ return false;
473
+ const ka = Object.keys(a);
474
+ const kb = Object.keys(b);
475
+ if (ka.length !== kb.length)
476
+ return false;
477
+ for (const k of ka) {
478
+ if (!Object.prototype.hasOwnProperty.call(b, k) || !Object.is(a[k], b[k]))
479
+ return false;
480
+ }
481
+ return true;
482
+ }
483
+ // Scheduler.
484
+ const renderQueue = new Set();
485
+ const remountQueue = new Set();
486
+ const commitQueue = [];
487
+ let commitSeq = 0;
488
+ let commitScheduled = false;
489
+ // Render requests are processed when the current flush settles (krateOnSettle),
490
+ // so class DOM writes land in the same pass as other bindings.
491
+ function schedule() {
492
+ krateRequestFlush();
493
+ }
494
+ // Lifecycles run after the pass's DOM writes and the effects.ts layout commit.
495
+ function commit(c, fn) {
496
+ commitQueue.push({ c, fn, n: commitSeq++ });
497
+ if (!commitScheduled) {
498
+ commitScheduled = true;
499
+ krateRequestFlush();
500
+ }
501
+ }
502
+ function preOrder(a, b) {
503
+ if (a.owner && b.owner)
504
+ return a.owner.order - b.owner.order;
505
+ return a.seq - b.seq;
506
+ }
507
+ function isAncestor(a, b) {
508
+ for (let p = b.parent; p; p = p.parent)
509
+ if (p === a)
510
+ return true;
511
+ return false;
512
+ }
513
+ // Children commit before their parents; unrelated owners keep tree order.
514
+ function postOrder(a, b) {
515
+ const oa = a.c.owner;
516
+ const ob = b.c.owner;
517
+ if (oa && ob && oa !== ob) {
518
+ if (isAncestor(oa, ob))
519
+ return 1;
520
+ if (isAncestor(ob, oa))
521
+ return -1;
522
+ return oa.order - ob.order;
523
+ }
524
+ if (oa === ob)
525
+ return a.n - b.n;
526
+ return a.c.seq - b.c.seq || a.n - b.n;
527
+ }
528
+ function runRender() {
529
+ for (let guard = 0; renderQueue.size > 0 && guard < 1000; guard++) {
530
+ const list = Array.from(renderQueue).sort(preOrder);
531
+ renderQueue.clear();
532
+ krateRenderPhase(() => {
533
+ for (const c of list)
534
+ c.performUpdate();
535
+ });
536
+ }
537
+ }
538
+ // Set while lifecycles run: errors there are commit-phase errors, which React
539
+ // does not retry.
540
+ let inCommitPhase = false;
541
+ function runCommit() {
542
+ commitScheduled = false;
543
+ inCommitPhase = true;
544
+ try {
545
+ runCommitEntries();
546
+ }
547
+ finally {
548
+ inCommitPhase = false;
549
+ }
550
+ }
551
+ function runCommitEntries() {
552
+ if (remountQueue.size > 0) {
553
+ const owners = Array.from(remountQueue).sort((a, b) => a.order - b.order);
554
+ remountQueue.clear();
555
+ for (const o of owners)
556
+ remountChildren(o);
557
+ }
558
+ if (commitQueue.length > 0) {
559
+ const list = commitQueue.splice(0).sort(postOrder);
560
+ for (const entry of list)
561
+ entry.c.runLifecycle(entry.fn);
562
+ }
563
+ }
564
+ krateOnSettle(runRender);
565
+ // Lifecycles commit in the layout phase: after layout effects, before passive.
566
+ krateOnLayoutCommit(() => {
567
+ if (commitScheduled || commitQueue.length > 0 || remountQueue.size > 0)
568
+ runCommit();
569
+ });
570
+ /**
571
+ * Creates the instance behind a lowered class component. `props` is the
572
+ * component's props registry object, `getState`/`setState` the committed-state
573
+ * signal its render reads.
574
+ */
575
+ export function __krateClass(props, getState, setState, spec) {
576
+ return new Controller(props, getState, setState, spec).inst;
577
+ }
578
+ const hooks = [
579
+ 'shouldComponentUpdate',
580
+ 'getSnapshotBeforeUpdate',
581
+ 'componentDidMount',
582
+ 'componentDidUpdate',
583
+ 'componentWillUnmount',
584
+ 'componentDidCatch',
585
+ 'componentWillReceiveProps',
586
+ 'componentWillUpdate',
587
+ ];
588
+ /**
589
+ * Mounts a class component at runtime (React.createElement(MyClass) in code
590
+ * the compiler did not lower). The instance's setState/forceUpdate go through
591
+ * the same controller as lowered classes; each committed update re-runs
592
+ * render() and replaces the host's content.
593
+ */
594
+ export function mountClassElement(Klass, props, toNode) {
595
+ const instance = new Klass(props);
596
+ const [state, setState] = createSignal(instance.state || {});
597
+ const spec = {
598
+ name: Klass.displayName || Klass.name,
599
+ pure: !!Klass.prototype.isPureReactComponent,
600
+ defaultProps: Klass.defaultProps || null,
601
+ getDerivedStateFromProps: Klass.getDerivedStateFromProps,
602
+ getDerivedStateFromError: Klass.getDerivedStateFromError,
603
+ };
604
+ for (const name of hooks) {
605
+ const fn = instance[name] ?? instance['UNSAFE_' + name];
606
+ if (typeof fn === 'function') {
607
+ spec[name] = (...args) => fn.apply(instance, args);
608
+ }
609
+ }
610
+ const inst = __krateClass(props, state, setState, spec);
611
+ Object.defineProperty(instance, 'props', { get: () => inst.props, configurable: true });
612
+ Object.defineProperty(instance, 'state', {
613
+ get: () => inst.state,
614
+ set: (v) => {
615
+ inst.state = v;
616
+ },
617
+ configurable: true,
618
+ });
619
+ instance.setState = inst.setState;
620
+ instance.forceUpdate = inst.forceUpdate;
621
+ const host = document.createElement('div');
622
+ host.style.display = 'contents';
623
+ createEffect(() => {
624
+ state();
625
+ const node = untrack(() => toNode(instance.render()));
626
+ host.replaceChildren(node);
627
+ });
628
+ return host;
629
+ }
630
+ /** Reports an error from lowered class code to the nearest boundary. */
631
+ export function __krateClassError(err) {
632
+ handleError(err, getOwner());
633
+ }
@@ -0,0 +1 @@
1
+ export {};