@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
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
2
|
+
import { initRouter, destroyRouter, krateRouter } from './router';
|
|
3
|
+
import { createEffect, createSignal, onCleanup, disposeAll, krateDisposeExcept, krateEnterScope, krateLeaveScope, krateKeepScopes, } from './signal';
|
|
4
|
+
function routeInfo(chain) {
|
|
5
|
+
return `<script type="application/json" id="__krate_route">${JSON.stringify({ v: 1, chain })}</script>`;
|
|
6
|
+
}
|
|
7
|
+
const LAYOUT = { id: '_lroot_', k: 'layout', d: 0, p: true };
|
|
8
|
+
function shell(inner, title = 'Nav') {
|
|
9
|
+
return `<header id="nav"><input id="q">${title}</header><!--kl:_lroot_-->${inner}<!--/kl:_lroot_-->`;
|
|
10
|
+
}
|
|
11
|
+
function doc(title, rootHTML, chain) {
|
|
12
|
+
return `<!doctype html><html><head><title>${title}</title></head><body><div id="root">${rootHTML}</div>${routeInfo(chain)}</body></html>`;
|
|
13
|
+
}
|
|
14
|
+
let routes;
|
|
15
|
+
function setup(rootHTML, chain, path = '/a') {
|
|
16
|
+
history.replaceState(null, '', path);
|
|
17
|
+
document.head.innerHTML = '<title>A</title>';
|
|
18
|
+
document.body.innerHTML = `<div id="root">${rootHTML}</div>${routeInfo(chain)}`;
|
|
19
|
+
initRouter();
|
|
20
|
+
}
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
destroyRouter();
|
|
23
|
+
disposeAll();
|
|
24
|
+
globalThis.__KRATE_CFG__ = { viewTransitions: false };
|
|
25
|
+
const g = globalThis;
|
|
26
|
+
g.krateDisposeExcept = krateDisposeExcept;
|
|
27
|
+
g.krateKeepScopes = krateKeepScopes;
|
|
28
|
+
g.disposeAll = disposeAll;
|
|
29
|
+
g.__krateHardNavigate = () => { };
|
|
30
|
+
routes = {};
|
|
31
|
+
vi.stubGlobal('fetch', vi.fn(async (input) => {
|
|
32
|
+
const u = new URL(input, location.href);
|
|
33
|
+
const body = routes[u.pathname];
|
|
34
|
+
return {
|
|
35
|
+
ok: !!body,
|
|
36
|
+
status: body ? 200 : 404,
|
|
37
|
+
url: u.href,
|
|
38
|
+
headers: new Headers({ 'content-type': 'text/html' }),
|
|
39
|
+
text: async () => body || '',
|
|
40
|
+
};
|
|
41
|
+
}));
|
|
42
|
+
});
|
|
43
|
+
afterEach(() => {
|
|
44
|
+
destroyRouter();
|
|
45
|
+
vi.unstubAllGlobals();
|
|
46
|
+
const g = globalThis;
|
|
47
|
+
delete g.krateDisposeExcept;
|
|
48
|
+
delete g.krateKeepScopes;
|
|
49
|
+
delete g.disposeAll;
|
|
50
|
+
delete g.__krateHardNavigate;
|
|
51
|
+
delete g.__KRATE_CFG__;
|
|
52
|
+
});
|
|
53
|
+
describe('layout persistence', () => {
|
|
54
|
+
it('keeps a shared layout mounted and swaps only its children', async () => {
|
|
55
|
+
setup(shell('<main>Page A</main>'), [LAYOUT]);
|
|
56
|
+
const nav = document.getElementById('nav');
|
|
57
|
+
document.getElementById('q').value = 'typed';
|
|
58
|
+
routes['/b'] = doc('B', shell('<main>Page B</main>'), [LAYOUT]);
|
|
59
|
+
await krateRouter.push('/b');
|
|
60
|
+
expect(document.querySelector('main').textContent).toBe('Page B');
|
|
61
|
+
expect(document.getElementById('nav')).toBe(nav);
|
|
62
|
+
expect(document.getElementById('q').value).toBe('typed');
|
|
63
|
+
});
|
|
64
|
+
it('keeps effects of the kept layout scope and disposes page effects', async () => {
|
|
65
|
+
setup(shell('<main>Page A</main>'), [LAYOUT]);
|
|
66
|
+
const log = [];
|
|
67
|
+
const [count, setCount] = createSignal(0);
|
|
68
|
+
// Layout hydration code runs inside its route scope.
|
|
69
|
+
expect(krateEnterScope('_lroot_')).toBe(true);
|
|
70
|
+
createEffect(() => {
|
|
71
|
+
log.push('layout:' + count());
|
|
72
|
+
onCleanup(() => log.push('layout-cleanup'));
|
|
73
|
+
});
|
|
74
|
+
krateLeaveScope();
|
|
75
|
+
// Page hydration code (no scope).
|
|
76
|
+
createEffect(() => {
|
|
77
|
+
onCleanup(() => log.push('page-cleanup'));
|
|
78
|
+
});
|
|
79
|
+
onCleanup(() => log.push('page-root-cleanup'));
|
|
80
|
+
log.length = 0;
|
|
81
|
+
routes['/b'] = doc('B', shell('<main>Page B</main>'), [LAYOUT]);
|
|
82
|
+
await krateRouter.push('/b');
|
|
83
|
+
expect(log).toContain('page-cleanup');
|
|
84
|
+
expect(log).toContain('page-root-cleanup');
|
|
85
|
+
expect(log).not.toContain('layout-cleanup');
|
|
86
|
+
log.length = 0;
|
|
87
|
+
setCount(1);
|
|
88
|
+
await new Promise((r) => setTimeout(r, 0));
|
|
89
|
+
// The layout effect is still live.
|
|
90
|
+
expect(log).toEqual(['layout-cleanup', 'layout:1']);
|
|
91
|
+
});
|
|
92
|
+
it('remounts layouts the compiler marked as not persistable', async () => {
|
|
93
|
+
globalThis.__KRATE_CFG__ = { viewTransitions: false, dev: true };
|
|
94
|
+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => { });
|
|
95
|
+
const blocked = { ...LAYOUT, p: false, why: 'the page uses class components' };
|
|
96
|
+
setup(shell('<main>Page A</main>'), [blocked]);
|
|
97
|
+
const nav = document.getElementById('nav');
|
|
98
|
+
routes['/b'] = doc('B', shell('<main>Page B</main>'), [blocked]);
|
|
99
|
+
await krateRouter.push('/b');
|
|
100
|
+
expect(document.getElementById('nav')).not.toBe(nav);
|
|
101
|
+
expect(warn.mock.calls.some((c) => String(c[0]).includes('class components'))).toBe(true);
|
|
102
|
+
warn.mockRestore();
|
|
103
|
+
});
|
|
104
|
+
it('swaps the whole root when the layouts differ', async () => {
|
|
105
|
+
setup(shell('<main>Page A</main>'), [LAYOUT]);
|
|
106
|
+
const nav = document.getElementById('nav');
|
|
107
|
+
const other = { id: '_lother_', k: 'layout', d: 0, p: true };
|
|
108
|
+
routes['/b'] = doc('B', '<aside>other</aside><!--kl:_lother_--><main>B</main><!--/kl:_lother_-->', [other]);
|
|
109
|
+
await krateRouter.push('/b');
|
|
110
|
+
expect(nav.isConnected).toBe(false);
|
|
111
|
+
expect(document.querySelector('aside')).not.toBeNull();
|
|
112
|
+
});
|
|
113
|
+
it('remounts a layout under a dynamic segment when the segment value changes', async () => {
|
|
114
|
+
const section = { id: '_lpost_', k: 'layout', d: 2, p: true };
|
|
115
|
+
const html = (slug) => shell(`<div id="post-shell">${slug}<!--kl:_lpost_--><p>${slug}</p><!--/kl:_lpost_--></div>`);
|
|
116
|
+
setup(html('a'), [LAYOUT, section], '/blog/a');
|
|
117
|
+
const nav = document.getElementById('nav');
|
|
118
|
+
const postShell = document.getElementById('post-shell');
|
|
119
|
+
routes['/blog/b'] = doc('B', html('b'), [LAYOUT, section]);
|
|
120
|
+
await krateRouter.push('/blog/b');
|
|
121
|
+
// The root layout stays, the [slug] layout remounts.
|
|
122
|
+
expect(document.getElementById('nav')).toBe(nav);
|
|
123
|
+
expect(document.getElementById('post-shell')).not.toBe(postShell);
|
|
124
|
+
expect(document.querySelector('p').textContent).toBe('b');
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
describe('templates', () => {
|
|
128
|
+
it('remounts the template on every navigation, keeping the parent layout', async () => {
|
|
129
|
+
const tpl = { id: '_ltpl_', k: 'template', d: 0, p: false };
|
|
130
|
+
const html = (page) => shell(`<section id="tpl"><input id="t"><!--kl:_ltpl_--><main>${page}</main><!--/kl:_ltpl_--></section>`);
|
|
131
|
+
setup(html('A'), [LAYOUT, tpl]);
|
|
132
|
+
const nav = document.getElementById('nav');
|
|
133
|
+
const section = document.getElementById('tpl');
|
|
134
|
+
document.getElementById('t').value = 'stale';
|
|
135
|
+
routes['/b'] = doc('B', html('B'), [LAYOUT, tpl]);
|
|
136
|
+
await krateRouter.push('/b');
|
|
137
|
+
expect(document.getElementById('nav')).toBe(nav);
|
|
138
|
+
expect(document.getElementById('tpl')).not.toBe(section);
|
|
139
|
+
expect(document.getElementById('t').value).toBe('');
|
|
140
|
+
expect(document.querySelector('main').textContent).toBe('B');
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
describe('route scopes', () => {
|
|
144
|
+
it('krateEnterScope skips kept scopes only while the router keeps them', () => {
|
|
145
|
+
krateKeepScopes(['_lroot_']);
|
|
146
|
+
expect(krateEnterScope('_lroot_')).toBe(false);
|
|
147
|
+
expect(krateEnterScope('_lother_')).toBe(true);
|
|
148
|
+
krateLeaveScope();
|
|
149
|
+
krateKeepScopes([]);
|
|
150
|
+
expect(krateEnterScope('_lroot_')).toBe(true);
|
|
151
|
+
krateLeaveScope();
|
|
152
|
+
});
|
|
153
|
+
it('krateDisposeExcept([]) is a full teardown', () => {
|
|
154
|
+
const log = [];
|
|
155
|
+
krateEnterScope('_lroot_');
|
|
156
|
+
onCleanup(() => log.push('layout'));
|
|
157
|
+
krateLeaveScope();
|
|
158
|
+
onCleanup(() => log.push('page'));
|
|
159
|
+
krateDisposeExcept([]);
|
|
160
|
+
expect(log.sort()).toEqual(['layout', 'page']);
|
|
161
|
+
});
|
|
162
|
+
it('effects created later by a kept effect inherit its scope', async () => {
|
|
163
|
+
const log = [];
|
|
164
|
+
const [show, setShow] = createSignal(false);
|
|
165
|
+
krateEnterScope('_lroot_');
|
|
166
|
+
createEffect(() => {
|
|
167
|
+
if (show()) {
|
|
168
|
+
createEffect(() => onCleanup(() => log.push('nested')));
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
krateLeaveScope();
|
|
172
|
+
setShow(true);
|
|
173
|
+
await new Promise((r) => setTimeout(r, 0));
|
|
174
|
+
krateDisposeExcept(['_lroot_']);
|
|
175
|
+
expect(log).toEqual([]);
|
|
176
|
+
disposeAll();
|
|
177
|
+
expect(log).toEqual(['nested']);
|
|
178
|
+
});
|
|
179
|
+
});
|
package/dist/router-lite.d.ts
CHANGED
|
@@ -3,6 +3,31 @@ export interface NavigateOptions {
|
|
|
3
3
|
scroll?: boolean;
|
|
4
4
|
state?: unknown;
|
|
5
5
|
}
|
|
6
|
+
export interface NavigationState {
|
|
7
|
+
url: string;
|
|
8
|
+
pathname: string;
|
|
9
|
+
search: string;
|
|
10
|
+
hash: string;
|
|
11
|
+
status: 'idle' | 'loading';
|
|
12
|
+
}
|
|
13
|
+
export declare function getNavigationState(): NavigationState;
|
|
14
|
+
/** Without the SPA router the URL only changes in-document on hash changes. */
|
|
15
|
+
export declare function subscribeNavigation(fn: (s: NavigationState) => void): () => void;
|
|
16
|
+
export declare function getHistoryState(): unknown;
|
|
17
|
+
/** No prefetch cache exists without the SPA router. */
|
|
18
|
+
export declare function invalidatePrefetchCache(_url?: string): void;
|
|
6
19
|
export declare function initRouter(): void;
|
|
7
20
|
export declare function reinitRouter(): void;
|
|
21
|
+
export declare function destroyRouter(): void;
|
|
8
22
|
export declare function navigate(url: string, opts?: NavigateOptions): boolean;
|
|
23
|
+
export declare const krateRouter: {
|
|
24
|
+
push(url: string, opts?: NavigateOptions): Promise<void>;
|
|
25
|
+
replace(url: string, opts?: NavigateOptions): Promise<void>;
|
|
26
|
+
back(): void;
|
|
27
|
+
forward(): void;
|
|
28
|
+
refresh(): Promise<void>;
|
|
29
|
+
prefetch(_url: string): Promise<void>;
|
|
30
|
+
subscribe: typeof subscribeNavigation;
|
|
31
|
+
getState: typeof getNavigationState;
|
|
32
|
+
invalidate: typeof invalidatePrefetchCache;
|
|
33
|
+
};
|
package/dist/router-lite.js
CHANGED
|
@@ -1,5 +1,40 @@
|
|
|
1
|
+
// Router API for sites with the SPA router disabled (`spa: false`). Every
|
|
2
|
+
// navigation is a full document load; the API surface matches router.ts so
|
|
3
|
+
// application code (and the Next.js migration shim) works unchanged.
|
|
4
|
+
function basePath() {
|
|
5
|
+
const cfg = globalThis.__KRATE_CFG__;
|
|
6
|
+
const bp = (cfg && cfg.basePath) || '';
|
|
7
|
+
if (!bp || bp === '/')
|
|
8
|
+
return '';
|
|
9
|
+
return '/' + bp.replace(/^\/+|\/+$/g, '');
|
|
10
|
+
}
|
|
11
|
+
export function getNavigationState() {
|
|
12
|
+
if (typeof location === 'undefined')
|
|
13
|
+
return { url: '', pathname: '/', search: '', hash: '', status: 'idle' };
|
|
14
|
+
const bp = basePath();
|
|
15
|
+
let pathname = location.pathname;
|
|
16
|
+
if (bp && (pathname === bp || pathname.startsWith(bp + '/')))
|
|
17
|
+
pathname = pathname.slice(bp.length) || '/';
|
|
18
|
+
return { url: location.href, pathname, search: location.search, hash: location.hash, status: 'idle' };
|
|
19
|
+
}
|
|
20
|
+
/** Without the SPA router the URL only changes in-document on hash changes. */
|
|
21
|
+
export function subscribeNavigation(fn) {
|
|
22
|
+
if (typeof window === 'undefined')
|
|
23
|
+
return () => { };
|
|
24
|
+
const handler = () => fn(getNavigationState());
|
|
25
|
+
window.addEventListener('hashchange', handler);
|
|
26
|
+
return () => {
|
|
27
|
+
window.removeEventListener('hashchange', handler);
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export function getHistoryState() {
|
|
31
|
+
return typeof history === 'undefined' ? null : history.state;
|
|
32
|
+
}
|
|
33
|
+
/** No prefetch cache exists without the SPA router. */
|
|
34
|
+
export function invalidatePrefetchCache(_url) { }
|
|
1
35
|
export function initRouter() { }
|
|
2
36
|
export function reinitRouter() { }
|
|
37
|
+
export function destroyRouter() { }
|
|
3
38
|
export function navigate(url, opts = {}) {
|
|
4
39
|
if (typeof location === 'undefined')
|
|
5
40
|
return true;
|
|
@@ -9,3 +44,41 @@ export function navigate(url, opts = {}) {
|
|
|
9
44
|
location.assign(url);
|
|
10
45
|
return true;
|
|
11
46
|
}
|
|
47
|
+
export const krateRouter = {
|
|
48
|
+
push(url, opts = {}) {
|
|
49
|
+
navigate(url, { ...opts, replace: false });
|
|
50
|
+
return Promise.resolve();
|
|
51
|
+
},
|
|
52
|
+
replace(url, opts = {}) {
|
|
53
|
+
navigate(url, { ...opts, replace: true });
|
|
54
|
+
return Promise.resolve();
|
|
55
|
+
},
|
|
56
|
+
back() {
|
|
57
|
+
if (typeof history !== 'undefined')
|
|
58
|
+
history.back();
|
|
59
|
+
},
|
|
60
|
+
forward() {
|
|
61
|
+
if (typeof history !== 'undefined')
|
|
62
|
+
history.forward();
|
|
63
|
+
},
|
|
64
|
+
refresh() {
|
|
65
|
+
if (typeof location !== 'undefined')
|
|
66
|
+
location.reload();
|
|
67
|
+
return Promise.resolve();
|
|
68
|
+
},
|
|
69
|
+
// Full-page navigations go through the browser's HTTP cache; there is no
|
|
70
|
+
// client document cache to warm.
|
|
71
|
+
prefetch(_url) {
|
|
72
|
+
return Promise.resolve();
|
|
73
|
+
},
|
|
74
|
+
subscribe: subscribeNavigation,
|
|
75
|
+
getState: getNavigationState,
|
|
76
|
+
invalidate: invalidatePrefetchCache,
|
|
77
|
+
};
|
|
78
|
+
if (typeof window !== 'undefined') {
|
|
79
|
+
// Revalidated server data is only visible after a reload without the SPA router.
|
|
80
|
+
window.addEventListener('krate:revalidate', () => {
|
|
81
|
+
if (typeof location !== 'undefined')
|
|
82
|
+
location.reload();
|
|
83
|
+
});
|
|
84
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|