@krate/runtime 0.5.0-alpha.2 → 0.5.0-alpha.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/action-context.d.ts +56 -0
- package/dist/action-context.js +73 -0
- package/dist/action-context.test.d.ts +1 -0
- package/dist/action-context.test.js +47 -0
- package/dist/actions.d.ts +120 -0
- package/dist/actions.js +550 -0
- package/dist/actions.test.d.ts +1 -0
- package/dist/actions.test.js +417 -0
- package/dist/boundary.d.ts +121 -0
- package/dist/boundary.js +429 -0
- package/dist/cache-scope.d.ts +118 -0
- package/dist/cache-scope.js +311 -0
- package/dist/cache.d.ts +55 -0
- package/dist/cache.js +155 -0
- package/dist/cache.test.d.ts +1 -0
- package/dist/cache.test.js +217 -0
- package/dist/class-component.d.ts +66 -0
- package/dist/class-component.js +633 -0
- package/dist/class-component.test.d.ts +1 -0
- package/dist/class-component.test.js +411 -0
- package/dist/context.d.ts +13 -0
- package/dist/context.js +58 -32
- package/dist/context.test.d.ts +1 -0
- package/dist/context.test.js +57 -0
- package/dist/derived.test.d.ts +1 -0
- package/dist/derived.test.js +140 -0
- package/dist/discrete.test.d.ts +1 -0
- package/dist/discrete.test.js +100 -0
- package/dist/dom.d.ts +55 -0
- package/dist/dom.js +414 -63
- package/dist/effects.d.ts +88 -0
- package/dist/effects.js +790 -0
- package/dist/effects.test.d.ts +1 -0
- package/dist/effects.test.js +485 -0
- package/dist/events.d.ts +58 -0
- package/dist/events.js +503 -0
- package/dist/events.test.d.ts +1 -0
- package/dist/events.test.js +447 -0
- package/dist/index.d.ts +14 -4
- package/dist/index.js +9 -3
- package/dist/jsx-types.d.ts +2 -0
- package/dist/krate-hydrate-lite.js +7 -1
- package/dist/krate-hydrate.js +7 -1
- package/dist/krate-runtime.js +7 -1
- package/dist/krate-suspense.js +2 -0
- package/dist/lanes.test.d.ts +1 -0
- package/dist/lanes.test.js +59 -0
- package/dist/list.d.ts +20 -0
- package/dist/list.js +466 -0
- package/dist/list.test.d.ts +1 -0
- package/dist/list.test.js +344 -0
- package/dist/optimistic-lanes.test.d.ts +1 -0
- package/dist/optimistic-lanes.test.js +45 -0
- package/dist/portal.d.ts +47 -0
- package/dist/portal.js +189 -0
- package/dist/portal.test.d.ts +1 -0
- package/dist/portal.test.js +99 -0
- package/dist/react-effects.test.d.ts +1 -0
- package/dist/react-effects.test.js +96 -0
- package/dist/react-suspense.test.d.ts +1 -0
- package/dist/react-suspense.test.js +194 -0
- package/dist/react.d.ts +132 -32
- package/dist/react.js +335 -81
- package/dist/react.test.js +12 -2
- package/dist/reconcile.js +13 -0
- package/dist/refs.d.ts +33 -0
- package/dist/refs.js +142 -0
- package/dist/refs.test.d.ts +1 -0
- package/dist/refs.test.js +257 -0
- package/dist/request-context.d.ts +62 -0
- package/dist/request-context.js +107 -0
- package/dist/resource.js +20 -1
- package/dist/router-layout.test.d.ts +1 -0
- package/dist/router-layout.test.js +179 -0
- package/dist/router-lite.d.ts +25 -0
- package/dist/router-lite.js +73 -0
- package/dist/router-nav.test.d.ts +1 -0
- package/dist/router-nav.test.js +399 -0
- package/dist/router-slots.test.d.ts +1 -0
- package/dist/router-slots.test.js +144 -0
- package/dist/router.d.ts +66 -2
- package/dist/router.js +1535 -650
- package/dist/server-async.test.d.ts +1 -0
- package/dist/server-async.test.js +174 -0
- package/dist/server-renderer.js +494 -119
- package/dist/server.d.ts +27 -1
- package/dist/server.js +187 -1
- package/dist/server.test.js +20 -0
- package/dist/signal-lanes.test.d.ts +1 -0
- package/dist/signal-lanes.test.js +32 -0
- package/dist/signal.d.ts +186 -1
- package/dist/signal.js +896 -66
- package/dist/slots.d.ts +22 -0
- package/dist/slots.js +134 -0
- package/dist/slots.test.d.ts +1 -0
- package/dist/slots.test.js +169 -0
- package/dist/snapshots.test.d.ts +1 -0
- package/dist/snapshots.test.js +208 -0
- package/dist/suspense-actions.test.d.ts +1 -0
- package/dist/suspense-actions.test.js +72 -0
- package/dist/suspense.d.ts +220 -0
- package/dist/suspense.js +901 -0
- package/dist/suspense.test.d.ts +1 -0
- package/dist/suspense.test.js +518 -0
- package/package.json +5 -1
package/dist/refs.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React ref semantics for elements and imperative handles.
|
|
3
|
+
*
|
|
4
|
+
* - Object refs get `current = node` on attach and `current = null` on detach.
|
|
5
|
+
* - Callback refs are called with the node on attach. On detach, a React 19
|
|
6
|
+
* cleanup function returned by the callback is called instead; otherwise the
|
|
7
|
+
* callback is called with null.
|
|
8
|
+
* - A ref that is neither (a plain variable target) uses the `assign` setter.
|
|
9
|
+
*
|
|
10
|
+
* Every attached element ref is registered so a conditional branch swapped out
|
|
11
|
+
* of the document detaches its refs and swapping it back re-attaches them
|
|
12
|
+
* (krateRefsLeave / krateRefsEnter). Refs are called untracked, so a callback
|
|
13
|
+
* reading signals never subscribes the surrounding effect.
|
|
14
|
+
*/
|
|
15
|
+
import { createEffect } from './signal.js';
|
|
16
|
+
const live = new Set();
|
|
17
|
+
function untracked(fn) {
|
|
18
|
+
// A throwaway effect gives fn its own (discarded) tracking scope.
|
|
19
|
+
let err;
|
|
20
|
+
let failed = false;
|
|
21
|
+
createEffect(() => {
|
|
22
|
+
try {
|
|
23
|
+
fn();
|
|
24
|
+
}
|
|
25
|
+
catch (x) {
|
|
26
|
+
err = x;
|
|
27
|
+
failed = true;
|
|
28
|
+
}
|
|
29
|
+
})();
|
|
30
|
+
if (failed)
|
|
31
|
+
throw err;
|
|
32
|
+
}
|
|
33
|
+
function attach(e) {
|
|
34
|
+
if (e.on)
|
|
35
|
+
return;
|
|
36
|
+
e.on = true;
|
|
37
|
+
const r = e.ref;
|
|
38
|
+
untracked(() => {
|
|
39
|
+
if (typeof r === 'function') {
|
|
40
|
+
const ret = r(e.el);
|
|
41
|
+
e.undo = typeof ret === 'function' ? ret : () => r(null);
|
|
42
|
+
}
|
|
43
|
+
else if (r && typeof r === 'object') {
|
|
44
|
+
r.current = e.el;
|
|
45
|
+
e.undo = () => {
|
|
46
|
+
r.current = null;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
else if (e.assign) {
|
|
50
|
+
const set = e.assign;
|
|
51
|
+
set(e.el);
|
|
52
|
+
e.undo = () => set(null);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
e.undo = null;
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
function detach(e) {
|
|
60
|
+
if (!e.on)
|
|
61
|
+
return;
|
|
62
|
+
e.on = false;
|
|
63
|
+
const undo = e.undo;
|
|
64
|
+
e.undo = null;
|
|
65
|
+
if (undo)
|
|
66
|
+
untracked(undo);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Attaches `value` (an element or an imperative handle) to `ref` and returns
|
|
70
|
+
* the detach function. Used by useImperativeHandle.
|
|
71
|
+
*/
|
|
72
|
+
export function krateSetRef(ref, value) {
|
|
73
|
+
const e = { el: value, ref, on: false, undo: null };
|
|
74
|
+
attach(e);
|
|
75
|
+
return () => detach(e);
|
|
76
|
+
}
|
|
77
|
+
// Hydration refs waiting for krateRefsFlush; null when no hydration script is
|
|
78
|
+
// holding them.
|
|
79
|
+
let deferred = null;
|
|
80
|
+
/**
|
|
81
|
+
* Starts holding hydration refs (called at the start of a page's hydration
|
|
82
|
+
* script) until krateRefsFlush. Components mounted later attach at once.
|
|
83
|
+
*/
|
|
84
|
+
export function krateRefsHold() {
|
|
85
|
+
if (deferred)
|
|
86
|
+
return;
|
|
87
|
+
deferred = [];
|
|
88
|
+
// Safety net if the script stops before its flush.
|
|
89
|
+
queueMicrotask(krateRefsFlush);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Attaches the hydration refs queued so far whose element is in the
|
|
93
|
+
* document. The hydration script calls it after every component has bound its
|
|
94
|
+
* DOM (and before the effect commit), so a ref on server-rendered content that
|
|
95
|
+
* a binding replaced (a re-rendered list) is never attached, as in React.
|
|
96
|
+
*/
|
|
97
|
+
export function krateRefsFlush() {
|
|
98
|
+
const list = deferred;
|
|
99
|
+
deferred = null;
|
|
100
|
+
if (!list)
|
|
101
|
+
return;
|
|
102
|
+
for (const e of list) {
|
|
103
|
+
if (live.has(e) && e.el.isConnected)
|
|
104
|
+
attach(e);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Registers an element ref. mode 'now' attaches immediately (a node built by
|
|
109
|
+
* h() that is about to be inserted); 'defer' queues it for krateRefsFlush
|
|
110
|
+
* while a hydration script holds refs (krateRefsHold); otherwise it attaches when the element is in the document. A
|
|
111
|
+
* ref on a detached node waits until a conditional branch holding it is
|
|
112
|
+
* shown. Returns the release function (detach plus unregister), safe to call
|
|
113
|
+
* more than once.
|
|
114
|
+
*/
|
|
115
|
+
export function krateAttachRef(el, ref, assign, mode) {
|
|
116
|
+
const e = { el, ref, assign, on: false, undo: null };
|
|
117
|
+
live.add(e);
|
|
118
|
+
if (mode === 'defer' && deferred) {
|
|
119
|
+
deferred.push(e);
|
|
120
|
+
}
|
|
121
|
+
else if (mode === 'now' || el.isConnected) {
|
|
122
|
+
attach(e);
|
|
123
|
+
}
|
|
124
|
+
return () => {
|
|
125
|
+
live.delete(e);
|
|
126
|
+
detach(e);
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
/** Detaches refs of registered elements inside `root` (a branch leaving the document). */
|
|
130
|
+
export function krateRefsLeave(root) {
|
|
131
|
+
for (const e of live) {
|
|
132
|
+
if (e.on && root.contains(e.el))
|
|
133
|
+
detach(e);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/** Attaches registered refs whose element is now in the document. */
|
|
137
|
+
export function krateRefsEnter() {
|
|
138
|
+
for (const e of live) {
|
|
139
|
+
if (!e.on && e.el.isConnected)
|
|
140
|
+
attach(e);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach } from 'vitest';
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import { dirname, resolve } from 'node:path';
|
|
5
|
+
import { krateAttachRef, krateSetRef, krateRefsFlush, krateRefsHold } from './refs';
|
|
6
|
+
import { krateBindRef, h, disposeNode, krateOn, krateControl } from './dom';
|
|
7
|
+
import { createSignal, createEffect, onCleanup, disposeAll } from './signal';
|
|
8
|
+
import { krateCondSwap, krateCondDetached, krateCondReset, krateSanitizeAttr } from './slots';
|
|
9
|
+
import { useImperativeHandle, createRef } from './react';
|
|
10
|
+
const tick = () => new Promise((r) => setTimeout(r, 0));
|
|
11
|
+
function bootstrap() {
|
|
12
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
13
|
+
const src = readFileSync(resolve(here, '../../compiler/internal/renderer/newhydrate.go'), 'utf8');
|
|
14
|
+
const start = src.indexOf('const HydrationBootstrapJS = `') + 'const HydrationBootstrapJS = `'.length;
|
|
15
|
+
const end = src.indexOf('`\n', start);
|
|
16
|
+
new Function(src.slice(start, end))();
|
|
17
|
+
}
|
|
18
|
+
describe('element refs', () => {
|
|
19
|
+
beforeEach(() => {
|
|
20
|
+
document.body.innerHTML = '<div id="root"><input id="i"></div>';
|
|
21
|
+
});
|
|
22
|
+
it('sets and clears object refs', () => {
|
|
23
|
+
const el = document.getElementById('i');
|
|
24
|
+
const ref = { current: null };
|
|
25
|
+
const release = krateAttachRef(el, ref);
|
|
26
|
+
expect(ref.current).toBe(el);
|
|
27
|
+
release();
|
|
28
|
+
expect(ref.current).toBe(null);
|
|
29
|
+
});
|
|
30
|
+
it('calls callback refs with the node, then null', () => {
|
|
31
|
+
const el = document.getElementById('i');
|
|
32
|
+
const calls = [];
|
|
33
|
+
const release = krateAttachRef(el, (n) => {
|
|
34
|
+
calls.push(n);
|
|
35
|
+
});
|
|
36
|
+
release();
|
|
37
|
+
release();
|
|
38
|
+
expect(calls).toEqual([el, null]);
|
|
39
|
+
});
|
|
40
|
+
it('calls a React 19 cleanup function instead of null', () => {
|
|
41
|
+
const el = document.getElementById('i');
|
|
42
|
+
const log = [];
|
|
43
|
+
const release = krateAttachRef(el, () => {
|
|
44
|
+
log.push('attach');
|
|
45
|
+
return () => log.push('cleanup');
|
|
46
|
+
});
|
|
47
|
+
release();
|
|
48
|
+
expect(log).toEqual(['attach', 'cleanup']);
|
|
49
|
+
});
|
|
50
|
+
it('re-binds when the callback identity changes', async () => {
|
|
51
|
+
const el = document.getElementById('i');
|
|
52
|
+
const log = [];
|
|
53
|
+
const a = (n) => log.push('a ' + (n ? 'node' : 'null'));
|
|
54
|
+
const b = (n) => log.push('b ' + (n ? 'node' : 'null'));
|
|
55
|
+
const [which, setWhich] = createSignal(a);
|
|
56
|
+
const dispose = krateBindRef(el, () => which());
|
|
57
|
+
krateRefsFlush();
|
|
58
|
+
setWhich(() => b);
|
|
59
|
+
await tick();
|
|
60
|
+
dispose();
|
|
61
|
+
expect(log).toEqual(['a node', 'a null', 'b node', 'b null']);
|
|
62
|
+
});
|
|
63
|
+
it('does not subscribe to signals read inside a callback ref', async () => {
|
|
64
|
+
const el = document.getElementById('i');
|
|
65
|
+
const [n, setN] = createSignal(0);
|
|
66
|
+
let calls = 0;
|
|
67
|
+
krateBindRef(el, () => () => {
|
|
68
|
+
calls++;
|
|
69
|
+
n();
|
|
70
|
+
});
|
|
71
|
+
setN(1);
|
|
72
|
+
await tick();
|
|
73
|
+
expect(calls).toBe(1);
|
|
74
|
+
});
|
|
75
|
+
it('assigns plain variable targets through the setter', () => {
|
|
76
|
+
const el = document.getElementById('i');
|
|
77
|
+
let target = undefined;
|
|
78
|
+
krateRefsHold();
|
|
79
|
+
const dispose = krateBindRef(el, () => undefined, (v) => {
|
|
80
|
+
target = v;
|
|
81
|
+
});
|
|
82
|
+
// Hydration binds queue until the flush at the end of the page script.
|
|
83
|
+
expect(target).toBe(undefined);
|
|
84
|
+
krateRefsFlush();
|
|
85
|
+
expect(target).toBe(el);
|
|
86
|
+
dispose();
|
|
87
|
+
expect(target).toBe(null);
|
|
88
|
+
});
|
|
89
|
+
it('keeps the right element per list item and detaches removed items', async () => {
|
|
90
|
+
const root = document.getElementById('root');
|
|
91
|
+
const refs = new Map();
|
|
92
|
+
const [items, setItems] = createSignal([1, 2, 3]);
|
|
93
|
+
const ul = document.createElement('ul');
|
|
94
|
+
root.appendChild(ul);
|
|
95
|
+
createEffect(() => {
|
|
96
|
+
while (ul.firstChild) {
|
|
97
|
+
disposeNode(ul.firstChild);
|
|
98
|
+
ul.removeChild(ul.firstChild);
|
|
99
|
+
}
|
|
100
|
+
for (const i of items()) {
|
|
101
|
+
ul.appendChild(h('li', { ref: (el) => (el ? refs.set(i, el) : refs.delete(i)) }, String(i)));
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
expect([...refs.keys()]).toEqual([1, 2, 3]);
|
|
105
|
+
expect(refs.get(2).textContent).toBe('2');
|
|
106
|
+
setItems([1, 3]);
|
|
107
|
+
await tick();
|
|
108
|
+
expect([...refs.keys()].sort()).toEqual([1, 3]);
|
|
109
|
+
expect(refs.get(3).textContent).toBe('3');
|
|
110
|
+
});
|
|
111
|
+
it('releases h() refs when the node is disposed', () => {
|
|
112
|
+
const ref = { current: null };
|
|
113
|
+
const el = h('div', { ref });
|
|
114
|
+
expect(ref.current).toBe(el);
|
|
115
|
+
disposeNode(el);
|
|
116
|
+
expect(ref.current).toBe(null);
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
describe('imperative handles', () => {
|
|
120
|
+
it('attaches the handle and re-creates it when deps change', async () => {
|
|
121
|
+
const ref = createRef();
|
|
122
|
+
const [n, setN] = createSignal(1);
|
|
123
|
+
let creates = 0;
|
|
124
|
+
const [other, setOther] = createSignal(0);
|
|
125
|
+
createEffect(() => {
|
|
126
|
+
useImperativeHandle(ref, () => {
|
|
127
|
+
creates++;
|
|
128
|
+
other();
|
|
129
|
+
return { v: n() };
|
|
130
|
+
}, () => [n()]);
|
|
131
|
+
});
|
|
132
|
+
// Handles attach in the layout phase of the commit (a microtask).
|
|
133
|
+
expect(ref.current).toBe(null);
|
|
134
|
+
await Promise.resolve();
|
|
135
|
+
expect(ref.current).toEqual({ v: 1 });
|
|
136
|
+
setOther(1);
|
|
137
|
+
await tick();
|
|
138
|
+
expect(creates).toBe(1);
|
|
139
|
+
setN(2);
|
|
140
|
+
await tick();
|
|
141
|
+
expect(ref.current).toEqual({ v: 2 });
|
|
142
|
+
expect(creates).toBe(2);
|
|
143
|
+
});
|
|
144
|
+
it('gives callback refs the handle and cleans up', () => {
|
|
145
|
+
const log = [];
|
|
146
|
+
const undo = krateSetRef((v) => {
|
|
147
|
+
log.push(v);
|
|
148
|
+
}, { focus: 1 });
|
|
149
|
+
undo();
|
|
150
|
+
expect(log).toEqual([{ focus: 1 }, null]);
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
describe('hydration bootstrap refs and events', () => {
|
|
154
|
+
beforeEach(() => {
|
|
155
|
+
const w = window;
|
|
156
|
+
Object.assign(w, {
|
|
157
|
+
createEffect,
|
|
158
|
+
onCleanup,
|
|
159
|
+
createSignal,
|
|
160
|
+
krateCondSwap,
|
|
161
|
+
krateCondDetached,
|
|
162
|
+
krateCondReset,
|
|
163
|
+
krateSanitizeAttr,
|
|
164
|
+
krateBindRef,
|
|
165
|
+
krateOn,
|
|
166
|
+
krateControl,
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
it('attaches refs before queued effects and detaches on teardown', () => {
|
|
170
|
+
document.body.innerHTML = '<div id="root"><input data-k="k:i"></div>';
|
|
171
|
+
bootstrap();
|
|
172
|
+
const w = window;
|
|
173
|
+
const ref = { current: null };
|
|
174
|
+
const log = [];
|
|
175
|
+
w.kbindRef('i', () => ref, (v) => (ref.current = v));
|
|
176
|
+
krateRefsFlush();
|
|
177
|
+
createEffect(() => {
|
|
178
|
+
log.push(ref.current);
|
|
179
|
+
});
|
|
180
|
+
expect(log[0]).toBe(document.querySelector('input'));
|
|
181
|
+
disposeAll();
|
|
182
|
+
expect(ref.current).toBe(null);
|
|
183
|
+
});
|
|
184
|
+
it('detaches refs of a hidden conditional branch and re-attaches when shown', async () => {
|
|
185
|
+
document.body.innerHTML =
|
|
186
|
+
'<div id="root"><p><!--k:c--><span data-k="k:s">on</span><!--/k:c--></p>' +
|
|
187
|
+
'<template data-kc="c" data-kb="a"><i data-k="k:o">off</i></template></div>';
|
|
188
|
+
bootstrap();
|
|
189
|
+
const w = window;
|
|
190
|
+
const log = [];
|
|
191
|
+
const [on, setOn] = createSignal(true);
|
|
192
|
+
w.kbindCond('c', () => on());
|
|
193
|
+
w.kbindRef('s', () => (el) => log.push('s ' + (el ? el.tagName : 'null')));
|
|
194
|
+
w.kbindRef('o', () => (el) => log.push('o ' + (el ? el.tagName : 'null')));
|
|
195
|
+
krateRefsFlush();
|
|
196
|
+
expect(log).toEqual(['s SPAN']);
|
|
197
|
+
setOn(false);
|
|
198
|
+
await tick();
|
|
199
|
+
expect(log).toEqual(['s SPAN', 's null', 'o I']);
|
|
200
|
+
setOn(true);
|
|
201
|
+
await tick();
|
|
202
|
+
expect(log).toEqual(['s SPAN', 's null', 'o I', 'o null', 's SPAN']);
|
|
203
|
+
disposeAll();
|
|
204
|
+
});
|
|
205
|
+
it('stringifies boolean aria-* and data-* attribute bindings', async () => {
|
|
206
|
+
document.body.innerHTML = '<div id="root"><input data-k="k:i"></div>';
|
|
207
|
+
bootstrap();
|
|
208
|
+
const [bad, setBad] = createSignal(false);
|
|
209
|
+
window.kbindAttr('i', 'aria-invalid', () => bad());
|
|
210
|
+
const input = document.querySelector('input');
|
|
211
|
+
expect(input.getAttribute('aria-invalid')).toBe('false');
|
|
212
|
+
setBad(true);
|
|
213
|
+
await tick();
|
|
214
|
+
expect(input.getAttribute('aria-invalid')).toBe('true');
|
|
215
|
+
disposeAll();
|
|
216
|
+
});
|
|
217
|
+
it('binds handlers and controlled values', () => {
|
|
218
|
+
document.body.innerHTML = '<div id="root"><input data-k="k:i" value="a"></div>';
|
|
219
|
+
bootstrap();
|
|
220
|
+
const w = window;
|
|
221
|
+
const log = [];
|
|
222
|
+
w.kbindHandler('i', 'onChange', (e) => log.push(e.type + ' ' + e.target.value));
|
|
223
|
+
w.kbindProp('i', 'value', () => 'a');
|
|
224
|
+
const input = document.querySelector('input');
|
|
225
|
+
input.value = 'ab';
|
|
226
|
+
input.dispatchEvent(new Event('input', { bubbles: true }));
|
|
227
|
+
expect(log).toEqual(['change ab']);
|
|
228
|
+
expect(input.value).toBe('a');
|
|
229
|
+
disposeAll();
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
describe('hydration ref queue', () => {
|
|
233
|
+
it('never attaches refs of server-rendered nodes replaced before the flush', () => {
|
|
234
|
+
document.body.innerHTML = '<div id="root"><ul><li id="ssr">a</li></ul></div>';
|
|
235
|
+
const log = [];
|
|
236
|
+
const ssr = document.getElementById('ssr');
|
|
237
|
+
krateRefsHold();
|
|
238
|
+
krateBindRef(ssr, () => (n) => {
|
|
239
|
+
log.push('ssr ' + (n ? 'node' : 'null'));
|
|
240
|
+
});
|
|
241
|
+
// A list binding replaces the server-rendered item before the flush.
|
|
242
|
+
disposeNode(ssr);
|
|
243
|
+
ssr.remove();
|
|
244
|
+
const fresh = h('li', { ref: (n) => log.push('fresh ' + (n ? 'node' : 'null')) });
|
|
245
|
+
document.querySelector('ul').appendChild(fresh);
|
|
246
|
+
krateRefsFlush();
|
|
247
|
+
expect(log).toEqual(['fresh node']);
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
describe('refs outside hydration', () => {
|
|
251
|
+
it('attach at once for a component mounted after hydration', () => {
|
|
252
|
+
document.body.innerHTML = '<div id="root"><input id="late"></div>';
|
|
253
|
+
const ref = { current: null };
|
|
254
|
+
krateBindRef(document.getElementById('late'), () => ref);
|
|
255
|
+
expect(ref.current).toBe(document.getElementById('late'));
|
|
256
|
+
});
|
|
257
|
+
});
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Request context for server components, exported from `@krate/runtime/server`.
|
|
3
|
+
*
|
|
4
|
+
* The SSR sidecar renders every page and region inside an AsyncLocalStorage
|
|
5
|
+
* scope and publishes it on `globalThis[Symbol.for('krate.request.store')]`.
|
|
6
|
+
* `headers()` and `cookies()` read that scope while a server component renders
|
|
7
|
+
* (and the action scope while a server action runs), so the same imports work
|
|
8
|
+
* in both places.
|
|
9
|
+
*/
|
|
10
|
+
/** The request a server component is rendering for. */
|
|
11
|
+
export interface RequestContext {
|
|
12
|
+
url: string;
|
|
13
|
+
method: string;
|
|
14
|
+
headers: Headers;
|
|
15
|
+
/** Request cookies parsed from the Cookie header. */
|
|
16
|
+
cookies: Map<string, string>;
|
|
17
|
+
params: Record<string, string>;
|
|
18
|
+
searchParams: Record<string, string>;
|
|
19
|
+
}
|
|
20
|
+
/** The current render request, or undefined outside a server render. */
|
|
21
|
+
export declare function getRequestContext(): RequestContext | undefined;
|
|
22
|
+
/** Parses a Cookie request header into name/value pairs. */
|
|
23
|
+
export declare function parseCookieHeader(header: string | null | undefined): Map<string, string>;
|
|
24
|
+
/**
|
|
25
|
+
* thenable makes a value usable both synchronously and with `await`, matching
|
|
26
|
+
* Next.js 15 where `params`, `searchParams`, `cookies()` and `headers()` are
|
|
27
|
+
* Promises: `params.slug` and `(await params).slug` both work. The `then` is
|
|
28
|
+
* non-enumerable, so spreading or JSON-serializing the value is unaffected, and
|
|
29
|
+
* it resolves to a plain copy so awaiting never loops.
|
|
30
|
+
*/
|
|
31
|
+
export declare function thenable<T extends object>(value: T, resolved: () => unknown): T & PromiseLike<T>;
|
|
32
|
+
/** thenableRecord wraps a plain record (route params, search params). */
|
|
33
|
+
export declare function thenableRecord<T extends Record<string, any>>(rec: T | undefined): T & PromiseLike<T>;
|
|
34
|
+
/** Read-only cookie view used while a server component renders. */
|
|
35
|
+
export interface RequestCookies {
|
|
36
|
+
get(name: string): {
|
|
37
|
+
name: string;
|
|
38
|
+
value: string;
|
|
39
|
+
} | undefined;
|
|
40
|
+
getAll(): Array<{
|
|
41
|
+
name: string;
|
|
42
|
+
value: string;
|
|
43
|
+
}>;
|
|
44
|
+
has(name: string): boolean;
|
|
45
|
+
set(name: string, value: string, options?: unknown): never;
|
|
46
|
+
delete(name: string, options?: unknown): never;
|
|
47
|
+
}
|
|
48
|
+
export declare function requestCookies(jar: Map<string, string>): RequestCookies;
|
|
49
|
+
/** Thrown by notFound(); the sidecar turns it into a 404 response. */
|
|
50
|
+
export interface NotFoundSignal extends Error {
|
|
51
|
+
__krateNotFound: true;
|
|
52
|
+
digest: string;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Stops rendering and responds with the 404 page. Works in server components
|
|
56
|
+
* (async pages and their children) rendered by the SSR sidecar.
|
|
57
|
+
*/
|
|
58
|
+
export declare function notFound(): never;
|
|
59
|
+
/** isNotFoundSignal reports whether err was thrown by notFound(). */
|
|
60
|
+
export declare function isNotFoundSignal(err: unknown): boolean;
|
|
61
|
+
/** redirectTarget returns the URL of a redirect() signal, or "" for other errors. */
|
|
62
|
+
export declare function redirectTarget(err: unknown): string;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Request context for server components, exported from `@krate/runtime/server`.
|
|
3
|
+
*
|
|
4
|
+
* The SSR sidecar renders every page and region inside an AsyncLocalStorage
|
|
5
|
+
* scope and publishes it on `globalThis[Symbol.for('krate.request.store')]`.
|
|
6
|
+
* `headers()` and `cookies()` read that scope while a server component renders
|
|
7
|
+
* (and the action scope while a server action runs), so the same imports work
|
|
8
|
+
* in both places.
|
|
9
|
+
*/
|
|
10
|
+
const REQUEST_STORE = Symbol.for('krate.request.store');
|
|
11
|
+
/** The current render request, or undefined outside a server render. */
|
|
12
|
+
export function getRequestContext() {
|
|
13
|
+
const store = globalThis[REQUEST_STORE];
|
|
14
|
+
return store && typeof store.getStore === 'function' ? store.getStore() : undefined;
|
|
15
|
+
}
|
|
16
|
+
/** Parses a Cookie request header into name/value pairs. */
|
|
17
|
+
export function parseCookieHeader(header) {
|
|
18
|
+
const out = new Map();
|
|
19
|
+
if (!header)
|
|
20
|
+
return out;
|
|
21
|
+
for (const part of header.split(';')) {
|
|
22
|
+
const eq = part.indexOf('=');
|
|
23
|
+
if (eq <= 0)
|
|
24
|
+
continue;
|
|
25
|
+
const name = part.slice(0, eq).trim();
|
|
26
|
+
let value = part.slice(eq + 1).trim();
|
|
27
|
+
if (value.startsWith('"') && value.endsWith('"'))
|
|
28
|
+
value = value.slice(1, -1);
|
|
29
|
+
try {
|
|
30
|
+
value = decodeURIComponent(value);
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
// keep the raw value when it is not valid percent-encoding
|
|
34
|
+
}
|
|
35
|
+
if (name && !out.has(name))
|
|
36
|
+
out.set(name, value);
|
|
37
|
+
}
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* thenable makes a value usable both synchronously and with `await`, matching
|
|
42
|
+
* Next.js 15 where `params`, `searchParams`, `cookies()` and `headers()` are
|
|
43
|
+
* Promises: `params.slug` and `(await params).slug` both work. The `then` is
|
|
44
|
+
* non-enumerable, so spreading or JSON-serializing the value is unaffected, and
|
|
45
|
+
* it resolves to a plain copy so awaiting never loops.
|
|
46
|
+
*/
|
|
47
|
+
export function thenable(value, resolved) {
|
|
48
|
+
Object.defineProperty(value, 'then', {
|
|
49
|
+
value: (onFulfilled, onRejected) => Promise.resolve(resolved()).then(onFulfilled, onRejected),
|
|
50
|
+
enumerable: false,
|
|
51
|
+
configurable: true,
|
|
52
|
+
});
|
|
53
|
+
return value;
|
|
54
|
+
}
|
|
55
|
+
/** thenableRecord wraps a plain record (route params, search params). */
|
|
56
|
+
export function thenableRecord(rec) {
|
|
57
|
+
const plain = { ...(rec || {}) };
|
|
58
|
+
return thenable({ ...plain }, () => ({ ...plain }));
|
|
59
|
+
}
|
|
60
|
+
export function requestCookies(jar) {
|
|
61
|
+
const readOnly = (op) => {
|
|
62
|
+
throw new Error(`cookies().${op}() cannot be called while a server component renders; ` +
|
|
63
|
+
`modify cookies in a server action ("use server") or an API route instead`);
|
|
64
|
+
};
|
|
65
|
+
const make = () => ({
|
|
66
|
+
get: (name) => (jar.has(name) ? { name, value: jar.get(name) } : undefined),
|
|
67
|
+
getAll: () => [...jar.entries()].map(([name, value]) => ({ name, value })),
|
|
68
|
+
has: (name) => jar.has(name),
|
|
69
|
+
set: () => readOnly('set'),
|
|
70
|
+
delete: () => readOnly('delete'),
|
|
71
|
+
});
|
|
72
|
+
return thenable(make(), make);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Stops rendering and responds with the 404 page. Works in server components
|
|
76
|
+
* (async pages and their children) rendered by the SSR sidecar.
|
|
77
|
+
*/
|
|
78
|
+
export function notFound() {
|
|
79
|
+
const err = new Error('NEXT_NOT_FOUND');
|
|
80
|
+
err.__krateNotFound = true;
|
|
81
|
+
err.digest = 'NEXT_NOT_FOUND';
|
|
82
|
+
throw err;
|
|
83
|
+
}
|
|
84
|
+
/** isNotFoundSignal reports whether err was thrown by notFound(). */
|
|
85
|
+
export function isNotFoundSignal(err) {
|
|
86
|
+
if (!err || typeof err !== 'object')
|
|
87
|
+
return false;
|
|
88
|
+
const e = err;
|
|
89
|
+
return e.__krateNotFound === true || e.digest === 'NEXT_NOT_FOUND' || e.message === 'NEXT_NOT_FOUND' ||
|
|
90
|
+
(typeof e.digest === 'string' && e.digest.startsWith('NEXT_HTTP_ERROR_FALLBACK;404'));
|
|
91
|
+
}
|
|
92
|
+
/** redirectTarget returns the URL of a redirect() signal, or "" for other errors. */
|
|
93
|
+
export function redirectTarget(err) {
|
|
94
|
+
if (!err || typeof err !== 'object')
|
|
95
|
+
return '';
|
|
96
|
+
const e = err;
|
|
97
|
+
if (typeof e.__krateRedirect === 'string')
|
|
98
|
+
return e.__krateRedirect;
|
|
99
|
+
if (typeof e.message === 'string' && e.message.startsWith('NEXT_REDIRECT:'))
|
|
100
|
+
return e.message.slice('NEXT_REDIRECT:'.length);
|
|
101
|
+
if (typeof e.digest === 'string' && e.digest.startsWith('NEXT_REDIRECT;')) {
|
|
102
|
+
const parts = e.digest.split(';');
|
|
103
|
+
if (parts.length >= 3)
|
|
104
|
+
return parts[2];
|
|
105
|
+
}
|
|
106
|
+
return '';
|
|
107
|
+
}
|
package/dist/resource.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { createSignal, createEffect, onCleanup } from './signal.js';
|
|
2
|
+
function suspenseHook() {
|
|
3
|
+
return globalThis.__krateSuspenseHook;
|
|
4
|
+
}
|
|
2
5
|
export function createResource(source, fetcher) {
|
|
3
6
|
const [data, setData] = createSignal(undefined);
|
|
4
7
|
const [loading, setLoading] = createSignal(true);
|
|
@@ -6,10 +9,22 @@ export function createResource(source, fetcher) {
|
|
|
6
9
|
const [state, setState] = createSignal('unresolved');
|
|
7
10
|
const [version, setVersion] = createSignal(0);
|
|
8
11
|
let abortController;
|
|
12
|
+
const owner = suspenseHook()?.current() ?? null;
|
|
13
|
+
let inflight = null;
|
|
14
|
+
let notified = null;
|
|
9
15
|
const getSourceValue = () => {
|
|
10
16
|
return typeof source === 'function' ? source() : source;
|
|
11
17
|
};
|
|
12
|
-
const fetch =
|
|
18
|
+
const fetch = () => {
|
|
19
|
+
const run = doFetch();
|
|
20
|
+
inflight = run;
|
|
21
|
+
run.then(() => {
|
|
22
|
+
if (inflight === run)
|
|
23
|
+
inflight = null;
|
|
24
|
+
});
|
|
25
|
+
return run;
|
|
26
|
+
};
|
|
27
|
+
const doFetch = async () => {
|
|
13
28
|
if (abortController) {
|
|
14
29
|
abortController.abort();
|
|
15
30
|
}
|
|
@@ -61,6 +76,10 @@ export function createResource(source, fetcher) {
|
|
|
61
76
|
// as a plain data property, snapshotting state/loading/error once at creation
|
|
62
77
|
// so they never track reactive updates. defineProperties installs live accessors.
|
|
63
78
|
const resource = function () {
|
|
79
|
+
if (owner && inflight && notified !== inflight) {
|
|
80
|
+
notified = inflight;
|
|
81
|
+
suspenseHook()?.suspend(owner, inflight);
|
|
82
|
+
}
|
|
64
83
|
return data();
|
|
65
84
|
};
|
|
66
85
|
Object.defineProperties(resource, {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|