@krate/runtime 0.5.0-beta.3 → 0.5.0-rc.2
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/actions.js +2 -5
- package/dist/base-path.d.ts +7 -0
- package/dist/base-path.js +19 -0
- package/dist/base-path.test.d.ts +1 -0
- package/dist/base-path.test.js +88 -0
- package/dist/dev-overlay.js +31 -19
- package/dist/dev-overlay.test.js +14 -1
- package/dist/dom.d.ts +4 -0
- package/dist/dom.js +59 -15
- package/dist/dom.test.js +8 -0
- package/dist/effect-errors.test.js +37 -1
- package/dist/effects.js +41 -8
- package/dist/events.d.ts +2 -0
- package/dist/events.js +18 -0
- package/dist/index.d.ts +5 -0
- package/dist/krate-core.js +1 -1
- package/dist/krate-dev.js +27 -19
- package/dist/krate-f-actions.js +1 -1
- package/dist/krate-f-list.js +1 -1
- package/dist/krate-f-router-lite.js +1 -1
- package/dist/krate-f-router.js +1 -1
- package/dist/krate-features.json +1 -0
- package/dist/krate-hydrate-lite.js +5 -5
- package/dist/krate-hydrate.js +5 -5
- package/dist/krate-runtime.js +5 -5
- package/dist/krate-suspense.js +1 -1
- package/dist/list.js +26 -18
- package/dist/list.test.js +91 -0
- package/dist/metadata.js +3 -0
- package/dist/patterns.test.js +27 -0
- package/dist/react-effects.test.js +37 -0
- package/dist/router-lite.js +3 -9
- package/dist/router-nav.test.js +95 -0
- package/dist/router.js +59 -19
- package/dist/server-handler.d.ts +1 -0
- package/dist/server-handler.js +39 -5
- package/dist/server-handler.test.js +46 -0
- package/dist/server.js +2 -1
- package/dist/signal.js +47 -14
- package/dist/slots.test.js +18 -0
- package/dist/suspense.js +34 -2
- package/dist/suspense.test.js +39 -1
- package/package.json +1 -1
package/dist/actions.js
CHANGED
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
* "revalidated":true?,"revalidation":{"paths":[...],"tags":[...]}?}
|
|
22
22
|
* {"ok":false,"error":{"message":M,"digest":D}}
|
|
23
23
|
*/
|
|
24
|
+
import { basePath, withBase } from './base-path.js';
|
|
24
25
|
import { createEffect, onCleanup, krateRunEvent, krateTransitionWrites } from './signal.js';
|
|
25
26
|
/** Field and query parameter carrying the JSON leading arguments of a form call. */
|
|
26
27
|
export const ACTION_ARGS_FIELD = '__krate_args';
|
|
@@ -34,10 +35,6 @@ export class ServerActionError extends Error {
|
|
|
34
35
|
this.digest = digest;
|
|
35
36
|
}
|
|
36
37
|
}
|
|
37
|
-
function basePath() {
|
|
38
|
-
const cfg = globalThis.__KRATE_CFG__;
|
|
39
|
-
return cfg && cfg.basePath ? cfg.basePath : '';
|
|
40
|
-
}
|
|
41
38
|
/** True for a server action proxy created by `__krateAction`. */
|
|
42
39
|
export function isServerAction(value) {
|
|
43
40
|
return typeof value === 'function' && typeof value.$$krateAction === 'string';
|
|
@@ -53,7 +50,7 @@ function navigateTo(url) {
|
|
|
53
50
|
if (typeof nav === 'function')
|
|
54
51
|
nav(safe);
|
|
55
52
|
else if (typeof location !== 'undefined')
|
|
56
|
-
location.assign(safe);
|
|
53
|
+
location.assign(withBase(safe));
|
|
57
54
|
}
|
|
58
55
|
/** Only same-origin absolute paths are followed after an action. */
|
|
59
56
|
function safeRedirect(url) {
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** The configured base path as "" or "/prefix" (no trailing slash). */
|
|
2
|
+
export declare function basePath(): string;
|
|
3
|
+
/**
|
|
4
|
+
* Prefixes a root-relative URL with the base path. Relative URLs, fragments,
|
|
5
|
+
* queries, absolute URLs and protocol-relative URLs are returned unchanged.
|
|
6
|
+
*/
|
|
7
|
+
export declare function withBase(url: string): string;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// The site base path, shared by the router, actions and link rendering. The
|
|
2
|
+
// compiler sets `__KRATE_CFG__` in the page head; server bundles set it on
|
|
3
|
+
// globalThis so server-rendered links get the same prefix.
|
|
4
|
+
/** The configured base path as "" or "/prefix" (no trailing slash). */
|
|
5
|
+
export function basePath() {
|
|
6
|
+
const cfg = globalThis.__KRATE_CFG__;
|
|
7
|
+
const bp = (cfg && cfg.basePath) || '';
|
|
8
|
+
if (!bp || bp === '/')
|
|
9
|
+
return '';
|
|
10
|
+
return '/' + bp.replace(/^\/+|\/+$/g, '');
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Prefixes a root-relative URL with the base path. Relative URLs, fragments,
|
|
14
|
+
* queries, absolute URLs and protocol-relative URLs are returned unchanged.
|
|
15
|
+
*/
|
|
16
|
+
export function withBase(url) {
|
|
17
|
+
const bp = basePath();
|
|
18
|
+
return bp && url.charCodeAt(0) === 47 && url.charCodeAt(1) !== 47 ? bp + url : url;
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { describe, it, expect, afterEach, vi } from 'vitest';
|
|
2
|
+
import { basePath, withBase } from './base-path';
|
|
3
|
+
import { h } from './dom';
|
|
4
|
+
import { Link, renderToString, jsx } from './server';
|
|
5
|
+
import { initRouter, destroyRouter, navigate, krateRouter } from './router';
|
|
6
|
+
function setBase(bp) {
|
|
7
|
+
globalThis.__KRATE_CFG__ = bp === undefined ? undefined : { basePath: bp, viewTransitions: false };
|
|
8
|
+
}
|
|
9
|
+
describe('base path helpers', () => {
|
|
10
|
+
afterEach(() => setBase(undefined));
|
|
11
|
+
it('normalizes the configured value', () => {
|
|
12
|
+
setBase(undefined);
|
|
13
|
+
expect(basePath()).toBe('');
|
|
14
|
+
setBase('/');
|
|
15
|
+
expect(basePath()).toBe('');
|
|
16
|
+
setBase('repo/');
|
|
17
|
+
expect(basePath()).toBe('/repo');
|
|
18
|
+
});
|
|
19
|
+
it('prefixes only root-relative URLs', () => {
|
|
20
|
+
setBase('/repo');
|
|
21
|
+
expect(withBase('/a?x=1#y')).toBe('/repo/a?x=1#y');
|
|
22
|
+
for (const u of ['//cdn.test/a', 'https://e.test/a', 'rel', '#top', '?q=1', '', 'mailto:a@b.c']) {
|
|
23
|
+
expect(withBase(u)).toBe(u);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
it('leaves URLs alone at the root', () => {
|
|
27
|
+
setBase('');
|
|
28
|
+
expect(withBase('/a')).toBe('/a');
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
describe('<Link> rendering under a base path', () => {
|
|
32
|
+
afterEach(() => setBase(undefined));
|
|
33
|
+
it('prefixes a client-created link', () => {
|
|
34
|
+
setBase('/repo');
|
|
35
|
+
const a = h('a', { href: '/about', 'data-krate-link': true });
|
|
36
|
+
expect(a.getAttribute('href')).toBe('/repo/about');
|
|
37
|
+
});
|
|
38
|
+
it('prefixes a reactive href each time', () => {
|
|
39
|
+
setBase('/repo');
|
|
40
|
+
const a = h('a', { href: () => '/x', 'data-krate-link': true });
|
|
41
|
+
expect(a.getAttribute('href')).toBe('/repo/x');
|
|
42
|
+
});
|
|
43
|
+
it('leaves a plain anchor as written', () => {
|
|
44
|
+
setBase('/repo');
|
|
45
|
+
const a = h('a', { href: '/about' });
|
|
46
|
+
expect(a.getAttribute('href')).toBe('/about');
|
|
47
|
+
});
|
|
48
|
+
it('prefixes a server-rendered link and leaves external ones', async () => {
|
|
49
|
+
setBase('/repo');
|
|
50
|
+
expect(await renderToString(jsx(Link, { href: '/about', children: 'a' }))).toContain('href="/repo/about"');
|
|
51
|
+
expect(await renderToString(jsx(Link, { href: 'https://e.test/', children: 'a' }))).toContain('href="https://e.test/"');
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
describe('navigation under a base path', () => {
|
|
55
|
+
afterEach(() => {
|
|
56
|
+
destroyRouter();
|
|
57
|
+
setBase(undefined);
|
|
58
|
+
vi.unstubAllGlobals();
|
|
59
|
+
});
|
|
60
|
+
it('prefixes a navigation that falls back to a document load', () => {
|
|
61
|
+
destroyRouter();
|
|
62
|
+
setBase('/repo');
|
|
63
|
+
const assign = vi.fn();
|
|
64
|
+
const replace = vi.fn();
|
|
65
|
+
vi.stubGlobal('location', { assign, replace, href: 'http://x.test/repo/' });
|
|
66
|
+
expect(navigate('/about')).toBe(true);
|
|
67
|
+
expect(assign).toHaveBeenCalledWith('/repo/about');
|
|
68
|
+
navigate('/about', { replace: true });
|
|
69
|
+
expect(replace).toHaveBeenCalledWith('/repo/about');
|
|
70
|
+
void krateRouter.push('/a');
|
|
71
|
+
expect(assign).toHaveBeenCalledWith('/repo/a');
|
|
72
|
+
navigate('https://e.test/');
|
|
73
|
+
expect(assign).toHaveBeenCalledWith('https://e.test/');
|
|
74
|
+
});
|
|
75
|
+
it('scopes the speculation rules to the base path', () => {
|
|
76
|
+
HTMLScriptElement.supports = (t) => t === 'speculationrules';
|
|
77
|
+
setBase('/repo');
|
|
78
|
+
document.head.innerHTML = '<title>x</title>';
|
|
79
|
+
document.body.innerHTML = '<div id="root"></div>';
|
|
80
|
+
initRouter();
|
|
81
|
+
const rules = JSON.parse(document.querySelector('script[type="speculationrules"]').textContent);
|
|
82
|
+
expect(rules.prefetch[0].where.and).toEqual([
|
|
83
|
+
{ href_matches: '/repo/*' },
|
|
84
|
+
{ not: { href_matches: '/repo/__krate/*' } },
|
|
85
|
+
{ not: { href_matches: '/repo/api/*' } },
|
|
86
|
+
]);
|
|
87
|
+
});
|
|
88
|
+
});
|
package/dist/dev-overlay.js
CHANGED
|
@@ -522,26 +522,38 @@ class KrateDev {
|
|
|
522
522
|
}
|
|
523
523
|
}
|
|
524
524
|
captureRuntimeErrors() {
|
|
525
|
-
window.addEventListener('error', (e) =>
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
525
|
+
window.addEventListener('error', (e) => this.onError(e));
|
|
526
|
+
window.addEventListener('unhandledrejection', (e) => this.onRejection(e));
|
|
527
|
+
// This bundle loads after the page script, so the page's first render ran
|
|
528
|
+
// before these listeners existed. The head buffers what happened until now.
|
|
529
|
+
const early = window.__KRATE_EARLY_ERRORS__;
|
|
530
|
+
window.__KRATE_EARLY_ERRORS__ = undefined;
|
|
531
|
+
for (const e of early ?? []) {
|
|
532
|
+
if (e.type === 'error')
|
|
533
|
+
this.onError(e);
|
|
534
|
+
else
|
|
535
|
+
this.onRejection(e);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
onError(e) {
|
|
539
|
+
const stack = e.error?.stack || '';
|
|
540
|
+
const message = e.message || 'Unknown error';
|
|
541
|
+
this.report({
|
|
542
|
+
message,
|
|
543
|
+
stack,
|
|
544
|
+
url: e.filename,
|
|
545
|
+
line: e.lineno,
|
|
546
|
+
col: e.colno,
|
|
547
|
+
kind: 'error',
|
|
544
548
|
});
|
|
549
|
+
this.addRuntime('Runtime Error', message, stack);
|
|
550
|
+
}
|
|
551
|
+
onRejection(e) {
|
|
552
|
+
const reason = e.reason;
|
|
553
|
+
const message = reason instanceof Error ? reason.message : String(reason);
|
|
554
|
+
const stack = reason instanceof Error ? reason.stack : '';
|
|
555
|
+
this.report({ message, stack, url: location.href, kind: 'unhandledrejection' });
|
|
556
|
+
this.addRuntime('Unhandled Promise Rejection', message, stack);
|
|
545
557
|
}
|
|
546
558
|
report(payload) {
|
|
547
559
|
const key = payload.message + '|' + (payload.stack || '');
|
package/dist/dev-overlay.test.js
CHANGED
|
@@ -16,12 +16,20 @@ class FakeEventSource {
|
|
|
16
16
|
}
|
|
17
17
|
FakeEventSource.last = null;
|
|
18
18
|
let overlay;
|
|
19
|
+
const beacons = [];
|
|
19
20
|
// File-level setup: globals must exist BEFORE the overlay module auto-inits on
|
|
20
21
|
// import, and the export is captured from the same module instance.
|
|
21
22
|
beforeAll(async () => {
|
|
22
23
|
globalThis.EventSource = FakeEventSource;
|
|
23
24
|
// Error reports must not reach the (absent) dev server from the test.
|
|
24
|
-
navigator.sendBeacon = () =>
|
|
25
|
+
navigator.sendBeacon = (_url, body) => {
|
|
26
|
+
beacons.push(body);
|
|
27
|
+
return true;
|
|
28
|
+
};
|
|
29
|
+
// What the inline head script buffers before this bundle loads.
|
|
30
|
+
window.__KRATE_EARLY_ERRORS__ = [
|
|
31
|
+
new ErrorEvent('error', { message: 'thrown during first render', error: new Error('thrown during first render') }),
|
|
32
|
+
];
|
|
25
33
|
window.__KRATE_DEV__ = {
|
|
26
34
|
sse: '/__krate/hotreload',
|
|
27
35
|
errors: '/__krate/client-error',
|
|
@@ -36,6 +44,11 @@ function devRoot() {
|
|
|
36
44
|
return host ? host.shadowRoot : null;
|
|
37
45
|
}
|
|
38
46
|
describe('dev-overlay', () => {
|
|
47
|
+
it('reports errors thrown before the bundle loaded', async () => {
|
|
48
|
+
const sent = await Promise.all(beacons.map((b) => b.text()));
|
|
49
|
+
expect(sent.some((t) => t.includes('thrown during first render'))).toBe(true);
|
|
50
|
+
expect(window.__KRATE_EARLY_ERRORS__).toBeUndefined();
|
|
51
|
+
});
|
|
39
52
|
it('mounts a shadow-root host with a toolbar', () => {
|
|
40
53
|
const shadow = devRoot();
|
|
41
54
|
expect(shadow).toBeTruthy();
|
package/dist/dom.d.ts
CHANGED
|
@@ -68,6 +68,10 @@ export interface KrateVNode {
|
|
|
68
68
|
tag: string;
|
|
69
69
|
props: Record<string, unknown> | null;
|
|
70
70
|
children: unknown[];
|
|
71
|
+
/** Effect owner current when h() ran: the component the element belongs to. */
|
|
72
|
+
o?: unknown;
|
|
73
|
+
/** Components this element is the root of; unmounted with the element. */
|
|
74
|
+
u?: unknown[];
|
|
71
75
|
}
|
|
72
76
|
/** A value that knows how to mount itself or adopt server-rendered DOM. */
|
|
73
77
|
export interface KrateAdoptable {
|
package/dist/dom.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { createEffect, onCleanup } from './signal.js';
|
|
1
|
+
import { createEffect, onCleanup, krateGetOwner, krateSetOwner } from './signal.js';
|
|
2
2
|
import { krateWithOwner, krateUnmount } from './effects.js';
|
|
3
3
|
import { krateSanitizeAttr } from './slots.js';
|
|
4
4
|
import { krateOn, krateControl, krateSetControl } from './events.js';
|
|
5
5
|
import { krateAttachRef } from './refs.js';
|
|
6
|
+
import { withBase } from './base-path.js';
|
|
6
7
|
export { krateOn, krateControl, krateSetControl, krateSynthetic, krateEventRoot, krateEventName, krateMountFields, } from './events.js';
|
|
7
8
|
export { krateAttachRef, krateSetRef, krateRefsEnter, krateRefsLeave, krateRefsFlush, krateRefsHold } from './refs.js';
|
|
8
9
|
// per-node effect tracking
|
|
@@ -37,6 +38,16 @@ function trackOwner(value, owner) {
|
|
|
37
38
|
for (const v of value)
|
|
38
39
|
trackOwner(v, owner);
|
|
39
40
|
}
|
|
41
|
+
else if (isVNode(value)) {
|
|
42
|
+
// Not mounted yet: the unmount is registered on the element it becomes.
|
|
43
|
+
(value.u || (value.u = [])).push(owner);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
// Registers the unmounts of the components a vnode is the root of.
|
|
47
|
+
function trackVNodeOwners(v, el) {
|
|
48
|
+
if (v.u)
|
|
49
|
+
for (const o of v.u)
|
|
50
|
+
trackEffect(el, () => krateUnmount(o));
|
|
40
51
|
}
|
|
41
52
|
/**
|
|
42
53
|
* Binds the ref returned by `get` to `el` (hydration `ref={...}`). The ref is
|
|
@@ -126,7 +137,7 @@ export function insert(parent, value, startMarker) {
|
|
|
126
137
|
function insertBefore(parent, value, before) {
|
|
127
138
|
if (value == null || value === false || value === true)
|
|
128
139
|
return;
|
|
129
|
-
if (typeof value === 'string' || typeof value === 'number') {
|
|
140
|
+
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'bigint') {
|
|
130
141
|
parent.insertBefore(document.createTextNode(String(value)), before);
|
|
131
142
|
}
|
|
132
143
|
else if (value instanceof Node) {
|
|
@@ -265,7 +276,9 @@ function setAttr(el, key, value) {
|
|
|
265
276
|
// aria-* and data-* stringify booleans (aria-selected={false} renders
|
|
266
277
|
// "false"), as in React.
|
|
267
278
|
const stringly = /^(aria|data)-/.test(name);
|
|
268
|
-
|
|
279
|
+
// A function (or symbol) is not an attribute value: React drops it rather
|
|
280
|
+
// than writing its source, e.g. a signal getter inside a spread object.
|
|
281
|
+
if (value === null || value === undefined || typeof value === 'function' || typeof value === 'symbol' || (value === false && !stringly)) {
|
|
269
282
|
if (el.hasAttribute(name))
|
|
270
283
|
el.removeAttribute(name);
|
|
271
284
|
}
|
|
@@ -276,9 +289,20 @@ function setAttr(el, key, value) {
|
|
|
276
289
|
}
|
|
277
290
|
}
|
|
278
291
|
}
|
|
292
|
+
/** A built-in <Link> lowers to these props; plain anchors are left as written. */
|
|
293
|
+
function isLinkProps(props) {
|
|
294
|
+
return 'data-krate-link' in props || 'data-krate-external' in props;
|
|
295
|
+
}
|
|
296
|
+
function linkHref(href) {
|
|
297
|
+
if (typeof href === 'string')
|
|
298
|
+
return withBase(href);
|
|
299
|
+
if (typeof href === 'function')
|
|
300
|
+
return () => linkHref(href());
|
|
301
|
+
return href;
|
|
302
|
+
}
|
|
279
303
|
function applyProps(el, props) {
|
|
280
304
|
for (const key in props) {
|
|
281
|
-
const value = props[key];
|
|
305
|
+
const value = key === 'href' && isLinkProps(props) ? linkHref(props[key]) : props[key];
|
|
282
306
|
if (typeof value === 'function' && (key === 'value' || key === 'checked')) {
|
|
283
307
|
trackEffect(el, krateControl(el, key, value));
|
|
284
308
|
}
|
|
@@ -311,7 +335,8 @@ export function krateSpread(el, get) {
|
|
|
311
335
|
if (key === 'children' || key === 'key' || key === 'dangerouslySetInnerHTML')
|
|
312
336
|
continue;
|
|
313
337
|
next.add(key);
|
|
314
|
-
|
|
338
|
+
const isLink = key === 'href' && (isLinkProps(props) || el.hasAttribute('data-krate-link'));
|
|
339
|
+
setAttr(el, key, isLink ? linkHref(props[key]) : props[key]);
|
|
315
340
|
}
|
|
316
341
|
const prev = spreadKeys.get(el);
|
|
317
342
|
if (prev) {
|
|
@@ -327,6 +352,20 @@ export function krateSpread(el, get) {
|
|
|
327
352
|
spreadKeys.set(el, next);
|
|
328
353
|
}));
|
|
329
354
|
}
|
|
355
|
+
// Runs fn with the vnode's component as the current effect owner, so the
|
|
356
|
+
// effects and components created when the vnode is adopted or materialized
|
|
357
|
+
// (later, outside the component's own call) are its children.
|
|
358
|
+
function withOwner(owner, fn) {
|
|
359
|
+
if (owner === undefined)
|
|
360
|
+
return fn();
|
|
361
|
+
const prev = krateSetOwner(owner);
|
|
362
|
+
try {
|
|
363
|
+
return fn();
|
|
364
|
+
}
|
|
365
|
+
finally {
|
|
366
|
+
krateSetOwner(prev);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
330
369
|
let vnodeDepth = 0;
|
|
331
370
|
/** Evaluate `fn` with h() producing virtual nodes (see KrateVNode). */
|
|
332
371
|
export function withVNodes(fn) {
|
|
@@ -402,7 +441,9 @@ function buildElement(tag, props, children) {
|
|
|
402
441
|
return el;
|
|
403
442
|
}
|
|
404
443
|
function materialize(v) {
|
|
405
|
-
|
|
444
|
+
const el = withOwner(v.o, () => withDOM(() => buildElement(v.tag, v.props, v.children)));
|
|
445
|
+
trackVNodeOwners(v, el);
|
|
446
|
+
return el;
|
|
406
447
|
}
|
|
407
448
|
function isTextLike(v) {
|
|
408
449
|
return v == null || typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean' || typeof v === 'bigint';
|
|
@@ -558,14 +599,17 @@ function adoptElement(v, c) {
|
|
|
558
599
|
if (n && n.nodeType === 1 && n.localName.toLowerCase() === v.tag.toLowerCase()) {
|
|
559
600
|
c.next = n.nextSibling;
|
|
560
601
|
const el = n;
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
602
|
+
trackVNodeOwners(v, el);
|
|
603
|
+
withOwner(v.o, () => {
|
|
604
|
+
if (v.props)
|
|
605
|
+
applyProps(el, v.props);
|
|
606
|
+
if (!v.props || !v.props.dangerouslySetInnerHTML) {
|
|
607
|
+
const inner = { parent: el, next: el.firstChild };
|
|
608
|
+
for (const child of v.children)
|
|
609
|
+
adoptValue(child, inner);
|
|
610
|
+
removeRest(inner, null);
|
|
611
|
+
}
|
|
612
|
+
});
|
|
569
613
|
return;
|
|
570
614
|
}
|
|
571
615
|
insertAt(c, materialize(v));
|
|
@@ -633,7 +677,7 @@ export function h(tag, props, ...children) {
|
|
|
633
677
|
return value;
|
|
634
678
|
}
|
|
635
679
|
if (vnodeDepth > 0) {
|
|
636
|
-
return { __kv: 1, tag, props, children };
|
|
680
|
+
return { __kv: 1, tag, props, children, o: krateGetOwner() };
|
|
637
681
|
}
|
|
638
682
|
return buildElement(tag, props, children);
|
|
639
683
|
}
|
package/dist/dom.test.js
CHANGED
|
@@ -28,4 +28,12 @@ describe('insert', () => {
|
|
|
28
28
|
expect(parent.textContent).toBe('new');
|
|
29
29
|
expect(parent.textContent.includes('old')).toBe(false);
|
|
30
30
|
});
|
|
31
|
+
it('inserts a bigint as text', () => {
|
|
32
|
+
const parent = document.createElement('div');
|
|
33
|
+
const start = document.createComment('k');
|
|
34
|
+
const end = document.createComment('k');
|
|
35
|
+
parent.append(start, document.createTextNode('old'), end);
|
|
36
|
+
insert(parent, [1n, 'a', 20n], start);
|
|
37
|
+
expect(parent.textContent).toBe('1a20');
|
|
38
|
+
});
|
|
31
39
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
2
|
-
import { createSignal, createEffect, disposeAll, krateFlushSync, krateOnSettle, setOwnerHooks, } from './signal';
|
|
2
|
+
import { createSignal, createEffect, onCleanup, disposeAll, krateFlushSync, krateOnSettle, setOwnerHooks, } from './signal';
|
|
3
3
|
// Settle hooks cannot be unregistered; this one counts flushes for the whole file.
|
|
4
4
|
let settleRuns = 0;
|
|
5
5
|
let settleThrows = false;
|
|
@@ -161,3 +161,39 @@ describe('a throwing effect does not strand the others', () => {
|
|
|
161
161
|
expect(seen).toEqual([0, 5]);
|
|
162
162
|
});
|
|
163
163
|
});
|
|
164
|
+
describe('a throwing cleanup does not strand the others', () => {
|
|
165
|
+
beforeEach(() => disposeAll());
|
|
166
|
+
afterEach(() => vi.restoreAllMocks());
|
|
167
|
+
it('runs the remaining cleanups of an effect that re-runs', () => {
|
|
168
|
+
const [a, setA] = createSignal(0);
|
|
169
|
+
const log = [];
|
|
170
|
+
createEffect(() => {
|
|
171
|
+
a();
|
|
172
|
+
onCleanup(() => {
|
|
173
|
+
log.push('first');
|
|
174
|
+
throw new Error('cleanup boom');
|
|
175
|
+
});
|
|
176
|
+
onCleanup(() => log.push('second'));
|
|
177
|
+
});
|
|
178
|
+
setA(1);
|
|
179
|
+
expect(() => krateFlushSync()).toThrow('cleanup boom');
|
|
180
|
+
expect(log).toEqual(['first', 'second']);
|
|
181
|
+
});
|
|
182
|
+
it('keeps tearing down the page when a cleanup throws', () => {
|
|
183
|
+
const logged = vi.spyOn(console, 'error').mockImplementation(() => { });
|
|
184
|
+
const log = [];
|
|
185
|
+
createEffect(() => {
|
|
186
|
+
onCleanup(() => {
|
|
187
|
+
log.push('a');
|
|
188
|
+
throw new Error('teardown boom');
|
|
189
|
+
});
|
|
190
|
+
onCleanup(() => log.push('a2'));
|
|
191
|
+
});
|
|
192
|
+
createEffect(() => {
|
|
193
|
+
onCleanup(() => log.push('b'));
|
|
194
|
+
});
|
|
195
|
+
expect(() => disposeAll()).not.toThrow();
|
|
196
|
+
expect(log).toEqual(['a', 'a2', 'b']);
|
|
197
|
+
expect(logged).toHaveBeenCalledTimes(1);
|
|
198
|
+
});
|
|
199
|
+
});
|
package/dist/effects.js
CHANGED
|
@@ -169,7 +169,7 @@ function disposeTree(owner, active, passive) {
|
|
|
169
169
|
continue;
|
|
170
170
|
}
|
|
171
171
|
if (hook.phase === 'passive') {
|
|
172
|
-
const c =
|
|
172
|
+
const c = takePassiveCleanup(hook);
|
|
173
173
|
if (c)
|
|
174
174
|
passive.push(c);
|
|
175
175
|
}
|
|
@@ -320,7 +320,7 @@ function hideTree(owner, passive) {
|
|
|
320
320
|
for (const hook of owner.hooks) {
|
|
321
321
|
dirty.delete(hook);
|
|
322
322
|
if (hook.phase === 'passive') {
|
|
323
|
-
const c =
|
|
323
|
+
const c = takePassiveCleanup(hook);
|
|
324
324
|
if (c)
|
|
325
325
|
passive.push(c);
|
|
326
326
|
}
|
|
@@ -485,6 +485,12 @@ function runCleanup(hook) {
|
|
|
485
485
|
if (c)
|
|
486
486
|
runGuarded(c, hook.owner);
|
|
487
487
|
}
|
|
488
|
+
// A passive cleanup detached for the passive phase; its errors go to the
|
|
489
|
+
// owner's error boundary like a layout cleanup's.
|
|
490
|
+
function takePassiveCleanup(hook) {
|
|
491
|
+
const c = takeCleanup(hook);
|
|
492
|
+
return c ? () => runGuarded(c, hook.owner) : null;
|
|
493
|
+
}
|
|
488
494
|
function pruneSnapshots() {
|
|
489
495
|
let min = krateEpoch();
|
|
490
496
|
for (const e of liveEpochs.keys())
|
|
@@ -571,7 +577,7 @@ function commit() {
|
|
|
571
577
|
for (const h of hooks) {
|
|
572
578
|
if (h.phase !== 'passive')
|
|
573
579
|
continue;
|
|
574
|
-
const c =
|
|
580
|
+
const c = takePassiveCleanup(h);
|
|
575
581
|
if (c)
|
|
576
582
|
passiveCleanups.push(c);
|
|
577
583
|
passiveSetups.push([h, at]);
|
|
@@ -600,6 +606,17 @@ export function flushPassive() {
|
|
|
600
606
|
clearTimeout(passiveTimer);
|
|
601
607
|
passiveTimer = null;
|
|
602
608
|
}
|
|
609
|
+
let failed = false;
|
|
610
|
+
let first;
|
|
611
|
+
const remember = (err) => {
|
|
612
|
+
if (!failed) {
|
|
613
|
+
failed = true;
|
|
614
|
+
first = err;
|
|
615
|
+
}
|
|
616
|
+
else if (typeof console !== 'undefined') {
|
|
617
|
+
console.error(err);
|
|
618
|
+
}
|
|
619
|
+
};
|
|
603
620
|
// Loop: a passive cleanup or setup may unmount components, queueing more.
|
|
604
621
|
while (passiveCleanups.length || passiveSetups.length) {
|
|
605
622
|
const cleanups = passiveCleanups;
|
|
@@ -607,18 +624,34 @@ export function flushPassive() {
|
|
|
607
624
|
passiveCleanups = [];
|
|
608
625
|
passiveSetups = [];
|
|
609
626
|
krateCommitting(true);
|
|
627
|
+
// An error with no boundary to take it must not drop the cleanups and
|
|
628
|
+
// setups queued behind it: run them all, then rethrow the first.
|
|
610
629
|
try {
|
|
611
|
-
for (const c of cleanups)
|
|
612
|
-
|
|
630
|
+
for (const c of cleanups) {
|
|
631
|
+
try {
|
|
632
|
+
c();
|
|
633
|
+
}
|
|
634
|
+
catch (err) {
|
|
635
|
+
remember(err);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
613
638
|
for (const [h, at] of setups) {
|
|
614
|
-
if (isActive(h.owner))
|
|
639
|
+
if (!isActive(h.owner))
|
|
640
|
+
continue;
|
|
641
|
+
try {
|
|
615
642
|
runSetup(h, at);
|
|
643
|
+
}
|
|
644
|
+
catch (err) {
|
|
645
|
+
remember(err);
|
|
646
|
+
}
|
|
616
647
|
}
|
|
617
648
|
}
|
|
618
649
|
finally {
|
|
619
650
|
krateCommitting(false);
|
|
620
651
|
}
|
|
621
652
|
}
|
|
653
|
+
if (failed)
|
|
654
|
+
throw first;
|
|
622
655
|
}
|
|
623
656
|
/**
|
|
624
657
|
* React's useMemo with a dependency array: the getter recomputes
|
|
@@ -712,7 +745,7 @@ function unmountExcept(gone) {
|
|
|
712
745
|
hook.stop();
|
|
713
746
|
dirty.delete(hook);
|
|
714
747
|
if (hook.phase === 'passive') {
|
|
715
|
-
const c =
|
|
748
|
+
const c = takePassiveCleanup(hook);
|
|
716
749
|
if (c)
|
|
717
750
|
passive.push(c);
|
|
718
751
|
}
|
|
@@ -764,7 +797,7 @@ function unmountAll(gone) {
|
|
|
764
797
|
for (const hook of root.hooks) {
|
|
765
798
|
hook.stop();
|
|
766
799
|
if (hook.phase === 'passive') {
|
|
767
|
-
const c =
|
|
800
|
+
const c = takePassiveCleanup(hook);
|
|
768
801
|
if (c)
|
|
769
802
|
passive.push(c);
|
|
770
803
|
}
|
package/dist/events.d.ts
CHANGED
|
@@ -26,6 +26,8 @@ export declare function krateSynthetic<E extends Event>(native: E, type?: string
|
|
|
26
26
|
* to on it.
|
|
27
27
|
*/
|
|
28
28
|
export declare function krateEventRoot(node: Node): void;
|
|
29
|
+
/** Runs fn with delegated and direct handlers silenced (internal). */
|
|
30
|
+
export declare function krateMuteEvents<T>(fn: () => T): T;
|
|
29
31
|
/** Splits a React prop (`onClickCapture`) into its event name and phase. */
|
|
30
32
|
export declare function krateEventName(prop: string): [name: string, capture: boolean];
|
|
31
33
|
/**
|
package/dist/events.js
CHANGED
|
@@ -187,7 +187,23 @@ function onRootCapture(native) {
|
|
|
187
187
|
function onRootBubble(native) {
|
|
188
188
|
dispatchRoot(native, false);
|
|
189
189
|
}
|
|
190
|
+
// React turns its event system off while it commits: the blur and focus a
|
|
191
|
+
// browser fires when a focused node is moved or removed must not reach
|
|
192
|
+
// handlers. Keyed list updates run their DOM moves inside this.
|
|
193
|
+
let muted = 0;
|
|
194
|
+
/** Runs fn with delegated and direct handlers silenced (internal). */
|
|
195
|
+
export function krateMuteEvents(fn) {
|
|
196
|
+
muted++;
|
|
197
|
+
try {
|
|
198
|
+
return fn();
|
|
199
|
+
}
|
|
200
|
+
finally {
|
|
201
|
+
muted--;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
190
204
|
function dispatchRoot(native, capture) {
|
|
205
|
+
if (muted > 0)
|
|
206
|
+
return;
|
|
191
207
|
// Several roots can be on the path (a portal inside the root); the first
|
|
192
208
|
// listener to see the event handles the whole path.
|
|
193
209
|
const flag = capture ? '__kdc' : '__kdb';
|
|
@@ -219,6 +235,8 @@ function onDirectCapture(native) {
|
|
|
219
235
|
directDispatch(this, native, true);
|
|
220
236
|
}
|
|
221
237
|
function directDispatch(el, native, capture) {
|
|
238
|
+
if (muted > 0)
|
|
239
|
+
return;
|
|
222
240
|
const m = el.__kh;
|
|
223
241
|
const h = m && m[native.type + (capture ? '$' : '')];
|
|
224
242
|
if (!h)
|
package/dist/index.d.ts
CHANGED
|
@@ -29,4 +29,9 @@ export type { ResourceReturn, ResourceActions } from './resource.js';
|
|
|
29
29
|
export type { Component, PropsWithChildren, ComponentProps, RefCallback } from './dom.js';
|
|
30
30
|
export type { RefObject, Ref, UseRefResult } from './signal.js';
|
|
31
31
|
import './jsx-types.js';
|
|
32
|
+
export declare const Link: typeof globalThis.Link;
|
|
33
|
+
export declare const Head: typeof globalThis.Head;
|
|
34
|
+
export declare const Icon: typeof globalThis.Icon;
|
|
35
|
+
export declare const Suspense: typeof globalThis.Suspense;
|
|
36
|
+
export declare const SyntaxHighlight: typeof globalThis.SyntaxHighlight;
|
|
32
37
|
import './css-modules.js';
|