@teyik0/furin 0.6.0-alpha.1 → 0.7.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapter/runtime-build.d.ts +1 -1
- package/dist/build/client.d.ts +4 -1
- package/dist/build/elysia-aot-worker.d.ts +22 -0
- package/dist/build/elysia-aot.d.ts +1 -1
- package/dist/build/entry-template.d.ts +3 -0
- package/dist/build/entry-template.ts +7 -0
- package/dist/build/hydrate.d.ts +1 -1
- package/dist/build/index.js +1880 -401
- package/dist/build/preload-manifest.d.ts +17 -0
- package/dist/build/types.d.ts +6 -0
- package/dist/cli/index.js +1878 -408
- package/dist/client/client-module.d.ts +26 -0
- package/dist/client/define-route.d.ts +9 -0
- package/dist/client/document.d.ts +2 -0
- package/dist/client/internal/runtime-types.d.ts +2 -0
- package/dist/client/link.js +942 -122
- package/dist/client/mutation.d.ts +38 -0
- package/dist/client/query-store.d.ts +85 -0
- package/dist/client/query.d.ts +14 -0
- package/dist/client/route-api.d.ts +11 -0
- package/dist/client/router/context.d.ts +3 -2
- package/dist/client/router/index.d.ts +1 -1
- package/dist/client/router/link-utils.d.ts +8 -1
- package/dist/client/router/navigation.d.ts +2 -9
- package/dist/client/router/optimistic.d.ts +41 -0
- package/dist/client/router/search/index.d.ts +3 -3
- package/dist/client/router/search-store.d.ts +1 -0
- package/dist/client/router/sync-catch-up.d.ts +3 -0
- package/dist/client/router/types.d.ts +51 -7
- package/dist/client/sync.d.ts +39 -22
- package/dist/client.d.ts +11 -33
- package/dist/client.js +1219 -59
- package/dist/config.d.ts +7 -0
- package/dist/config.js +1 -0
- package/dist/define-route.d.ts +78 -32
- package/dist/env.d.ts +2 -2
- package/dist/furin.d.ts +1 -2
- package/dist/furin.js +2484 -708
- package/dist/plugin/hmr-dependencies.d.ts +2 -0
- package/dist/plugin/index.js +349 -18
- package/dist/plugin/transform-client-module.d.ts +16 -0
- package/dist/plugin/transform-client.d.ts +2 -0
- package/dist/plugin/transform-isomorphic.d.ts +1 -1
- package/dist/routes.d.ts +6 -0
- package/dist/rsc-client.js +87 -16
- package/dist/rsc.js +87 -16
- package/dist/server/auto-invalidate/registry.d.ts +1 -0
- package/dist/server/auto-invalidate/types.d.ts +2 -0
- package/dist/server/cache/isr.d.ts +2 -4
- package/dist/server/cache/route-cache.d.ts +5 -0
- package/dist/server/dev/graph.d.ts +1 -0
- package/dist/server/dev-page-plugin.d.ts +5 -19
- package/dist/server/internal.d.ts +3 -0
- package/dist/server/internal.js +55 -35
- package/dist/server/production-server.js +54 -34
- package/dist/server/render/loaders.d.ts +4 -2
- package/dist/server/render/react-stream.d.ts +3 -0
- package/dist/server/render/ssr.d.ts +2 -1
- package/dist/server/render/template.d.ts +4 -0
- package/dist/server/router/defined-route.d.ts +2 -0
- package/dist/server/router/hmr.d.ts +1 -1
- package/dist/server/router/patterns.d.ts +2 -0
- package/dist/server/router/types.d.ts +1 -1
- package/dist/server/sync/adapter.d.ts +25 -4
- package/dist/server/sync/atomic.d.ts +25 -0
- package/dist/server/sync/drizzle/index.d.ts +45 -0
- package/dist/server/sync/drizzle/index.js +540 -0
- package/dist/server/sync/execution-error.d.ts +2 -0
- package/dist/server/sync/index.d.ts +2 -1
- package/dist/server/sync/index.js +533 -105
- package/dist/server/sync/mutation-handler.d.ts +4 -0
- package/dist/server/sync/plugin.d.ts +34 -11
- package/dist/server/sync/postgres/execute.d.ts +3 -0
- package/dist/server/sync/postgres/index.d.ts +12 -1
- package/dist/server/sync/postgres/index.js +34 -13
- package/dist/server/sync/postgres/sql.d.ts +8 -0
- package/dist/server/sync/prisma/index.d.ts +24 -0
- package/dist/server/sync/prisma/index.js +340 -0
- package/dist/server/sync/queries.d.ts +21 -0
- package/dist/server/sync/query-context.d.ts +5 -0
- package/dist/server/sync/redis/index.js +3 -3
- package/dist/server/sync/redis/scripts.d.ts +1 -1
- package/dist/server/sync/response.d.ts +1 -0
- package/dist/server/sync/sqlite/index.d.ts +3 -0
- package/dist/server/sync/sqlite/index.js +10 -4
- package/dist/server/sync/stream.d.ts +2 -5
- package/dist/server/sync/validation.d.ts +4 -0
- package/dist/shared/defer.d.ts +31 -0
- package/dist/shared/error.d.ts +2 -2
- package/dist/shared/page-key.d.ts +7 -0
- package/dist/shared/query-bindings.d.ts +12 -0
- package/dist/shared/route-frame.d.ts +1 -0
- package/dist/shared/sync-query.d.ts +40 -0
- package/package.json +29 -6
- package/src/adapter/package.ts +12 -2
- package/src/adapter/runtime-build.ts +63 -12
- package/src/adapter/static.ts +7 -2
- package/src/adapter/vercel.ts +15 -13
- package/src/build/client.ts +23 -5
- package/src/build/elysia-aot-worker.ts +51 -0
- package/src/build/elysia-aot.ts +83 -19
- package/src/build/entry-template.ts +7 -0
- package/src/build/hydrate.ts +21 -3
- package/src/build/preload-manifest.ts +100 -0
- package/src/build/production-instrumentation.ts +6 -3
- package/src/build/types.ts +4 -0
- package/src/cli/index.ts +1 -0
- package/src/client/browser-events-runtime.ts +4 -0
- package/src/client/client-module.ts +57 -0
- package/src/client/define-route.ts +11 -1
- package/src/client/document.tsx +28 -18
- package/src/client/hmr.ts +2 -1
- package/src/client/internal/runtime-types.ts +2 -0
- package/src/client/link.tsx +8 -8
- package/src/client/mutation.ts +136 -0
- package/src/client/query-store.ts +418 -0
- package/src/client/query.tsx +67 -0
- package/src/client/route-api.tsx +75 -0
- package/src/client/router/boundary-tree.tsx +19 -12
- package/src/client/router/context.ts +57 -4
- package/src/client/router/index.ts +3 -0
- package/src/client/router/link-utils.ts +29 -1
- package/src/client/router/navigation.ts +3 -50
- package/src/client/router/optimistic.ts +233 -0
- package/src/client/router/provider.tsx +306 -46
- package/src/client/router/search/index.ts +37 -17
- package/src/client/router/search-store.ts +4 -0
- package/src/client/router/sync-catch-up.ts +7 -0
- package/src/client/router/types.ts +73 -9
- package/src/client/sync.ts +438 -65
- package/src/client.ts +20 -60
- package/src/config.ts +5 -0
- package/src/define-route.ts +113 -55
- package/src/env.d.ts +2 -2
- package/src/furin.ts +75 -13
- package/src/plugin/hmr-dependencies.ts +58 -0
- package/src/plugin/index.ts +38 -2
- package/src/plugin/routes.ts +28 -16
- package/src/plugin/transform-client-module.ts +123 -0
- package/src/plugin/transform-client.ts +293 -13
- package/src/plugin/transform-isomorphic.ts +11 -5
- package/src/routes.ts +8 -0
- package/src/rsc/shared.tsx +104 -18
- package/src/server/auto-invalidate/registry.ts +4 -0
- package/src/server/auto-invalidate/runtime.ts +17 -0
- package/src/server/auto-invalidate/types.ts +6 -0
- package/src/server/cache/isr.ts +6 -34
- package/src/server/cache/route-cache.ts +40 -4
- package/src/server/dev/graph.ts +16 -2
- package/src/server/dev-page-plugin.ts +93 -59
- package/src/server/internal.ts +3 -0
- package/src/server/render/document.tsx +10 -1
- package/src/server/render/element.tsx +21 -11
- package/src/server/render/isr.ts +61 -29
- package/src/server/render/loaders.ts +118 -47
- package/src/server/render/mixed-cache.ts +33 -4
- package/src/server/render/not-found.ts +4 -7
- package/src/server/render/ppr-document.ts +2 -1
- package/src/server/render/ppr-route.ts +30 -10
- package/src/server/render/react-stream.ts +17 -0
- package/src/server/render/route-frame-transport.ts +3 -0
- package/src/server/render/ssg.ts +51 -18
- package/src/server/render/ssr.ts +53 -8
- package/src/server/render/template.ts +23 -0
- package/src/server/router/defined-route.ts +3 -0
- package/src/server/router/discovery.ts +25 -9
- package/src/server/router/hmr.ts +3 -2
- package/src/server/router/patterns.ts +16 -0
- package/src/server/router/plugin.ts +27 -12
- package/src/server/router/types.ts +1 -1
- package/src/server/sync/adapter.ts +41 -4
- package/src/server/sync/atomic.ts +303 -0
- package/src/server/sync/drizzle/index.ts +145 -0
- package/src/server/sync/execution-error.ts +1 -0
- package/src/server/sync/fingerprint.ts +2 -6
- package/src/server/sync/index.ts +6 -0
- package/src/server/sync/mutation-handler.ts +69 -0
- package/src/server/sync/plugin.ts +177 -60
- package/src/server/sync/postgres/execute.ts +20 -0
- package/src/server/sync/postgres/index.ts +52 -16
- package/src/server/sync/postgres/sql.ts +20 -0
- package/src/server/sync/prisma/index.ts +70 -0
- package/src/server/sync/queries.ts +75 -0
- package/src/server/sync/query-context.ts +85 -0
- package/src/server/sync/redis/index.ts +7 -3
- package/src/server/sync/redis/scripts.ts +1 -1
- package/src/server/sync/response.ts +21 -0
- package/src/server/sync/sqlite/index.ts +19 -8
- package/src/server/sync/stream.ts +14 -22
- package/src/server/sync/validation.ts +34 -0
- package/src/shared/defer.ts +48 -0
- package/src/shared/error.ts +2 -2
- package/src/shared/page-key.ts +36 -0
- package/src/shared/query-bindings.ts +138 -0
- package/src/shared/route-frame.ts +13 -1
- package/src/shared/route-map.ts +12 -2
- package/src/shared/sync-query.ts +138 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { RoutePatternMap } from "@teyik0/furin/routes";
|
|
2
|
+
import { type Context, createContext, createElement, type ReactNode, useContext } from "react";
|
|
3
|
+
import type { RouteLoaderData, RouteParams } from "../define-route.ts";
|
|
4
|
+
|
|
5
|
+
interface RouteSnapshot {
|
|
6
|
+
data: object;
|
|
7
|
+
params: object;
|
|
8
|
+
pattern: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const RESERVED_RENDER_KEYS = new Set([
|
|
12
|
+
"catch",
|
|
13
|
+
"children",
|
|
14
|
+
"finally",
|
|
15
|
+
"key",
|
|
16
|
+
"ref",
|
|
17
|
+
"then",
|
|
18
|
+
"toJSON",
|
|
19
|
+
]);
|
|
20
|
+
|
|
21
|
+
// Separate client entrypoints share the context, never request-specific data.
|
|
22
|
+
const ROUTE_CONTEXT = Symbol.for("furin.route.context.v1");
|
|
23
|
+
const contextGlobal = globalThis as typeof globalThis & {
|
|
24
|
+
[ROUTE_CONTEXT]?: Context<RouteSnapshot | null>;
|
|
25
|
+
};
|
|
26
|
+
const RouteContext = (contextGlobal[ROUTE_CONTEXT] ??= createContext<RouteSnapshot | null>(null));
|
|
27
|
+
|
|
28
|
+
type ResolvedRouteParams<Route> = [RouteParams<Route>] extends [never]
|
|
29
|
+
? Record<PropertyKey, never>
|
|
30
|
+
: unknown extends RouteParams<Route>
|
|
31
|
+
? Record<PropertyKey, never>
|
|
32
|
+
: keyof RouteParams<Route> extends never
|
|
33
|
+
? Record<PropertyKey, never>
|
|
34
|
+
: RouteParams<Route>;
|
|
35
|
+
|
|
36
|
+
/** @internal Supplies the same rendered snapshot on the server and client. */
|
|
37
|
+
export function withRouteSnapshot(element: ReactNode, pattern: string, props: object): ReactNode {
|
|
38
|
+
const {
|
|
39
|
+
params,
|
|
40
|
+
path: _path,
|
|
41
|
+
query: _query,
|
|
42
|
+
...data
|
|
43
|
+
} = props as {
|
|
44
|
+
params?: object;
|
|
45
|
+
path?: string;
|
|
46
|
+
query?: object;
|
|
47
|
+
};
|
|
48
|
+
const loaderData = Object.fromEntries(
|
|
49
|
+
Object.entries(data).filter(
|
|
50
|
+
([key]) => !(RESERVED_RENDER_KEYS.has(key) || key.startsWith("__furin"))
|
|
51
|
+
)
|
|
52
|
+
);
|
|
53
|
+
return createElement(
|
|
54
|
+
RouteContext.Provider,
|
|
55
|
+
{ value: { data: loaderData, params: params ?? {}, pattern } },
|
|
56
|
+
element
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function useRouteSnapshot(pattern: string): RouteSnapshot {
|
|
61
|
+
const snapshot = useContext(RouteContext);
|
|
62
|
+
if (snapshot?.pattern !== pattern) {
|
|
63
|
+
throw new Error(`[furin] getRouteApi("${pattern}") does not match the active route.`);
|
|
64
|
+
}
|
|
65
|
+
return snapshot;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function getRouteApi<Pattern extends keyof RoutePatternMap & string>(pattern: Pattern) {
|
|
69
|
+
return {
|
|
70
|
+
useLoaderData: (): RouteLoaderData<RoutePatternMap[Pattern]> =>
|
|
71
|
+
useRouteSnapshot(pattern).data as RouteLoaderData<RoutePatternMap[Pattern]>,
|
|
72
|
+
useParams: (): ResolvedRouteParams<RoutePatternMap[Pattern]> =>
|
|
73
|
+
useRouteSnapshot(pattern).params as ResolvedRouteParams<RoutePatternMap[Pattern]>,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type React from "react";
|
|
2
2
|
import type { ElementType } from "react";
|
|
3
3
|
import { createElement } from "react";
|
|
4
|
+
import { pageKey } from "../../shared/page-key.ts";
|
|
4
5
|
import { type BoundaryOptions, FurinErrorBoundary, wrapSegmentBoundaries } from "../boundaries.tsx";
|
|
5
6
|
import type { RuntimeRoute } from "../internal/runtime-types.ts";
|
|
7
|
+
import { withRouteSnapshot } from "../route-api.tsx";
|
|
6
8
|
import { FurinServerError } from "../server-error.ts";
|
|
7
9
|
import { RouterContext } from "./context.ts";
|
|
8
10
|
import type {
|
|
@@ -62,15 +64,14 @@ export function buildPageElement(
|
|
|
62
64
|
): React.ReactNode {
|
|
63
65
|
let element: React.ReactNode = error
|
|
64
66
|
? createElement(RouteErrorThrower, { error })
|
|
65
|
-
: createElement(match.component,
|
|
67
|
+
: createElement(match.component, {
|
|
68
|
+
...data,
|
|
69
|
+
key: pageKey(match.pattern, data, match.pageRoute.remountDeps),
|
|
70
|
+
});
|
|
66
71
|
|
|
67
72
|
// Reconstruct the FULL route chain (shallow→deep, index 0 = root) by walking
|
|
68
|
-
// parents.
|
|
69
|
-
//
|
|
70
|
-
// `buildElement` exactly, which is what guarantees the per-segment boundaries
|
|
71
|
-
// and layouts attach at the same depths on both sides (hydration parity).
|
|
72
|
-
// Compacting to layouts-only (the previous approach) misaligned boundaries
|
|
73
|
-
// and dropped the first nested layout whenever an ancestor lacked a layout.
|
|
73
|
+
// parents. Boundary depths refer to these indices, including routes without
|
|
74
|
+
// layouts, so server and client attach boundaries at the same positions.
|
|
74
75
|
const chain: RuntimeRoute[] = [];
|
|
75
76
|
let current: RuntimeRoute | undefined = match.pageRoute;
|
|
76
77
|
while (current) {
|
|
@@ -80,9 +81,11 @@ export function buildPageElement(
|
|
|
80
81
|
|
|
81
82
|
// Index boundaries by depth for O(1) lookup. A boundary's `depth` maps 1:1 to
|
|
82
83
|
// the route-chain index (depth 0 = root layout, handled separately below).
|
|
83
|
-
const byDepth = new Map<number, ClientSegmentBoundary>();
|
|
84
|
+
const byDepth = new Map<number, ClientSegmentBoundary[]>();
|
|
84
85
|
for (const segment of match.segmentBoundaries ?? []) {
|
|
85
|
-
byDepth.
|
|
86
|
+
const boundaries = byDepth.get(segment.depth) ?? [];
|
|
87
|
+
boundaries.unshift(segment);
|
|
88
|
+
byDepth.set(segment.depth, boundaries);
|
|
86
89
|
}
|
|
87
90
|
|
|
88
91
|
// When a root route is present it occupies chain index 0 and is wrapped
|
|
@@ -96,7 +99,9 @@ export function buildPageElement(
|
|
|
96
99
|
// boundary (so the boundary sits INSIDE the layout), then wrap with the
|
|
97
100
|
// layout itself when this route declares one.
|
|
98
101
|
for (let i = chain.length - 1; i >= rootOffset; i -= 1) {
|
|
99
|
-
|
|
102
|
+
for (const segment of byDepth.get(i) ?? []) {
|
|
103
|
+
element = wrapSegmentBoundaries(element, segment, options);
|
|
104
|
+
}
|
|
100
105
|
const Layout = chain[i]?.layout;
|
|
101
106
|
if (Layout) {
|
|
102
107
|
element = createElement(Layout as ElementType, data, element);
|
|
@@ -105,11 +110,13 @@ export function buildPageElement(
|
|
|
105
110
|
|
|
106
111
|
if (root) {
|
|
107
112
|
// Depth 0 boundary wraps EVERYTHING below the root layout.
|
|
108
|
-
|
|
113
|
+
for (const segment of byDepth.get(0) ?? []) {
|
|
114
|
+
element = wrapSegmentBoundaries(element, segment, options);
|
|
115
|
+
}
|
|
109
116
|
if (root.layout) {
|
|
110
117
|
element = createElement(root.layout as ElementType, data, element);
|
|
111
118
|
}
|
|
112
119
|
}
|
|
113
120
|
|
|
114
|
-
return element;
|
|
121
|
+
return error ? element : withRouteSnapshot(element, match.pattern, data);
|
|
115
122
|
}
|
|
@@ -1,6 +1,12 @@
|
|
|
1
|
-
import { createContext, useContext } from "react";
|
|
2
|
-
import { navigationHrefPolicy } from "./link-utils.ts";
|
|
3
|
-
import type {
|
|
1
|
+
import { createContext, useCallback, useContext, useMemo } from "react";
|
|
2
|
+
import { navigationHrefPolicy, type RuntimeRouteTarget, resolveRouteTarget } from "./link-utils.ts";
|
|
3
|
+
import type {
|
|
4
|
+
NavigationOptions,
|
|
5
|
+
Prefetch,
|
|
6
|
+
Router,
|
|
7
|
+
RouterContextValue,
|
|
8
|
+
RouterNavigate,
|
|
9
|
+
} from "./types.ts";
|
|
4
10
|
|
|
5
11
|
export const RouterContext = createContext<RouterContextValue | null>(null);
|
|
6
12
|
|
|
@@ -38,7 +44,54 @@ export const CLIENT_FALLBACK_ROUTER: RouterContextValue = {
|
|
|
38
44
|
* Returns the current router context.
|
|
39
45
|
* Provides a graceful fallback (full-page navigation) when used outside RouterProvider.
|
|
40
46
|
*/
|
|
41
|
-
export function
|
|
47
|
+
export function useRouterContext(): RouterContextValue {
|
|
42
48
|
const ctx = useContext(RouterContext);
|
|
43
49
|
return ctx ?? CLIENT_FALLBACK_ROUTER;
|
|
44
50
|
}
|
|
51
|
+
|
|
52
|
+
export function useRouter(): Router {
|
|
53
|
+
const context = useRouterContext();
|
|
54
|
+
const navigate: RouterNavigate = useCallback(
|
|
55
|
+
(next: string | (RuntimeRouteTarget & NavigationOptions), opts?: NavigationOptions) => {
|
|
56
|
+
const href = typeof next === "string" ? next : resolveRouteTarget(next, context.searchRoutes);
|
|
57
|
+
const policy = navigationHrefPolicy(
|
|
58
|
+
href,
|
|
59
|
+
typeof window === "undefined" ? undefined : window.location.origin
|
|
60
|
+
);
|
|
61
|
+
if (policy === "blocked") {
|
|
62
|
+
return Promise.reject(new Error("[furin] Unsafe navigation URL."));
|
|
63
|
+
}
|
|
64
|
+
if (policy === "external" && typeof window !== "undefined") {
|
|
65
|
+
window.location.assign(href);
|
|
66
|
+
return Promise.resolve();
|
|
67
|
+
}
|
|
68
|
+
if (typeof next === "string") {
|
|
69
|
+
return context.navigate(href, opts);
|
|
70
|
+
}
|
|
71
|
+
const options =
|
|
72
|
+
next.replace === undefined && next.resetScroll === undefined
|
|
73
|
+
? undefined
|
|
74
|
+
: { replace: next.replace, resetScroll: next.resetScroll };
|
|
75
|
+
return context.navigate(href, options);
|
|
76
|
+
},
|
|
77
|
+
[context.navigate, context.searchRoutes]
|
|
78
|
+
);
|
|
79
|
+
const prefetch: Prefetch = useCallback(
|
|
80
|
+
(
|
|
81
|
+
next: string | (RuntimeRouteTarget & { staleTime?: number }),
|
|
82
|
+
opts?: { staleTime?: number }
|
|
83
|
+
) => {
|
|
84
|
+
const href = typeof next === "string" ? next : resolveRouteTarget(next, context.searchRoutes);
|
|
85
|
+
const policy = navigationHrefPolicy(
|
|
86
|
+
href,
|
|
87
|
+
typeof window === "undefined" ? undefined : window.location.origin
|
|
88
|
+
);
|
|
89
|
+
if (policy !== "internal") {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
context.prefetch(href, typeof next === "string" ? opts : { staleTime: next.staleTime });
|
|
93
|
+
},
|
|
94
|
+
[context.prefetch, context.searchRoutes]
|
|
95
|
+
);
|
|
96
|
+
return useMemo(() => ({ ...context, navigate, prefetch }), [context, navigate, prefetch]);
|
|
97
|
+
}
|
|
@@ -47,11 +47,14 @@ export type {
|
|
|
47
47
|
ClientSegmentBoundary,
|
|
48
48
|
LinkProps,
|
|
49
49
|
LoadedClientRoute,
|
|
50
|
+
Prefetch,
|
|
50
51
|
PreloadStrategy,
|
|
51
52
|
RootBoundaryOptions,
|
|
52
53
|
RouteManifest,
|
|
53
54
|
RouteParamsOf,
|
|
55
|
+
Router,
|
|
54
56
|
RouterContextValue,
|
|
57
|
+
RouterNavigate,
|
|
55
58
|
RouterProviderProps,
|
|
56
59
|
RouterState,
|
|
57
60
|
RouteSearch,
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
buildSearchParams,
|
|
3
|
+
findSearchDefaultsForRouteTarget,
|
|
4
|
+
type SearchParamsInput,
|
|
5
|
+
type SearchRouteMetadata,
|
|
6
|
+
} from "../../shared/search-params.ts";
|
|
2
7
|
import type { CacheEntry } from "./types.ts";
|
|
3
8
|
|
|
4
9
|
const HASH_FRAGMENT_RE = /#.*$/;
|
|
@@ -94,6 +99,29 @@ export function applyLinkParams(
|
|
|
94
99
|
return url;
|
|
95
100
|
}
|
|
96
101
|
|
|
102
|
+
export interface RuntimeRouteTarget {
|
|
103
|
+
hash?: string;
|
|
104
|
+
params?: object;
|
|
105
|
+
search?: object;
|
|
106
|
+
to: string;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function resolveRouteTarget(
|
|
110
|
+
target: RuntimeRouteTarget,
|
|
111
|
+
routes: SearchRouteMetadata[]
|
|
112
|
+
): string {
|
|
113
|
+
const path = applyLinkParams(
|
|
114
|
+
target.to,
|
|
115
|
+
target.params as Record<string, string | number> | undefined
|
|
116
|
+
);
|
|
117
|
+
return buildHref(
|
|
118
|
+
path,
|
|
119
|
+
target.search as SearchParamsInput | undefined,
|
|
120
|
+
target.hash,
|
|
121
|
+
findSearchDefaultsForRouteTarget(path, routes)
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
97
125
|
/**
|
|
98
126
|
* Builds a full href string from a pathname, optional search params, and optional hash.
|
|
99
127
|
* Null/undefined search values are omitted.
|
|
@@ -1,55 +1,8 @@
|
|
|
1
|
-
import { useCallback } from "react";
|
|
2
|
-
import {
|
|
3
|
-
findSearchDefaultsForRouteTarget,
|
|
4
|
-
type SearchParamsInput,
|
|
5
|
-
} from "../../shared/search-params.ts";
|
|
6
1
|
import { useRouter } from "./context.ts";
|
|
7
|
-
import {
|
|
8
|
-
import type { RouteSearch, RouteTo } from "./types.ts";
|
|
2
|
+
import type { Navigate } from "./types.ts";
|
|
9
3
|
|
|
10
|
-
export
|
|
11
|
-
hash?: string;
|
|
12
|
-
replace?: boolean;
|
|
13
|
-
resetScroll?: boolean;
|
|
14
|
-
search?: RouteSearch<To>;
|
|
15
|
-
to: To;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export type Navigate = <To extends RouteTo>(next: NavigateInput<To>) => Promise<void>;
|
|
19
|
-
type NavigateOptions = Parameters<ReturnType<typeof useRouter>["navigate"]>[1];
|
|
4
|
+
export type { Navigate, NavigateInput } from "./types.ts";
|
|
20
5
|
|
|
21
6
|
export function useNavigate(): Navigate {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
return useCallback<Navigate>(
|
|
25
|
-
(next) => {
|
|
26
|
-
const searchDefaults = findSearchDefaultsForRouteTarget(
|
|
27
|
-
next.to as string,
|
|
28
|
-
router.searchRoutes
|
|
29
|
-
);
|
|
30
|
-
const href = buildHref(
|
|
31
|
-
next.to as string,
|
|
32
|
-
next.search as SearchParamsInput | null | undefined,
|
|
33
|
-
next.hash,
|
|
34
|
-
searchDefaults
|
|
35
|
-
);
|
|
36
|
-
const policy = navigationHrefPolicy(
|
|
37
|
-
href,
|
|
38
|
-
typeof window === "undefined" ? undefined : window.location.origin
|
|
39
|
-
);
|
|
40
|
-
if (policy === "blocked") {
|
|
41
|
-
return Promise.reject(new Error("[furin] Unsafe navigation URL."));
|
|
42
|
-
}
|
|
43
|
-
if (policy === "external" && typeof window !== "undefined") {
|
|
44
|
-
window.location.assign(href);
|
|
45
|
-
return Promise.resolve();
|
|
46
|
-
}
|
|
47
|
-
const opts: NavigateOptions =
|
|
48
|
-
next.replace === undefined && next.resetScroll === undefined
|
|
49
|
-
? undefined
|
|
50
|
-
: { replace: next.replace, resetScroll: next.resetScroll };
|
|
51
|
-
return router.navigate(href, opts);
|
|
52
|
-
},
|
|
53
|
-
[router]
|
|
54
|
-
);
|
|
7
|
+
return useRouter().navigate;
|
|
55
8
|
}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import type { SearchParamsInput } from "../../shared/search-params.ts";
|
|
2
|
+
import type { QueryStore } from "../query-store.ts";
|
|
3
|
+
import type { OptimisticCache } from "../sync.ts";
|
|
4
|
+
import { buildHref } from "./link-utils.ts";
|
|
5
|
+
|
|
6
|
+
interface Transformation {
|
|
7
|
+
href: string;
|
|
8
|
+
transform: (data: object) => object;
|
|
9
|
+
}
|
|
10
|
+
interface Operation {
|
|
11
|
+
onRemove?: () => void;
|
|
12
|
+
pending: boolean;
|
|
13
|
+
queryHref?: string;
|
|
14
|
+
transforms: Transformation[];
|
|
15
|
+
}
|
|
16
|
+
interface Snapshot {
|
|
17
|
+
href: string;
|
|
18
|
+
loaded: boolean;
|
|
19
|
+
pattern: string | undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const SYNC_REQUEST = Symbol.for("furin.sync.request.v1");
|
|
23
|
+
|
|
24
|
+
export interface OptimisticRuntime {
|
|
25
|
+
basePath: string;
|
|
26
|
+
begin: (
|
|
27
|
+
optimistic: ((cache: OptimisticCache) => void) | undefined,
|
|
28
|
+
onRemove?: () => void,
|
|
29
|
+
hasQueries?: () => boolean
|
|
30
|
+
) => Operation;
|
|
31
|
+
commit: (href: string) => void;
|
|
32
|
+
finish: (
|
|
33
|
+
operation: Operation,
|
|
34
|
+
outcome: "success" | "error" | "ambiguous",
|
|
35
|
+
response: Response | undefined
|
|
36
|
+
) => void;
|
|
37
|
+
has: (href: string) => boolean;
|
|
38
|
+
project: <Data extends object>(data: Data, href: string) => Data;
|
|
39
|
+
publishable: (href: string, revision: number) => boolean;
|
|
40
|
+
queries?: QueryStore;
|
|
41
|
+
revision: (href?: string) => number;
|
|
42
|
+
subscribe: (listener: () => void) => () => void;
|
|
43
|
+
wait: (href: string, signal: AbortSignal | undefined) => Promise<void>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface RuntimeOptions {
|
|
47
|
+
basePath: string;
|
|
48
|
+
onResponse: (response: Response | undefined, optimistic: boolean) => void;
|
|
49
|
+
queries?: QueryStore;
|
|
50
|
+
snapshot: () => Snapshot;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function identity(href: string): string {
|
|
54
|
+
const url = new URL(href, "http://furin.local");
|
|
55
|
+
url.searchParams.sort();
|
|
56
|
+
return url.pathname + url.search;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const RUNTIMES = Symbol.for("furin.optimistic.runtimes.v1");
|
|
60
|
+
const runtimeGlobal = globalThis as typeof globalThis & { [RUNTIMES]?: Set<OptimisticRuntime> };
|
|
61
|
+
const runtimes = (runtimeGlobal[RUNTIMES] ??= new Set<OptimisticRuntime>());
|
|
62
|
+
|
|
63
|
+
export function registerOptimisticRuntime(runtime: OptimisticRuntime): () => void {
|
|
64
|
+
runtimes.add(runtime);
|
|
65
|
+
return () => {
|
|
66
|
+
runtimes.delete(runtime);
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function findOptimisticRuntime(domain: string): OptimisticRuntime | undefined {
|
|
71
|
+
if (
|
|
72
|
+
typeof window === "undefined" ||
|
|
73
|
+
new URL(domain, window.location.origin).origin !== window.location.origin
|
|
74
|
+
) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const path = window.location.pathname;
|
|
78
|
+
const matches = [...runtimes]
|
|
79
|
+
.filter(
|
|
80
|
+
({ basePath }) => basePath === "" || path === basePath || path.startsWith(`${basePath}/`)
|
|
81
|
+
)
|
|
82
|
+
.sort((a, b) => b.basePath.length - a.basePath.length);
|
|
83
|
+
// Overlapping providers must not receive one another's projections.
|
|
84
|
+
return matches[0]?.basePath === matches[1]?.basePath ? undefined : matches[0];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function createOptimisticRuntime(options: RuntimeOptions): OptimisticRuntime {
|
|
88
|
+
const operations = new Set<Operation>();
|
|
89
|
+
const listeners = new Set<() => void>();
|
|
90
|
+
const revisions = new Map<string, number>();
|
|
91
|
+
let version = 0;
|
|
92
|
+
const notify = (operation: Operation) => {
|
|
93
|
+
version += 1;
|
|
94
|
+
for (const { href } of operation.transforms) {
|
|
95
|
+
revisions.set(href, (revisions.get(href) ?? 0) + 1);
|
|
96
|
+
}
|
|
97
|
+
if (operation.queryHref) {
|
|
98
|
+
revisions.set(operation.queryHref, (revisions.get(operation.queryHref) ?? 0) + 1);
|
|
99
|
+
}
|
|
100
|
+
for (const listener of listeners) {
|
|
101
|
+
listener();
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
const pending = (href: string) =>
|
|
105
|
+
[...operations].some(
|
|
106
|
+
(operation) =>
|
|
107
|
+
operation.pending &&
|
|
108
|
+
(operation.queryHref === identity(href) ||
|
|
109
|
+
operation.transforms.some((transform) => transform.href === identity(href)))
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
basePath: options.basePath,
|
|
114
|
+
queries: options.queries,
|
|
115
|
+
begin(optimistic, onRemove, hasQueries) {
|
|
116
|
+
const operation: Operation = { onRemove, pending: true, transforms: [] };
|
|
117
|
+
const cache = {
|
|
118
|
+
update(
|
|
119
|
+
target: string | { path: string; search?: SearchParamsInput },
|
|
120
|
+
transform: (data: object) => object
|
|
121
|
+
) {
|
|
122
|
+
const snapshot = options.snapshot();
|
|
123
|
+
const path =
|
|
124
|
+
typeof target === "string" ? target : buildHref(target.path, target.search, undefined);
|
|
125
|
+
const href = identity(
|
|
126
|
+
typeof target === "string" && target === snapshot.pattern ? snapshot.href : path
|
|
127
|
+
);
|
|
128
|
+
if (snapshot.loaded && href === identity(snapshot.href)) {
|
|
129
|
+
operation.transforms.push({ href, transform });
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
} as unknown as OptimisticCache;
|
|
133
|
+
optimistic?.(cache);
|
|
134
|
+
if (hasQueries?.() && options.snapshot().loaded) {
|
|
135
|
+
operation.queryHref = identity(options.snapshot().href);
|
|
136
|
+
}
|
|
137
|
+
if (operation.transforms.length > 0 || operation.queryHref !== undefined) {
|
|
138
|
+
operations.add(operation);
|
|
139
|
+
notify(operation);
|
|
140
|
+
}
|
|
141
|
+
return operation;
|
|
142
|
+
},
|
|
143
|
+
commit(href) {
|
|
144
|
+
for (const operation of operations) {
|
|
145
|
+
if (!operation.pending) {
|
|
146
|
+
if (operation.queryHref === identity(href)) {
|
|
147
|
+
operation.queryHref = undefined;
|
|
148
|
+
}
|
|
149
|
+
operation.transforms = operation.transforms.filter(
|
|
150
|
+
(transform) => transform.href !== identity(href)
|
|
151
|
+
);
|
|
152
|
+
if (operation.transforms.length === 0 && operation.queryHref === undefined) {
|
|
153
|
+
operations.delete(operation);
|
|
154
|
+
operation.onRemove?.();
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
// Router publishes the new base and removal together; no intermediate notification.
|
|
159
|
+
},
|
|
160
|
+
finish(operation, outcome, response) {
|
|
161
|
+
operation.pending = false;
|
|
162
|
+
if (outcome === "error") {
|
|
163
|
+
operations.delete(operation);
|
|
164
|
+
}
|
|
165
|
+
if (
|
|
166
|
+
outcome === "error" ||
|
|
167
|
+
(operation.transforms.length === 0 && operation.queryHref === undefined)
|
|
168
|
+
) {
|
|
169
|
+
operation.onRemove?.();
|
|
170
|
+
}
|
|
171
|
+
if (operation.transforms.length > 0 || operation.queryHref !== undefined) {
|
|
172
|
+
notify(operation);
|
|
173
|
+
}
|
|
174
|
+
options.onResponse(
|
|
175
|
+
response,
|
|
176
|
+
outcome !== "error" &&
|
|
177
|
+
(operation.transforms.length > 0 || operation.queryHref !== undefined)
|
|
178
|
+
);
|
|
179
|
+
},
|
|
180
|
+
has: (href) =>
|
|
181
|
+
[...operations].some(
|
|
182
|
+
(operation) =>
|
|
183
|
+
operation.queryHref === identity(href) ||
|
|
184
|
+
operation.transforms.some((transform) => transform.href === identity(href))
|
|
185
|
+
),
|
|
186
|
+
project(data, href) {
|
|
187
|
+
let projected: object = data;
|
|
188
|
+
for (const operation of operations) {
|
|
189
|
+
for (const transformation of operation.transforms) {
|
|
190
|
+
if (transformation.href === identity(href)) {
|
|
191
|
+
projected = { ...projected, ...transformation.transform(projected) };
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return projected as typeof data;
|
|
196
|
+
},
|
|
197
|
+
publishable: (href, revision) =>
|
|
198
|
+
!pending(href) && (revisions.get(identity(href)) ?? 0) === revision,
|
|
199
|
+
revision: (href) => (href === undefined ? version : (revisions.get(identity(href)) ?? 0)),
|
|
200
|
+
subscribe(listener) {
|
|
201
|
+
listeners.add(listener);
|
|
202
|
+
return () => {
|
|
203
|
+
listeners.delete(listener);
|
|
204
|
+
};
|
|
205
|
+
},
|
|
206
|
+
wait(href, signal) {
|
|
207
|
+
if (signal?.aborted) {
|
|
208
|
+
return Promise.reject(signal.reason);
|
|
209
|
+
}
|
|
210
|
+
if (!pending(href)) {
|
|
211
|
+
return Promise.resolve();
|
|
212
|
+
}
|
|
213
|
+
return new Promise<void>((resolve, reject) => {
|
|
214
|
+
const cleanup = () => {
|
|
215
|
+
listeners.delete(check);
|
|
216
|
+
signal?.removeEventListener("abort", abort);
|
|
217
|
+
};
|
|
218
|
+
const abort = () => {
|
|
219
|
+
cleanup();
|
|
220
|
+
reject(signal?.reason);
|
|
221
|
+
};
|
|
222
|
+
const check = () => {
|
|
223
|
+
if (!pending(href)) {
|
|
224
|
+
cleanup();
|
|
225
|
+
resolve();
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
listeners.add(check);
|
|
229
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
230
|
+
});
|
|
231
|
+
},
|
|
232
|
+
};
|
|
233
|
+
}
|