@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
package/dist/dom.js
CHANGED
|
@@ -1,18 +1,21 @@
|
|
|
1
|
-
import { createEffect } from './signal.js';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
change: 'input',
|
|
9
|
-
};
|
|
1
|
+
import { createEffect, onCleanup } from './signal.js';
|
|
2
|
+
import { krateWithOwner, krateUnmount } from './effects.js';
|
|
3
|
+
import { krateSanitizeAttr } from './slots.js';
|
|
4
|
+
import { krateOn, krateControl, krateSetControl } from './events.js';
|
|
5
|
+
import { krateAttachRef } from './refs.js';
|
|
6
|
+
export { krateOn, krateControl, krateSetControl, krateSynthetic, krateEventRoot, krateEventName, krateMountFields, } from './events.js';
|
|
7
|
+
export { krateAttachRef, krateSetRef, krateRefsEnter, krateRefsLeave, krateRefsFlush, krateRefsHold } from './refs.js';
|
|
10
8
|
// per-node effect tracking
|
|
11
9
|
// Reactive props/children create effects that must be disposed when the node is
|
|
12
10
|
// removed from the DOM (list items, conditional branches, SPA reconciliation).
|
|
13
11
|
// Without this, effects and their closures accumulate for the page's lifetime.
|
|
14
12
|
const effectsByNode = new WeakMap();
|
|
15
|
-
|
|
13
|
+
/** Register a disposer that runs when `node` (or an ancestor) is disposed. */
|
|
14
|
+
export function trackEffect(node, dispose) {
|
|
15
|
+
krateTrackNode(node, dispose);
|
|
16
|
+
}
|
|
17
|
+
/** Registers `dispose` to run when `node` is disposed (removed by a binding). */
|
|
18
|
+
export function krateTrackNode(node, dispose) {
|
|
16
19
|
let arr = effectsByNode.get(node);
|
|
17
20
|
if (!arr) {
|
|
18
21
|
arr = [];
|
|
@@ -20,6 +23,39 @@ function trackEffect(node, dispose) {
|
|
|
20
23
|
}
|
|
21
24
|
arr.push(dispose);
|
|
22
25
|
}
|
|
26
|
+
function trackOwner(value, owner) {
|
|
27
|
+
const unmount = () => krateUnmount(owner);
|
|
28
|
+
if (typeof DocumentFragment !== 'undefined' && value instanceof DocumentFragment) {
|
|
29
|
+
// Fragment children are moved out on insertion; track each of them.
|
|
30
|
+
for (let i = 0; i < value.childNodes.length; i++)
|
|
31
|
+
trackEffect(value.childNodes[i], unmount);
|
|
32
|
+
}
|
|
33
|
+
else if (typeof Node !== 'undefined' && value instanceof Node) {
|
|
34
|
+
trackEffect(value, unmount);
|
|
35
|
+
}
|
|
36
|
+
else if (Array.isArray(value)) {
|
|
37
|
+
for (const v of value)
|
|
38
|
+
trackOwner(v, owner);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Binds the ref returned by `get` to `el` (hydration `ref={...}`). The ref is
|
|
43
|
+
* re-bound (old detached, new attached) when the value read by `get` changes
|
|
44
|
+
* identity, and released when the owning scope or the node is disposed.
|
|
45
|
+
* `assign` handles plain-variable targets.
|
|
46
|
+
*/
|
|
47
|
+
export function krateBindRef(el, get, assign) {
|
|
48
|
+
let first = true;
|
|
49
|
+
const dispose = createEffect(() => {
|
|
50
|
+
// The first binding happens during hydration and is queued until every
|
|
51
|
+
// component has bound its DOM; a later re-bind attaches at once.
|
|
52
|
+
const release = krateAttachRef(el, get ? get() : undefined, assign, first ? 'defer' : undefined);
|
|
53
|
+
first = false;
|
|
54
|
+
onCleanup(release);
|
|
55
|
+
});
|
|
56
|
+
trackEffect(el, dispose);
|
|
57
|
+
return dispose;
|
|
58
|
+
}
|
|
23
59
|
/** Dispose all effects registered for a node and its subtree. */
|
|
24
60
|
export function disposeNode(node) {
|
|
25
61
|
const arr = effectsByNode.get(node);
|
|
@@ -101,62 +137,187 @@ function insertBefore(parent, value, before) {
|
|
|
101
137
|
insertBefore(parent, item, before);
|
|
102
138
|
}
|
|
103
139
|
}
|
|
140
|
+
// Prop names whose DOM attribute differs from the JSX spelling.
|
|
141
|
+
const ATTR_ALIASES = { htmlFor: 'for' };
|
|
142
|
+
// CSS properties (hyphenated, vendor prefix removed) whose numeric values
|
|
143
|
+
// take no px unit; mirrors the compiler's htmlspec table (React's
|
|
144
|
+
// isUnitlessNumber).
|
|
145
|
+
const UNITLESS = new Set([
|
|
146
|
+
'animation-iteration-count', 'aspect-ratio', 'border-image-outset', 'border-image-slice',
|
|
147
|
+
'border-image-width', 'box-flex', 'box-flex-group', 'box-ordinal-group', 'column-count',
|
|
148
|
+
'columns', 'flex', 'flex-grow', 'flex-positive', 'flex-shrink', 'flex-negative', 'flex-order',
|
|
149
|
+
'grid-area', 'grid-row', 'grid-row-end', 'grid-row-span', 'grid-row-start', 'grid-column',
|
|
150
|
+
'grid-column-end', 'grid-column-span', 'grid-column-start', 'font-weight', 'line-clamp',
|
|
151
|
+
'line-height', 'opacity', 'order', 'orphans', 'scale', 'tab-size', 'widows', 'z-index', 'zoom',
|
|
152
|
+
'fill-opacity', 'flood-opacity', 'stop-opacity', 'stroke-dasharray', 'stroke-dashoffset',
|
|
153
|
+
'stroke-miterlimit', 'stroke-opacity', 'stroke-width',
|
|
154
|
+
]);
|
|
155
|
+
function cssName(key) {
|
|
156
|
+
if (key.startsWith('--'))
|
|
157
|
+
return key;
|
|
158
|
+
const name = key.replace(/[A-Z]/g, (c) => '-' + c.toLowerCase());
|
|
159
|
+
return name.startsWith('ms-') ? '-' + name : name;
|
|
160
|
+
}
|
|
161
|
+
const appliedStyles = new WeakMap();
|
|
162
|
+
/**
|
|
163
|
+
* Applies a React style object to el: camelCase keys are hyphenated, numbers
|
|
164
|
+
* get px unless the property is unitless, null/boolean/empty values are
|
|
165
|
+
* skipped, and properties set by the previous object but absent now are
|
|
166
|
+
* removed. Other inline styles are left alone.
|
|
167
|
+
*/
|
|
168
|
+
export function krateApplyStyle(el, style) {
|
|
169
|
+
const decl = el.style;
|
|
170
|
+
if (!decl)
|
|
171
|
+
return;
|
|
172
|
+
const next = new Set();
|
|
173
|
+
for (const key of Object.keys(style)) {
|
|
174
|
+
const v = style[key];
|
|
175
|
+
if (v == null || typeof v === 'boolean' || v === '')
|
|
176
|
+
continue;
|
|
177
|
+
const name = cssName(key);
|
|
178
|
+
const bare = name.replace(/^-(webkit|moz|ms|o)-/, '');
|
|
179
|
+
const text = typeof v === 'number' && v !== 0 && !name.startsWith('--') && !UNITLESS.has(bare) ? v + 'px' : String(v);
|
|
180
|
+
decl.setProperty(name, text);
|
|
181
|
+
next.add(name);
|
|
182
|
+
}
|
|
183
|
+
const prev = appliedStyles.get(el);
|
|
184
|
+
if (prev) {
|
|
185
|
+
for (const name of prev) {
|
|
186
|
+
if (!next.has(name))
|
|
187
|
+
decl.removeProperty(name);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
appliedStyles.set(el, next);
|
|
191
|
+
}
|
|
104
192
|
function setAttr(el, key, value) {
|
|
105
193
|
if (key === 'class' || key === 'className') {
|
|
106
|
-
//
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
el.
|
|
194
|
+
// Guard against false/null/undefined before String() conversion.
|
|
195
|
+
const next = value === false || value === null || value === undefined ? '' : String(value);
|
|
196
|
+
if (el.getAttribute('class') !== next) {
|
|
197
|
+
// className="" renders class="" (as in React); a missing class stays
|
|
198
|
+
// missing.
|
|
199
|
+
if (next === '' && !el.hasAttribute('class') && typeof value !== 'string')
|
|
200
|
+
return;
|
|
201
|
+
el.setAttribute('class', next);
|
|
112
202
|
}
|
|
113
203
|
}
|
|
114
204
|
else if (key === 'style') {
|
|
115
205
|
if (typeof value === 'string') {
|
|
116
|
-
el.
|
|
206
|
+
if (el.getAttribute('style') !== value)
|
|
207
|
+
el.setAttribute('style', value);
|
|
117
208
|
}
|
|
118
209
|
else if (value && typeof value === 'object') {
|
|
119
|
-
|
|
210
|
+
krateApplyStyle(el, value);
|
|
211
|
+
}
|
|
212
|
+
else if (value == null || value === false) {
|
|
213
|
+
el.removeAttribute('style');
|
|
120
214
|
}
|
|
121
215
|
}
|
|
122
216
|
else if (key.startsWith('on') && typeof value === 'function') {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
const raw = key.slice(2).toLowerCase();
|
|
127
|
-
const eventKey = EVENT_ALIASES[raw] || raw;
|
|
128
|
-
const handlerKey = `__krate_${eventKey}`;
|
|
129
|
-
const prevHandler = el[handlerKey];
|
|
130
|
-
if (prevHandler) {
|
|
131
|
-
el.removeEventListener(eventKey, prevHandler);
|
|
132
|
-
}
|
|
133
|
-
el.addEventListener(eventKey, value);
|
|
134
|
-
el[handlerKey] = value;
|
|
135
|
-
}
|
|
136
|
-
else if (key === 'value' || key === 'checked' || key === 'selected') {
|
|
217
|
+
krateOn(el, key, value);
|
|
218
|
+
}
|
|
219
|
+
else if (key === 'value' || key === 'checked' || key === 'selected' || key === 'defaultValue' || key === 'defaultChecked') {
|
|
137
220
|
// Form-control state is a DOM property, not an attribute: setting the
|
|
138
221
|
// attribute after user interaction has no effect.
|
|
139
|
-
|
|
140
|
-
|
|
222
|
+
krateSetControl(el, key, value);
|
|
223
|
+
}
|
|
224
|
+
else if (key === 'ref') {
|
|
225
|
+
// Attached now (the node is about to be inserted), detached when the node
|
|
226
|
+
// is disposed or the rendering effect re-runs. Moving a list row does not
|
|
227
|
+
// dispose it, so its refs stay attached.
|
|
228
|
+
const release = krateAttachRef(el, value, undefined, 'now');
|
|
229
|
+
trackEffect(el, release);
|
|
230
|
+
onCleanup(release);
|
|
231
|
+
}
|
|
232
|
+
else if (key === 'key' || key === 'children' || key === 'dangerouslySetInnerHTML') {
|
|
233
|
+
// key is reconciliation metadata; children are appended separately.
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
const name = ATTR_ALIASES[key] || key;
|
|
237
|
+
// aria-* and data-* stringify booleans (aria-selected={false} renders
|
|
238
|
+
// "false"), as in React.
|
|
239
|
+
const stringly = /^(aria|data)-/.test(name);
|
|
240
|
+
if (value === null || value === undefined || (value === false && !stringly)) {
|
|
241
|
+
if (el.hasAttribute(name))
|
|
242
|
+
el.removeAttribute(name);
|
|
141
243
|
}
|
|
142
244
|
else {
|
|
143
|
-
|
|
245
|
+
const next = value === true && !stringly ? '' : krateSanitizeAttr(name, String(value));
|
|
246
|
+
if (el.getAttribute(name) !== next)
|
|
247
|
+
el.setAttribute(name, next);
|
|
144
248
|
}
|
|
145
249
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
250
|
+
}
|
|
251
|
+
function applyProps(el, props) {
|
|
252
|
+
for (const key in props) {
|
|
253
|
+
const value = props[key];
|
|
254
|
+
if (typeof value === 'function' && (key === 'value' || key === 'checked')) {
|
|
255
|
+
trackEffect(el, krateControl(el, key, value));
|
|
256
|
+
}
|
|
257
|
+
else if (typeof value === 'function' && !key.startsWith('on') && key !== 'ref') {
|
|
258
|
+
trackEffect(el, createEffect(() => setAttr(el, key, value())));
|
|
259
|
+
}
|
|
260
|
+
else {
|
|
261
|
+
setAttr(el, key, value);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
const html = props.dangerouslySetInnerHTML;
|
|
265
|
+
if (html && typeof html === 'object' && html.__html != null) {
|
|
266
|
+
const next = String(html.__html);
|
|
267
|
+
if (el.innerHTML !== next)
|
|
268
|
+
el.innerHTML = next;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
let vnodeDepth = 0;
|
|
272
|
+
/** Evaluate `fn` with h() producing virtual nodes (see KrateVNode). */
|
|
273
|
+
export function withVNodes(fn) {
|
|
274
|
+
const saved = vnodeDepth;
|
|
275
|
+
vnodeDepth = saved + 1;
|
|
276
|
+
try {
|
|
277
|
+
return fn();
|
|
278
|
+
}
|
|
279
|
+
finally {
|
|
280
|
+
vnodeDepth = saved;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
/** Evaluate `fn` with h() producing real DOM, even inside withVNodes. */
|
|
284
|
+
export function withDOM(fn) {
|
|
285
|
+
const saved = vnodeDepth;
|
|
286
|
+
vnodeDepth = 0;
|
|
287
|
+
try {
|
|
288
|
+
return fn();
|
|
151
289
|
}
|
|
152
|
-
|
|
153
|
-
|
|
290
|
+
finally {
|
|
291
|
+
vnodeDepth = saved;
|
|
154
292
|
}
|
|
155
|
-
|
|
156
|
-
|
|
293
|
+
}
|
|
294
|
+
function isVNode(v) {
|
|
295
|
+
return !!v && typeof v === 'object' && v.__kv === 1;
|
|
296
|
+
}
|
|
297
|
+
function isAdoptable(v) {
|
|
298
|
+
return !!v && typeof v === 'object' && v.__ka === 1;
|
|
299
|
+
}
|
|
300
|
+
function buildElement(tag, props, children) {
|
|
301
|
+
const el = document.createElement(tag);
|
|
302
|
+
if (props)
|
|
303
|
+
applyProps(el, props);
|
|
304
|
+
if (!props || !props.dangerouslySetInnerHTML) {
|
|
305
|
+
for (const child of children)
|
|
306
|
+
appendChild(el, child);
|
|
157
307
|
}
|
|
308
|
+
return el;
|
|
309
|
+
}
|
|
310
|
+
function materialize(v) {
|
|
311
|
+
return withDOM(() => buildElement(v.tag, v.props, v.children));
|
|
312
|
+
}
|
|
313
|
+
function isTextLike(v) {
|
|
314
|
+
return v == null || typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean' || typeof v === 'bigint';
|
|
158
315
|
}
|
|
159
|
-
function
|
|
316
|
+
function textOf(v) {
|
|
317
|
+
return v == null || typeof v === 'boolean' ? '' : String(v);
|
|
318
|
+
}
|
|
319
|
+
/** Append any renderable value (node, text, array, reactive function) to parent. */
|
|
320
|
+
export function appendChild(parent, child) {
|
|
160
321
|
if (child == null || child === false || child === true)
|
|
161
322
|
return;
|
|
162
323
|
if (Array.isArray(child)) {
|
|
@@ -165,32 +326,222 @@ function appendChild(parent, child) {
|
|
|
165
326
|
return;
|
|
166
327
|
}
|
|
167
328
|
if (typeof child === 'function') {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
329
|
+
createRegion(parent, child, null);
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
if (isVNode(child)) {
|
|
333
|
+
parent.appendChild(materialize(child));
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
if (isAdoptable(child)) {
|
|
337
|
+
parent.appendChild(child.mount());
|
|
171
338
|
return;
|
|
172
339
|
}
|
|
173
340
|
parent.appendChild(child instanceof Node ? child : document.createTextNode(String(child)));
|
|
174
341
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
342
|
+
/** Move the disposers tracked on one node to another. */
|
|
343
|
+
function retrack(from, to) {
|
|
344
|
+
const arr = effectsByNode.get(from);
|
|
345
|
+
if (!arr)
|
|
346
|
+
return;
|
|
347
|
+
effectsByNode.delete(from);
|
|
348
|
+
for (const d of arr)
|
|
349
|
+
trackEffect(to, d);
|
|
350
|
+
}
|
|
351
|
+
/** Remove (and dispose) every node strictly between start and end. */
|
|
352
|
+
export function clearBetween(start, end) {
|
|
353
|
+
const parent = start.parentNode;
|
|
354
|
+
if (!parent)
|
|
355
|
+
return;
|
|
356
|
+
let node = start.nextSibling;
|
|
357
|
+
while (node && node !== end) {
|
|
358
|
+
const next = node.nextSibling;
|
|
359
|
+
disposeNode(node);
|
|
360
|
+
parent.removeChild(node);
|
|
361
|
+
node = next;
|
|
178
362
|
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
363
|
+
}
|
|
364
|
+
function regionToMarkers(r) {
|
|
365
|
+
if (!r.text)
|
|
366
|
+
return;
|
|
367
|
+
const t = r.text;
|
|
368
|
+
const p = t.parentNode;
|
|
369
|
+
r.start = document.createComment('');
|
|
370
|
+
r.end = document.createComment('');
|
|
371
|
+
p.insertBefore(r.start, t);
|
|
372
|
+
p.insertBefore(r.end, t.nextSibling);
|
|
373
|
+
retrack(t, r.start);
|
|
374
|
+
p.removeChild(t);
|
|
375
|
+
r.text = null;
|
|
376
|
+
}
|
|
377
|
+
function updateRegion(r, v) {
|
|
378
|
+
if (isTextLike(v)) {
|
|
379
|
+
const s = textOf(v);
|
|
380
|
+
if (r.text) {
|
|
381
|
+
if (r.text.data !== s)
|
|
382
|
+
r.text.data = s;
|
|
383
|
+
return;
|
|
188
384
|
}
|
|
385
|
+
clearBetween(r.start, r.end);
|
|
386
|
+
if (s !== '')
|
|
387
|
+
r.end.parentNode.insertBefore(document.createTextNode(s), r.end);
|
|
388
|
+
return;
|
|
189
389
|
}
|
|
190
|
-
|
|
191
|
-
|
|
390
|
+
regionToMarkers(r);
|
|
391
|
+
clearBetween(r.start, r.end);
|
|
392
|
+
const frag = document.createDocumentFragment();
|
|
393
|
+
appendChild(frag, v);
|
|
394
|
+
r.end.parentNode.insertBefore(frag, r.end);
|
|
395
|
+
}
|
|
396
|
+
function createRegion(parent, fn, before) {
|
|
397
|
+
const r = { text: null, start: null, end: null };
|
|
398
|
+
let first = true;
|
|
399
|
+
const dispose = createEffect(() => {
|
|
400
|
+
const v = fn();
|
|
401
|
+
if (!first) {
|
|
402
|
+
updateRegion(r, v);
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
first = false;
|
|
406
|
+
if (isTextLike(v)) {
|
|
407
|
+
r.text = document.createTextNode(textOf(v));
|
|
408
|
+
parent.insertBefore(r.text, before);
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
r.start = document.createComment('');
|
|
412
|
+
r.end = document.createComment('');
|
|
413
|
+
parent.insertBefore(r.start, before);
|
|
414
|
+
const frag = document.createDocumentFragment();
|
|
415
|
+
appendChild(frag, v);
|
|
416
|
+
parent.insertBefore(frag, before);
|
|
417
|
+
parent.insertBefore(r.end, before);
|
|
418
|
+
});
|
|
419
|
+
trackEffect((r.text || r.start), dispose);
|
|
420
|
+
}
|
|
421
|
+
// Adoption (hydration without re-rendering)
|
|
422
|
+
// adoptValue walks a rendered value (virtual nodes, text, arrays, regions,
|
|
423
|
+
// lists) against the server-rendered DOM at the cursor, binding props and
|
|
424
|
+
// effects to the existing nodes. A node that does not match is replaced by a
|
|
425
|
+
// freshly built one, so a mismatch degrades to a local client render.
|
|
426
|
+
function skipComments(c) {
|
|
427
|
+
while (c.next && c.next.nodeType === 8)
|
|
428
|
+
c.next = c.next.nextSibling;
|
|
429
|
+
}
|
|
430
|
+
/** Insert a node at the cursor (before the next unclaimed node). */
|
|
431
|
+
export function insertAt(c, node) {
|
|
432
|
+
c.parent.insertBefore(node, c.next);
|
|
433
|
+
}
|
|
434
|
+
function claimText(c, s) {
|
|
435
|
+
skipComments(c);
|
|
436
|
+
const n = c.next;
|
|
437
|
+
if (s !== '' && n && n.nodeType === 3) {
|
|
438
|
+
const t = n;
|
|
439
|
+
if (t.data.length > s.length && t.data.startsWith(s))
|
|
440
|
+
t.splitText(s.length);
|
|
441
|
+
else if (t.data !== s)
|
|
442
|
+
t.data = s;
|
|
443
|
+
c.next = t.nextSibling;
|
|
444
|
+
return t;
|
|
192
445
|
}
|
|
193
|
-
|
|
446
|
+
const t = document.createTextNode(s);
|
|
447
|
+
insertAt(c, t);
|
|
448
|
+
return t;
|
|
449
|
+
}
|
|
450
|
+
/** Remove (and dispose) the nodes from the cursor up to `end` (or the end). */
|
|
451
|
+
export function removeRest(c, end) {
|
|
452
|
+
let n = c.next;
|
|
453
|
+
while (n && n !== end) {
|
|
454
|
+
const next = n.nextSibling;
|
|
455
|
+
disposeNode(n);
|
|
456
|
+
c.parent.removeChild(n);
|
|
457
|
+
n = next;
|
|
458
|
+
}
|
|
459
|
+
c.next = end;
|
|
460
|
+
}
|
|
461
|
+
function adoptElement(v, c) {
|
|
462
|
+
skipComments(c);
|
|
463
|
+
const n = c.next;
|
|
464
|
+
if (n && n.nodeType === 1 && n.localName.toLowerCase() === v.tag.toLowerCase()) {
|
|
465
|
+
c.next = n.nextSibling;
|
|
466
|
+
const el = n;
|
|
467
|
+
if (v.props)
|
|
468
|
+
applyProps(el, v.props);
|
|
469
|
+
if (!v.props || !v.props.dangerouslySetInnerHTML) {
|
|
470
|
+
const inner = { parent: el, next: el.firstChild };
|
|
471
|
+
for (const child of v.children)
|
|
472
|
+
adoptValue(child, inner);
|
|
473
|
+
removeRest(inner, null);
|
|
474
|
+
}
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
insertAt(c, materialize(v));
|
|
478
|
+
}
|
|
479
|
+
function adoptRegion(fn, c) {
|
|
480
|
+
const r = { text: null, start: null, end: null };
|
|
481
|
+
let first = true;
|
|
482
|
+
const dispose = createEffect(() => {
|
|
483
|
+
if (!first) {
|
|
484
|
+
updateRegion(r, fn());
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
first = false;
|
|
488
|
+
const v = withVNodes(fn);
|
|
489
|
+
if (isTextLike(v)) {
|
|
490
|
+
r.text = claimText(c, textOf(v));
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
r.start = document.createComment('');
|
|
494
|
+
insertAt(c, r.start);
|
|
495
|
+
adoptValue(v, c);
|
|
496
|
+
r.end = document.createComment('');
|
|
497
|
+
insertAt(c, r.end);
|
|
498
|
+
});
|
|
499
|
+
trackEffect((r.text || r.start), dispose);
|
|
500
|
+
}
|
|
501
|
+
/** Adopt server-rendered DOM at the cursor for a rendered value. */
|
|
502
|
+
export function adoptValue(v, c) {
|
|
503
|
+
if (v == null || typeof v === 'boolean')
|
|
504
|
+
return;
|
|
505
|
+
if (Array.isArray(v)) {
|
|
506
|
+
for (const x of v)
|
|
507
|
+
adoptValue(x, c);
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
if (typeof v === 'function') {
|
|
511
|
+
adoptRegion(v, c);
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
if (isVNode(v)) {
|
|
515
|
+
adoptElement(v, c);
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
if (isAdoptable(v)) {
|
|
519
|
+
v.adopt(c);
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
if (v instanceof Node) {
|
|
523
|
+
insertAt(c, v);
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
claimText(c, String(v));
|
|
527
|
+
}
|
|
528
|
+
export function h(tag, props, ...children) {
|
|
529
|
+
if (typeof tag === 'function') {
|
|
530
|
+
// Keep the props object (not a spread copy) so getter props stay live: a
|
|
531
|
+
// list row passes `get item(){...}` accessors that read row signals. Each
|
|
532
|
+
// component call gets an effect owner, so its React effects run
|
|
533
|
+
// children-before-parents and are cleaned up when its nodes are disposed.
|
|
534
|
+
const p = props || {};
|
|
535
|
+
if (children.length > 0 || !('children' in p))
|
|
536
|
+
p.children = children;
|
|
537
|
+
const { value, owner } = krateWithOwner(() => tag(p));
|
|
538
|
+
trackOwner(value, owner);
|
|
539
|
+
return value;
|
|
540
|
+
}
|
|
541
|
+
if (vnodeDepth > 0) {
|
|
542
|
+
return { __kv: 1, tag, props, children };
|
|
543
|
+
}
|
|
544
|
+
return buildElement(tag, props, children);
|
|
194
545
|
}
|
|
195
546
|
function getContainer(container) {
|
|
196
547
|
if (typeof container === 'string') {
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/** Effect phase: React's useEffect, useLayoutEffect and useInsertionEffect. */
|
|
2
|
+
export type EffectPhase = 'passive' | 'layout' | 'insertion';
|
|
3
|
+
/**
|
|
4
|
+
* Registers fn to run whenever a component owner unmounts (parent first),
|
|
5
|
+
* after its effect cleanups and before its bindings are disposed. Lets other
|
|
6
|
+
* lowerings (class component lifecycles) observe unmount and remount.
|
|
7
|
+
*/
|
|
8
|
+
export declare function krateOnUnmount(fn: (owner: unknown, id: string | null) => void): void;
|
|
9
|
+
/** The id of a component owner (null for runtime-created components). */
|
|
10
|
+
export declare function krateOwnerId(owner: unknown): string | null;
|
|
11
|
+
/**
|
|
12
|
+
* Starts a hydrated component scope (emitted at the top of each component's
|
|
13
|
+
* hydration script). `parentId` names the enclosing component, `conds` the
|
|
14
|
+
* conditional branches `[slotId, "c" | "a", initiallyShown]` it renders
|
|
15
|
+
* inside, and `slot` the content slot (list) whose client-side replacement
|
|
16
|
+
* unmounts it. `krateOwner(null)` ends hydration scopes.
|
|
17
|
+
*/
|
|
18
|
+
export declare function krateOwner(id: string | null, parentId?: string | null, conds?: Array<[string, string, number | boolean]>, slot?: string | null): void;
|
|
19
|
+
/** Runs fn as the body of a new child component of the current owner. */
|
|
20
|
+
export declare function krateWithOwner<T>(fn: () => T): {
|
|
21
|
+
value: T;
|
|
22
|
+
owner: unknown;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Unmounts a component created by krateWithOwner/krateOwner: cleanups run
|
|
26
|
+
* parent before child (insertion and layout now, passive in the passive
|
|
27
|
+
* phase) and its hooks never run again.
|
|
28
|
+
*/
|
|
29
|
+
export declare function krateUnmount(value: unknown): void;
|
|
30
|
+
/**
|
|
31
|
+
* Unmounts the hydrated component `id` and forgets it, so running its
|
|
32
|
+
* hydration scope again (a remount by the class/boundary owner tree in
|
|
33
|
+
* boundary.ts) creates a fresh owner whose effects mount again.
|
|
34
|
+
*/
|
|
35
|
+
export declare function krateForgetOwner(id: string): void;
|
|
36
|
+
/**
|
|
37
|
+
* Declares a remountable hydrated component (emitted for components inside
|
|
38
|
+
* a conditional branch). `fn` is the component's hydration scope; it runs now
|
|
39
|
+
* when the branch is shown, and again, as a fresh instance, every time the
|
|
40
|
+
* branch is shown after being hidden. Hiding the branch unmounts it:
|
|
41
|
+
* cleanups run, refs and bindings are released and its state is discarded.
|
|
42
|
+
*/
|
|
43
|
+
export declare function krateMount(id: string, parentId: string | null, conds: Array<[string, string, number | boolean]> | null, slot: string | null, fn: () => void): void;
|
|
44
|
+
/**
|
|
45
|
+
* Reports that the hydrated content of slot `id` was replaced by its binding
|
|
46
|
+
* (a list re-render): hydrated components inside it are unmounted.
|
|
47
|
+
*/
|
|
48
|
+
export declare function krateSlotReplaced(id: string): void;
|
|
49
|
+
/**
|
|
50
|
+
* Reports which branch ("c" consequent, "a" alternate) conditional slot `id`
|
|
51
|
+
* shows. Components in the hidden branch keep their state but have their
|
|
52
|
+
* effects cleaned up (React's Activity semantics); showing the branch again
|
|
53
|
+
* re-runs every effect setup in the next commit.
|
|
54
|
+
*/
|
|
55
|
+
export declare function krateCondActivity(id: string, branch: string): void;
|
|
56
|
+
/**
|
|
57
|
+
* Registers a React effect. `deps` returns the dependency array (read inside
|
|
58
|
+
* a tracking scope, so signal getters it calls are subscribed); the effect
|
|
59
|
+
* re-runs in the next commit when any element changed by Object.is. A null
|
|
60
|
+
* thunk means "no dependency array" and the effect runs only on mount here;
|
|
61
|
+
* the compiler synthesizes the component's state and props as dependencies
|
|
62
|
+
* for that case instead. `fn` may return a cleanup function.
|
|
63
|
+
*/
|
|
64
|
+
export declare function createEffectDeps(fn: () => unknown, deps?: (() => unknown) | null, phase?: EffectPhase): void;
|
|
65
|
+
/** Installs the handler for errors thrown by hook effects (internal). */
|
|
66
|
+
export declare function krateOnHookError(fn: ((err: unknown, ownerId: string | null) => boolean) | null): void;
|
|
67
|
+
/** Registers fn to run in the layout phase of every commit (internal). */
|
|
68
|
+
export declare function krateOnLayoutCommit(fn: () => void): void;
|
|
69
|
+
/** Runs pending passive effect cleanups and setups now. */
|
|
70
|
+
export declare function flushPassive(): void;
|
|
71
|
+
/**
|
|
72
|
+
* React's useMemo with a dependency array: the getter recomputes
|
|
73
|
+
* synchronously, on read, when the dependency array changed (Object.is), so
|
|
74
|
+
* the value is current within the same update. Signals read by `fn` are not
|
|
75
|
+
* tracked; only the dependencies are.
|
|
76
|
+
*/
|
|
77
|
+
export declare function createMemoDeps<T>(fn: () => T, deps: () => unknown): () => T;
|
|
78
|
+
/**
|
|
79
|
+
* React's useCallback with a dependency array: a getter for the callback
|
|
80
|
+
* whose identity only changes when the dependency array changes.
|
|
81
|
+
*/
|
|
82
|
+
export declare function createCallbackDeps<T>(fn: T, deps: () => unknown): () => T;
|
|
83
|
+
/**
|
|
84
|
+
* Dependency values of a component parameter: every own property of a props
|
|
85
|
+
* object (read live, so prop getters subscribe) except children, or the
|
|
86
|
+
* value itself for anything else (a custom hook argument).
|
|
87
|
+
*/
|
|
88
|
+
export declare function krateDepsOf(value: unknown): unknown[];
|