@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.
- package/dist/action-context.d.ts +56 -0
- package/dist/action-context.js +73 -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 +550 -0
- package/dist/actions.test.d.ts +1 -0
- package/dist/actions.test.js +417 -0
- package/dist/boundary.d.ts +121 -0
- package/dist/boundary.js +429 -0
- package/dist/cache-scope.d.ts +118 -0
- package/dist/cache-scope.js +311 -0
- package/dist/cache.d.ts +55 -0
- package/dist/cache.js +155 -0
- package/dist/cache.test.d.ts +1 -0
- package/dist/cache.test.js +217 -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 +790 -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 +14 -4
- package/dist/index.js +9 -3
- package/dist/jsx-types.d.ts +2 -0
- package/dist/krate-hydrate-lite.js +7 -1
- 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 +47 -0
- package/dist/portal.js +189 -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/request-context.d.ts +62 -0
- package/dist/request-context.js +107 -0
- package/dist/resource.js +20 -1
- package/dist/router-layout.test.d.ts +1 -0
- package/dist/router-layout.test.js +179 -0
- package/dist/router-lite.d.ts +25 -0
- package/dist/router-lite.js +73 -0
- package/dist/router-nav.test.d.ts +1 -0
- package/dist/router-nav.test.js +399 -0
- package/dist/router-slots.test.d.ts +1 -0
- package/dist/router-slots.test.js +144 -0
- package/dist/router.d.ts +66 -2
- package/dist/router.js +1535 -650
- package/dist/server-async.test.d.ts +1 -0
- package/dist/server-async.test.js +174 -0
- package/dist/server-renderer.js +494 -119
- package/dist/server.d.ts +27 -1
- package/dist/server.js +187 -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 +186 -1
- package/dist/signal.js +896 -66
- package/dist/slots.d.ts +22 -0
- package/dist/slots.js +134 -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 +5 -1
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach } from 'vitest';
|
|
2
|
+
import { krateMountPortals, kratePortalFind, kratePortalReset, kratePortalScan, kratePortalTarget } from './portal';
|
|
3
|
+
import { krateCondSwap } from './slots';
|
|
4
|
+
import { krateOn } from './events';
|
|
5
|
+
import { disposeNode } from './dom';
|
|
6
|
+
import { createPortal } from './react';
|
|
7
|
+
function startMarker(root, id) {
|
|
8
|
+
const w = document.createTreeWalker(root, NodeFilter.SHOW_COMMENT);
|
|
9
|
+
while (w.nextNode()) {
|
|
10
|
+
if (w.currentNode.nodeValue === 'k:' + id)
|
|
11
|
+
return w.currentNode;
|
|
12
|
+
}
|
|
13
|
+
throw new Error('marker not found: ' + id);
|
|
14
|
+
}
|
|
15
|
+
const bodyChildren = () => Array.from(document.body.children).map((el) => el.id || el.tagName.toLowerCase());
|
|
16
|
+
describe('portals', () => {
|
|
17
|
+
beforeEach(() => {
|
|
18
|
+
kratePortalReset();
|
|
19
|
+
document.body.innerHTML = '';
|
|
20
|
+
});
|
|
21
|
+
it('mounts portal content into body and bubbles events through the React tree', () => {
|
|
22
|
+
document.body.innerHTML =
|
|
23
|
+
'<div id="root"><div id="host"><template data-krate-portal="p1"><div id="toast"><button id="btn">ok</button></div></template></div></div>';
|
|
24
|
+
const host = document.getElementById('host');
|
|
25
|
+
const btn = document.querySelector('template').content.querySelector('#btn');
|
|
26
|
+
const seen = [];
|
|
27
|
+
krateOn(btn, 'onClick', () => seen.push('button'));
|
|
28
|
+
krateOn(host, 'onClick', () => seen.push('host'));
|
|
29
|
+
krateMountPortals();
|
|
30
|
+
expect(bodyChildren()).toEqual(['root', 'toast']);
|
|
31
|
+
expect(document.getElementById('btn')).toBe(btn);
|
|
32
|
+
btn.click();
|
|
33
|
+
expect(seen).toEqual(['button', 'host']);
|
|
34
|
+
});
|
|
35
|
+
it('mounts into a selector or registered container', () => {
|
|
36
|
+
document.body.innerHTML =
|
|
37
|
+
'<div id="root"><template data-krate-portal="a" data-krate-portal-target="#modal-root"><p id="a">a</p></template>' +
|
|
38
|
+
'<template data-krate-portal="b"><p id="b">b</p></template></div><div id="modal-root"></div><div id="side"></div>';
|
|
39
|
+
kratePortalTarget('b', () => document.getElementById('side'));
|
|
40
|
+
krateMountPortals();
|
|
41
|
+
expect(document.getElementById('a').parentElement.id).toBe('modal-root');
|
|
42
|
+
expect(document.getElementById('b').parentElement.id).toBe('side');
|
|
43
|
+
});
|
|
44
|
+
it('follows its conditional branch: unmounts when hidden, remounts when shown', () => {
|
|
45
|
+
document.body.innerHTML =
|
|
46
|
+
'<div id="root"><!--k:c--><!--/k:c--></div>' +
|
|
47
|
+
'<template data-kc="c" data-kb="c"><template data-krate-portal="m"><div id="modal">hi</div></template></template>';
|
|
48
|
+
const start = startMarker(document.body, 'c');
|
|
49
|
+
const tpl = document.querySelector('template[data-kc]');
|
|
50
|
+
krateCondSwap(start, false, tpl);
|
|
51
|
+
krateMountPortals();
|
|
52
|
+
expect(document.getElementById('modal')).toBeNull();
|
|
53
|
+
krateCondSwap(start, true, tpl);
|
|
54
|
+
expect(bodyChildren()).toContain('modal');
|
|
55
|
+
const modal = document.getElementById('modal');
|
|
56
|
+
krateCondSwap(start, false, tpl);
|
|
57
|
+
expect(document.getElementById('modal')).toBeNull();
|
|
58
|
+
krateCondSwap(start, true, tpl);
|
|
59
|
+
expect(document.getElementById('modal')).toBe(modal);
|
|
60
|
+
});
|
|
61
|
+
it('drops the content when the anchor is disposed', () => {
|
|
62
|
+
document.body.innerHTML = '<div id="root"><span id="wrap"><template data-krate-portal="d"><i id="x">x</i></template></span></div>';
|
|
63
|
+
krateMountPortals();
|
|
64
|
+
expect(document.getElementById('x')).not.toBeNull();
|
|
65
|
+
disposeNode(document.getElementById('wrap'));
|
|
66
|
+
expect(document.getElementById('x')).toBeNull();
|
|
67
|
+
});
|
|
68
|
+
it('finds bound nodes in portal templates before and after mounting', () => {
|
|
69
|
+
document.body.innerHTML = '<div id="root"><template data-krate-portal="f"><b data-k="k:9">b</b><!--k:8--><!--/k:8--></template></div>';
|
|
70
|
+
const root = document.getElementById('root');
|
|
71
|
+
const before = kratePortalFind('[data-k="k:9"]', [root]);
|
|
72
|
+
expect(before).not.toBeNull();
|
|
73
|
+
const comments = [];
|
|
74
|
+
kratePortalScan([root], (content) => {
|
|
75
|
+
const w = document.createTreeWalker(content, NodeFilter.SHOW_COMMENT);
|
|
76
|
+
while (w.nextNode())
|
|
77
|
+
comments.push(String(w.currentNode.nodeValue));
|
|
78
|
+
});
|
|
79
|
+
expect(comments).toContain('k:8');
|
|
80
|
+
krateMountPortals();
|
|
81
|
+
expect(kratePortalFind('[data-k="k:9"]', [root])).toBe(before);
|
|
82
|
+
});
|
|
83
|
+
it('runtime createPortal returns an anchor and removes content on dispose', () => {
|
|
84
|
+
document.body.innerHTML = '<div id="root"><div id="host"></div></div>';
|
|
85
|
+
const host = document.getElementById('host');
|
|
86
|
+
const content = document.createElement('p');
|
|
87
|
+
content.id = 'rt';
|
|
88
|
+
const seen = [];
|
|
89
|
+
krateOn(host, 'onClick', () => seen.push('host'));
|
|
90
|
+
const anchor = createPortal(content, document.body);
|
|
91
|
+
host.appendChild(anchor);
|
|
92
|
+
expect(anchor.nodeType).toBe(8);
|
|
93
|
+
expect(content.parentNode).toBe(document.body);
|
|
94
|
+
content.click();
|
|
95
|
+
expect(seen).toEqual(['host']);
|
|
96
|
+
disposeNode(anchor);
|
|
97
|
+
expect(document.getElementById('rt')).toBeNull();
|
|
98
|
+
});
|
|
99
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach } from 'vitest';
|
|
2
|
+
import { useEffect, useLayoutEffect, useInsertionEffect, useState, useReducer, useMemo, useCallback, useImperativeHandle, createElement, } from './react';
|
|
3
|
+
import { disposeAll } from './signal';
|
|
4
|
+
import { disposeNode } from './dom';
|
|
5
|
+
const tick = () => new Promise((r) => setTimeout(r, 0));
|
|
6
|
+
async function settle() {
|
|
7
|
+
for (let i = 0; i < 3; i++)
|
|
8
|
+
await tick();
|
|
9
|
+
}
|
|
10
|
+
describe('React compat hooks (unlowered code paths)', () => {
|
|
11
|
+
beforeEach(() => disposeAll());
|
|
12
|
+
it('runs insertion, layout and passive effects in React order', async () => {
|
|
13
|
+
const log = [];
|
|
14
|
+
const Child = () => {
|
|
15
|
+
useEffect(() => void log.push('child passive'), []);
|
|
16
|
+
useLayoutEffect(() => void log.push('child layout'), []);
|
|
17
|
+
useInsertionEffect(() => void log.push('child insertion'), []);
|
|
18
|
+
return createElement('i', null);
|
|
19
|
+
};
|
|
20
|
+
const Parent = () => {
|
|
21
|
+
useEffect(() => void log.push('parent passive'), []);
|
|
22
|
+
useLayoutEffect(() => void log.push('parent layout'), []);
|
|
23
|
+
return createElement('div', null, createElement(Child, null));
|
|
24
|
+
};
|
|
25
|
+
createElement(Parent, null);
|
|
26
|
+
await settle();
|
|
27
|
+
expect(log).toEqual(['child insertion', 'child layout', 'parent layout', 'child passive', 'parent passive']);
|
|
28
|
+
});
|
|
29
|
+
it('honours dependency arrays whose elements are state getters', async () => {
|
|
30
|
+
const log = [];
|
|
31
|
+
let setCount;
|
|
32
|
+
let other;
|
|
33
|
+
const Comp = () => {
|
|
34
|
+
const [count, set] = useState(0);
|
|
35
|
+
const [, setOther] = useState(0);
|
|
36
|
+
setCount = set;
|
|
37
|
+
other = setOther;
|
|
38
|
+
useEffect(() => void log.push(count()), [count]);
|
|
39
|
+
return createElement('span', null);
|
|
40
|
+
};
|
|
41
|
+
createElement(Comp, null);
|
|
42
|
+
await settle();
|
|
43
|
+
other(1);
|
|
44
|
+
await settle();
|
|
45
|
+
expect(log).toEqual([0]);
|
|
46
|
+
setCount(1);
|
|
47
|
+
await settle();
|
|
48
|
+
expect(log).toEqual([0, 1]);
|
|
49
|
+
});
|
|
50
|
+
it('runs cleanups when the component unmounts', async () => {
|
|
51
|
+
const log = [];
|
|
52
|
+
const Comp = () => {
|
|
53
|
+
useEffect(() => () => void log.push('passive cleanup'), []);
|
|
54
|
+
useLayoutEffect(() => () => void log.push('layout cleanup'), []);
|
|
55
|
+
return createElement('span', null);
|
|
56
|
+
};
|
|
57
|
+
const node = createElement(Comp, null);
|
|
58
|
+
await settle();
|
|
59
|
+
disposeNode(node);
|
|
60
|
+
await settle();
|
|
61
|
+
expect(log).toEqual(['layout cleanup', 'passive cleanup']);
|
|
62
|
+
});
|
|
63
|
+
it('keeps useMemo and useCallback values stable for the component lifetime', () => {
|
|
64
|
+
let calls = 0;
|
|
65
|
+
const fn = () => 1;
|
|
66
|
+
const memo = useMemo(() => ++calls, [1]);
|
|
67
|
+
expect(memo).toBe(1);
|
|
68
|
+
expect(useCallback(fn, [])).toBe(fn);
|
|
69
|
+
});
|
|
70
|
+
it('supports the useReducer lazy initializer and bail-out', async () => {
|
|
71
|
+
const [state, dispatch] = useReducer((s, a) => (a === 0 ? s : s + a), 2, (n) => n * 3);
|
|
72
|
+
expect(state()).toBe(6);
|
|
73
|
+
let runs = 0;
|
|
74
|
+
useEffect(() => void runs++, [state]);
|
|
75
|
+
await settle();
|
|
76
|
+
dispatch(0);
|
|
77
|
+
await settle();
|
|
78
|
+
expect(runs).toBe(1);
|
|
79
|
+
dispatch(1);
|
|
80
|
+
await settle();
|
|
81
|
+
expect(runs).toBe(2);
|
|
82
|
+
});
|
|
83
|
+
it('attaches imperative handles in the layout phase and detaches on unmount', async () => {
|
|
84
|
+
const ref = { current: null };
|
|
85
|
+
const Comp = () => {
|
|
86
|
+
useImperativeHandle(ref, () => ({ focus: () => 'focused' }), []);
|
|
87
|
+
return createElement('input', null);
|
|
88
|
+
};
|
|
89
|
+
const node = createElement(Comp, null);
|
|
90
|
+
expect(ref.current).toBe(null);
|
|
91
|
+
await Promise.resolve();
|
|
92
|
+
expect(ref.current?.focus()).toBe('focused');
|
|
93
|
+
disposeNode(node);
|
|
94
|
+
expect(ref.current).toBe(null);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { Profiler, StrictMode, Suspense, createElement, jsx, lazy, startTransition, use, useDeferredValue, useState, useSyncExternalStore, useTransition, createContext, } from './react.js';
|
|
3
|
+
import { setFallbackThrottle, setSuspenseErrorHandler } from './suspense.js';
|
|
4
|
+
async function flush(n = 20) {
|
|
5
|
+
for (let i = 0; i < n; i++)
|
|
6
|
+
await Promise.resolve();
|
|
7
|
+
}
|
|
8
|
+
function deferred() {
|
|
9
|
+
let resolve;
|
|
10
|
+
let reject;
|
|
11
|
+
const promise = new Promise((res, rej) => {
|
|
12
|
+
resolve = res;
|
|
13
|
+
reject = rej;
|
|
14
|
+
});
|
|
15
|
+
return { promise, resolve, reject };
|
|
16
|
+
}
|
|
17
|
+
function mountInto(node) {
|
|
18
|
+
const root = document.createElement('div');
|
|
19
|
+
root.appendChild(node);
|
|
20
|
+
document.body.innerHTML = '';
|
|
21
|
+
document.body.appendChild(root);
|
|
22
|
+
return root;
|
|
23
|
+
}
|
|
24
|
+
beforeEach(() => setFallbackThrottle(0));
|
|
25
|
+
afterEach(() => {
|
|
26
|
+
setSuspenseErrorHandler(null);
|
|
27
|
+
vi.useRealTimers();
|
|
28
|
+
});
|
|
29
|
+
describe('compat Suspense and use()', () => {
|
|
30
|
+
it('shows the fallback until use(promise) resolves, then renders the value', async () => {
|
|
31
|
+
const d = deferred();
|
|
32
|
+
const Name = () => createElement('b', null, use(d.promise));
|
|
33
|
+
const root = mountInto(createElement(Suspense, { fallback: createElement('i', null, 'loading') }, createElement(Name, null)));
|
|
34
|
+
expect(root.textContent).toBe('loading');
|
|
35
|
+
d.resolve('Ada');
|
|
36
|
+
await flush();
|
|
37
|
+
expect(root.textContent).toBe('Ada');
|
|
38
|
+
expect(root.querySelector('b')?.textContent).toBe('Ada');
|
|
39
|
+
});
|
|
40
|
+
it('reveals siblings together', async () => {
|
|
41
|
+
const a = deferred();
|
|
42
|
+
const b = deferred();
|
|
43
|
+
const A = () => createElement('span', null, use(a.promise));
|
|
44
|
+
const B = () => createElement('span', null, use(b.promise));
|
|
45
|
+
const root = mountInto(createElement(Suspense, { fallback: 'wait' }, createElement(A, null), createElement(B, null)));
|
|
46
|
+
a.resolve('a');
|
|
47
|
+
await flush();
|
|
48
|
+
expect(root.textContent).toBe('wait');
|
|
49
|
+
b.resolve('b');
|
|
50
|
+
await flush();
|
|
51
|
+
expect(root.textContent).toBe('ab');
|
|
52
|
+
});
|
|
53
|
+
it('suspends the innermost boundary only', async () => {
|
|
54
|
+
const d = deferred();
|
|
55
|
+
const Slow = () => createElement('span', null, use(d.promise));
|
|
56
|
+
const root = mountInto(createElement(Suspense, { fallback: 'outer' }, createElement('p', null, 'shell'), createElement(Suspense, { fallback: 'inner' }, createElement(Slow, null))));
|
|
57
|
+
expect(root.textContent).toBe('shellinner');
|
|
58
|
+
d.resolve('done');
|
|
59
|
+
await flush();
|
|
60
|
+
expect(root.textContent).toBe('shelldone');
|
|
61
|
+
});
|
|
62
|
+
it('does not re-suspend on a resolved promise and allows conditional use()', async () => {
|
|
63
|
+
const p = Promise.resolve('v');
|
|
64
|
+
await flush();
|
|
65
|
+
let renders = 0;
|
|
66
|
+
const C = (props) => {
|
|
67
|
+
renders++;
|
|
68
|
+
return createElement('span', null, props.on ? use(p) : 'off');
|
|
69
|
+
};
|
|
70
|
+
// First read tracks the promise; it is already resolved by the next read.
|
|
71
|
+
mountInto(createElement(Suspense, { fallback: 'x' }, createElement(C, { on: true })));
|
|
72
|
+
await flush();
|
|
73
|
+
const root = mountInto(createElement(Suspense, { fallback: 'x' }, createElement(C, { on: true })));
|
|
74
|
+
expect(root.textContent).toBe('v');
|
|
75
|
+
const off = mountInto(createElement(C, { on: false }));
|
|
76
|
+
expect(off.textContent).toBe('off');
|
|
77
|
+
expect(renders).toBeGreaterThan(0);
|
|
78
|
+
});
|
|
79
|
+
it('reads a context with use()', () => {
|
|
80
|
+
const Ctx = createContext('ctx');
|
|
81
|
+
const C = () => createElement('span', null, use(Ctx));
|
|
82
|
+
expect(mountInto(createElement(C, null)).textContent).toBe('ctx');
|
|
83
|
+
});
|
|
84
|
+
it('routes a rejected promise to the error handler', async () => {
|
|
85
|
+
const errors = [];
|
|
86
|
+
setSuspenseErrorHandler((e) => {
|
|
87
|
+
errors.push(e);
|
|
88
|
+
return true;
|
|
89
|
+
});
|
|
90
|
+
const d = deferred();
|
|
91
|
+
const C = () => createElement('span', null, use(d.promise));
|
|
92
|
+
mountInto(createElement(Suspense, { fallback: 'x' }, createElement(C, null)));
|
|
93
|
+
d.reject(new Error('bad'));
|
|
94
|
+
await flush();
|
|
95
|
+
expect(errors[0].message).toBe('bad');
|
|
96
|
+
});
|
|
97
|
+
it('jsx() renders through the same suspending path', async () => {
|
|
98
|
+
const d = deferred();
|
|
99
|
+
const C = () => jsx('span', { children: use(d.promise) });
|
|
100
|
+
const root = mountInto(jsx(Suspense, { fallback: 'f', children: jsx(C, {}) }));
|
|
101
|
+
expect(root.textContent).toBe('f');
|
|
102
|
+
d.resolve('ok');
|
|
103
|
+
await flush();
|
|
104
|
+
expect(root.textContent).toBe('ok');
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
describe('compat lazy()', () => {
|
|
108
|
+
it('starts loading on first render and suspends until the module loads', async () => {
|
|
109
|
+
const d = deferred();
|
|
110
|
+
let calls = 0;
|
|
111
|
+
const Lazy = lazy(() => {
|
|
112
|
+
calls++;
|
|
113
|
+
return d.promise;
|
|
114
|
+
});
|
|
115
|
+
expect(calls).toBe(0);
|
|
116
|
+
const root = mountInto(createElement(Suspense, { fallback: 'loading' }, createElement(Lazy, { name: 'x' })));
|
|
117
|
+
expect(calls).toBe(1);
|
|
118
|
+
expect(root.textContent).toBe('loading');
|
|
119
|
+
d.resolve({ default: (p) => createElement('em', null, 'hi ' + p.name) });
|
|
120
|
+
await flush();
|
|
121
|
+
expect(root.textContent).toBe('hi x');
|
|
122
|
+
expect(calls).toBe(1);
|
|
123
|
+
});
|
|
124
|
+
it('reports a module without a component default export', async () => {
|
|
125
|
+
const errors = [];
|
|
126
|
+
setSuspenseErrorHandler((e) => {
|
|
127
|
+
errors.push(e);
|
|
128
|
+
return true;
|
|
129
|
+
});
|
|
130
|
+
const Lazy = lazy(() => Promise.resolve({ default: 42 }));
|
|
131
|
+
mountInto(createElement(Suspense, { fallback: 'x' }, createElement(Lazy, null)));
|
|
132
|
+
await flush();
|
|
133
|
+
expect(String(errors[0].message)).toContain('default export');
|
|
134
|
+
});
|
|
135
|
+
it('reports a failed import', async () => {
|
|
136
|
+
const errors = [];
|
|
137
|
+
setSuspenseErrorHandler((e) => {
|
|
138
|
+
errors.push(e);
|
|
139
|
+
return true;
|
|
140
|
+
});
|
|
141
|
+
const Lazy = lazy(() => Promise.reject(new Error('chunk failed')));
|
|
142
|
+
mountInto(createElement(Suspense, { fallback: 'x' }, createElement(Lazy, null)));
|
|
143
|
+
await flush();
|
|
144
|
+
expect(errors[0].message).toBe('chunk failed');
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
describe('compat transitions and deferred values', () => {
|
|
148
|
+
it('useTransition stays pending until the async action settles', async () => {
|
|
149
|
+
vi.useFakeTimers();
|
|
150
|
+
const [isPending, start] = useTransition();
|
|
151
|
+
start(async () => {
|
|
152
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
153
|
+
});
|
|
154
|
+
expect(isPending()).toBe(true);
|
|
155
|
+
await vi.advanceTimersByTimeAsync(100);
|
|
156
|
+
expect(isPending()).toBe(false);
|
|
157
|
+
});
|
|
158
|
+
it('startTransition runs the action', () => {
|
|
159
|
+
let ran = false;
|
|
160
|
+
startTransition(() => {
|
|
161
|
+
ran = true;
|
|
162
|
+
});
|
|
163
|
+
expect(ran).toBe(true);
|
|
164
|
+
});
|
|
165
|
+
it('useDeferredValue defers a getter and passes plain values through', async () => {
|
|
166
|
+
vi.useFakeTimers();
|
|
167
|
+
const [q, setQ] = useState('a');
|
|
168
|
+
const d = useDeferredValue(q);
|
|
169
|
+
setQ('b');
|
|
170
|
+
await flush();
|
|
171
|
+
expect(d()).toBe('a');
|
|
172
|
+
await vi.advanceTimersByTimeAsync(1);
|
|
173
|
+
expect(d()).toBe('b');
|
|
174
|
+
expect(useDeferredValue(5)).toBe(5);
|
|
175
|
+
});
|
|
176
|
+
it('useSyncExternalStore reads and follows the store', () => {
|
|
177
|
+
let v = 1;
|
|
178
|
+
let notify = () => { };
|
|
179
|
+
const get = useSyncExternalStore((fn) => {
|
|
180
|
+
notify = fn;
|
|
181
|
+
return () => { };
|
|
182
|
+
}, () => v);
|
|
183
|
+
expect(get()).toBe(1);
|
|
184
|
+
v = 2;
|
|
185
|
+
notify();
|
|
186
|
+
expect(get()).toBe(2);
|
|
187
|
+
});
|
|
188
|
+
it('StrictMode and Profiler render their children', () => {
|
|
189
|
+
expect(mountInto(createElement(StrictMode, null, 'a')).textContent).toBe('a');
|
|
190
|
+
const onRender = vi.fn();
|
|
191
|
+
expect(mountInto(createElement(Profiler, { id: 'p', onRender }, 'b')).textContent).toBe('b');
|
|
192
|
+
expect(onRender).not.toHaveBeenCalled();
|
|
193
|
+
});
|
|
194
|
+
});
|
package/dist/react.d.ts
CHANGED
|
@@ -9,12 +9,16 @@
|
|
|
9
9
|
* Krate components run once and update through signals, so hooks here are
|
|
10
10
|
* signal-backed rather than re-render based. Pure APIs (`createElement`,
|
|
11
11
|
* `Children`, `cloneElement`, `isValidElement`, `createRef`, `createContext`)
|
|
12
|
-
* behave as expected
|
|
13
|
-
*
|
|
12
|
+
* behave as expected. `Suspense`, `lazy` and `use` suspend through the
|
|
13
|
+
* Suspense runtime (see suspense.ts): a component that reads a pending promise
|
|
14
|
+
* renders a placeholder that is replaced once the promise settles, and the
|
|
15
|
+
* nearest `<Suspense>` shows its fallback until all of its placeholders
|
|
16
|
+
* resolved. `useTransition`/`startTransition` support async actions and
|
|
17
|
+
* `useDeferredValue` defers getter values. Class components are intentionally
|
|
14
18
|
* minimal.
|
|
15
19
|
*/
|
|
16
|
-
import { h, type Component } from './dom.js';
|
|
17
|
-
import {
|
|
20
|
+
import { h, type Component as FnComponent } from './dom.js';
|
|
21
|
+
import { requestFormReset as krateRequestFormReset, type FormStatus } from './actions.js';
|
|
18
22
|
/** React's `Fragment` marker; `createElement(Fragment, ...)` yields a fragment. */
|
|
19
23
|
export declare const Fragment: unique symbol;
|
|
20
24
|
export type ReactChild = unknown;
|
|
@@ -23,8 +27,8 @@ export type ReactChild = unknown;
|
|
|
23
27
|
* (elements) or the result of the component function, matching `h`.
|
|
24
28
|
*/
|
|
25
29
|
export declare function createElement(type: unknown, config?: Record<string, unknown> | null, ...children: unknown[]): Node;
|
|
26
|
-
export declare
|
|
27
|
-
export declare const jsxs: typeof
|
|
30
|
+
export declare function jsx(type: unknown, props: Record<string, unknown> | null, _key?: unknown): Node;
|
|
31
|
+
export declare const jsxs: typeof jsx;
|
|
28
32
|
export declare function jsxDEV(type: unknown, props: Record<string, unknown> | null, _key?: unknown, _isStatic?: boolean, _source?: unknown, _self?: unknown): Node;
|
|
29
33
|
/** `React.Children` helpers over arrays and single values. */
|
|
30
34
|
export declare const Children: {
|
|
@@ -41,12 +45,39 @@ export interface RefObject<T> {
|
|
|
41
45
|
}
|
|
42
46
|
export declare function createRef<T = unknown>(): RefObject<T>;
|
|
43
47
|
export declare function forwardRef(render: unknown): unknown;
|
|
44
|
-
export declare function memo<P>(component:
|
|
45
|
-
|
|
48
|
+
export declare function memo<P>(component: FnComponent<P>, _compare?: unknown): FnComponent<P>;
|
|
49
|
+
/**
|
|
50
|
+
* `React.lazy(() => import('./X'))`. The loader runs on the first render (as
|
|
51
|
+
* in React); until the module resolves the component suspends to the nearest
|
|
52
|
+
* `<Suspense>`. The module's default export must be a component; a rejected
|
|
53
|
+
* import or a non-component default is reported to the nearest error
|
|
54
|
+
* boundary.
|
|
55
|
+
*/
|
|
56
|
+
export declare function lazy<P>(loader: () => PromiseLike<{
|
|
57
|
+
default: unknown;
|
|
58
|
+
}>): FnComponent<P>;
|
|
59
|
+
/** StrictMode renders its children; Krate has no double-render checks. */
|
|
46
60
|
export declare const StrictMode: symbol;
|
|
47
|
-
|
|
48
|
-
|
|
61
|
+
/**
|
|
62
|
+
* Profiler renders its children. `onRender` is never called: Krate does not
|
|
63
|
+
* re-render components, so there are no commit timings to report.
|
|
64
|
+
*/
|
|
65
|
+
export declare function Profiler(props: {
|
|
66
|
+
id?: string;
|
|
67
|
+
onRender?: unknown;
|
|
68
|
+
children?: unknown;
|
|
69
|
+
}): Node;
|
|
70
|
+
/**
|
|
71
|
+
* A Suspense boundary. Children that suspend render as placeholders; while
|
|
72
|
+
* any placeholder under this boundary is pending the fallback is shown, and
|
|
73
|
+
* the content is revealed once all of them resolved (siblings together).
|
|
74
|
+
*/
|
|
75
|
+
export declare function Suspense(props: {
|
|
76
|
+
fallback?: unknown;
|
|
77
|
+
children?: unknown;
|
|
78
|
+
}): Node;
|
|
49
79
|
export interface ReactContext<T> {
|
|
80
|
+
readonly $$typeof?: symbol;
|
|
50
81
|
readonly _id: number;
|
|
51
82
|
readonly defaultValue: T;
|
|
52
83
|
readonly Provider: (props: {
|
|
@@ -62,39 +93,108 @@ export interface ReactContext<T> {
|
|
|
62
93
|
export declare function createContext<T>(defaultValue: T): ReactContext<T>;
|
|
63
94
|
export declare function useContext<T>(ctx: ReactContext<T>): T;
|
|
64
95
|
export declare function useState<T>(initial: T | (() => T)): [() => T, (value: T | ((prev: T) => T)) => void];
|
|
65
|
-
export declare function useReducer<S, A>(reducer: (state: S, action: A) => S, initial: S): [() => S, (action: A) => void];
|
|
96
|
+
export declare function useReducer<S, A, I = S>(reducer: (state: S, action: A) => S, initial: I, init?: (arg: I) => S): [() => S, (action: A) => void];
|
|
97
|
+
/**
|
|
98
|
+
* Memoized value. The component body runs once, so the factory runs once and
|
|
99
|
+
* the value is stable for the component's lifetime (React recomputes only
|
|
100
|
+
* when a dependency changes, which a single run cannot observe).
|
|
101
|
+
*/
|
|
66
102
|
export declare function useMemo<T>(factory: () => T, _deps?: unknown[]): T;
|
|
103
|
+
/** Memoized callback: one identity for the component's lifetime. */
|
|
67
104
|
export declare function useCallback<T extends (...args: never[]) => unknown>(fn: T, _deps?: unknown[]): T;
|
|
68
105
|
export declare function useRef<T = unknown>(initial?: T): RefObject<T>;
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
export declare const
|
|
72
|
-
|
|
106
|
+
type EffectCallback = () => void | (() => void);
|
|
107
|
+
/** Passive effect: runs after paint, re-runs when a live dependency changes. */
|
|
108
|
+
export declare const useEffect: (effect: EffectCallback, deps?: readonly unknown[] | null) => void;
|
|
109
|
+
/** Layout effect: runs synchronously after the DOM is updated, before paint. */
|
|
110
|
+
export declare const useLayoutEffect: (effect: EffectCallback, deps?: readonly unknown[] | null) => void;
|
|
111
|
+
/** Insertion effect: runs before layout effects in the same commit. */
|
|
112
|
+
export declare const useInsertionEffect: (effect: EffectCallback, deps?: readonly unknown[] | null) => void;
|
|
113
|
+
export declare function useImperativeHandle<T>(ref: RefObject<T> | ((v: T | null) => unknown) | null | undefined, create: () => T, deps?: readonly unknown[] | (() => unknown) | null): void;
|
|
73
114
|
export declare function useDebugValue(_value?: unknown): void;
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
export declare function
|
|
86
|
-
|
|
115
|
+
/**
|
|
116
|
+
* `useSyncExternalStore` returning a snapshot getter. Snapshots are compared
|
|
117
|
+
* with Object.is, a change between the first read and the subscription is
|
|
118
|
+
* picked up, and the store is unsubscribed on teardown.
|
|
119
|
+
*/
|
|
120
|
+
export declare function useSyncExternalStore<T>(subscribe: (onChange: () => void) => (() => void) | void, getSnapshot: () => T, getServerSnapshot?: () => T): () => T;
|
|
121
|
+
/**
|
|
122
|
+
* `[isPending, startTransition]`. isPending (a getter) stays true until the
|
|
123
|
+
* action passed to startTransition settles, including async actions, and
|
|
124
|
+
* until every overlapping transition started here settled.
|
|
125
|
+
*/
|
|
126
|
+
export declare function useTransition(): [() => boolean, (fn: () => unknown) => void];
|
|
127
|
+
/**
|
|
128
|
+
* React 19 `useOptimistic`. `state` may be a value or a getter; optimistic
|
|
129
|
+
* updates apply on top of it while an action is pending and are dropped when
|
|
130
|
+
* the action settles.
|
|
131
|
+
*/
|
|
132
|
+
export declare function useOptimistic<T, A = T>(state: T | (() => T), update?: (state: T, action: A) => T): [() => T, (action: A) => void];
|
|
133
|
+
/**
|
|
134
|
+
* React 19 `useActionState`: returns `[state, dispatch, isPending]` getters and
|
|
135
|
+
* a dispatch usable as a form action. Calls are queued in order and each
|
|
136
|
+
* receives the previous state; server actions receive it as their first
|
|
137
|
+
* argument over the action transport.
|
|
138
|
+
*/
|
|
139
|
+
export declare function useActionState<State, Payload>(action: (prev: State, payload: Payload) => State | Promise<State>, initial: State, _permalink?: string): [() => State, (payload: Payload) => void, () => boolean];
|
|
140
|
+
/**
|
|
141
|
+
* React DOM `useFormStatus`. Source components are lowered by the compiler to
|
|
142
|
+
* a form-scoped status bound to the component's element; called from compiled
|
|
143
|
+
* code without an element, it reports an idle status.
|
|
144
|
+
*/
|
|
145
|
+
export declare function useFormStatus(): FormStatus;
|
|
146
|
+
/** React DOM `requestFormReset(form)`. */
|
|
147
|
+
export declare const requestFormReset: typeof krateRequestFormReset;
|
|
148
|
+
/**
|
|
149
|
+
* `useDeferredValue(value, initialValue?)`. A getter value is deferred: the
|
|
150
|
+
* returned getter keeps the previous value during urgent updates and catches
|
|
151
|
+
* up in a later task, starting from `initialValue` when given. A plain value
|
|
152
|
+
* cannot change without a re-render in Krate and is returned as is.
|
|
153
|
+
*/
|
|
154
|
+
export declare function useDeferredValue<T>(value: T | (() => T), ...initial: [] | [T]): T | (() => T);
|
|
155
|
+
/** Top-level `startTransition`; async actions keep the transition pending. */
|
|
156
|
+
export declare function startTransition(fn: () => unknown): void;
|
|
157
|
+
/**
|
|
158
|
+
* React 19 `use()`: reads a context, or unwraps a promise. A pending promise
|
|
159
|
+
* suspends the calling component (it re-renders when the promise settles); a
|
|
160
|
+
* rejected one throws its reason to the nearest error boundary. The same
|
|
161
|
+
* promise object never suspends again once it resolved. Unlike hooks, `use`
|
|
162
|
+
* may be called conditionally.
|
|
163
|
+
*/
|
|
164
|
+
export declare function use<T>(value: PromiseLike<T> | ReactContext<T>): T;
|
|
87
165
|
export declare function flushSync<T>(fn: () => T): T;
|
|
88
|
-
|
|
89
|
-
|
|
166
|
+
/**
|
|
167
|
+
* Runtime portal (content rendered by `h()` after hydration; compiled source
|
|
168
|
+
* components lower createPortal to an anchor instead, see portal.ts). The
|
|
169
|
+
* children are appended to `container`; the returned comment stands in the
|
|
170
|
+
* React tree: events bubble from the content through it, and disposing it
|
|
171
|
+
* removes the content.
|
|
172
|
+
*/
|
|
173
|
+
export declare function createPortal(children: unknown, container: Element | DocumentFragment): Node;
|
|
174
|
+
/**
|
|
175
|
+
* Class component base for React source the compiler did not lower (for
|
|
176
|
+
* example classes created at runtime). Rendering it with createElement/jsx
|
|
177
|
+
* mounts it through the same instance controller as lowered classes
|
|
178
|
+
* (class-component.ts): setState is batched, lifecycles run in React order,
|
|
179
|
+
* and each committed update re-runs render(). Compiled npm packages do not
|
|
180
|
+
* use this: they run on Preact in islands.
|
|
181
|
+
*/
|
|
90
182
|
export declare class ComponentBase {
|
|
91
183
|
props: Record<string, unknown>;
|
|
92
184
|
state: Record<string, unknown>;
|
|
185
|
+
context: unknown;
|
|
93
186
|
constructor(props?: Record<string, unknown>);
|
|
94
|
-
|
|
95
|
-
|
|
187
|
+
/** Replaced by the controller on mount; before that it merges synchronously. */
|
|
188
|
+
setState(update: unknown, _callback?: () => void): void;
|
|
189
|
+
forceUpdate(_callback?: () => void): void;
|
|
190
|
+
render(): unknown;
|
|
191
|
+
}
|
|
192
|
+
export declare class Component extends ComponentBase {
|
|
193
|
+
}
|
|
194
|
+
export declare class PureComponent extends ComponentBase {
|
|
96
195
|
}
|
|
97
196
|
export declare const reactCompat: {
|
|
98
197
|
h: typeof h;
|
|
99
198
|
Fragment: symbol;
|
|
100
199
|
};
|
|
200
|
+
export {};
|