@krate/runtime 0.5.0-beta.3 → 0.5.0-rc.10
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.js +1 -1
- package/dist/actions.d.ts +7 -0
- package/dist/actions.js +34 -10
- package/dist/after.d.ts +28 -0
- package/dist/after.js +48 -0
- package/dist/assets-static.d.ts +10 -1
- package/dist/assets-static.js +10 -2
- package/dist/base-path.d.ts +7 -0
- package/dist/base-path.js +20 -0
- package/dist/boundary.js +1 -1
- package/dist/builtin-tags.d.ts +12 -0
- package/dist/builtin-tags.js +42 -0
- package/dist/cache-adapter.js +1 -1
- package/dist/cache-scope.d.ts +8 -0
- package/dist/cache-scope.js +14 -1
- package/dist/cloudflare-workers.d.ts +2 -0
- package/dist/cloudflare-workers.js +27 -0
- package/dist/context.d.ts +5 -0
- package/dist/context.js +28 -17
- package/dist/dev-overlay.d.ts +54 -3
- package/dist/dev-overlay.js +496 -91
- package/dist/dom.d.ts +4 -0
- package/dist/dom.js +62 -16
- package/dist/effects.js +43 -10
- package/dist/events.d.ts +2 -0
- package/dist/events.js +18 -0
- package/dist/flight-props.d.ts +7 -0
- package/dist/flight-props.js +105 -0
- package/dist/handler-html.js +4 -4
- package/dist/handler-routing.d.ts +2 -23
- package/dist/handler-routing.js +9 -190
- package/dist/handler-segments.d.ts +6 -0
- package/dist/handler-segments.js +18 -4
- package/dist/head-tags.d.ts +9 -0
- package/dist/head-tags.js +93 -6
- package/dist/highlight-bind.js +1 -1
- package/dist/highlight.d.ts +3 -2
- package/dist/highlight.js +17 -12
- package/dist/hints.d.ts +25 -2
- package/dist/hints.js +147 -74
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -0
- package/dist/island-swap.d.ts +47 -0
- package/dist/island-swap.js +635 -0
- package/dist/jsx-dev-runtime.d.ts +1 -1
- package/dist/jsx-types.d.ts +99 -3
- package/dist/krate-core.js +1 -1
- package/dist/krate-dev.js +407 -80
- package/dist/krate-f-actions.js +1 -1
- package/dist/krate-f-classes.js +1 -1
- package/dist/krate-f-highlight.js +1 -1
- package/dist/krate-f-list.js +1 -1
- package/dist/krate-f-navigation.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 +455 -0
- package/dist/krate-hydrate-lite.js +5 -5
- package/dist/krate-hydrate.js +5 -5
- package/dist/krate-island-swap.js +1 -0
- package/dist/krate-runtime.js +5 -5
- package/dist/krate-suspense.js +1 -1
- package/dist/list.d.ts +14 -4
- package/dist/list.js +30 -23
- package/dist/metadata.js +5 -2
- package/dist/navigation-react.d.ts +13 -0
- package/dist/navigation-react.js +86 -0
- package/dist/navigation.d.ts +19 -0
- package/dist/navigation.js +35 -0
- package/dist/node-server.js +3 -3
- package/dist/og.js +1 -1
- package/dist/platform.d.ts +52 -0
- package/dist/platform.js +57 -0
- package/dist/react-compat.d.ts +39 -0
- package/dist/react-compat.js +359 -0
- package/dist/react-server.d.ts +63 -0
- package/dist/react-server.js +125 -0
- package/dist/region-wire.d.ts +10 -0
- package/dist/region-wire.js +154 -0
- package/dist/request-context.js +1 -1
- package/dist/request-extras.d.ts +4 -0
- package/dist/request-extras.js +51 -0
- package/dist/resp.js +1 -1
- package/dist/route-source.d.ts +39 -0
- package/dist/route-source.js +532 -0
- package/dist/router-lite.js +3 -9
- package/dist/router.d.ts +1 -1
- package/dist/router.js +675 -100
- package/dist/server-handler.d.ts +69 -1
- package/dist/server-handler.js +515 -96
- package/dist/server-renderer.js +16 -7
- package/dist/server.d.ts +73 -16
- package/dist/server.js +619 -102
- package/dist/sha256.js +9 -9
- package/dist/signal.js +47 -20
- package/dist/site-config.d.ts +15 -0
- package/dist/site-config.js +28 -0
- package/dist/slot.d.ts +12 -0
- package/dist/slot.js +23 -0
- package/dist/ssr-core.d.ts +2 -0
- package/dist/ssr-core.js +12 -4
- package/dist/suspense.js +34 -2
- package/dist/testing.d.ts +62 -0
- package/dist/testing.js +90 -0
- package/dist/use-id.js +1 -1
- package/package.json +28 -3
- package/dist/action-context.test.d.ts +0 -1
- package/dist/action-context.test.js +0 -72
- package/dist/actions.test.d.ts +0 -1
- package/dist/actions.test.js +0 -477
- package/dist/cache-adapter.test.d.ts +0 -1
- package/dist/cache-adapter.test.js +0 -244
- package/dist/cache.test.d.ts +0 -1
- package/dist/cache.test.js +0 -233
- package/dist/class-component.test.d.ts +0 -1
- package/dist/class-component.test.js +0 -462
- package/dist/class-helpers.test.d.ts +0 -1
- package/dist/class-helpers.test.js +0 -169
- package/dist/context.test.d.ts +0 -1
- package/dist/context.test.js +0 -57
- package/dist/derived.test.d.ts +0 -1
- package/dist/derived.test.js +0 -140
- package/dist/dev-overlay.test.d.ts +0 -1
- package/dist/dev-overlay.test.js +0 -152
- package/dist/discrete.test.d.ts +0 -1
- package/dist/discrete.test.js +0 -100
- package/dist/dom.test.d.ts +0 -1
- package/dist/dom.test.js +0 -31
- package/dist/effect-errors.test.d.ts +0 -1
- package/dist/effect-errors.test.js +0 -163
- package/dist/effects.test.d.ts +0 -1
- package/dist/effects.test.js +0 -485
- package/dist/events.test.d.ts +0 -1
- package/dist/events.test.js +0 -447
- package/dist/handler-html.test.d.ts +0 -1
- package/dist/handler-html.test.js +0 -256
- package/dist/handler-routing.test.d.ts +0 -1
- package/dist/handler-routing.test.js +0 -99
- package/dist/handler-segments.test.d.ts +0 -1
- package/dist/handler-segments.test.js +0 -217
- package/dist/head-tags.test.d.ts +0 -1
- package/dist/head-tags.test.js +0 -131
- package/dist/highlight-bind.test.d.ts +0 -1
- package/dist/highlight-bind.test.js +0 -124
- package/dist/highlight.test.d.ts +0 -1
- package/dist/highlight.test.js +0 -56
- package/dist/lanes.test.d.ts +0 -1
- package/dist/lanes.test.js +0 -59
- package/dist/list.test.d.ts +0 -1
- package/dist/list.test.js +0 -344
- package/dist/metadata.test.d.ts +0 -1
- package/dist/metadata.test.js +0 -167
- package/dist/navigation.test.d.ts +0 -1
- package/dist/navigation.test.js +0 -130
- package/dist/node-server.test.d.ts +0 -1
- package/dist/node-server.test.js +0 -492
- package/dist/optimistic-lanes.test.d.ts +0 -1
- package/dist/optimistic-lanes.test.js +0 -45
- package/dist/patterns.test.d.ts +0 -1
- package/dist/patterns.test.js +0 -98
- package/dist/portal.test.d.ts +0 -1
- package/dist/portal.test.js +0 -99
- package/dist/react-effects.test.d.ts +0 -1
- package/dist/react-effects.test.js +0 -96
- package/dist/react-suspense.test.d.ts +0 -1
- package/dist/react-suspense.test.js +0 -194
- package/dist/react.test.d.ts +0 -1
- package/dist/react.test.js +0 -60
- package/dist/react19.test.d.ts +0 -1
- package/dist/react19.test.js +0 -176
- package/dist/reactivity-core.test.d.ts +0 -1
- package/dist/reactivity-core.test.js +0 -152
- package/dist/reconcile.test.d.ts +0 -1
- package/dist/reconcile.test.js +0 -110
- package/dist/refs.test.d.ts +0 -1
- package/dist/refs.test.js +0 -257
- package/dist/router-layout.test.d.ts +0 -1
- package/dist/router-layout.test.js +0 -249
- package/dist/router-nav.test.d.ts +0 -1
- package/dist/router-nav.test.js +0 -464
- package/dist/router-slots.test.d.ts +0 -1
- package/dist/router-slots.test.js +0 -144
- package/dist/router-speculation.test.d.ts +0 -1
- package/dist/router-speculation.test.js +0 -25
- package/dist/router-styles.test.d.ts +0 -1
- package/dist/router-styles.test.js +0 -98
- package/dist/router.test.d.ts +0 -1
- package/dist/router.test.js +0 -36
- package/dist/server-async.test.d.ts +0 -1
- package/dist/server-async.test.js +0 -216
- package/dist/server-handler.test.d.ts +0 -1
- package/dist/server-handler.test.js +0 -535
- package/dist/server-highlight.test.d.ts +0 -1
- package/dist/server-highlight.test.js +0 -32
- package/dist/server-renderer.test.d.ts +0 -1
- package/dist/server-renderer.test.js +0 -266
- package/dist/server.test.d.ts +0 -1
- package/dist/server.test.js +0 -88
- package/dist/sha256.test.d.ts +0 -1
- package/dist/sha256.test.js +0 -13
- package/dist/signal-lanes.test.d.ts +0 -1
- package/dist/signal-lanes.test.js +0 -32
- package/dist/signal.test.d.ts +0 -1
- package/dist/signal.test.js +0 -68
- package/dist/slots.test.d.ts +0 -1
- package/dist/slots.test.js +0 -169
- package/dist/snapshots.test.d.ts +0 -1
- package/dist/snapshots.test.js +0 -208
- package/dist/ssr-core-behavior.test.d.ts +0 -1
- package/dist/ssr-core-behavior.test.js +0 -620
- package/dist/ssr-core.test.d.ts +0 -1
- package/dist/ssr-core.test.js +0 -324
- package/dist/static-memory.test.d.ts +0 -1
- package/dist/static-memory.test.js +0 -26
- package/dist/storage.test.d.ts +0 -1
- package/dist/storage.test.js +0 -46
- package/dist/suspense-actions.test.d.ts +0 -1
- package/dist/suspense-actions.test.js +0 -72
- package/dist/suspense.test.d.ts +0 -1
- package/dist/suspense.test.js +0 -518
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
// React 19 APIs that preact/compat lacks, for React-model code that runs on
|
|
2
|
+
// Preact: a "use client" module, an island, an npm React library. The
|
|
3
|
+
// compiler's island builds re-export these next to everything preact/compat
|
|
4
|
+
// provides, as `react` (and `useFormStatus` as `react-dom`).
|
|
5
|
+
//
|
|
6
|
+
// The module takes the bundle's own compat namespace and Preact `options`, so
|
|
7
|
+
// it never imports Preact and stays on the project's single Preact instance.
|
|
8
|
+
const IDLE = Object.freeze({ pending: false, data: null, method: null, action: null });
|
|
9
|
+
const EMPTY = Object.freeze([]);
|
|
10
|
+
function isThenable(v) {
|
|
11
|
+
return !!v && typeof v.then === 'function';
|
|
12
|
+
}
|
|
13
|
+
function reportError(err) {
|
|
14
|
+
const g = globalThis;
|
|
15
|
+
if (typeof g.reportError === 'function')
|
|
16
|
+
g.reportError(err);
|
|
17
|
+
else
|
|
18
|
+
console.error(err);
|
|
19
|
+
}
|
|
20
|
+
const SHARED = '__krateReactCompat';
|
|
21
|
+
/**
|
|
22
|
+
* Builds the additions once per Preact instance: the `react` and `react-dom`
|
|
23
|
+
* modules of a bundle (and of every island chunk) share one action scope and
|
|
24
|
+
* one form-status context.
|
|
25
|
+
*/
|
|
26
|
+
export function createReactCompatExtras(c, options) {
|
|
27
|
+
if (options[SHARED])
|
|
28
|
+
return options[SHARED];
|
|
29
|
+
const extras = build(c, options);
|
|
30
|
+
options[SHARED] = extras;
|
|
31
|
+
return extras;
|
|
32
|
+
}
|
|
33
|
+
function build(c, options) {
|
|
34
|
+
const { useState, useRef, useEffect, useCallback, useContext, createContext, createElement } = c;
|
|
35
|
+
// Action scope: optimistic updates stay applied while any transition or
|
|
36
|
+
// form action is pending and are dropped when the last one settles.
|
|
37
|
+
let open = 0;
|
|
38
|
+
const settled = new Set();
|
|
39
|
+
const begin = () => {
|
|
40
|
+
open++;
|
|
41
|
+
let done = false;
|
|
42
|
+
return () => {
|
|
43
|
+
if (done)
|
|
44
|
+
return;
|
|
45
|
+
done = true;
|
|
46
|
+
if (--open === 0)
|
|
47
|
+
for (const fn of Array.from(settled))
|
|
48
|
+
fn();
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
function startTransition(fn) {
|
|
52
|
+
const end = begin();
|
|
53
|
+
let r;
|
|
54
|
+
try {
|
|
55
|
+
r = fn();
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
end();
|
|
59
|
+
throw err;
|
|
60
|
+
}
|
|
61
|
+
if (isThenable(r))
|
|
62
|
+
r.then(end, (err) => { end(); reportError(err); });
|
|
63
|
+
else
|
|
64
|
+
end();
|
|
65
|
+
}
|
|
66
|
+
function useTransition() {
|
|
67
|
+
const [pending, setPending] = useState(false);
|
|
68
|
+
const start = useCallback((fn) => {
|
|
69
|
+
const end = begin();
|
|
70
|
+
let r;
|
|
71
|
+
try {
|
|
72
|
+
r = fn();
|
|
73
|
+
}
|
|
74
|
+
catch (err) {
|
|
75
|
+
end();
|
|
76
|
+
throw err;
|
|
77
|
+
}
|
|
78
|
+
if (!isThenable(r)) {
|
|
79
|
+
end();
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
setPending(true);
|
|
83
|
+
const finish = () => { setPending(false); end(); };
|
|
84
|
+
r.then(finish, (err) => { finish(); reportError(err); });
|
|
85
|
+
}, []);
|
|
86
|
+
return [pending, start];
|
|
87
|
+
}
|
|
88
|
+
function useOptimistic(passthrough, reducer) {
|
|
89
|
+
const [updates, setUpdates] = useState(EMPTY);
|
|
90
|
+
useEffect(() => {
|
|
91
|
+
const revert = () => setUpdates(EMPTY);
|
|
92
|
+
settled.add(revert);
|
|
93
|
+
return () => { settled.delete(revert); };
|
|
94
|
+
}, []);
|
|
95
|
+
let value = passthrough;
|
|
96
|
+
for (const a of updates)
|
|
97
|
+
value = reducer ? reducer(value, a) : a;
|
|
98
|
+
const add = useCallback((action) => {
|
|
99
|
+
setUpdates((u) => u.concat([action]));
|
|
100
|
+
// An update made outside any transition is dropped right away.
|
|
101
|
+
if (open === 0) {
|
|
102
|
+
Promise.resolve().then(() => { if (open === 0)
|
|
103
|
+
setUpdates(EMPTY); });
|
|
104
|
+
}
|
|
105
|
+
}, []);
|
|
106
|
+
return [value, add];
|
|
107
|
+
}
|
|
108
|
+
const StatusCtx = createContext(IDLE);
|
|
109
|
+
function FormScope(props) {
|
|
110
|
+
const [status, setStatus] = useState(IDLE);
|
|
111
|
+
props.box.set = setStatus;
|
|
112
|
+
return createElement(StatusCtx.Provider, { value: status }, props.children);
|
|
113
|
+
}
|
|
114
|
+
const isPlainAction = (f) => typeof f === 'function' && typeof f.$$krateAction !== 'string';
|
|
115
|
+
function withRef(vnode, attach) {
|
|
116
|
+
const orig = vnode.ref;
|
|
117
|
+
vnode.ref = (el) => {
|
|
118
|
+
if (el)
|
|
119
|
+
attach(el);
|
|
120
|
+
if (typeof orig === 'function')
|
|
121
|
+
orig(el);
|
|
122
|
+
else if (orig && typeof orig === 'object')
|
|
123
|
+
orig.current = el;
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
function onSubmit(e) {
|
|
127
|
+
const form = e.target;
|
|
128
|
+
if (!form || form.tagName !== 'FORM' || e.defaultPrevented)
|
|
129
|
+
return;
|
|
130
|
+
const submitter = e.submitter || null;
|
|
131
|
+
const box = form.__krateForm;
|
|
132
|
+
const fn = (submitter && submitter.__krateFormAction) || (box && box.fn);
|
|
133
|
+
if (typeof fn !== 'function')
|
|
134
|
+
return;
|
|
135
|
+
e.preventDefault();
|
|
136
|
+
let data;
|
|
137
|
+
try {
|
|
138
|
+
data = submitter ? new FormData(form, submitter) : new FormData(form);
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
data = new FormData(form);
|
|
142
|
+
}
|
|
143
|
+
const end = begin();
|
|
144
|
+
if (box && box.set)
|
|
145
|
+
box.set({ pending: true, data, method: 'post', action: fn });
|
|
146
|
+
let ok = false;
|
|
147
|
+
Promise.resolve()
|
|
148
|
+
.then(() => fn(data))
|
|
149
|
+
.then(() => { ok = true; }, reportError)
|
|
150
|
+
.then(() => {
|
|
151
|
+
if (box && box.set)
|
|
152
|
+
box.set(IDLE);
|
|
153
|
+
end();
|
|
154
|
+
if (ok && form.isConnected)
|
|
155
|
+
form.reset();
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
{
|
|
159
|
+
const prev = options.vnode;
|
|
160
|
+
options.vnode = (vnode) => {
|
|
161
|
+
const p = vnode.props;
|
|
162
|
+
const t = vnode.type;
|
|
163
|
+
if (p && vnode.ref != null && typeof t === 'function' && !t._forwarded && !(t.prototype && t.prototype.render) && t !== c.Fragment) {
|
|
164
|
+
// React 19: a function component receives ref as an ordinary prop.
|
|
165
|
+
p.ref = vnode.ref;
|
|
166
|
+
vnode.ref = null;
|
|
167
|
+
}
|
|
168
|
+
if (p && typeof vnode.type === 'string') {
|
|
169
|
+
if (vnode.type === 'form' && isPlainAction(p.action)) {
|
|
170
|
+
const box = { fn: p.action };
|
|
171
|
+
p.action = undefined;
|
|
172
|
+
withRef(vnode, (el) => { el.__krateForm = box; });
|
|
173
|
+
p.children = createElement(FormScope, { box }, p.children);
|
|
174
|
+
}
|
|
175
|
+
else if (isPlainAction(p.formAction)) {
|
|
176
|
+
const fn = p.formAction;
|
|
177
|
+
p.formAction = undefined;
|
|
178
|
+
withRef(vnode, (el) => { el.__krateFormAction = fn; });
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (prev)
|
|
182
|
+
prev(vnode);
|
|
183
|
+
};
|
|
184
|
+
if (typeof document !== 'undefined')
|
|
185
|
+
document.addEventListener('submit', onSubmit, false);
|
|
186
|
+
}
|
|
187
|
+
function useFormStatus() {
|
|
188
|
+
return useContext(StatusCtx);
|
|
189
|
+
}
|
|
190
|
+
function requestFormReset(form) {
|
|
191
|
+
form.reset();
|
|
192
|
+
}
|
|
193
|
+
function useActionState(action, initialState, _permalink) {
|
|
194
|
+
const [state, setState] = useState(initialState);
|
|
195
|
+
const [pending, setPending] = useState(false);
|
|
196
|
+
const [failure, setFailure] = useState(null);
|
|
197
|
+
const live = useRef(null);
|
|
198
|
+
if (!live.current)
|
|
199
|
+
live.current = { state, action, queued: 0, chain: Promise.resolve(), cancelled: false };
|
|
200
|
+
const l = live.current;
|
|
201
|
+
l.action = action;
|
|
202
|
+
l.state = state;
|
|
203
|
+
const dispatch = useCallback((payload) => {
|
|
204
|
+
l.queued++;
|
|
205
|
+
setPending(true);
|
|
206
|
+
const end = begin();
|
|
207
|
+
const run = l.chain.then(async () => {
|
|
208
|
+
if (l.cancelled)
|
|
209
|
+
return;
|
|
210
|
+
const next = await l.action(l.state, payload);
|
|
211
|
+
l.state = next;
|
|
212
|
+
setState(() => next);
|
|
213
|
+
});
|
|
214
|
+
l.chain = run.then(undefined, (error) => {
|
|
215
|
+
// An error cancels the queued calls and surfaces at the nearest error boundary.
|
|
216
|
+
l.cancelled = true;
|
|
217
|
+
setFailure({ error });
|
|
218
|
+
});
|
|
219
|
+
return l.chain.then(() => {
|
|
220
|
+
if (--l.queued === 0) {
|
|
221
|
+
l.cancelled = false;
|
|
222
|
+
setPending(false);
|
|
223
|
+
}
|
|
224
|
+
end();
|
|
225
|
+
});
|
|
226
|
+
}, []);
|
|
227
|
+
if (failure)
|
|
228
|
+
throw failure.error;
|
|
229
|
+
return [state, dispatch, pending];
|
|
230
|
+
}
|
|
231
|
+
function use(usable) {
|
|
232
|
+
if (isThenable(usable)) {
|
|
233
|
+
const p = usable;
|
|
234
|
+
if (p.status === 'fulfilled')
|
|
235
|
+
return p.value;
|
|
236
|
+
if (p.status === 'rejected')
|
|
237
|
+
throw p.reason;
|
|
238
|
+
if (p.status !== 'pending') {
|
|
239
|
+
p.status = 'pending';
|
|
240
|
+
p.then((v) => { p.status = 'fulfilled'; p.value = v; }, (e) => { p.status = 'rejected'; p.reason = e; });
|
|
241
|
+
}
|
|
242
|
+
// preact/compat's Suspense catches a thrown promise and re-renders when it settles.
|
|
243
|
+
throw p;
|
|
244
|
+
}
|
|
245
|
+
// Preact's createContext returns a function carrying Provider and Consumer.
|
|
246
|
+
if (usable && (typeof usable === 'object' || typeof usable === 'function') && 'Provider' in usable)
|
|
247
|
+
return useContext(usable);
|
|
248
|
+
throw new Error('use() received something that is neither a promise nor a context.');
|
|
249
|
+
}
|
|
250
|
+
function cache(fn) {
|
|
251
|
+
const root = new Map();
|
|
252
|
+
const result = Symbol('result');
|
|
253
|
+
return function (...args) {
|
|
254
|
+
let node = root;
|
|
255
|
+
for (const a of args) {
|
|
256
|
+
let next = node.get(a);
|
|
257
|
+
if (!next)
|
|
258
|
+
node.set(a, (next = new Map()));
|
|
259
|
+
node = next;
|
|
260
|
+
}
|
|
261
|
+
if (!node.has(result)) {
|
|
262
|
+
try {
|
|
263
|
+
node.set(result, { value: fn.apply(this, args) });
|
|
264
|
+
}
|
|
265
|
+
catch (error) {
|
|
266
|
+
node.set(result, { error, failed: true });
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
const r = node.get(result);
|
|
270
|
+
if (r.failed)
|
|
271
|
+
throw r.error;
|
|
272
|
+
return r.value;
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
function useEffectEvent(fn) {
|
|
276
|
+
const ref = useRef(fn);
|
|
277
|
+
ref.current = fn;
|
|
278
|
+
return useCallback((...args) => ref.current(...args), []);
|
|
279
|
+
}
|
|
280
|
+
return {
|
|
281
|
+
useOptimistic,
|
|
282
|
+
useActionState,
|
|
283
|
+
useFormState: useActionState,
|
|
284
|
+
useFormStatus,
|
|
285
|
+
requestFormReset,
|
|
286
|
+
use,
|
|
287
|
+
cache,
|
|
288
|
+
useEffectEvent,
|
|
289
|
+
startTransition,
|
|
290
|
+
useTransition,
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
// Events React treats as discrete: the effects of a render they cause are
|
|
294
|
+
// flushed at the end of its commit, not after paint.
|
|
295
|
+
const DISCRETE_EVENTS = [
|
|
296
|
+
'beforeinput', 'blur', 'change', 'click', 'compositionend', 'compositionstart', 'contextmenu', 'copy', 'cut',
|
|
297
|
+
'dblclick', 'dragend', 'dragstart', 'drop', 'focusin', 'focusout', 'input', 'keydown', 'keypress', 'keyup',
|
|
298
|
+
'mousedown', 'mouseup', 'paste', 'pointercancel', 'pointerdown', 'pointerup', 'reset', 'submit',
|
|
299
|
+
'touchcancel', 'touchend', 'touchstart',
|
|
300
|
+
];
|
|
301
|
+
/**
|
|
302
|
+
* React 18 flushes the passive effects of a render caused by a discrete event
|
|
303
|
+
* (a click, a key press, typing) synchronously at the end of its commit, and
|
|
304
|
+
* renders the updates those effects make in the same task. Preact runs them
|
|
305
|
+
* after the next frame, so a quick second event could render a component with
|
|
306
|
+
* a new prop and a state update from its previous effects at once: react-colorful
|
|
307
|
+
* then took its own color prop for user input and called onChange with a
|
|
308
|
+
* stale colour. While a discrete event's task runs, this flushes Preact's
|
|
309
|
+
* effect queue after every commit (Preact's render loop picks up the updates
|
|
310
|
+
* they make); otherwise effects keep running after paint. The compiler's
|
|
311
|
+
* client builds call it from their `react` module, once per Preact instance.
|
|
312
|
+
*/
|
|
313
|
+
export function installDiscreteEffects(options) {
|
|
314
|
+
if (typeof window === 'undefined' || typeof document === 'undefined' || options.__krateDiscrete)
|
|
315
|
+
return;
|
|
316
|
+
options.__krateDiscrete = true;
|
|
317
|
+
let discrete = false;
|
|
318
|
+
let clearing = false;
|
|
319
|
+
let flushEffects = null;
|
|
320
|
+
const onEvent = () => {
|
|
321
|
+
discrete = true;
|
|
322
|
+
if (clearing)
|
|
323
|
+
return;
|
|
324
|
+
clearing = true;
|
|
325
|
+
setTimeout(() => {
|
|
326
|
+
discrete = false;
|
|
327
|
+
clearing = false;
|
|
328
|
+
}, 0);
|
|
329
|
+
};
|
|
330
|
+
for (const type of DISCRETE_EVENTS)
|
|
331
|
+
window.addEventListener(type, onEvent, true);
|
|
332
|
+
// Preact's default afterNextFrame, used outside discrete events. The flush
|
|
333
|
+
// function preact/hooks hands over is always the same one, so it is kept.
|
|
334
|
+
const prevRaf = options.requestAnimationFrame;
|
|
335
|
+
options.requestAnimationFrame = (cb) => {
|
|
336
|
+
flushEffects = cb;
|
|
337
|
+
if (prevRaf)
|
|
338
|
+
return prevRaf(cb);
|
|
339
|
+
let raf = 0;
|
|
340
|
+
const done = () => {
|
|
341
|
+
clearTimeout(timeout);
|
|
342
|
+
if (typeof cancelAnimationFrame === 'function')
|
|
343
|
+
cancelAnimationFrame(raf);
|
|
344
|
+
setTimeout(cb);
|
|
345
|
+
};
|
|
346
|
+
const timeout = setTimeout(done, 100);
|
|
347
|
+
if (typeof requestAnimationFrame === 'function')
|
|
348
|
+
raf = requestAnimationFrame(done);
|
|
349
|
+
};
|
|
350
|
+
// options._commit (mangled __c) runs after refs and, through preact/hooks'
|
|
351
|
+
// own wrapper, after layout effects.
|
|
352
|
+
const prevCommit = options.__c;
|
|
353
|
+
options.__c = (vnode, queue) => {
|
|
354
|
+
if (prevCommit)
|
|
355
|
+
prevCommit(vnode, queue);
|
|
356
|
+
if (discrete && flushEffects)
|
|
357
|
+
flushEffects();
|
|
358
|
+
};
|
|
359
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `react` module of a server bundle (an SSR page or server component).
|
|
3
|
+
*
|
|
4
|
+
* The compiler maps `import { ... } from "react"` in a server bundle onto this
|
|
5
|
+
* module. It exports the part of React's API that has a meaning while a server
|
|
6
|
+
* renders: effects are no-ops (React's server renderer never runs them),
|
|
7
|
+
* `cache` memoizes per request, and the transition/optimistic/action hooks
|
|
8
|
+
* return their initial state. Names that would behave differently from React
|
|
9
|
+
* here (useState, useRef, createContext, ...) are not exported, so importing
|
|
10
|
+
* them fails the build instead of silently misbehaving.
|
|
11
|
+
*/
|
|
12
|
+
import { Suspense, Fragment, lazy, createSignal, createEffect, createMemo, onCleanup, onMount } from './server.js';
|
|
13
|
+
export { Suspense, Fragment, lazy, createSignal, createEffect, createMemo, onCleanup, onMount };
|
|
14
|
+
type AnyFn = (...args: any[]) => any;
|
|
15
|
+
/** Effects never run while a server renders. */
|
|
16
|
+
export declare function useEffect(_effect: () => unknown, _deps?: readonly unknown[] | null): void;
|
|
17
|
+
export declare function useLayoutEffect(_effect: () => unknown, _deps?: readonly unknown[] | null): void;
|
|
18
|
+
export declare function useInsertionEffect(_effect: () => unknown, _deps?: readonly unknown[] | null): void;
|
|
19
|
+
export declare function createElement(type: any, props?: Record<string, any> | null, ...children: unknown[]): unknown;
|
|
20
|
+
export declare function forwardRef<P = any>(render: (props: P, ref: unknown) => unknown): (props: P) => unknown;
|
|
21
|
+
export declare function memo<C>(component: C, _compare?: unknown): C;
|
|
22
|
+
export declare function startTransition(fn: () => unknown): void;
|
|
23
|
+
export declare function useTransition(): [boolean, (fn: () => unknown) => void];
|
|
24
|
+
export declare function useOptimistic<T>(passthrough: T, _reducer?: unknown): [T, (action: unknown) => void];
|
|
25
|
+
export declare function useActionState<S>(_action: unknown, initialState: S, _permalink?: string): [S, () => void, boolean];
|
|
26
|
+
/** Ids are unique within the process; server-rendered output is never hydrated by React. */
|
|
27
|
+
export declare function useId(): string;
|
|
28
|
+
/**
|
|
29
|
+
* use(promise) cannot suspend a synchronous server render. A promise that
|
|
30
|
+
* already settled through React's own bookkeeping is read; anything else
|
|
31
|
+
* points the author at an async component.
|
|
32
|
+
*/
|
|
33
|
+
export declare function use<T>(usable: PromiseLike<T> | {
|
|
34
|
+
_currentValue?: T;
|
|
35
|
+
defaultValue?: T;
|
|
36
|
+
}): T;
|
|
37
|
+
/**
|
|
38
|
+
* React's cache(): memoizes fn by argument identity for the request being
|
|
39
|
+
* rendered. Results and thrown errors are both remembered, so a promise
|
|
40
|
+
* returned by fn is shared by every caller in the request. Outside a render
|
|
41
|
+
* (an API route, module scope) there is no request to scope the memo to, so
|
|
42
|
+
* fn runs on every call, as in React.
|
|
43
|
+
*/
|
|
44
|
+
export declare function cache<F extends AnyFn>(fn: F): F;
|
|
45
|
+
declare const _default: {
|
|
46
|
+
Suspense: typeof Suspense;
|
|
47
|
+
Fragment: typeof Fragment;
|
|
48
|
+
lazy: typeof lazy;
|
|
49
|
+
createElement: typeof createElement;
|
|
50
|
+
forwardRef: typeof forwardRef;
|
|
51
|
+
memo: typeof memo;
|
|
52
|
+
cache: typeof cache;
|
|
53
|
+
use: typeof use;
|
|
54
|
+
useId: typeof useId;
|
|
55
|
+
startTransition: typeof startTransition;
|
|
56
|
+
useTransition: typeof useTransition;
|
|
57
|
+
useOptimistic: typeof useOptimistic;
|
|
58
|
+
useActionState: typeof useActionState;
|
|
59
|
+
useEffect: typeof useEffect;
|
|
60
|
+
useLayoutEffect: typeof useLayoutEffect;
|
|
61
|
+
useInsertionEffect: typeof useInsertionEffect;
|
|
62
|
+
};
|
|
63
|
+
export default _default;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `react` module of a server bundle (an SSR page or server component).
|
|
3
|
+
*
|
|
4
|
+
* The compiler maps `import { ... } from "react"` in a server bundle onto this
|
|
5
|
+
* module. It exports the part of React's API that has a meaning while a server
|
|
6
|
+
* renders: effects are no-ops (React's server renderer never runs them),
|
|
7
|
+
* `cache` memoizes per request, and the transition/optimistic/action hooks
|
|
8
|
+
* return their initial state. Names that would behave differently from React
|
|
9
|
+
* here (useState, useRef, createContext, ...) are not exported, so importing
|
|
10
|
+
* them fails the build instead of silently misbehaving.
|
|
11
|
+
*/
|
|
12
|
+
import { Suspense, Fragment, jsx, lazy, createSignal, createEffect, createMemo, onCleanup, onMount } from './server.js';
|
|
13
|
+
import { currentRenderScope } from './cache-scope.js';
|
|
14
|
+
export { Suspense, Fragment, lazy, createSignal, createEffect, createMemo, onCleanup, onMount };
|
|
15
|
+
/** Effects never run while a server renders. */
|
|
16
|
+
export function useEffect(_effect, _deps) { }
|
|
17
|
+
export function useLayoutEffect(_effect, _deps) { }
|
|
18
|
+
export function useInsertionEffect(_effect, _deps) { }
|
|
19
|
+
export function createElement(type, props, ...children) {
|
|
20
|
+
const p = { ...props };
|
|
21
|
+
delete p.key;
|
|
22
|
+
delete p.ref;
|
|
23
|
+
if (children.length === 1)
|
|
24
|
+
p.children = children[0];
|
|
25
|
+
else if (children.length > 1)
|
|
26
|
+
p.children = children;
|
|
27
|
+
return jsx(type, p);
|
|
28
|
+
}
|
|
29
|
+
export function forwardRef(render) {
|
|
30
|
+
return (props) => render(props, null);
|
|
31
|
+
}
|
|
32
|
+
export function memo(component, _compare) {
|
|
33
|
+
return component;
|
|
34
|
+
}
|
|
35
|
+
export function startTransition(fn) {
|
|
36
|
+
fn();
|
|
37
|
+
}
|
|
38
|
+
export function useTransition() {
|
|
39
|
+
return [false, startTransition];
|
|
40
|
+
}
|
|
41
|
+
export function useOptimistic(passthrough, _reducer) {
|
|
42
|
+
return [passthrough, () => { }];
|
|
43
|
+
}
|
|
44
|
+
export function useActionState(_action, initialState, _permalink) {
|
|
45
|
+
return [initialState, () => { }, false];
|
|
46
|
+
}
|
|
47
|
+
let idCounter = 0;
|
|
48
|
+
/** Ids are unique within the process; server-rendered output is never hydrated by React. */
|
|
49
|
+
export function useId() {
|
|
50
|
+
return ':R' + (idCounter++).toString(36) + ':';
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* use(promise) cannot suspend a synchronous server render. A promise that
|
|
54
|
+
* already settled through React's own bookkeeping is read; anything else
|
|
55
|
+
* points the author at an async component.
|
|
56
|
+
*/
|
|
57
|
+
export function use(usable) {
|
|
58
|
+
const u = usable;
|
|
59
|
+
if (u && typeof u.then === 'function') {
|
|
60
|
+
if (u.status === 'fulfilled')
|
|
61
|
+
return u.value;
|
|
62
|
+
if (u.status === 'rejected')
|
|
63
|
+
throw u.reason;
|
|
64
|
+
throw new Error('use(promise) cannot wait for a promise while a Krate server renders. Make the component async and await the promise.');
|
|
65
|
+
}
|
|
66
|
+
if (u && ('_currentValue' in u || 'defaultValue' in u))
|
|
67
|
+
return (u._currentValue ?? u.defaultValue);
|
|
68
|
+
throw new Error('use() received something that is neither a promise nor a context.');
|
|
69
|
+
}
|
|
70
|
+
const perRequest = new WeakMap();
|
|
71
|
+
/**
|
|
72
|
+
* React's cache(): memoizes fn by argument identity for the request being
|
|
73
|
+
* rendered. Results and thrown errors are both remembered, so a promise
|
|
74
|
+
* returned by fn is shared by every caller in the request. Outside a render
|
|
75
|
+
* (an API route, module scope) there is no request to scope the memo to, so
|
|
76
|
+
* fn runs on every call, as in React.
|
|
77
|
+
*/
|
|
78
|
+
export function cache(fn) {
|
|
79
|
+
const wrapped = function (...args) {
|
|
80
|
+
const scope = currentRenderScope();
|
|
81
|
+
if (!scope)
|
|
82
|
+
return fn.apply(this, args);
|
|
83
|
+
let byFn = perRequest.get(scope);
|
|
84
|
+
if (!byFn) {
|
|
85
|
+
byFn = new WeakMap();
|
|
86
|
+
perRequest.set(scope, byFn);
|
|
87
|
+
}
|
|
88
|
+
let node = byFn.get(wrapped);
|
|
89
|
+
if (!node) {
|
|
90
|
+
node = { done: false };
|
|
91
|
+
byFn.set(wrapped, node);
|
|
92
|
+
}
|
|
93
|
+
for (const arg of args) {
|
|
94
|
+
if (!node.next)
|
|
95
|
+
node.next = new Map();
|
|
96
|
+
let child = node.next.get(arg);
|
|
97
|
+
if (!child) {
|
|
98
|
+
child = { done: false };
|
|
99
|
+
node.next.set(arg, child);
|
|
100
|
+
}
|
|
101
|
+
node = child;
|
|
102
|
+
}
|
|
103
|
+
if (node.done) {
|
|
104
|
+
if (node.failed)
|
|
105
|
+
throw node.error;
|
|
106
|
+
return node.value;
|
|
107
|
+
}
|
|
108
|
+
try {
|
|
109
|
+
node.value = fn.apply(this, args);
|
|
110
|
+
}
|
|
111
|
+
catch (e) {
|
|
112
|
+
node.failed = true;
|
|
113
|
+
node.error = e;
|
|
114
|
+
node.done = true;
|
|
115
|
+
throw e;
|
|
116
|
+
}
|
|
117
|
+
node.done = true;
|
|
118
|
+
return node.value;
|
|
119
|
+
};
|
|
120
|
+
return wrapped;
|
|
121
|
+
}
|
|
122
|
+
export default {
|
|
123
|
+
Suspense, Fragment, lazy, createElement, forwardRef, memo, cache, use, useId, startTransition, useTransition,
|
|
124
|
+
useOptimistic, useActionState, useEffect, useLayoutEffect, useInsertionEffect,
|
|
125
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare const REGION_FRAMES_TYPE = "application/vnd.krate.frames";
|
|
2
|
+
/** True when the request's Accept header asks for the binary frames. */
|
|
3
|
+
export declare function wantsRegionFrames(accept: string | string[] | undefined): boolean;
|
|
4
|
+
/** Encodes one frame object (the shape the renderer writes as an NDJSON line). */
|
|
5
|
+
export declare function encodeFrame(frame: Record<string, any>): Buffer;
|
|
6
|
+
/**
|
|
7
|
+
* Decodes a whole response body of frames. The sidecar never reads frames; the
|
|
8
|
+
* tests and tooling do. A truncated or corrupt stream throws.
|
|
9
|
+
*/
|
|
10
|
+
export declare function decodeFrames(data: Uint8Array): Array<Record<string, any>>;
|