@krate/runtime 0.5.0-alpha.3 → 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 +9 -2
- package/dist/action-context.js +39 -3
- package/dist/actions.js +18 -1
- package/dist/actions.test.js +26 -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/effects.js +74 -3
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/krate-hydrate-lite.js +5 -5
- package/dist/krate-hydrate.js +5 -5
- package/dist/krate-runtime.js +5 -5
- package/dist/portal.d.ts +5 -1
- package/dist/portal.js +15 -4
- package/dist/request-context.d.ts +62 -0
- package/dist/request-context.js +107 -0
- 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 +455 -118
- package/dist/server.d.ts +23 -0
- package/dist/server.js +157 -0
- package/dist/signal.d.ts +38 -2
- package/dist/signal.js +150 -5
- package/dist/slots.d.ts +5 -1
- package/dist/slots.js +28 -2
- package/package.json +5 -1
package/dist/server.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
export { getActionContext, headers, cookies, redirect } from './action-context.js';
|
|
2
2
|
export type { ActionContext, ActionCookies, ActionCookieOptions } from './action-context.js';
|
|
3
|
+
export { revalidatePath, revalidateTag, unstable_cache, unstable_noStore } from './cache.js';
|
|
4
|
+
export type { RevalidatePathType, UnstableCacheOptions } from './cache.js';
|
|
5
|
+
export { getRequestContext, notFound } from './request-context.js';
|
|
6
|
+
export type { RequestContext, RequestCookies } from './request-context.js';
|
|
3
7
|
export declare function createSignal<T>(initial: T): [() => T, (v: T | ((prev: T) => T)) => void];
|
|
4
8
|
export declare function createEffect(_fn: () => void): () => void;
|
|
5
9
|
export declare function createMemo<T>(fn: () => T): () => T;
|
|
@@ -23,6 +27,16 @@ export declare function Fragment(_props: {
|
|
|
23
27
|
export declare function jsx(type: string | Function, props: Record<string, any>, _key?: string): JSXElement;
|
|
24
28
|
export declare const jsxs: typeof jsx;
|
|
25
29
|
export declare function renderToString(node: JSXNode): string;
|
|
30
|
+
/**
|
|
31
|
+
* renderToStringAsync renders a tree that may contain async (server)
|
|
32
|
+
* components. Every Promise-returning component is awaited, at any depth, and
|
|
33
|
+
* sibling subtrees resolve in parallel. Errors thrown by a component (or a
|
|
34
|
+
* rejected Promise) reject the returned Promise; nothing renders as "".
|
|
35
|
+
*
|
|
36
|
+
* <Suspense> renders its resolved children: the whole tree is awaited before
|
|
37
|
+
* any HTML is produced, so there is no fallback to show.
|
|
38
|
+
*/
|
|
39
|
+
export declare function renderToStringAsync(node: unknown): Promise<string>;
|
|
26
40
|
export declare function setStreamingResolved(v: boolean): void;
|
|
27
41
|
export declare function resetBoundaryCounter(): void;
|
|
28
42
|
interface SuspenseProps {
|
|
@@ -30,6 +44,15 @@ interface SuspenseProps {
|
|
|
30
44
|
children: JSXNode;
|
|
31
45
|
}
|
|
32
46
|
export declare function Suspense(props: SuspenseProps): JSXNode;
|
|
47
|
+
type ClientRender = (component: unknown, props: Record<string, unknown>, childrenHTML: string | null) => string;
|
|
48
|
+
/**
|
|
49
|
+
* clientBoundary wraps an export of a `"use client"` module for the server
|
|
50
|
+
* bundle of a page that renders server components. The server renders the
|
|
51
|
+
* component's initial HTML (render) into a <krate-island> marker carrying its
|
|
52
|
+
* serialized props; the per-module client chunk at src hydrates it in the
|
|
53
|
+
* browser. Children are rendered on the server and passed as HTML.
|
|
54
|
+
*/
|
|
55
|
+
export declare function clientBoundary(moduleId: string, exportName: string, src: string, impl: unknown, render: ClientRender): unknown;
|
|
33
56
|
export declare function Head(props: {
|
|
34
57
|
children?: JSXNode;
|
|
35
58
|
}): JSXNode;
|
package/dist/server.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// @krate/runtime/server - Server-side rendering runtime
|
|
2
2
|
// Provides the same API as the client runtime but renders to HTML strings.
|
|
3
3
|
export { getActionContext, headers, cookies, redirect } from './action-context.js';
|
|
4
|
+
export { revalidatePath, revalidateTag, unstable_cache, unstable_noStore } from './cache.js';
|
|
5
|
+
export { getRequestContext, notFound } from './request-context.js';
|
|
4
6
|
// Signal (server: returns initial value, no reactivity)
|
|
5
7
|
export function createSignal(initial) {
|
|
6
8
|
let value = initial;
|
|
@@ -104,9 +106,22 @@ function escapeHTML(str) {
|
|
|
104
106
|
.replace(/"/g, """)
|
|
105
107
|
.replace(/'/g, "'");
|
|
106
108
|
}
|
|
109
|
+
function isThenable(v) {
|
|
110
|
+
return v != null && (typeof v === "object" || typeof v === "function") && typeof v.then === "function";
|
|
111
|
+
}
|
|
112
|
+
function componentName(fn) {
|
|
113
|
+
return fn.displayName || fn.name || "anonymous";
|
|
114
|
+
}
|
|
115
|
+
function asyncInSyncRender(where) {
|
|
116
|
+
return new Error(`${where} returned a Promise during a synchronous renderToString. Async server components ` +
|
|
117
|
+
`are rendered per request by the SSR sidecar through renderToStringAsync; a synchronous ` +
|
|
118
|
+
`render (for example inside goComponent children) cannot await them.`);
|
|
119
|
+
}
|
|
107
120
|
function renderNode(node) {
|
|
108
121
|
if (node == null || node === false)
|
|
109
122
|
return "";
|
|
123
|
+
if (isThenable(node))
|
|
124
|
+
throw asyncInSyncRender("A child value");
|
|
110
125
|
if (typeof node === "string")
|
|
111
126
|
return escapeHTML(node);
|
|
112
127
|
if (typeof node === "number")
|
|
@@ -153,11 +168,65 @@ function setAttr(key, val) {
|
|
|
153
168
|
export function renderToString(node) {
|
|
154
169
|
return renderNode(node);
|
|
155
170
|
}
|
|
171
|
+
/**
|
|
172
|
+
* renderToStringAsync renders a tree that may contain async (server)
|
|
173
|
+
* components. Every Promise-returning component is awaited, at any depth, and
|
|
174
|
+
* sibling subtrees resolve in parallel. Errors thrown by a component (or a
|
|
175
|
+
* rejected Promise) reject the returned Promise; nothing renders as "".
|
|
176
|
+
*
|
|
177
|
+
* <Suspense> renders its resolved children: the whole tree is awaited before
|
|
178
|
+
* any HTML is produced, so there is no fallback to show.
|
|
179
|
+
*/
|
|
180
|
+
export async function renderToStringAsync(node) {
|
|
181
|
+
const resolved = await resolveTree(node);
|
|
182
|
+
return renderNode(resolved);
|
|
183
|
+
}
|
|
184
|
+
function isElement(v) {
|
|
185
|
+
return v != null && typeof v === "object" && "type" in v && "props" in v && Array.isArray(v.children);
|
|
186
|
+
}
|
|
187
|
+
function isJSXValue(v) {
|
|
188
|
+
return isElement(v) || isThenable(v) || (Array.isArray(v) && v.some(isJSXValue));
|
|
189
|
+
}
|
|
190
|
+
// resolveTree expands function components top-down into a tree of host
|
|
191
|
+
// elements, raw HTML and primitives, awaiting async components. Built-ins that
|
|
192
|
+
// render their children eagerly (marked __krateEagerChildren, e.g. Link) get
|
|
193
|
+
// their children and JSX-valued props resolved first, so an async component
|
|
194
|
+
// passed as a child still renders.
|
|
195
|
+
async function resolveTree(node) {
|
|
196
|
+
if (isThenable(node))
|
|
197
|
+
return resolveTree(await node);
|
|
198
|
+
if (node == null || typeof node !== "object")
|
|
199
|
+
return node;
|
|
200
|
+
if (Array.isArray(node))
|
|
201
|
+
return Promise.all(node.map(resolveTree));
|
|
202
|
+
if ("__raw" in node)
|
|
203
|
+
return node;
|
|
204
|
+
if (!isElement(node))
|
|
205
|
+
return node;
|
|
206
|
+
const { type, props, children } = node;
|
|
207
|
+
if (typeof type !== "function") {
|
|
208
|
+
return { type, props, children: (await Promise.all(children.map(resolveTree))) };
|
|
209
|
+
}
|
|
210
|
+
const fn = type;
|
|
211
|
+
if (fn.__krateSuspense)
|
|
212
|
+
return resolveTree(children);
|
|
213
|
+
let callProps = { ...props, children };
|
|
214
|
+
if (fn.__krateEagerChildren) {
|
|
215
|
+
const entries = await Promise.all(Object.entries(props).map(async ([k, v]) => [k, isJSXValue(v) ? await resolveTree(v) : v]));
|
|
216
|
+
callProps = { ...Object.fromEntries(entries), children: await resolveTree(children) };
|
|
217
|
+
}
|
|
218
|
+
let out = fn(callProps);
|
|
219
|
+
if (isThenable(out))
|
|
220
|
+
out = await out;
|
|
221
|
+
return resolveTree(out);
|
|
222
|
+
}
|
|
156
223
|
function renderElement(el) {
|
|
157
224
|
const { type, props, children } = el;
|
|
158
225
|
// Component
|
|
159
226
|
if (typeof type === "function") {
|
|
160
227
|
const result = type({ ...props, children });
|
|
228
|
+
if (isThenable(result))
|
|
229
|
+
throw asyncInSyncRender(`<${componentName(type)}>`);
|
|
161
230
|
return renderNode(result);
|
|
162
231
|
}
|
|
163
232
|
// HTML element
|
|
@@ -204,6 +273,93 @@ export function Suspense(props) {
|
|
|
204
273
|
return raw(`<span data-suspense="${id}">${fallback}</span>` +
|
|
205
274
|
`<template data-suspense="${id}">${inner}</template>`);
|
|
206
275
|
}
|
|
276
|
+
Suspense.__krateSuspense = true;
|
|
277
|
+
function describeValue(v) {
|
|
278
|
+
if (typeof v === "function")
|
|
279
|
+
return "a function";
|
|
280
|
+
if (typeof v === "symbol")
|
|
281
|
+
return "a symbol";
|
|
282
|
+
if (typeof v === "bigint")
|
|
283
|
+
return "a bigint";
|
|
284
|
+
if (v && typeof v === "object") {
|
|
285
|
+
const ctor = Object.getPrototypeOf(v)?.constructor?.name;
|
|
286
|
+
return ctor ? `an instance of ${ctor}` : "a non-plain object";
|
|
287
|
+
}
|
|
288
|
+
return typeof v;
|
|
289
|
+
}
|
|
290
|
+
// serializeClientProp converts a prop crossing from the server to a client
|
|
291
|
+
// component into JSON-safe data. Server actions become references the client
|
|
292
|
+
// chunk turns back into callable proxies; anything else that JSON cannot carry
|
|
293
|
+
// faithfully throws with the offending prop path.
|
|
294
|
+
function serializeClientProp(v, path, component) {
|
|
295
|
+
if (v === null || typeof v === "string" || typeof v === "boolean")
|
|
296
|
+
return v;
|
|
297
|
+
if (typeof v === "number") {
|
|
298
|
+
if (!Number.isFinite(v))
|
|
299
|
+
throw clientPropError(path, component, `the number ${v}`);
|
|
300
|
+
return v;
|
|
301
|
+
}
|
|
302
|
+
if (v === undefined)
|
|
303
|
+
return undefined;
|
|
304
|
+
if (isServerAction(v))
|
|
305
|
+
return { __krateAction: v.actionUrl };
|
|
306
|
+
if (Array.isArray(v))
|
|
307
|
+
return v.map((x, i) => serializeClientProp(x, `${path}[${i}]`, component));
|
|
308
|
+
if (typeof v === "object") {
|
|
309
|
+
if (isElement(v) || "__raw" in v) {
|
|
310
|
+
throw clientPropError(path, component, "JSX", "only `children` can carry server-rendered JSX into a client component");
|
|
311
|
+
}
|
|
312
|
+
const proto = Object.getPrototypeOf(v);
|
|
313
|
+
if (proto !== Object.prototype && proto !== null)
|
|
314
|
+
throw clientPropError(path, component, describeValue(v));
|
|
315
|
+
const out = {};
|
|
316
|
+
for (const [k, x] of Object.entries(v)) {
|
|
317
|
+
const sv = serializeClientProp(x, `${path}.${k}`, component);
|
|
318
|
+
if (sv !== undefined)
|
|
319
|
+
out[k] = sv;
|
|
320
|
+
}
|
|
321
|
+
return out;
|
|
322
|
+
}
|
|
323
|
+
throw clientPropError(path, component, describeValue(v));
|
|
324
|
+
}
|
|
325
|
+
function clientPropError(path, component, what, hint) {
|
|
326
|
+
return new Error(`Prop \`${path}\` passed to client component <${component}> is ${what}, which cannot be sent from a server ` +
|
|
327
|
+
`component to the client. ` +
|
|
328
|
+
(hint || "Pass JSON-serializable data (strings, numbers, booleans, null, plain objects, arrays) or a server action; " +
|
|
329
|
+
"move event handlers and class instances into the client component."));
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* clientBoundary wraps an export of a `"use client"` module for the server
|
|
333
|
+
* bundle of a page that renders server components. The server renders the
|
|
334
|
+
* component's initial HTML (render) into a <krate-island> marker carrying its
|
|
335
|
+
* serialized props; the per-module client chunk at src hydrates it in the
|
|
336
|
+
* browser. Children are rendered on the server and passed as HTML.
|
|
337
|
+
*/
|
|
338
|
+
export function clientBoundary(moduleId, exportName, src, impl, render) {
|
|
339
|
+
if (typeof impl !== "function")
|
|
340
|
+
return impl;
|
|
341
|
+
const name = impl.displayName || impl.name || exportName;
|
|
342
|
+
const Boundary = async (props) => {
|
|
343
|
+
const { children, ...rest } = props || {};
|
|
344
|
+
const data = {};
|
|
345
|
+
for (const [k, v] of Object.entries(rest)) {
|
|
346
|
+
if (k === "key")
|
|
347
|
+
continue;
|
|
348
|
+
const sv = serializeClientProp(v, k, name);
|
|
349
|
+
if (sv !== undefined)
|
|
350
|
+
data[k] = sv;
|
|
351
|
+
}
|
|
352
|
+
const hasChildren = children != null && !(Array.isArray(children) && children.length === 0);
|
|
353
|
+
const childrenHTML = hasChildren ? await renderToStringAsync(children) : null;
|
|
354
|
+
if (childrenHTML !== null)
|
|
355
|
+
data.__krateChildren = childrenHTML;
|
|
356
|
+
const html = render(impl, data, childrenHTML);
|
|
357
|
+
return raw(`<krate-island style="display:contents" data-module="${escapeHTML(moduleId)}" data-export="${escapeHTML(exportName)}" ` +
|
|
358
|
+
`data-src="${escapeHTML(src)}" data-props="${escapeHTML(JSON.stringify(data))}">${html}</krate-island>`);
|
|
359
|
+
};
|
|
360
|
+
Object.defineProperty(Boundary, "name", { value: name });
|
|
361
|
+
return Boundary;
|
|
362
|
+
}
|
|
207
363
|
// Built-in Components (server stubs)
|
|
208
364
|
// Head - renders children into the page head at build time.
|
|
209
365
|
// On the server, we just render children; the build pipeline extracts them.
|
|
@@ -255,6 +411,7 @@ export function Link(props) {
|
|
|
255
411
|
const inner = children != null ? renderNode(children) : "";
|
|
256
412
|
return raw(`<a ${attrs.join(" ")}>${inner}</a>`);
|
|
257
413
|
}
|
|
414
|
+
Link.__krateEagerChildren = true;
|
|
258
415
|
// Image - renders an <img> tag (server stub; Go compiler handles full responsive output at build time).
|
|
259
416
|
export function Image(props) {
|
|
260
417
|
const { src = "", alt = "", width, height, ...rest } = props;
|
package/dist/signal.d.ts
CHANGED
|
@@ -39,6 +39,25 @@ export interface OwnerHooks {
|
|
|
39
39
|
}
|
|
40
40
|
/** Installs (or with null removes) the owner integration hooks. */
|
|
41
41
|
export declare function setOwnerHooks(hooks: OwnerHooks | null): void;
|
|
42
|
+
/**
|
|
43
|
+
* Enters the route scope `id` for the hydration code that follows. Returns
|
|
44
|
+
* false when the SPA router kept that layout mounted: its bindings and
|
|
45
|
+
* effects are still live and must not be created twice.
|
|
46
|
+
*/
|
|
47
|
+
export declare function krateEnterScope(id: string): boolean;
|
|
48
|
+
/** Leaves the current route scope (see krateEnterScope). */
|
|
49
|
+
export declare function krateLeaveScope(): void;
|
|
50
|
+
/**
|
|
51
|
+
* The route scope new effects and owners are tagged with (internal): the
|
|
52
|
+
* running effect's scope, else the scope whose hydration code is running,
|
|
53
|
+
* else the current component owner's scope (code running later, e.g. an
|
|
54
|
+
* event handler of a layout component).
|
|
55
|
+
*/
|
|
56
|
+
export declare function krateCurrentScope(): string | null;
|
|
57
|
+
/** Sets the layout scopes the router kept mounted for the next hydration (internal). */
|
|
58
|
+
export declare function krateKeepScopes(ids: string[]): void;
|
|
59
|
+
/** Records the route scope of an owner key (internal; see effects.ts). */
|
|
60
|
+
export declare function krateTagOwner(owner: unknown, scope: string | null): void;
|
|
42
61
|
/**
|
|
43
62
|
* Disposes every effect created under `owner` and runs its top-level
|
|
44
63
|
* onCleanup callbacks (internal, used when a component unmounts).
|
|
@@ -87,8 +106,12 @@ export declare function krateSetOwner(owner: unknown): unknown;
|
|
|
87
106
|
export declare function krateOnBeforeFlush(fn: () => void): void;
|
|
88
107
|
/** Registers fn to run after every settled effect flush (internal). */
|
|
89
108
|
export declare function krateOnAfterFlush(fn: () => void): void;
|
|
90
|
-
/**
|
|
91
|
-
|
|
109
|
+
/**
|
|
110
|
+
* Registers fn to run at the start of disposeAll (internal). For a partial
|
|
111
|
+
* teardown (krateDisposeExcept / krateDisposeScopes) `gone` reports which
|
|
112
|
+
* route scopes are torn down; it is null for a full teardown.
|
|
113
|
+
*/
|
|
114
|
+
export declare function krateOnDispose(fn: (gone: ((scope: string | null) => boolean) | null) => void): void;
|
|
92
115
|
/** Reports whether signal writes are waiting for the next flush. */
|
|
93
116
|
export declare function kratePendingFlush(): boolean;
|
|
94
117
|
/**
|
|
@@ -108,6 +131,19 @@ export declare function afterFlush(fn: () => void): void;
|
|
|
108
131
|
* from -0.
|
|
109
132
|
*/
|
|
110
133
|
export declare function sameValue(a: unknown, b: unknown): boolean;
|
|
134
|
+
/**
|
|
135
|
+
* Partial teardown for a soft navigation that keeps some layouts mounted:
|
|
136
|
+
* disposes every effect, cleanup and component owner whose route scope is
|
|
137
|
+
* not in `keep`. With an empty `keep` this is disposeAll().
|
|
138
|
+
*/
|
|
139
|
+
export declare function krateDisposeExcept(keep: string[]): void;
|
|
140
|
+
/**
|
|
141
|
+
* Disposes only the effects, cleanups and component owners of the given
|
|
142
|
+
* route scopes (a parallel-route slot whose content is replaced).
|
|
143
|
+
*/
|
|
144
|
+
export declare function krateDisposeScopes(ids: string[]): void;
|
|
145
|
+
/** The route scope encoded in a layout/slot id ("_lHASH_x" -> "_lHASH_"), else null. */
|
|
146
|
+
export declare function krateScopeOfId(id: string): string | null;
|
|
111
147
|
export declare function disposeAll(): void;
|
|
112
148
|
/** ARIA role presets and raw overrides accepted by every CSS primitive. */
|
|
113
149
|
export interface CSSARIAOptions {
|
package/dist/signal.js
CHANGED
|
@@ -24,14 +24,68 @@ const allEffects = new Set();
|
|
|
24
24
|
// gives component-level cleanup semantics: a component's top-level onCleanup
|
|
25
25
|
// registers once when its hydration IIFE runs and is invoked on unmount.
|
|
26
26
|
const rootCleanups = [];
|
|
27
|
+
// Route scopes (SPA layout persistence). Hydration code of a layout runs
|
|
28
|
+
// between krateEnterScope(id) and krateLeaveScope(), so every effect, cleanup
|
|
29
|
+
// and component owner it creates is tagged with the layout's id. On a soft
|
|
30
|
+
// navigation that keeps the layout mounted, the router disposes everything
|
|
31
|
+
// except the kept scopes (krateDisposeExcept) and the new page's hydration
|
|
32
|
+
// script skips the kept layouts' code (krateEnterScope returns false).
|
|
33
|
+
let routeScope = null;
|
|
34
|
+
let keptScopes = new Set();
|
|
35
|
+
// Scope of each owner key used by ownerEffects/ownerCleanups.
|
|
36
|
+
const ownerScopes = new Map();
|
|
37
|
+
/**
|
|
38
|
+
* Enters the route scope `id` for the hydration code that follows. Returns
|
|
39
|
+
* false when the SPA router kept that layout mounted: its bindings and
|
|
40
|
+
* effects are still live and must not be created twice.
|
|
41
|
+
*/
|
|
42
|
+
export function krateEnterScope(id) {
|
|
43
|
+
if (keptScopes.has(id))
|
|
44
|
+
return false;
|
|
45
|
+
routeScope = id;
|
|
46
|
+
currentOwner = null;
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
/** Leaves the current route scope (see krateEnterScope). */
|
|
50
|
+
export function krateLeaveScope() {
|
|
51
|
+
routeScope = null;
|
|
52
|
+
currentOwner = null;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The route scope new effects and owners are tagged with (internal): the
|
|
56
|
+
* running effect's scope, else the scope whose hydration code is running,
|
|
57
|
+
* else the current component owner's scope (code running later, e.g. an
|
|
58
|
+
* event handler of a layout component).
|
|
59
|
+
*/
|
|
60
|
+
export function krateCurrentScope() {
|
|
61
|
+
const top = context[context.length - 1];
|
|
62
|
+
if (top)
|
|
63
|
+
return top.scope;
|
|
64
|
+
if (routeScope !== null)
|
|
65
|
+
return routeScope;
|
|
66
|
+
if (currentOwner !== null && ownerScopes.has(currentOwner))
|
|
67
|
+
return ownerScopes.get(currentOwner) ?? null;
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
/** Sets the layout scopes the router kept mounted for the next hydration (internal). */
|
|
71
|
+
export function krateKeepScopes(ids) {
|
|
72
|
+
keptScopes = new Set(ids);
|
|
73
|
+
}
|
|
74
|
+
/** Records the route scope of an owner key (internal; see effects.ts). */
|
|
75
|
+
export function krateTagOwner(owner, scope) {
|
|
76
|
+
if (owner !== null && !ownerScopes.has(owner))
|
|
77
|
+
ownerScopes.set(owner, scope);
|
|
78
|
+
}
|
|
27
79
|
// Pending effects scheduled to re-run in the next microtask. Writes batch
|
|
28
80
|
// subscriber re-runs into a single flush so N writes in a loop (or one write
|
|
29
81
|
// fanning out) produce one pass instead of N synchronous effect runs.
|
|
30
82
|
const pending = [];
|
|
31
83
|
let flushScheduled = false;
|
|
32
84
|
function newEffectState(fn) {
|
|
33
|
-
const
|
|
85
|
+
const scope = krateCurrentScope();
|
|
86
|
+
const effect = { fn, cleanups: [], disposed: false, sources: new Set(), running: false, rerun: false, queued: false, owner: currentOwner, boundaryOwner: ownerHooks ? ownerHooks.current() : null, scope };
|
|
34
87
|
if (currentOwner !== null) {
|
|
88
|
+
krateTagOwner(currentOwner, scope);
|
|
35
89
|
let set = ownerEffects.get(currentOwner);
|
|
36
90
|
if (!set)
|
|
37
91
|
ownerEffects.set(currentOwner, (set = new Set()));
|
|
@@ -58,6 +112,7 @@ export function krateDisposeOwner(owner) {
|
|
|
58
112
|
}
|
|
59
113
|
const cleanups = ownerCleanups.get(owner);
|
|
60
114
|
ownerCleanups.delete(owner);
|
|
115
|
+
ownerScopes.delete(owner);
|
|
61
116
|
if (cleanups) {
|
|
62
117
|
for (const c of cleanups) {
|
|
63
118
|
try {
|
|
@@ -236,7 +291,11 @@ export function krateOnBeforeFlush(fn) {
|
|
|
236
291
|
export function krateOnAfterFlush(fn) {
|
|
237
292
|
afterFlushHooks.push(fn);
|
|
238
293
|
}
|
|
239
|
-
/**
|
|
294
|
+
/**
|
|
295
|
+
* Registers fn to run at the start of disposeAll (internal). For a partial
|
|
296
|
+
* teardown (krateDisposeExcept / krateDisposeScopes) `gone` reports which
|
|
297
|
+
* route scopes are torn down; it is null for a full teardown.
|
|
298
|
+
*/
|
|
240
299
|
export function krateOnDispose(fn) {
|
|
241
300
|
disposeHooks.push(fn);
|
|
242
301
|
}
|
|
@@ -299,10 +358,94 @@ function disposeEffect(effect) {
|
|
|
299
358
|
effect.cleanups.length = 0;
|
|
300
359
|
}
|
|
301
360
|
}
|
|
361
|
+
/**
|
|
362
|
+
* Partial teardown for a soft navigation that keeps some layouts mounted:
|
|
363
|
+
* disposes every effect, cleanup and component owner whose route scope is
|
|
364
|
+
* not in `keep`. With an empty `keep` this is disposeAll().
|
|
365
|
+
*/
|
|
366
|
+
export function krateDisposeExcept(keep) {
|
|
367
|
+
if (keep.length === 0) {
|
|
368
|
+
disposeAll();
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
const k = new Set(keep);
|
|
372
|
+
disposeMatching((scope) => !(scope != null && k.has(scope)), true);
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* Disposes only the effects, cleanups and component owners of the given
|
|
376
|
+
* route scopes (a parallel-route slot whose content is replaced).
|
|
377
|
+
*/
|
|
378
|
+
export function krateDisposeScopes(ids) {
|
|
379
|
+
if (ids.length === 0)
|
|
380
|
+
return;
|
|
381
|
+
const k = new Set(ids);
|
|
382
|
+
disposeMatching((scope) => scope != null && k.has(scope), false);
|
|
383
|
+
}
|
|
384
|
+
/** The route scope encoded in a layout/slot id ("_lHASH_x" -> "_lHASH_"), else null. */
|
|
385
|
+
export function krateScopeOfId(id) {
|
|
386
|
+
if (id.charCodeAt(0) !== 95 /* _ */)
|
|
387
|
+
return null;
|
|
388
|
+
const end = id.indexOf('_', 1);
|
|
389
|
+
return end > 0 ? id.slice(0, end + 1) : null;
|
|
390
|
+
}
|
|
391
|
+
function disposeMatching(gone, pageTeardown) {
|
|
392
|
+
for (const hook of disposeHooks) {
|
|
393
|
+
try {
|
|
394
|
+
hook(gone);
|
|
395
|
+
}
|
|
396
|
+
catch {
|
|
397
|
+
// Unmount errors must not prevent the rest of the teardown.
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
for (const effect of Array.from(allEffects)) {
|
|
401
|
+
if (gone(effect.scope)) {
|
|
402
|
+
disposeEffect(effect);
|
|
403
|
+
allEffects.delete(effect);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
const kept = [];
|
|
407
|
+
for (const cleanup of rootCleanups.splice(0)) {
|
|
408
|
+
if (!gone(cleanup.scope)) {
|
|
409
|
+
kept.push(cleanup);
|
|
410
|
+
continue;
|
|
411
|
+
}
|
|
412
|
+
try {
|
|
413
|
+
cleanup.fn();
|
|
414
|
+
}
|
|
415
|
+
catch {
|
|
416
|
+
// Cleanup errors must not prevent remaining cleanups or DOM swap.
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
rootCleanups.push(...kept);
|
|
420
|
+
const owners = new Set([...ownerCleanups.keys(), ...ownerEffects.keys()]);
|
|
421
|
+
for (const owner of owners) {
|
|
422
|
+
if (gone(ownerScopes.get(owner) ?? null))
|
|
423
|
+
krateDisposeOwner(owner);
|
|
424
|
+
}
|
|
425
|
+
if (!pageTeardown)
|
|
426
|
+
return;
|
|
427
|
+
// Islands are only mounted by page code (layouts with islands are never kept).
|
|
428
|
+
const g = globalThis;
|
|
429
|
+
const islands = g.__krate_island_cleanups;
|
|
430
|
+
if (Array.isArray(islands)) {
|
|
431
|
+
g.__krate_island_cleanups = [];
|
|
432
|
+
for (const cleanup of islands) {
|
|
433
|
+
try {
|
|
434
|
+
cleanup();
|
|
435
|
+
}
|
|
436
|
+
catch {
|
|
437
|
+
// Island unmount errors must not prevent the SPA swap.
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
routeScope = null;
|
|
442
|
+
resetContexts();
|
|
443
|
+
}
|
|
302
444
|
export function disposeAll() {
|
|
445
|
+
routeScope = null;
|
|
303
446
|
for (const hook of disposeHooks) {
|
|
304
447
|
try {
|
|
305
|
-
hook();
|
|
448
|
+
hook(null);
|
|
306
449
|
}
|
|
307
450
|
catch {
|
|
308
451
|
// Unmount errors must not prevent the rest of the teardown.
|
|
@@ -314,7 +457,7 @@ export function disposeAll() {
|
|
|
314
457
|
allEffects.clear();
|
|
315
458
|
for (const cleanup of rootCleanups) {
|
|
316
459
|
try {
|
|
317
|
-
cleanup();
|
|
460
|
+
cleanup.fn();
|
|
318
461
|
}
|
|
319
462
|
catch {
|
|
320
463
|
// Cleanup errors must not prevent remaining cleanups or DOM swap.
|
|
@@ -325,6 +468,7 @@ export function disposeAll() {
|
|
|
325
468
|
krateDisposeOwner(owner);
|
|
326
469
|
}
|
|
327
470
|
ownerEffects.clear();
|
|
471
|
+
ownerScopes.clear();
|
|
328
472
|
changedCells.clear();
|
|
329
473
|
historyCells.clear();
|
|
330
474
|
snapshotEpoch = null;
|
|
@@ -885,13 +1029,14 @@ export function onCleanup(fn) {
|
|
|
885
1029
|
// No active effect: a component-level cleanup. It runs when its owner
|
|
886
1030
|
// unmounts, or on disposeAll() (route change / page teardown).
|
|
887
1031
|
if (currentOwner !== null) {
|
|
1032
|
+
krateTagOwner(currentOwner, krateCurrentScope());
|
|
888
1033
|
let list = ownerCleanups.get(currentOwner);
|
|
889
1034
|
if (!list)
|
|
890
1035
|
ownerCleanups.set(currentOwner, (list = []));
|
|
891
1036
|
list.push(fn);
|
|
892
1037
|
}
|
|
893
1038
|
else {
|
|
894
|
-
rootCleanups.push(fn);
|
|
1039
|
+
rootCleanups.push({ fn, scope: krateCurrentScope() });
|
|
895
1040
|
}
|
|
896
1041
|
}
|
|
897
1042
|
}
|
package/dist/slots.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
export * from './portal.js';
|
|
2
2
|
/** Fragments holding currently hidden conditional branches. */
|
|
3
3
|
export declare function krateCondDetached(): DocumentFragment[];
|
|
4
|
-
/**
|
|
4
|
+
/**
|
|
5
|
+
* Forgets hidden branches of the previous page (called on slot rescans).
|
|
6
|
+
* Branches whose conditional is still in the document (a layout the SPA
|
|
7
|
+
* router kept mounted), directly or inside another kept hidden branch, stay.
|
|
8
|
+
*/
|
|
5
9
|
export declare function krateCondReset(): void;
|
|
6
10
|
/**
|
|
7
11
|
* Shows the consequent (`show` true) or alternate branch of the conditional
|
package/dist/slots.js
CHANGED
|
@@ -16,13 +16,38 @@ export * from './portal.js';
|
|
|
16
16
|
// Branches swapped out of the document. Hydration lookups (findSlot) search
|
|
17
17
|
// them too, so a binding registered after a swap still finds its node.
|
|
18
18
|
const detached = new Set();
|
|
19
|
+
// Start marker of the conditional each detached fragment belongs to.
|
|
20
|
+
const detachedStart = new WeakMap();
|
|
19
21
|
/** Fragments holding currently hidden conditional branches. */
|
|
20
22
|
export function krateCondDetached() {
|
|
21
23
|
return Array.from(detached);
|
|
22
24
|
}
|
|
23
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* Forgets hidden branches of the previous page (called on slot rescans).
|
|
27
|
+
* Branches whose conditional is still in the document (a layout the SPA
|
|
28
|
+
* router kept mounted), directly or inside another kept hidden branch, stay.
|
|
29
|
+
*/
|
|
24
30
|
export function krateCondReset() {
|
|
25
|
-
|
|
31
|
+
const keep = new Set();
|
|
32
|
+
let grew = true;
|
|
33
|
+
while (grew) {
|
|
34
|
+
grew = false;
|
|
35
|
+
for (const frag of detached) {
|
|
36
|
+
if (keep.has(frag))
|
|
37
|
+
continue;
|
|
38
|
+
const start = detachedStart.get(frag);
|
|
39
|
+
if (!start)
|
|
40
|
+
continue;
|
|
41
|
+
const home = start.getRootNode();
|
|
42
|
+
if (start.isConnected || keep.has(home)) {
|
|
43
|
+
keep.add(frag);
|
|
44
|
+
grew = true;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
for (const frag of Array.from(detached))
|
|
49
|
+
if (!keep.has(frag))
|
|
50
|
+
detached.delete(frag);
|
|
26
51
|
}
|
|
27
52
|
function findCondEnd(start) {
|
|
28
53
|
const want = '/' + (start.nodeValue || '');
|
|
@@ -86,6 +111,7 @@ export function krateCondSwap(start, show, tpl) {
|
|
|
86
111
|
kratePortalsEnter(portals);
|
|
87
112
|
st.other = out;
|
|
88
113
|
detached.add(out);
|
|
114
|
+
detachedStart.set(out, start);
|
|
89
115
|
st.shown = want;
|
|
90
116
|
krateRefsEnter();
|
|
91
117
|
if (auto) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@krate/runtime",
|
|
3
|
-
"version": "0.5.0-alpha.
|
|
3
|
+
"version": "0.5.0-alpha.4",
|
|
4
4
|
"description": "Client-side runtime for Krate — signals, hydration, SPA router, resources, and context.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -34,6 +34,10 @@
|
|
|
34
34
|
"types": "./dist/server.d.ts",
|
|
35
35
|
"default": "./dist/server.js"
|
|
36
36
|
},
|
|
37
|
+
"./cache": {
|
|
38
|
+
"types": "./dist/cache.d.ts",
|
|
39
|
+
"default": "./dist/cache.js"
|
|
40
|
+
},
|
|
37
41
|
"./server/jsx-runtime": {
|
|
38
42
|
"types": "./dist/server-jsx-runtime.d.ts",
|
|
39
43
|
"default": "./dist/server-jsx-runtime.js"
|