@krate/runtime 0.5.0-alpha.2 → 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 +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 +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/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
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
import { describe, it, expect, vi, afterEach } from 'vitest';
|
|
2
|
+
import { createSignal, onMount, onCleanup } from './signal';
|
|
3
|
+
import { h } from './dom';
|
|
4
|
+
import { krateList, krateBindList, longestIncreasingSubsequence } from './list';
|
|
5
|
+
import { createEffectDeps } from './effects';
|
|
6
|
+
const tick = () => new Promise((r) => setTimeout(r, 0));
|
|
7
|
+
function mountList(node) {
|
|
8
|
+
const root = document.createElement('div');
|
|
9
|
+
document.body.appendChild(root);
|
|
10
|
+
root.appendChild(node);
|
|
11
|
+
return root;
|
|
12
|
+
}
|
|
13
|
+
function rowTexts(root, selector = 'li') {
|
|
14
|
+
return Array.from(root.querySelectorAll(selector)).map((el) => el.textContent || '');
|
|
15
|
+
}
|
|
16
|
+
afterEach(() => {
|
|
17
|
+
document.body.innerHTML = '';
|
|
18
|
+
});
|
|
19
|
+
describe('longestIncreasingSubsequence', () => {
|
|
20
|
+
it('ignores negative entries and returns indices in order', () => {
|
|
21
|
+
expect(longestIncreasingSubsequence([2, 0, 1, -1, 3])).toEqual([1, 2, 4]);
|
|
22
|
+
expect(longestIncreasingSubsequence([3, 2, 1, 0])).toHaveLength(1);
|
|
23
|
+
expect(longestIncreasingSubsequence([])).toEqual([]);
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
describe('keyed lists', () => {
|
|
27
|
+
it('reorders by key keeping DOM nodes, input values and focus', async () => {
|
|
28
|
+
const [items, setItems] = createSignal([
|
|
29
|
+
{ id: 1, label: 'a' },
|
|
30
|
+
{ id: 2, label: 'b' },
|
|
31
|
+
{ id: 3, label: 'c' },
|
|
32
|
+
]);
|
|
33
|
+
const root = mountList(krateList(() => items(), (t) => t.id, (t) => h('li', null, () => t().label, h('input', { class: () => 'in-' + t().id })), 1).mount());
|
|
34
|
+
const inputs = Array.from(root.querySelectorAll('input'));
|
|
35
|
+
inputs[0].value = 'typed-a';
|
|
36
|
+
inputs[2].focus();
|
|
37
|
+
const lis = Array.from(root.querySelectorAll('li'));
|
|
38
|
+
setItems([...items()].reverse());
|
|
39
|
+
await tick();
|
|
40
|
+
expect(rowTexts(root)).toEqual(['c', 'b', 'a']);
|
|
41
|
+
const after = Array.from(root.querySelectorAll('li'));
|
|
42
|
+
expect(after).toEqual([lis[2], lis[1], lis[0]]);
|
|
43
|
+
expect(root.querySelector('.in-1').value).toBe('typed-a');
|
|
44
|
+
expect(document.activeElement).toBe(inputs[2]);
|
|
45
|
+
});
|
|
46
|
+
it('keeps component state per key and updates item values in place', async () => {
|
|
47
|
+
const [items, setItems] = createSignal([
|
|
48
|
+
{ id: 1, label: 'one' },
|
|
49
|
+
{ id: 2, label: 'two' },
|
|
50
|
+
]);
|
|
51
|
+
function Row(props) {
|
|
52
|
+
const [clicks, setClicks] = createSignal(0);
|
|
53
|
+
return h('li', null, h('span', { class: 'label' }, () => props.item.label), h('button', { onClick: () => setClicks(clicks() + 1) }, () => clicks()), h('em', null, () => props.index));
|
|
54
|
+
}
|
|
55
|
+
const root = mountList(krateList(() => items(), (t) => t.id, (t, i) => h(Row, {
|
|
56
|
+
get item() {
|
|
57
|
+
return t();
|
|
58
|
+
},
|
|
59
|
+
get index() {
|
|
60
|
+
return i();
|
|
61
|
+
},
|
|
62
|
+
}), 1).mount());
|
|
63
|
+
root.querySelectorAll('button')[1].click();
|
|
64
|
+
root.querySelectorAll('button')[1].click();
|
|
65
|
+
await tick();
|
|
66
|
+
expect(root.querySelectorAll('button')[1].textContent).toBe('2');
|
|
67
|
+
// New object for key 2 and a move to the front: state stays, label and index update.
|
|
68
|
+
setItems([{ id: 2, label: 'TWO' }, { id: 1, label: 'one' }]);
|
|
69
|
+
await tick();
|
|
70
|
+
expect(rowTexts(root, '.label')).toEqual(['TWO', 'one']);
|
|
71
|
+
expect(root.querySelectorAll('button')[0].textContent).toBe('2');
|
|
72
|
+
expect(rowTexts(root, 'em')).toEqual(['0', '1']);
|
|
73
|
+
});
|
|
74
|
+
it('runs cleanups for removed rows before new rows mount', async () => {
|
|
75
|
+
const log = [];
|
|
76
|
+
const [items, setItems] = createSignal([1, 2, 3]);
|
|
77
|
+
function Row(props) {
|
|
78
|
+
const id = props.id;
|
|
79
|
+
onMount(() => {
|
|
80
|
+
log.push('mount ' + id);
|
|
81
|
+
onCleanup(() => log.push('unmount ' + id));
|
|
82
|
+
});
|
|
83
|
+
return h('li', null, String(id));
|
|
84
|
+
}
|
|
85
|
+
const root = mountList(krateList(() => items(), (n) => n, (n) => h(Row, { get id() { return n(); } }), 1).mount());
|
|
86
|
+
await tick();
|
|
87
|
+
expect(log).toEqual(['mount 1', 'mount 2', 'mount 3']);
|
|
88
|
+
log.length = 0;
|
|
89
|
+
setItems([1, 4, 3]);
|
|
90
|
+
await tick();
|
|
91
|
+
expect(rowTexts(root)).toEqual(['1', '4', '3']);
|
|
92
|
+
expect(log).toEqual(['unmount 2', 'mount 4']);
|
|
93
|
+
});
|
|
94
|
+
it('inserts and removes in the middle with minimal DOM work', async () => {
|
|
95
|
+
const [items, setItems] = createSignal([1, 2, 3, 4]);
|
|
96
|
+
const root = mountList(krateList(() => items(), (n) => n, (n) => h('li', null, () => String(n())), 1).mount());
|
|
97
|
+
const before = Array.from(root.querySelectorAll('li'));
|
|
98
|
+
setItems([1, 2, 9, 3, 4]);
|
|
99
|
+
await tick();
|
|
100
|
+
expect(rowTexts(root)).toEqual(['1', '2', '9', '3', '4']);
|
|
101
|
+
const after = Array.from(root.querySelectorAll('li'));
|
|
102
|
+
expect(after[0]).toBe(before[0]);
|
|
103
|
+
expect(after[3]).toBe(before[2]);
|
|
104
|
+
setItems([1, 9, 4]);
|
|
105
|
+
await tick();
|
|
106
|
+
expect(rowTexts(root)).toEqual(['1', '9', '4']);
|
|
107
|
+
expect(root.querySelectorAll('li')[2]).toBe(before[3]);
|
|
108
|
+
});
|
|
109
|
+
it('moves only rows outside the longest increasing run', async () => {
|
|
110
|
+
const [items, setItems] = createSignal([1, 2, 3, 4, 5]);
|
|
111
|
+
const root = mountList(krateList(() => items(), (n) => n, (n) => h('li', null, String(n())), 1).mount());
|
|
112
|
+
const ul = root;
|
|
113
|
+
const spy = vi.spyOn(ul, 'insertBefore');
|
|
114
|
+
setItems([5, 1, 2, 3, 4]);
|
|
115
|
+
await tick();
|
|
116
|
+
expect(rowTexts(root)).toEqual(['5', '1', '2', '3', '4']);
|
|
117
|
+
// One row moved: its start marker, element and end marker.
|
|
118
|
+
expect(spy.mock.calls.length).toBe(3);
|
|
119
|
+
spy.mockRestore();
|
|
120
|
+
});
|
|
121
|
+
it('warns on duplicate keys and keeps the first occurrence on its row', async () => {
|
|
122
|
+
const err = vi.spyOn(console, 'error').mockImplementation(() => { });
|
|
123
|
+
const [items, setItems] = createSignal([
|
|
124
|
+
{ id: 1, label: 'a' },
|
|
125
|
+
{ id: 1, label: 'b' },
|
|
126
|
+
]);
|
|
127
|
+
const root = mountList(krateList(() => items(), (t) => t.id, (t) => h('li', null, () => t().label), 1).mount());
|
|
128
|
+
expect(rowTexts(root)).toEqual(['a', 'b']);
|
|
129
|
+
expect(err).toHaveBeenCalled();
|
|
130
|
+
expect(String(err.mock.calls[0][0])).toContain('same key');
|
|
131
|
+
const first = root.querySelector('li');
|
|
132
|
+
setItems([{ id: 1, label: 'a2' }, { id: 1, label: 'b2' }, { id: 2, label: 'c' }]);
|
|
133
|
+
await tick();
|
|
134
|
+
expect(rowTexts(root)).toEqual(['a2', 'b2', 'c']);
|
|
135
|
+
expect(root.querySelector('li')).toBe(first);
|
|
136
|
+
err.mockRestore();
|
|
137
|
+
});
|
|
138
|
+
it('compares keys as strings like React', async () => {
|
|
139
|
+
const [items, setItems] = createSignal([{ k: 1 }, { k: 2 }]);
|
|
140
|
+
const root = mountList(krateList(() => items(), (t) => t.k, () => h('li', null, h('input', null)), 1).mount());
|
|
141
|
+
root.querySelectorAll('input')[0].value = 'kept';
|
|
142
|
+
setItems([{ k: '2' }, { k: '1' }]);
|
|
143
|
+
await tick();
|
|
144
|
+
expect(root.querySelectorAll('input')[1].value).toBe('kept');
|
|
145
|
+
});
|
|
146
|
+
it('remounts a keyed single element when its key changes', async () => {
|
|
147
|
+
const log = [];
|
|
148
|
+
const [user, setUser] = createSignal('ada');
|
|
149
|
+
function Profile(props) {
|
|
150
|
+
const [draft, setDraft] = createSignal('');
|
|
151
|
+
const name = props.name;
|
|
152
|
+
onMount(() => {
|
|
153
|
+
log.push('mount ' + name);
|
|
154
|
+
onCleanup(() => log.push('unmount ' + name));
|
|
155
|
+
});
|
|
156
|
+
return h('input', { value: () => draft(), onInput: (e) => setDraft(e.target.value), 'data-user': name });
|
|
157
|
+
}
|
|
158
|
+
const root = mountList(krateList(() => [user()], (k) => k, () => h(Profile, { get name() { return user(); } }), 1).mount());
|
|
159
|
+
await tick();
|
|
160
|
+
const input = root.querySelector('input');
|
|
161
|
+
input.value = 'draft';
|
|
162
|
+
input.dispatchEvent(new Event('input'));
|
|
163
|
+
setUser('ada');
|
|
164
|
+
await tick();
|
|
165
|
+
expect(root.querySelector('input')).toBe(input);
|
|
166
|
+
setUser('grace');
|
|
167
|
+
await tick();
|
|
168
|
+
const next = root.querySelector('input');
|
|
169
|
+
expect(next).not.toBe(input);
|
|
170
|
+
expect(next.value).toBe('');
|
|
171
|
+
expect(next.getAttribute('data-user')).toBe('grace');
|
|
172
|
+
expect(log).toEqual(['mount ada', 'unmount ada', 'mount grace']);
|
|
173
|
+
});
|
|
174
|
+
it('renders keyed fragments, text rows and nested lists', async () => {
|
|
175
|
+
const [groups, setGroups] = createSignal([
|
|
176
|
+
{ id: 'g1', items: ['x', 'y'] },
|
|
177
|
+
{ id: 'g2', items: ['z'] },
|
|
178
|
+
]);
|
|
179
|
+
const root = mountList(krateList(() => groups(), (g) => g.id, (g) => [
|
|
180
|
+
h('dt', null, () => g().id),
|
|
181
|
+
h('dd', null, krateList(() => g().items, null, (s) => () => s(), 1)),
|
|
182
|
+
], 1).mount());
|
|
183
|
+
expect(rowTexts(root, 'dt')).toEqual(['g1', 'g2']);
|
|
184
|
+
expect(rowTexts(root, 'dd')).toEqual(['xy', 'z']);
|
|
185
|
+
const dts = Array.from(root.querySelectorAll('dt'));
|
|
186
|
+
setGroups([{ id: 'g2', items: ['z', 'w'] }, { id: 'g1', items: ['y'] }]);
|
|
187
|
+
await tick();
|
|
188
|
+
expect(rowTexts(root, 'dt')).toEqual(['g2', 'g1']);
|
|
189
|
+
expect(rowTexts(root, 'dd')).toEqual(['zw', 'y']);
|
|
190
|
+
expect(Array.from(root.querySelectorAll('dt'))).toEqual([dts[1], dts[0]]);
|
|
191
|
+
});
|
|
192
|
+
it('switches between empty and non-empty and works inside tables', async () => {
|
|
193
|
+
const [rows, setRows] = createSignal([]);
|
|
194
|
+
const table = document.createElement('table');
|
|
195
|
+
const tbody = document.createElement('tbody');
|
|
196
|
+
table.appendChild(tbody);
|
|
197
|
+
document.body.appendChild(table);
|
|
198
|
+
tbody.appendChild(krateList(() => rows(), (n) => n, (n) => h('tr', null, h('td', null, () => String(n()))), 1).mount());
|
|
199
|
+
expect(tbody.querySelectorAll('tr')).toHaveLength(0);
|
|
200
|
+
setRows([1, 2]);
|
|
201
|
+
await tick();
|
|
202
|
+
expect(rowTexts(tbody, 'td')).toEqual(['1', '2']);
|
|
203
|
+
setRows([]);
|
|
204
|
+
await tick();
|
|
205
|
+
expect(tbody.querySelectorAll('tr')).toHaveLength(0);
|
|
206
|
+
expect(Array.from(tbody.childNodes).every((n) => n.nodeType === 8)).toBe(true);
|
|
207
|
+
});
|
|
208
|
+
it('uses index keys for unkeyed lists (state stays with the position)', async () => {
|
|
209
|
+
const [items, setItems] = createSignal(['a', 'b']);
|
|
210
|
+
const root = mountList(krateList(() => items(), null, (s) => h('li', null, () => s(), h('input', null)), 1).mount());
|
|
211
|
+
root.querySelectorAll('input')[0].value = 'first';
|
|
212
|
+
setItems(['b', 'a']);
|
|
213
|
+
await tick();
|
|
214
|
+
expect(rowTexts(root)).toEqual(['b', 'a']);
|
|
215
|
+
expect(root.querySelectorAll('input')[0].value).toBe('first');
|
|
216
|
+
});
|
|
217
|
+
it('value mode re-renders a row whose item changed', async () => {
|
|
218
|
+
const [items, setItems] = createSignal([{ id: 1, label: 'a' }]);
|
|
219
|
+
const root = mountList(krateList(() => items(), (t) => t.id, (t) => h('li', null, t.label), 0).mount());
|
|
220
|
+
const li = root.querySelector('li');
|
|
221
|
+
setItems([{ id: 1, label: 'b' }]);
|
|
222
|
+
await tick();
|
|
223
|
+
expect(rowTexts(root)).toEqual(['b']);
|
|
224
|
+
expect(root.querySelector('li')).not.toBe(li);
|
|
225
|
+
});
|
|
226
|
+
it('detaches refs of removed rows', async () => {
|
|
227
|
+
const refs = [];
|
|
228
|
+
const [items, setItems] = createSignal([1]);
|
|
229
|
+
mountList(krateList(() => items(), (n) => n, () => h('li', { ref: (el) => refs.push(el) }), 1).mount());
|
|
230
|
+
setItems([]);
|
|
231
|
+
await tick();
|
|
232
|
+
expect(refs).toHaveLength(2);
|
|
233
|
+
expect(refs[1]).toBeNull();
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
describe('React effects in rows', () => {
|
|
237
|
+
it('runs passive effects per key: none on reorder, cleanup on removal, setup for new keys', async () => {
|
|
238
|
+
const log = [];
|
|
239
|
+
const settle = async () => {
|
|
240
|
+
for (let i = 0; i < 3; i++)
|
|
241
|
+
await tick();
|
|
242
|
+
};
|
|
243
|
+
const [items, setItems] = createSignal([1, 2]);
|
|
244
|
+
function Row(props) {
|
|
245
|
+
const id = props.id;
|
|
246
|
+
createEffectDeps(() => {
|
|
247
|
+
log.push('effect ' + id);
|
|
248
|
+
return () => log.push('cleanup ' + id);
|
|
249
|
+
}, () => []);
|
|
250
|
+
return h('li', null, String(id));
|
|
251
|
+
}
|
|
252
|
+
mountList(krateList(() => items(), (n) => n, (n) => h(Row, { get id() { return n(); } }), 1).mount());
|
|
253
|
+
await settle();
|
|
254
|
+
expect(log).toEqual(['effect 1', 'effect 2']);
|
|
255
|
+
log.length = 0;
|
|
256
|
+
setItems([2, 1]);
|
|
257
|
+
await settle();
|
|
258
|
+
expect(log).toEqual([]);
|
|
259
|
+
setItems([2, 3]);
|
|
260
|
+
await settle();
|
|
261
|
+
expect(log).toEqual(['cleanup 1', 'effect 3']);
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
describe('hydration adoption', () => {
|
|
265
|
+
it('adopts server-rendered rows by key without replacing nodes', async () => {
|
|
266
|
+
const root = document.createElement('ul');
|
|
267
|
+
root.innerHTML =
|
|
268
|
+
'<!--k:L--><!--k:L:$1--><li data-id="1">one<button>0</button></li><!--/k:L:$1-->' +
|
|
269
|
+
'<!--k:L:$2--><li data-id="2">two<button>0</button></li><!--/k:L:$2--><!--/k:L-->';
|
|
270
|
+
document.body.appendChild(root);
|
|
271
|
+
const ssr = Array.from(root.querySelectorAll('li'));
|
|
272
|
+
const [items, setItems] = createSignal([
|
|
273
|
+
{ id: 1, label: 'one' },
|
|
274
|
+
{ id: 2, label: 'two' },
|
|
275
|
+
]);
|
|
276
|
+
function Row(props) {
|
|
277
|
+
const [n, setN] = createSignal(0);
|
|
278
|
+
return h('li', { 'data-id': () => props.item.id }, () => props.item.label, h('button', { onClick: () => setN(n() + 1) }, () => n()));
|
|
279
|
+
}
|
|
280
|
+
krateBindList(root.firstChild, () => items(), (t) => t.id, (t) => h(Row, { get item() { return t(); } }), 1);
|
|
281
|
+
expect(Array.from(root.querySelectorAll('li'))).toEqual(ssr);
|
|
282
|
+
root.querySelectorAll('button')[1].click();
|
|
283
|
+
await tick();
|
|
284
|
+
expect(root.querySelectorAll('button')[1].textContent).toBe('1');
|
|
285
|
+
setItems([{ id: 2, label: 'two' }, { id: 1, label: 'uno' }]);
|
|
286
|
+
await tick();
|
|
287
|
+
expect(Array.from(root.querySelectorAll('li'))).toEqual([ssr[1], ssr[0]]);
|
|
288
|
+
expect(rowTexts(root)).toEqual(['two1', 'uno0']);
|
|
289
|
+
});
|
|
290
|
+
it('adopts by position when the build could not compute keys and patches mismatches', async () => {
|
|
291
|
+
const root = document.createElement('ul');
|
|
292
|
+
root.innerHTML = '<!--k:M--><!--k:M:0--><li>stale</li><!--/k:M:0--><!--k:M:1--><li>gone</li><!--/k:M:1--><!--/k:M-->';
|
|
293
|
+
document.body.appendChild(root);
|
|
294
|
+
const first = root.querySelector('li');
|
|
295
|
+
const [items] = createSignal([{ id: 'a', label: 'fresh' }]);
|
|
296
|
+
krateBindList(root.firstChild, () => items(), (t) => t.id, (t) => h('li', null, () => t().label), 1);
|
|
297
|
+
expect(root.querySelectorAll('li')).toHaveLength(1);
|
|
298
|
+
expect(root.querySelector('li')).toBe(first);
|
|
299
|
+
expect(rowTexts(root)).toEqual(['fresh']);
|
|
300
|
+
});
|
|
301
|
+
it('adopts nested server-rendered lists inside rows', () => {
|
|
302
|
+
const root = document.createElement('div');
|
|
303
|
+
root.innerHTML =
|
|
304
|
+
'<!--k:O--><!--k:O:$a--><section><h2>a</h2><ul><!--k:P--><!--k:P:$x--><li>x</li><!--/k:P:$x--><!--/k:P--></ul></section><!--/k:O:$a--><!--/k:O-->';
|
|
305
|
+
document.body.appendChild(root);
|
|
306
|
+
const li = root.querySelector('li');
|
|
307
|
+
const [groups] = createSignal([{ id: 'a', items: ['x'] }]);
|
|
308
|
+
krateBindList(root.firstChild, () => groups(), (g) => g.id, (g) => h('section', null, h('h2', null, () => g().id), h('ul', null, krateList(() => g().items, (s) => s, (s) => h('li', null, () => s()), 1))), 1);
|
|
309
|
+
expect(root.querySelector('li')).toBe(li);
|
|
310
|
+
expect(root.querySelectorAll('li')).toHaveLength(1);
|
|
311
|
+
});
|
|
312
|
+
});
|
|
313
|
+
describe('list performance', () => {
|
|
314
|
+
it('reorders, inserts and removes 1000 rows in roughly linear time', async () => {
|
|
315
|
+
const make = (n) => Array.from({ length: n }, (_, i) => ({ id: i, label: 'row ' + i }));
|
|
316
|
+
const [items, setItems] = createSignal(make(1000));
|
|
317
|
+
const root = mountList(krateList(() => items(), (t) => t.id, (t) => h('li', null, () => t().label), 1).mount());
|
|
318
|
+
expect(root.querySelectorAll('li')).toHaveLength(1000);
|
|
319
|
+
const time = async (next) => {
|
|
320
|
+
const t0 = performance.now();
|
|
321
|
+
setItems(next);
|
|
322
|
+
await tick();
|
|
323
|
+
return performance.now() - t0;
|
|
324
|
+
};
|
|
325
|
+
const reversed = await time([...items()].reverse());
|
|
326
|
+
expect(root.querySelector('li').textContent).toBe('row 999');
|
|
327
|
+
const shuffled = [...items()];
|
|
328
|
+
for (let i = shuffled.length - 1; i > 0; i--) {
|
|
329
|
+
const j = (i * 7919) % (i + 1);
|
|
330
|
+
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
|
|
331
|
+
}
|
|
332
|
+
const shuffle = await time(shuffled);
|
|
333
|
+
const inserted = [...items()];
|
|
334
|
+
inserted.splice(500, 0, { id: 5000, label: 'inserted' });
|
|
335
|
+
const insert = await time(inserted);
|
|
336
|
+
const removed = items().filter((_, i) => i % 2 === 0);
|
|
337
|
+
const remove = await time(removed);
|
|
338
|
+
expect(root.querySelectorAll('li')).toHaveLength(removed.length);
|
|
339
|
+
// Generous ceilings: happy-dom is slow, but a quadratic algorithm over
|
|
340
|
+
// 1000 rows takes far longer than this.
|
|
341
|
+
for (const ms of [reversed, shuffle, insert, remove])
|
|
342
|
+
expect(ms).toBeLessThan(2000);
|
|
343
|
+
});
|
|
344
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import './effects';
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach } from 'vitest';
|
|
2
|
+
import { createSignal, createEffect, disposeAll, krateHookState } from './signal';
|
|
3
|
+
import './effects';
|
|
4
|
+
import { __krateOptimistic, runActionTransition } from './actions';
|
|
5
|
+
const tick = () => new Promise((r) => setTimeout(r, 0));
|
|
6
|
+
async function settle() {
|
|
7
|
+
for (let i = 0; i < 6; i++)
|
|
8
|
+
await tick();
|
|
9
|
+
}
|
|
10
|
+
describe('state written at the end of an async action', () => {
|
|
11
|
+
beforeEach(() => disposeAll());
|
|
12
|
+
// The update cascades through several effects (optimistic state, a list
|
|
13
|
+
// binding, row bindings). The action settles, and its optimistic revert
|
|
14
|
+
// reads the committed state, in a microtask that used to run between two
|
|
15
|
+
// passes of that cascade, before the state was committed.
|
|
16
|
+
for (const hops of [0, 1, 2, 3, 4])
|
|
17
|
+
it('survives an unrelated plain signal write in a later microtask (' + hops + ' hops)', async () => {
|
|
18
|
+
const [messages, setMessages] = createSignal([{ text: 'hi' }], krateHookState);
|
|
19
|
+
const [optimistic, setOptimistic] = createSignal(messages());
|
|
20
|
+
const add = __krateOptimistic(() => messages(), setOptimistic, (s, text) => [...s, { text, sending: true }]);
|
|
21
|
+
const [rows, setRows] = createSignal([]);
|
|
22
|
+
createEffect(() => setRows(optimistic().map((m) => m.text + (m.sending ? '*' : ''))));
|
|
23
|
+
const [label, setLabel] = createSignal('');
|
|
24
|
+
createEffect(() => setLabel(rows().join(',')));
|
|
25
|
+
const shown = [];
|
|
26
|
+
createEffect(() => void shown.push(label()));
|
|
27
|
+
await settle();
|
|
28
|
+
void runActionTransition(async () => {
|
|
29
|
+
add('hello');
|
|
30
|
+
await new Promise((r) => setTimeout(r, 5));
|
|
31
|
+
// A startTransition that writes its own pending signal afterwards.
|
|
32
|
+
const s = createSignal(true);
|
|
33
|
+
setMessages((m) => [...m, { text: 'hello' }]);
|
|
34
|
+
queueMicrotask(() => s[1](false));
|
|
35
|
+
// The action resolves a few microtasks later (the form action wrapper).
|
|
36
|
+
for (let i = 0; i < hops; i++)
|
|
37
|
+
await null;
|
|
38
|
+
});
|
|
39
|
+
await settle();
|
|
40
|
+
await new Promise((r) => setTimeout(r, 20));
|
|
41
|
+
expect(messages().map((m) => m.text)).toEqual(['hi', 'hello']);
|
|
42
|
+
expect(optimistic().map((m) => m.text)).toEqual(['hi', 'hello']);
|
|
43
|
+
expect(shown[shown.length - 1]).toBe('hi,hello');
|
|
44
|
+
});
|
|
45
|
+
});
|
package/dist/portal.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
type Anchor = HTMLTemplateElement & {
|
|
2
|
+
/** Nodes currently mounted in the container. */
|
|
3
|
+
__kpn?: Node[];
|
|
4
|
+
/** Whether dispose tracking is installed. */
|
|
5
|
+
__kpd?: boolean;
|
|
6
|
+
};
|
|
7
|
+
/** Portal content nodes point back to their anchor (React parent). */
|
|
8
|
+
export type KratePortalNode = Node & {
|
|
9
|
+
__kpp?: Node;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Registers the container of portal `id` as an expression evaluated in the
|
|
13
|
+
* component's hydration scope (anything other than a literal selector).
|
|
14
|
+
*/
|
|
15
|
+
export declare function kratePortalTarget(id: string, get: () => unknown): void;
|
|
16
|
+
/** Moves one portal's content into its container. */
|
|
17
|
+
export declare function kratePortalMount(a: Anchor): void;
|
|
18
|
+
/**
|
|
19
|
+
* Takes a portal's content out of its container: back into the anchor (the
|
|
20
|
+
* branch may be shown again) or, with `drop`, discarded.
|
|
21
|
+
*/
|
|
22
|
+
export declare function kratePortalUnmount(a: Anchor, drop?: boolean): void;
|
|
23
|
+
/** Mounts every portal whose anchor is in the document (in tree order). */
|
|
24
|
+
export declare function krateMountPortals(root?: ParentNode): void;
|
|
25
|
+
/** Collects the portal anchors of nodes about to enter the document. */
|
|
26
|
+
export declare function kratePortalsIn(nodes: ParentNode): Anchor[];
|
|
27
|
+
/** Mounts anchors collected by kratePortalsIn once they are in the document. */
|
|
28
|
+
export declare function kratePortalsEnter(anchors: Anchor[]): void;
|
|
29
|
+
/** Unmounts the portals of nodes leaving the document (a hidden branch). */
|
|
30
|
+
export declare function kratePortalsLeave(nodes: ParentNode): void;
|
|
31
|
+
/**
|
|
32
|
+
* Visits the content of every portal template under `roots`, including
|
|
33
|
+
* portals nested in other templates, so hydration can index their markers.
|
|
34
|
+
*/
|
|
35
|
+
export declare function kratePortalScan(roots: ParentNode[], visit: (content: DocumentFragment) => void): void;
|
|
36
|
+
/**
|
|
37
|
+
* Finds the first element matching `selector` in portal content: mounted
|
|
38
|
+
* portals first, then portal templates under `roots` (not yet mounted).
|
|
39
|
+
*/
|
|
40
|
+
export declare function kratePortalFind(selector: string, roots: ParentNode[]): Element | null;
|
|
41
|
+
/** Forgets the portal state of the previous page (SPA navigation). */
|
|
42
|
+
export declare function kratePortalReset(): void;
|
|
43
|
+
export {};
|
package/dist/portal.js
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
// Portals: `createPortal(children, container)` and the built-in `<Portal>`.
|
|
2
|
+
//
|
|
3
|
+
// The compiler emits a portal as an inert anchor in the component's place:
|
|
4
|
+
//
|
|
5
|
+
// <template data-krate-portal="ID" data-krate-portal-target="#modal-root">
|
|
6
|
+
// ...portal children (hydration markers included)...
|
|
7
|
+
// </template>
|
|
8
|
+
//
|
|
9
|
+
// Like React, the content is not part of the server-rendered page; after the
|
|
10
|
+
// page's bindings are installed, krateMountPortals() moves the template's
|
|
11
|
+
// nodes into the container (document.body unless a target selector or a
|
|
12
|
+
// registered resolver says otherwise). The anchor stays in the component's
|
|
13
|
+
// place: React events bubble from portal content through the anchor to the
|
|
14
|
+
// component's ancestors, a hidden conditional branch takes its portals with it
|
|
15
|
+
// (kratePortalsLeave), and showing the branch again mounts them
|
|
16
|
+
// (kratePortalsEnter). Removing the anchor through a binding drops the content.
|
|
17
|
+
import { krateEventRoot } from './events.js';
|
|
18
|
+
import { krateTrackNode } from './dom.js';
|
|
19
|
+
const SELECTOR = 'template[data-krate-portal]';
|
|
20
|
+
const resolvers = new Map();
|
|
21
|
+
const mounted = new Set();
|
|
22
|
+
/**
|
|
23
|
+
* Registers the container of portal `id` as an expression evaluated in the
|
|
24
|
+
* component's hydration scope (anything other than a literal selector).
|
|
25
|
+
*/
|
|
26
|
+
export function kratePortalTarget(id, get) {
|
|
27
|
+
resolvers.set(id, get);
|
|
28
|
+
}
|
|
29
|
+
function isContainer(v) {
|
|
30
|
+
return !!v && typeof v === 'object' && (v.nodeType === 1 || v.nodeType === 11);
|
|
31
|
+
}
|
|
32
|
+
function containerOf(a) {
|
|
33
|
+
const id = a.getAttribute('data-krate-portal') || '';
|
|
34
|
+
const get = resolvers.get(id);
|
|
35
|
+
if (get) {
|
|
36
|
+
let v = null;
|
|
37
|
+
try {
|
|
38
|
+
v = get();
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
v = null;
|
|
42
|
+
}
|
|
43
|
+
return isContainer(v) ? v : null;
|
|
44
|
+
}
|
|
45
|
+
const sel = a.getAttribute('data-krate-portal-target');
|
|
46
|
+
if (!sel || sel === 'body')
|
|
47
|
+
return document.body;
|
|
48
|
+
try {
|
|
49
|
+
return document.querySelector(sel);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/** Moves one portal's content into its container. */
|
|
56
|
+
export function kratePortalMount(a) {
|
|
57
|
+
if (a.__kpn || !a.isConnected)
|
|
58
|
+
return;
|
|
59
|
+
const target = containerOf(a);
|
|
60
|
+
if (!target)
|
|
61
|
+
return;
|
|
62
|
+
const nodes = Array.from(a.content.childNodes);
|
|
63
|
+
const frag = document.createDocumentFragment();
|
|
64
|
+
for (const n of nodes) {
|
|
65
|
+
n.__kpp = a;
|
|
66
|
+
frag.appendChild(n);
|
|
67
|
+
}
|
|
68
|
+
target.appendChild(frag);
|
|
69
|
+
a.__kpn = nodes;
|
|
70
|
+
mounted.add(a);
|
|
71
|
+
// Delegated React events need a listener on containers outside the root.
|
|
72
|
+
const root = document.getElementById('root');
|
|
73
|
+
if (target.nodeType === 1 && !(root && root.contains(target)))
|
|
74
|
+
krateEventRoot(target);
|
|
75
|
+
if (!a.__kpd) {
|
|
76
|
+
a.__kpd = true;
|
|
77
|
+
krateTrackNode(a, () => kratePortalUnmount(a, true));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Takes a portal's content out of its container: back into the anchor (the
|
|
82
|
+
* branch may be shown again) or, with `drop`, discarded.
|
|
83
|
+
*/
|
|
84
|
+
export function kratePortalUnmount(a, drop) {
|
|
85
|
+
const nodes = a.__kpn;
|
|
86
|
+
if (!nodes)
|
|
87
|
+
return;
|
|
88
|
+
a.__kpn = undefined;
|
|
89
|
+
mounted.delete(a);
|
|
90
|
+
for (const n of nodes) {
|
|
91
|
+
if (drop) {
|
|
92
|
+
if (n.parentNode)
|
|
93
|
+
n.parentNode.removeChild(n);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
a.content.appendChild(n);
|
|
97
|
+
}
|
|
98
|
+
n.__kpp = undefined;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function anchorsIn(root) {
|
|
102
|
+
const out = [];
|
|
103
|
+
if (root.matches?.(SELECTOR))
|
|
104
|
+
out.push(root);
|
|
105
|
+
root.querySelectorAll(SELECTOR).forEach((a) => out.push(a));
|
|
106
|
+
return out;
|
|
107
|
+
}
|
|
108
|
+
/** Mounts every portal whose anchor is in the document (in tree order). */
|
|
109
|
+
export function krateMountPortals(root) {
|
|
110
|
+
if (typeof document === 'undefined')
|
|
111
|
+
return;
|
|
112
|
+
for (const a of anchorsIn(root || document))
|
|
113
|
+
kratePortalMount(a);
|
|
114
|
+
}
|
|
115
|
+
/** Collects the portal anchors of nodes about to enter the document. */
|
|
116
|
+
export function kratePortalsIn(nodes) {
|
|
117
|
+
return anchorsIn(nodes);
|
|
118
|
+
}
|
|
119
|
+
/** Mounts anchors collected by kratePortalsIn once they are in the document. */
|
|
120
|
+
export function kratePortalsEnter(anchors) {
|
|
121
|
+
for (const a of anchors)
|
|
122
|
+
kratePortalMount(a);
|
|
123
|
+
}
|
|
124
|
+
/** Unmounts the portals of nodes leaving the document (a hidden branch). */
|
|
125
|
+
export function kratePortalsLeave(nodes) {
|
|
126
|
+
for (const a of anchorsIn(nodes))
|
|
127
|
+
kratePortalUnmount(a);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Visits the content of every portal template under `roots`, including
|
|
131
|
+
* portals nested in other templates, so hydration can index their markers.
|
|
132
|
+
*/
|
|
133
|
+
export function kratePortalScan(roots, visit) {
|
|
134
|
+
const queue = roots.slice();
|
|
135
|
+
const seen = new Set();
|
|
136
|
+
while (queue.length) {
|
|
137
|
+
const r = queue.shift();
|
|
138
|
+
r.querySelectorAll('template').forEach((t) => {
|
|
139
|
+
if (seen.has(t))
|
|
140
|
+
return;
|
|
141
|
+
seen.add(t);
|
|
142
|
+
const tpl = t;
|
|
143
|
+
if (tpl.hasAttribute('data-krate-portal'))
|
|
144
|
+
visit(tpl.content);
|
|
145
|
+
queue.push(tpl.content);
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Finds the first element matching `selector` in portal content: mounted
|
|
151
|
+
* portals first, then portal templates under `roots` (not yet mounted).
|
|
152
|
+
*/
|
|
153
|
+
export function kratePortalFind(selector, roots) {
|
|
154
|
+
for (const a of mounted) {
|
|
155
|
+
for (const n of a.__kpn || []) {
|
|
156
|
+
if (n.nodeType !== 1)
|
|
157
|
+
continue;
|
|
158
|
+
const el = n;
|
|
159
|
+
if (el.matches(selector))
|
|
160
|
+
return el;
|
|
161
|
+
const inner = el.querySelector(selector);
|
|
162
|
+
if (inner)
|
|
163
|
+
return inner;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
let found = null;
|
|
167
|
+
kratePortalScan(roots, (content) => {
|
|
168
|
+
if (!found)
|
|
169
|
+
found = content.querySelector(selector);
|
|
170
|
+
});
|
|
171
|
+
return found;
|
|
172
|
+
}
|
|
173
|
+
/** Forgets the portal state of the previous page (SPA navigation). */
|
|
174
|
+
export function kratePortalReset() {
|
|
175
|
+
for (const a of Array.from(mounted))
|
|
176
|
+
kratePortalUnmount(a, true);
|
|
177
|
+
resolvers.clear();
|
|
178
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|