@krate/runtime 0.5.0-alpha.4 → 0.5.0-beta.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/action-context.d.ts +5 -1
- package/dist/action-context.js +33 -3
- package/dist/action-context.test.js +26 -1
- package/dist/actions.d.ts +12 -1
- package/dist/actions.js +84 -4
- package/dist/actions.test.js +61 -1
- package/dist/assets-static.d.ts +5 -0
- package/dist/assets-static.js +31 -0
- package/dist/boundary.d.ts +12 -0
- package/dist/boundary.js +53 -13
- package/dist/cache-adapter.d.ts +113 -0
- package/dist/cache-adapter.js +217 -0
- package/dist/cache-adapter.test.d.ts +1 -0
- package/dist/cache-adapter.test.js +244 -0
- package/dist/cache-redis.d.ts +27 -0
- package/dist/cache-redis.js +110 -0
- package/dist/cache-scope.d.ts +25 -9
- package/dist/cache-scope.js +64 -13
- package/dist/cache.d.ts +1 -0
- package/dist/cache.js +3 -3
- package/dist/cache.test.js +16 -0
- package/dist/class-component.js +6 -0
- package/dist/class-component.test.js +53 -2
- package/dist/class-helpers.d.ts +4 -4
- package/dist/class-helpers.js +357 -32
- package/dist/class-helpers.test.d.ts +1 -0
- package/dist/class-helpers.test.js +169 -0
- package/dist/context.d.ts +16 -2
- package/dist/context.js +44 -3
- package/dist/dev-overlay.js +85 -12
- package/dist/dev-overlay.test.js +22 -0
- package/dist/dom.d.ts +15 -0
- package/dist/dom.js +97 -3
- package/dist/effect-errors.test.d.ts +1 -0
- package/dist/effect-errors.test.js +163 -0
- package/dist/effects.d.ts +8 -0
- package/dist/effects.js +13 -0
- package/dist/error-overlay.js +7 -7
- package/dist/handler-html.d.ts +63 -0
- package/dist/handler-html.js +285 -0
- package/dist/handler-html.test.d.ts +1 -0
- package/dist/handler-html.test.js +256 -0
- package/dist/handler-routing.d.ts +83 -0
- package/dist/handler-routing.js +579 -0
- package/dist/handler-routing.test.d.ts +1 -0
- package/dist/handler-routing.test.js +99 -0
- package/dist/handler-segments.d.ts +43 -0
- package/dist/handler-segments.js +115 -0
- package/dist/handler-segments.test.d.ts +1 -0
- package/dist/handler-segments.test.js +217 -0
- package/dist/head-tags.d.ts +51 -0
- package/dist/head-tags.js +329 -0
- package/dist/head-tags.test.d.ts +1 -0
- package/dist/head-tags.test.js +131 -0
- package/dist/highlight-bind.d.ts +42 -0
- package/dist/highlight-bind.js +176 -0
- package/dist/highlight-bind.test.d.ts +1 -0
- package/dist/highlight-bind.test.js +124 -0
- package/dist/highlight.d.ts +93 -0
- package/dist/highlight.js +426 -0
- package/dist/highlight.test.d.ts +1 -0
- package/dist/highlight.test.js +56 -0
- package/dist/hints.d.ts +19 -0
- package/dist/hints.js +97 -0
- package/dist/hook-args.d.ts +7 -0
- package/dist/hook-args.js +10 -0
- package/dist/htmlspec-attributes.d.ts +1 -0
- package/dist/htmlspec-attributes.js +159 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/dist/jsx-types.d.ts +11 -0
- package/dist/krate-core.js +2 -0
- package/dist/krate-dev.js +75 -12
- package/dist/krate-f-actions.js +2 -0
- package/dist/krate-f-classes.js +8 -0
- package/dist/krate-f-helpers.js +2 -0
- package/dist/krate-f-highlight.js +2 -0
- package/dist/krate-f-list.js +2 -0
- package/dist/krate-f-navigation.js +2 -0
- package/dist/krate-f-router-lite.js +2 -0
- package/dist/krate-f-router.js +2 -0
- package/dist/krate-features.json +493 -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/list.js +4 -2
- package/dist/metadata-routes.d.ts +75 -0
- package/dist/metadata-routes.js +76 -0
- package/dist/metadata.d.ts +222 -0
- package/dist/metadata.js +452 -0
- package/dist/metadata.test.d.ts +1 -0
- package/dist/metadata.test.js +167 -0
- package/dist/navigation-server.d.ts +27 -0
- package/dist/navigation-server.js +90 -0
- package/dist/navigation.d.ts +37 -0
- package/dist/navigation.js +195 -0
- package/dist/navigation.test.d.ts +1 -0
- package/dist/navigation.test.js +130 -0
- package/dist/node-server.d.ts +22 -0
- package/dist/node-server.js +436 -0
- package/dist/node-server.test.d.ts +1 -0
- package/dist/node-server.test.js +492 -0
- package/dist/og-files.d.ts +2 -0
- package/dist/og-files.js +19 -0
- package/dist/og.d.ts +56 -0
- package/dist/og.js +156 -0
- package/dist/patterns.test.d.ts +1 -0
- package/dist/patterns.test.js +98 -0
- package/dist/react.d.ts +47 -0
- package/dist/react.js +59 -1
- package/dist/react19.test.d.ts +1 -0
- package/dist/react19.test.js +176 -0
- package/dist/reactivity-core.test.d.ts +1 -0
- package/dist/reactivity-core.test.js +152 -0
- package/dist/reconcile.js +4 -2
- package/dist/request-context.d.ts +23 -0
- package/dist/request-context.js +45 -0
- package/dist/resp.d.ts +55 -0
- package/dist/resp.js +239 -0
- package/dist/router-layout.test.js +70 -0
- package/dist/router-nav.test.js +65 -0
- package/dist/router-speculation.test.d.ts +1 -0
- package/dist/router-speculation.test.js +25 -0
- package/dist/router-styles.test.d.ts +1 -0
- package/dist/router-styles.test.js +98 -0
- package/dist/router.js +175 -36
- package/dist/segmented-lru.d.ts +19 -0
- package/dist/segmented-lru.js +87 -0
- package/dist/server-async.test.js +43 -1
- package/dist/server-handler.d.ts +165 -0
- package/dist/server-handler.js +1826 -0
- package/dist/server-handler.test.d.ts +1 -0
- package/dist/server-handler.test.js +522 -0
- package/dist/server-highlight.test.d.ts +1 -0
- package/dist/server-highlight.test.js +32 -0
- package/dist/server-renderer.js +165 -720
- package/dist/server-renderer.test.d.ts +1 -0
- package/dist/server-renderer.test.js +266 -0
- package/dist/server.d.ts +13 -1
- package/dist/server.js +131 -16
- package/dist/server.test.js +20 -0
- package/dist/sha256.d.ts +1 -0
- package/dist/sha256.js +66 -0
- package/dist/sha256.test.d.ts +1 -0
- package/dist/sha256.test.js +13 -0
- package/dist/shared-isr.d.ts +31 -0
- package/dist/shared-isr.js +142 -0
- package/dist/signal.d.ts +3 -2
- package/dist/signal.js +163 -23
- package/dist/slots.js +12 -1
- package/dist/ssr-core-behavior.test.d.ts +1 -0
- package/dist/ssr-core-behavior.test.js +620 -0
- package/dist/ssr-core.d.ts +175 -0
- package/dist/ssr-core.js +940 -0
- package/dist/ssr-core.test.d.ts +1 -0
- package/dist/ssr-core.test.js +324 -0
- package/dist/static-memory.d.ts +3 -0
- package/dist/static-memory.js +47 -0
- package/dist/static-memory.test.d.ts +1 -0
- package/dist/static-memory.test.js +26 -0
- package/dist/test-redis.d.ts +12 -0
- package/dist/test-redis.js +131 -0
- package/dist/use-id.d.ts +8 -0
- package/dist/use-id.js +40 -0
- package/package.json +30 -2
package/dist/context.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
let contextId = 0;
|
|
2
2
|
/** Marks Krate context objects so `use(x)` can tell them from promises. */
|
|
3
3
|
export const KRATE_CONTEXT = Symbol.for('krate.context');
|
|
4
|
+
/** A provider value that is read through a getter on every use. */
|
|
5
|
+
class LiveContext {
|
|
6
|
+
constructor(get) {
|
|
7
|
+
this.get = get;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
4
10
|
function toNode(v) {
|
|
5
11
|
if (v instanceof Node)
|
|
6
12
|
return v;
|
|
@@ -27,12 +33,12 @@ export function resetContexts() {
|
|
|
27
33
|
}
|
|
28
34
|
/**
|
|
29
35
|
* Create a context that carries a CSS scope handle (see {@link CSSScope}). The
|
|
30
|
-
* default is a harmless placeholder
|
|
36
|
+
* default is a harmless placeholder - a `<Provider>` value always wins - so no
|
|
31
37
|
* cast is needed:
|
|
32
38
|
*
|
|
33
39
|
* ```ts
|
|
34
40
|
* const TabsCtx = createScope<string>();
|
|
35
|
-
* // provide: <TabsCtx.Provider value={createCSSChoice('a')}
|
|
41
|
+
* // provide: <TabsCtx.Provider value={createCSSChoice('a')}>...</Provider>
|
|
36
42
|
* // consume: const tab = TabsCtx.use();
|
|
37
43
|
* ```
|
|
38
44
|
*/
|
|
@@ -49,10 +55,18 @@ export function createContext(defaultValue) {
|
|
|
49
55
|
const useContext = () => {
|
|
50
56
|
const stack = contextStack.get(id);
|
|
51
57
|
if (stack && stack.length > 0) {
|
|
52
|
-
|
|
58
|
+
const top = stack[stack.length - 1];
|
|
59
|
+
return (top instanceof LiveContext ? top.get() : top);
|
|
53
60
|
}
|
|
54
61
|
return defaultValue;
|
|
55
62
|
};
|
|
63
|
+
const useContextLive = () => {
|
|
64
|
+
const stack = contextStack.get(id);
|
|
65
|
+
const top = stack && stack.length > 0 ? stack[stack.length - 1] : defaultValue;
|
|
66
|
+
if (top instanceof LiveContext)
|
|
67
|
+
return () => top.get();
|
|
68
|
+
return () => top;
|
|
69
|
+
};
|
|
56
70
|
const Provider = (props) => {
|
|
57
71
|
const stack = contextStack.get(id) || [defaultValue];
|
|
58
72
|
stack.push(props.value);
|
|
@@ -98,5 +112,32 @@ export function createContext(defaultValue) {
|
|
|
98
112
|
ctx.Consumer = Consumer;
|
|
99
113
|
ctx.useContext = useContext;
|
|
100
114
|
ctx.use = useContext;
|
|
115
|
+
ctx.useContextLive = useContextLive;
|
|
101
116
|
return ctx;
|
|
102
117
|
}
|
|
118
|
+
/**
|
|
119
|
+
* Wraps a list row render function so the provider values of the enclosing
|
|
120
|
+
* `<Ctx.Provider value={...}>` (given as getters over the provider's state)
|
|
121
|
+
* are visible to the rows it creates, and stay live when the state changes.
|
|
122
|
+
* The compiler flattens providers at build time, so rows rendered later by
|
|
123
|
+
* the list runtime have no provider on the stack otherwise.
|
|
124
|
+
*/
|
|
125
|
+
export function krateContextRows(entries, render) {
|
|
126
|
+
return (...args) => {
|
|
127
|
+
const pushed = [];
|
|
128
|
+
for (const [ctx, get] of entries) {
|
|
129
|
+
const stack = contextStack.get(ctx.id);
|
|
130
|
+
if (!stack)
|
|
131
|
+
continue;
|
|
132
|
+
stack.push(new LiveContext(get));
|
|
133
|
+
pushed.push(stack);
|
|
134
|
+
}
|
|
135
|
+
try {
|
|
136
|
+
return render(...args);
|
|
137
|
+
}
|
|
138
|
+
finally {
|
|
139
|
+
for (const stack of pushed)
|
|
140
|
+
stack.pop();
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
}
|
package/dist/dev-overlay.js
CHANGED
|
@@ -194,11 +194,11 @@ const STYLE = `
|
|
|
194
194
|
:host {
|
|
195
195
|
--red: #f87171;
|
|
196
196
|
--green: #34d399;
|
|
197
|
-
--bg: #
|
|
198
|
-
--bg-2: #
|
|
199
|
-
--line: #
|
|
200
|
-
--fg: #
|
|
201
|
-
--muted: #
|
|
197
|
+
--bg: #111111;
|
|
198
|
+
--bg-2: #171717;
|
|
199
|
+
--line: #2a2a2a;
|
|
200
|
+
--fg: #e5e5e5;
|
|
201
|
+
--muted: #a3a3a3;
|
|
202
202
|
}
|
|
203
203
|
|
|
204
204
|
/* toolbar */
|
|
@@ -245,7 +245,7 @@ const STYLE = `
|
|
|
245
245
|
/* overlay */
|
|
246
246
|
.overlay {
|
|
247
247
|
position: fixed; inset: 0; z-index: 2147483000;
|
|
248
|
-
background: rgba(
|
|
248
|
+
background: rgba(10, 10, 10, 0.72); backdrop-filter: blur(6px);
|
|
249
249
|
display: flex; align-items: flex-start; justify-content: center;
|
|
250
250
|
padding: 4vh 16px; overflow: auto;
|
|
251
251
|
font: 13px/1.5 "SF Mono", Menlo, Monaco, Consolas, "Courier New", monospace;
|
|
@@ -269,9 +269,9 @@ const STYLE = `
|
|
|
269
269
|
border-radius: 9px; padding: 6px 12px; cursor: pointer; font: inherit; font-size: 12px;
|
|
270
270
|
transition: color .15s ease, background .15s ease, border-color .15s ease;
|
|
271
271
|
}
|
|
272
|
-
.btn:hover { color: var(--fg); background: rgba(255,255,255,.06); border-color: #
|
|
272
|
+
.btn:hover { color: var(--fg); background: rgba(255,255,255,.06); border-color: #444444; }
|
|
273
273
|
.list { padding: 6px 0; }
|
|
274
|
-
.diag { padding: 14px 18px; border-bottom: 1px solid #
|
|
274
|
+
.diag { padding: 14px 18px; border-bottom: 1px solid #1f1f1f; }
|
|
275
275
|
.diag:last-child { border-bottom: none; }
|
|
276
276
|
.actions { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
|
|
277
277
|
.loc {
|
|
@@ -282,10 +282,10 @@ const STYLE = `
|
|
|
282
282
|
.loc:hover { text-decoration: underline; }
|
|
283
283
|
.msg { color: #f3f4f6; white-space: pre-wrap; word-break: break-word; }
|
|
284
284
|
.frame {
|
|
285
|
-
margin-top: 10px; background: #
|
|
285
|
+
margin-top: 10px; background: #0b0b0b; border: 1px solid #262626;
|
|
286
286
|
border-radius: 7px; padding: 8px 10px; overflow-x: auto; white-space: pre;
|
|
287
287
|
}
|
|
288
|
-
.frame .src { color: #
|
|
288
|
+
.frame .src { color: #d4d4d4; }
|
|
289
289
|
.frame .caret { color: var(--red); }
|
|
290
290
|
.hint { margin-top: 8px; color: #a3e635; }
|
|
291
291
|
.stack {
|
|
@@ -410,18 +410,39 @@ class KrateDev {
|
|
|
410
410
|
catch {
|
|
411
411
|
return;
|
|
412
412
|
}
|
|
413
|
+
let connectedOnce = false;
|
|
414
|
+
sse.addEventListener('connected', () => {
|
|
415
|
+
// A second "connected" means the stream dropped and came back: the dev
|
|
416
|
+
// server restarted (config or env change), so the page is stale.
|
|
417
|
+
if (connectedOnce)
|
|
418
|
+
location.reload();
|
|
419
|
+
connectedOnce = true;
|
|
420
|
+
});
|
|
413
421
|
sse.addEventListener('reload', (e) => {
|
|
422
|
+
// A rebuild without diagnostics supersedes any compile error on screen,
|
|
423
|
+
// whichever route it touched.
|
|
424
|
+
if (this.buildDiags.length > 0) {
|
|
425
|
+
this.buildDiags = [];
|
|
426
|
+
this.setStatus(true);
|
|
427
|
+
this.render();
|
|
428
|
+
}
|
|
429
|
+
let cssOnly = false;
|
|
414
430
|
try {
|
|
415
431
|
const d = JSON.parse(e.data);
|
|
432
|
+
cssOnly = d.cssOnly === true;
|
|
416
433
|
if (d.pages &&
|
|
417
434
|
Array.isArray(d.pages) &&
|
|
418
|
-
!d.pages.some((p) => routeMatches(p, location.pathname, this.cfg.basePath))) {
|
|
435
|
+
!d.pages.some((p) => p === '/404' || p === '/500' || routeMatches(p, location.pathname, this.cfg.basePath))) {
|
|
419
436
|
return;
|
|
420
437
|
}
|
|
421
438
|
}
|
|
422
439
|
catch {
|
|
423
440
|
/* full reload */
|
|
424
441
|
}
|
|
442
|
+
if (cssOnly) {
|
|
443
|
+
this.swapStylesheets().catch(() => location.reload());
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
425
446
|
location.reload();
|
|
426
447
|
});
|
|
427
448
|
sse.addEventListener('build-error', (e) => {
|
|
@@ -441,6 +462,9 @@ class KrateDev {
|
|
|
441
462
|
if (!d.clientError)
|
|
442
463
|
return;
|
|
443
464
|
const err = d.clientError;
|
|
465
|
+
// This tab reported the error itself and already shows it.
|
|
466
|
+
if (this.seen.has(err.message + '|' + (err.stack || '')))
|
|
467
|
+
return;
|
|
444
468
|
this.addRuntime('Runtime Error', err.message, err.stack);
|
|
445
469
|
}
|
|
446
470
|
catch {
|
|
@@ -448,6 +472,55 @@ class KrateDev {
|
|
|
448
472
|
}
|
|
449
473
|
});
|
|
450
474
|
}
|
|
475
|
+
/**
|
|
476
|
+
* Apply a CSS-only rebuild without a full reload: fetch the freshly built
|
|
477
|
+
* document, add its stylesheet links, and drop the ones it no longer has.
|
|
478
|
+
* Inline <style> changes cannot be diffed safely, so they fall back to a
|
|
479
|
+
* reload (the caller reloads when this rejects).
|
|
480
|
+
*/
|
|
481
|
+
async swapStylesheets() {
|
|
482
|
+
const res = await fetch(location.href, { cache: 'no-store', headers: { Accept: 'text/html' } });
|
|
483
|
+
if (!res.ok)
|
|
484
|
+
throw new Error('fetch failed');
|
|
485
|
+
const next = new DOMParser().parseFromString(await res.text(), 'text/html');
|
|
486
|
+
const inline = (d) => Array.from(d.querySelectorAll('style'))
|
|
487
|
+
.map((el) => el.textContent || '')
|
|
488
|
+
.join('\u0000');
|
|
489
|
+
if (inline(next) !== inline(document))
|
|
490
|
+
throw new Error('inline styles changed');
|
|
491
|
+
const abs = (href) => new URL(href, location.href).href;
|
|
492
|
+
const want = Array.from(next.querySelectorAll('link[rel~="stylesheet"]')).map((l) => abs(l.getAttribute('href') || ''));
|
|
493
|
+
const current = Array.from(document.querySelectorAll('link[rel~="stylesheet"]'));
|
|
494
|
+
const have = new Set(current.map((l) => abs(l.getAttribute('href') || '')));
|
|
495
|
+
const loads = [];
|
|
496
|
+
for (const href of want) {
|
|
497
|
+
if (have.has(href))
|
|
498
|
+
continue;
|
|
499
|
+
const link = document.createElement('link');
|
|
500
|
+
link.rel = 'stylesheet';
|
|
501
|
+
link.href = href;
|
|
502
|
+
loads.push(new Promise((resolve, reject) => {
|
|
503
|
+
link.onload = () => resolve();
|
|
504
|
+
link.onerror = () => reject(new Error('stylesheet failed'));
|
|
505
|
+
}));
|
|
506
|
+
document.head.appendChild(link);
|
|
507
|
+
}
|
|
508
|
+
await Promise.all(loads);
|
|
509
|
+
const keep = new Set(want);
|
|
510
|
+
for (const l of current) {
|
|
511
|
+
if (!keep.has(abs(l.getAttribute('href') || '')))
|
|
512
|
+
l.remove();
|
|
513
|
+
}
|
|
514
|
+
// A same-named stylesheet is cache-busted in place.
|
|
515
|
+
for (const l of current) {
|
|
516
|
+
const href = abs(l.getAttribute('href') || '');
|
|
517
|
+
if (!keep.has(href) || !l.isConnected)
|
|
518
|
+
continue;
|
|
519
|
+
const u = new URL(href);
|
|
520
|
+
u.searchParams.set('_krate', String(Date.now()));
|
|
521
|
+
l.href = u.href;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
451
524
|
captureRuntimeErrors() {
|
|
452
525
|
window.addEventListener('error', (e) => {
|
|
453
526
|
const stack = e.error?.stack || '';
|
|
@@ -481,7 +554,7 @@ class KrateDev {
|
|
|
481
554
|
navigator.sendBeacon(this.cfg.errors, new Blob([body], { type: 'application/json' }));
|
|
482
555
|
}
|
|
483
556
|
else {
|
|
484
|
-
fetch(this.cfg.errors, { method: 'POST', body, headers: { 'Content-Type': 'application/json' }, keepalive: true });
|
|
557
|
+
fetch(this.cfg.errors, { method: 'POST', body, headers: { 'Content-Type': 'application/json' }, keepalive: true }).catch(() => { });
|
|
485
558
|
}
|
|
486
559
|
}
|
|
487
560
|
catch {
|
package/dist/dev-overlay.test.js
CHANGED
|
@@ -20,6 +20,8 @@ let overlay;
|
|
|
20
20
|
// import, and the export is captured from the same module instance.
|
|
21
21
|
beforeAll(async () => {
|
|
22
22
|
globalThis.EventSource = FakeEventSource;
|
|
23
|
+
// Error reports must not reach the (absent) dev server from the test.
|
|
24
|
+
navigator.sendBeacon = () => true;
|
|
23
25
|
window.__KRATE_DEV__ = {
|
|
24
26
|
sse: '/__krate/hotreload',
|
|
25
27
|
errors: '/__krate/client-error',
|
|
@@ -83,6 +85,26 @@ describe('dev-overlay', () => {
|
|
|
83
85
|
expect(overlay.hidden).toBe(true);
|
|
84
86
|
});
|
|
85
87
|
});
|
|
88
|
+
describe('dev-overlay rebuild handling', () => {
|
|
89
|
+
it('clears a build error when a rebuild of another route succeeds', () => {
|
|
90
|
+
history.pushState({}, '', '/current');
|
|
91
|
+
FakeEventSource.last.emit('build-error', { diagnostics: [{ file: 'b.tsx', message: 'stale-build-error' }], buildOk: false });
|
|
92
|
+
const overlayEl = devRoot().querySelector('.overlay');
|
|
93
|
+
expect(overlayEl.hidden).toBe(false);
|
|
94
|
+
expect(devRoot().querySelector('.list').textContent).toContain('stale-build-error');
|
|
95
|
+
// The fix landed on a different route: no reload here, but the error goes.
|
|
96
|
+
FakeEventSource.last.emit('reload', { pages: ['/elsewhere'] });
|
|
97
|
+
const text = devRoot().querySelector('.list').textContent || '';
|
|
98
|
+
expect(text).not.toContain('stale-build-error');
|
|
99
|
+
});
|
|
100
|
+
it('shows a runtime error once when the server echoes it back', () => {
|
|
101
|
+
const before = (devRoot().querySelector('.list').textContent || '').split('echo-dup-error').length - 1;
|
|
102
|
+
window.dispatchEvent(new ErrorEvent('error', { message: 'echo-dup-error', filename: 'x.js', lineno: 1, colno: 1 }));
|
|
103
|
+
FakeEventSource.last.emit('client-error', { clientError: { message: 'echo-dup-error', stack: '' } });
|
|
104
|
+
const text = devRoot().querySelector('.list').textContent || '';
|
|
105
|
+
expect(text.split('echo-dup-error').length - 1 - before).toBe(1);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
86
108
|
describe('routeMatches', () => {
|
|
87
109
|
it('matches unprefixed patterns against a base-path URL', () => {
|
|
88
110
|
expect(overlay.routeMatches('/about', '/docs/about', '/docs')).toBe(true);
|
package/dist/dom.d.ts
CHANGED
|
@@ -47,6 +47,21 @@ export declare function insert(parent: Node, value: unknown, startMarker: Node):
|
|
|
47
47
|
* removed. Other inline styles are left alone.
|
|
48
48
|
*/
|
|
49
49
|
export declare function krateApplyStyle(el: Element, style: Record<string, unknown>): void;
|
|
50
|
+
/**
|
|
51
|
+
* Sets a prop of a custom element the way React 19 does: objects, arrays and
|
|
52
|
+
* functions are properties, as is any value for a property the element
|
|
53
|
+
* already defines. Returns false when the prop is an attribute. An element
|
|
54
|
+
* that is not defined yet gets the property again once it is, so a class
|
|
55
|
+
* accessor is not shadowed by the early own property.
|
|
56
|
+
*/
|
|
57
|
+
export declare function krateCustomProp(el: Element, key: string, value: unknown): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Applies a spread props object (`<input {...field} />`) to el and re-applies
|
|
60
|
+
* it when the state `get` reads changes: attributes, form state, styles and
|
|
61
|
+
* event handlers are set, and keys the previous object had but the new one
|
|
62
|
+
* lacks are removed.
|
|
63
|
+
*/
|
|
64
|
+
export declare function krateSpread(el: Element, get: () => Record<string, unknown> | null | undefined): void;
|
|
50
65
|
/** A deferred element produced by h() while adopting server-rendered DOM. */
|
|
51
66
|
export interface KrateVNode {
|
|
52
67
|
__kv: 1;
|
package/dist/dom.js
CHANGED
|
@@ -189,7 +189,35 @@ export function krateApplyStyle(el, style) {
|
|
|
189
189
|
}
|
|
190
190
|
appliedStyles.set(el, next);
|
|
191
191
|
}
|
|
192
|
+
/**
|
|
193
|
+
* Sets a prop of a custom element the way React 19 does: objects, arrays and
|
|
194
|
+
* functions are properties, as is any value for a property the element
|
|
195
|
+
* already defines. Returns false when the prop is an attribute. An element
|
|
196
|
+
* that is not defined yet gets the property again once it is, so a class
|
|
197
|
+
* accessor is not shadowed by the early own property.
|
|
198
|
+
*/
|
|
199
|
+
export function krateCustomProp(el, key, value) {
|
|
200
|
+
if (el.localName.indexOf('-') < 1 || key.indexOf('-') >= 0)
|
|
201
|
+
return false;
|
|
202
|
+
const complex = (typeof value === 'object' && value !== null) || typeof value === 'function';
|
|
203
|
+
if (!complex && !(key in el))
|
|
204
|
+
return false;
|
|
205
|
+
el[key] = value;
|
|
206
|
+
const reg = typeof customElements !== 'undefined' ? customElements : null;
|
|
207
|
+
if (complex && reg && !reg.get(el.localName) && typeof reg.whenDefined === 'function') {
|
|
208
|
+
void reg.whenDefined(el.localName).then(() => {
|
|
209
|
+
const rec = el;
|
|
210
|
+
delete rec[key];
|
|
211
|
+
rec[key] = value;
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
192
216
|
function setAttr(el, key, value) {
|
|
217
|
+
if (el.localName.indexOf('-') > 0 && key !== 'class' && key !== 'className' && key !== 'style' && key !== 'ref' && key !== 'key' && key !== 'children' && !key.startsWith('on')) {
|
|
218
|
+
if (krateCustomProp(el, key, value))
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
193
221
|
if (key === 'class' || key === 'className') {
|
|
194
222
|
// Guard against false/null/undefined before String() conversion.
|
|
195
223
|
const next = value === false || value === null || value === undefined ? '' : String(value);
|
|
@@ -268,6 +296,37 @@ function applyProps(el, props) {
|
|
|
268
296
|
el.innerHTML = next;
|
|
269
297
|
}
|
|
270
298
|
}
|
|
299
|
+
const spreadKeys = new WeakMap();
|
|
300
|
+
/**
|
|
301
|
+
* Applies a spread props object (`<input {...field} />`) to el and re-applies
|
|
302
|
+
* it when the state `get` reads changes: attributes, form state, styles and
|
|
303
|
+
* event handlers are set, and keys the previous object had but the new one
|
|
304
|
+
* lacks are removed.
|
|
305
|
+
*/
|
|
306
|
+
export function krateSpread(el, get) {
|
|
307
|
+
trackEffect(el, createEffect(() => {
|
|
308
|
+
const props = get() || {};
|
|
309
|
+
const next = new Set();
|
|
310
|
+
for (const key of Object.keys(props)) {
|
|
311
|
+
if (key === 'children' || key === 'key' || key === 'dangerouslySetInnerHTML')
|
|
312
|
+
continue;
|
|
313
|
+
next.add(key);
|
|
314
|
+
setAttr(el, key, props[key]);
|
|
315
|
+
}
|
|
316
|
+
const prev = spreadKeys.get(el);
|
|
317
|
+
if (prev) {
|
|
318
|
+
for (const key of prev) {
|
|
319
|
+
if (next.has(key))
|
|
320
|
+
continue;
|
|
321
|
+
if (key.startsWith('on'))
|
|
322
|
+
krateOn(el, key, null);
|
|
323
|
+
else
|
|
324
|
+
setAttr(el, key, undefined);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
spreadKeys.set(el, next);
|
|
328
|
+
}));
|
|
329
|
+
}
|
|
271
330
|
let vnodeDepth = 0;
|
|
272
331
|
/** Evaluate `fn` with h() producing virtual nodes (see KrateVNode). */
|
|
273
332
|
export function withVNodes(fn) {
|
|
@@ -297,8 +356,43 @@ function isVNode(v) {
|
|
|
297
356
|
function isAdoptable(v) {
|
|
298
357
|
return !!v && typeof v === 'object' && v.__ka === 1;
|
|
299
358
|
}
|
|
359
|
+
const SVG_NS = 'http://www.w3.org/2000/svg';
|
|
360
|
+
// Tags that only exist in SVG. Ambiguous names (a, script, style, title) stay
|
|
361
|
+
// HTML here and are re-created in the SVG namespace when appended to an SVG
|
|
362
|
+
// parent (see adoptSvgChild).
|
|
363
|
+
const SVG_TAGS = new Set([
|
|
364
|
+
'svg', 'g', 'defs', 'symbol', 'use', 'switch', 'desc', 'metadata', 'path', 'circle', 'ellipse', 'line',
|
|
365
|
+
'polygon', 'polyline', 'rect', 'text', 'tspan', 'textPath', 'image', 'foreignObject', 'marker', 'mask',
|
|
366
|
+
'pattern', 'clipPath', 'linearGradient', 'radialGradient', 'stop', 'filter', 'feBlend', 'feColorMatrix',
|
|
367
|
+
'feComponentTransfer', 'feComposite', 'feConvolveMatrix', 'feDiffuseLighting', 'feDisplacementMap',
|
|
368
|
+
'feDistantLight', 'feDropShadow', 'feFlood', 'feFuncA', 'feFuncB', 'feFuncG', 'feFuncR',
|
|
369
|
+
'feGaussianBlur', 'feImage', 'feMerge', 'feMergeNode', 'feMorphology', 'feOffset', 'fePointLight',
|
|
370
|
+
'feSpecularLighting', 'feSpotLight', 'feTile', 'feTurbulence', 'animate', 'animateMotion',
|
|
371
|
+
'animateTransform', 'set', 'mpath', 'view',
|
|
372
|
+
]);
|
|
373
|
+
/** Creates an element in the namespace its tag belongs to. */
|
|
374
|
+
function createTagElement(tag) {
|
|
375
|
+
return SVG_TAGS.has(tag) ? document.createElementNS(SVG_NS, tag) : document.createElement(tag);
|
|
376
|
+
}
|
|
377
|
+
/** True when a child appended to parent belongs to the SVG namespace. */
|
|
378
|
+
function inSvg(parent) {
|
|
379
|
+
const p = parent;
|
|
380
|
+
return p.namespaceURI === SVG_NS && p.localName !== 'foreignObject';
|
|
381
|
+
}
|
|
382
|
+
/** Re-creates an HTML-namespace element (a, style, title, ...) as SVG. */
|
|
383
|
+
function adoptSvgChild(parent, child) {
|
|
384
|
+
const c = child;
|
|
385
|
+
if (c.nodeType !== 1 || c.namespaceURI === SVG_NS || !inSvg(parent))
|
|
386
|
+
return child;
|
|
387
|
+
const el = document.createElementNS(SVG_NS, c.localName);
|
|
388
|
+
for (const attr of Array.from(c.attributes))
|
|
389
|
+
el.setAttribute(attr.name, attr.value);
|
|
390
|
+
while (c.firstChild)
|
|
391
|
+
el.appendChild(c.firstChild);
|
|
392
|
+
return el;
|
|
393
|
+
}
|
|
300
394
|
function buildElement(tag, props, children) {
|
|
301
|
-
const el =
|
|
395
|
+
const el = createTagElement(tag);
|
|
302
396
|
if (props)
|
|
303
397
|
applyProps(el, props);
|
|
304
398
|
if (!props || !props.dangerouslySetInnerHTML) {
|
|
@@ -330,14 +424,14 @@ export function appendChild(parent, child) {
|
|
|
330
424
|
return;
|
|
331
425
|
}
|
|
332
426
|
if (isVNode(child)) {
|
|
333
|
-
parent.appendChild(materialize(child));
|
|
427
|
+
parent.appendChild(adoptSvgChild(parent, materialize(child)));
|
|
334
428
|
return;
|
|
335
429
|
}
|
|
336
430
|
if (isAdoptable(child)) {
|
|
337
431
|
parent.appendChild(child.mount());
|
|
338
432
|
return;
|
|
339
433
|
}
|
|
340
|
-
parent.appendChild(child instanceof Node ? child : document.createTextNode(String(child)));
|
|
434
|
+
parent.appendChild(child instanceof Node ? adoptSvgChild(parent, child) : document.createTextNode(String(child)));
|
|
341
435
|
}
|
|
342
436
|
/** Move the disposers tracked on one node to another. */
|
|
343
437
|
function retrack(from, to) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
2
|
+
import { createSignal, createEffect, disposeAll, krateFlushSync, krateOnSettle, setOwnerHooks, } from './signal';
|
|
3
|
+
// Settle hooks cannot be unregistered; this one counts flushes for the whole file.
|
|
4
|
+
let settleRuns = 0;
|
|
5
|
+
let settleThrows = false;
|
|
6
|
+
let settleAfterThrow = 0;
|
|
7
|
+
krateOnSettle(() => {
|
|
8
|
+
settleRuns++;
|
|
9
|
+
});
|
|
10
|
+
krateOnSettle(() => {
|
|
11
|
+
if (settleThrows)
|
|
12
|
+
throw new Error('settle boom');
|
|
13
|
+
});
|
|
14
|
+
krateOnSettle(() => {
|
|
15
|
+
settleAfterThrow++;
|
|
16
|
+
});
|
|
17
|
+
describe('a throwing effect does not strand the others', () => {
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
disposeAll();
|
|
20
|
+
settleRuns = 0;
|
|
21
|
+
settleThrows = false;
|
|
22
|
+
settleAfterThrow = 0;
|
|
23
|
+
});
|
|
24
|
+
afterEach(() => {
|
|
25
|
+
setOwnerHooks(null);
|
|
26
|
+
vi.restoreAllMocks();
|
|
27
|
+
});
|
|
28
|
+
it('runs the effects queued behind it, now and on later updates', () => {
|
|
29
|
+
const [a, setA] = createSignal(0);
|
|
30
|
+
const seen = [];
|
|
31
|
+
createEffect(() => {
|
|
32
|
+
if (a() > 0)
|
|
33
|
+
throw new Error('boom ' + a());
|
|
34
|
+
});
|
|
35
|
+
createEffect(() => {
|
|
36
|
+
seen.push(a());
|
|
37
|
+
});
|
|
38
|
+
expect(seen).toEqual([0]);
|
|
39
|
+
setA(1);
|
|
40
|
+
expect(() => krateFlushSync()).toThrow('boom 1');
|
|
41
|
+
expect(seen).toEqual([0, 1]);
|
|
42
|
+
// The second effect was not left marked as queued: it still reacts.
|
|
43
|
+
setA(2);
|
|
44
|
+
expect(() => krateFlushSync()).toThrow('boom 2');
|
|
45
|
+
expect(seen).toEqual([0, 1, 2]);
|
|
46
|
+
});
|
|
47
|
+
it('keeps an effect before and after the thrower working', () => {
|
|
48
|
+
const [a, setA] = createSignal(0);
|
|
49
|
+
const log = [];
|
|
50
|
+
createEffect(() => log.push('first ' + a()));
|
|
51
|
+
createEffect(() => {
|
|
52
|
+
if (a() === 1)
|
|
53
|
+
throw new Error('middle');
|
|
54
|
+
});
|
|
55
|
+
createEffect(() => log.push('last ' + a()));
|
|
56
|
+
setA(1);
|
|
57
|
+
expect(() => krateFlushSync()).toThrow('middle');
|
|
58
|
+
setA(2);
|
|
59
|
+
krateFlushSync();
|
|
60
|
+
expect(log).toEqual(['first 0', 'last 0', 'first 1', 'last 1', 'first 2', 'last 2']);
|
|
61
|
+
});
|
|
62
|
+
it('rethrows the first error and logs the others', () => {
|
|
63
|
+
const logged = vi.spyOn(console, 'error').mockImplementation(() => { });
|
|
64
|
+
const [a, setA] = createSignal(0);
|
|
65
|
+
createEffect(() => {
|
|
66
|
+
if (a() > 0)
|
|
67
|
+
throw new Error('one');
|
|
68
|
+
});
|
|
69
|
+
createEffect(() => {
|
|
70
|
+
if (a() > 0)
|
|
71
|
+
throw new Error('two');
|
|
72
|
+
});
|
|
73
|
+
setA(1);
|
|
74
|
+
expect(() => krateFlushSync()).toThrow('one');
|
|
75
|
+
expect(logged).toHaveBeenCalledTimes(1);
|
|
76
|
+
expect(String(logged.mock.calls[0][0].message)).toBe('two');
|
|
77
|
+
});
|
|
78
|
+
it('still runs the settle hooks before rethrowing', () => {
|
|
79
|
+
const [a, setA] = createSignal(0);
|
|
80
|
+
createEffect(() => {
|
|
81
|
+
if (a() > 0)
|
|
82
|
+
throw new Error('boom');
|
|
83
|
+
});
|
|
84
|
+
setA(1);
|
|
85
|
+
const before = settleRuns;
|
|
86
|
+
expect(() => krateFlushSync()).toThrow('boom');
|
|
87
|
+
expect(settleRuns).toBe(before + 1);
|
|
88
|
+
});
|
|
89
|
+
it('reports a throw from a settle hook after running the others', () => {
|
|
90
|
+
settleThrows = true;
|
|
91
|
+
const [a, setA] = createSignal(0);
|
|
92
|
+
createEffect(() => void a());
|
|
93
|
+
setA(1);
|
|
94
|
+
expect(() => krateFlushSync()).toThrow('settle boom');
|
|
95
|
+
expect(settleAfterThrow).toBe(1);
|
|
96
|
+
});
|
|
97
|
+
it('hands owned errors to the boundary and does not throw', () => {
|
|
98
|
+
const handled = [];
|
|
99
|
+
setOwnerHooks({
|
|
100
|
+
current: () => 'boundary',
|
|
101
|
+
run: (_o, fn) => fn(),
|
|
102
|
+
cleanup: () => { },
|
|
103
|
+
error: (_o, err) => {
|
|
104
|
+
handled.push(err);
|
|
105
|
+
return true;
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
const [a, setA] = createSignal(0);
|
|
109
|
+
const seen = [];
|
|
110
|
+
createEffect(() => {
|
|
111
|
+
if (a() > 0)
|
|
112
|
+
throw new Error('owned');
|
|
113
|
+
});
|
|
114
|
+
createEffect(() => {
|
|
115
|
+
seen.push(a());
|
|
116
|
+
});
|
|
117
|
+
setA(1);
|
|
118
|
+
expect(() => krateFlushSync()).not.toThrow();
|
|
119
|
+
expect(handled).toHaveLength(1);
|
|
120
|
+
expect(seen).toEqual([0, 1]);
|
|
121
|
+
});
|
|
122
|
+
it('rethrows an owned error the boundary declines', () => {
|
|
123
|
+
setOwnerHooks({
|
|
124
|
+
current: () => 'boundary',
|
|
125
|
+
run: (_o, fn) => fn(),
|
|
126
|
+
cleanup: () => { },
|
|
127
|
+
error: () => false,
|
|
128
|
+
});
|
|
129
|
+
const [a, setA] = createSignal(0);
|
|
130
|
+
const seen = [];
|
|
131
|
+
createEffect(() => {
|
|
132
|
+
if (a() > 0)
|
|
133
|
+
throw new Error('declined');
|
|
134
|
+
});
|
|
135
|
+
createEffect(() => {
|
|
136
|
+
seen.push(a());
|
|
137
|
+
});
|
|
138
|
+
setA(1);
|
|
139
|
+
expect(() => krateFlushSync()).toThrow('declined');
|
|
140
|
+
expect(seen).toEqual([0, 1]);
|
|
141
|
+
});
|
|
142
|
+
it('reports the effect error first when an update loop aborts the same flush', () => {
|
|
143
|
+
vi.spyOn(console, 'error').mockImplementation(() => { });
|
|
144
|
+
const [a, setA] = createSignal(0);
|
|
145
|
+
const [b, setB] = createSignal(0);
|
|
146
|
+
createEffect(() => {
|
|
147
|
+
if (a() > 0)
|
|
148
|
+
throw new Error('thrower');
|
|
149
|
+
});
|
|
150
|
+
createEffect(() => setB(a() + 1));
|
|
151
|
+
createEffect(() => setA(b() + 1));
|
|
152
|
+
expect(() => krateFlushSync()).toThrow('thrower');
|
|
153
|
+
// The loop was dropped: the queue is empty and effects still react.
|
|
154
|
+
const seen = [];
|
|
155
|
+
const [c, setC] = createSignal(0);
|
|
156
|
+
createEffect(() => {
|
|
157
|
+
seen.push(c());
|
|
158
|
+
});
|
|
159
|
+
setC(5);
|
|
160
|
+
krateFlushSync();
|
|
161
|
+
expect(seen).toEqual([0, 5]);
|
|
162
|
+
});
|
|
163
|
+
});
|
package/dist/effects.d.ts
CHANGED
|
@@ -75,6 +75,14 @@ export declare function flushPassive(): void;
|
|
|
75
75
|
* tracked; only the dependencies are.
|
|
76
76
|
*/
|
|
77
77
|
export declare function createMemoDeps<T>(fn: () => T, deps: () => unknown): () => T;
|
|
78
|
+
/**
|
|
79
|
+
* React 19.2's useEffectEvent: a function that always runs the latest closure
|
|
80
|
+
* and never subscribes its caller. Components run once and read props and
|
|
81
|
+
* state through live getters, so `fn` already sees the latest values; the
|
|
82
|
+
* wrapper only keeps the reads it makes from tracking, so calling the event
|
|
83
|
+
* from an effect does not make the effect depend on them.
|
|
84
|
+
*/
|
|
85
|
+
export declare function createEffectEvent<T extends (...args: never[]) => unknown>(fn: T): T;
|
|
78
86
|
/**
|
|
79
87
|
* React's useCallback with a dependency array: a getter for the callback
|
|
80
88
|
* whose identity only changes when the dependency array changes.
|
package/dist/effects.js
CHANGED
|
@@ -640,6 +640,19 @@ export function createMemoDeps(fn, deps) {
|
|
|
640
640
|
return value;
|
|
641
641
|
};
|
|
642
642
|
}
|
|
643
|
+
/**
|
|
644
|
+
* React 19.2's useEffectEvent: a function that always runs the latest closure
|
|
645
|
+
* and never subscribes its caller. Components run once and read props and
|
|
646
|
+
* state through live getters, so `fn` already sees the latest values; the
|
|
647
|
+
* wrapper only keeps the reads it makes from tracking, so calling the event
|
|
648
|
+
* from an effect does not make the effect depend on them.
|
|
649
|
+
*/
|
|
650
|
+
export function createEffectEvent(fn) {
|
|
651
|
+
const event = function (...args) {
|
|
652
|
+
return untrack(() => fn.apply(this, args));
|
|
653
|
+
};
|
|
654
|
+
return event;
|
|
655
|
+
}
|
|
643
656
|
/**
|
|
644
657
|
* React's useCallback with a dependency array: a getter for the callback
|
|
645
658
|
* whose identity only changes when the dependency array changes.
|