@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
package/src/client/hmr.ts
CHANGED
|
@@ -57,7 +57,8 @@ export function updateHotComponent<Props>(
|
|
|
57
57
|
boundary: (props) => slot.current(props),
|
|
58
58
|
current: component as ClientComponent<never>,
|
|
59
59
|
signature: readHookSignature(component),
|
|
60
|
-
stable: (props
|
|
60
|
+
stable: (props: object) =>
|
|
61
|
+
jsx(slot.boundary as (props: unknown) => ReactNode, { ...props }) as ReactNode,
|
|
61
62
|
};
|
|
62
63
|
registry.set(key, slot);
|
|
63
64
|
return slot.stable as ClientComponent<Props>;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type React from "react";
|
|
2
2
|
import type { HeadOptions, RenderingMode } from "../../client.ts";
|
|
3
3
|
import type { RequestLoaderContext } from "../../define-route.ts";
|
|
4
|
+
import type { RemountDeps } from "../../shared/page-key.ts";
|
|
4
5
|
|
|
5
6
|
export type RuntimeData = Record<string, unknown>;
|
|
6
7
|
|
|
@@ -22,6 +23,7 @@ export interface RuntimeRoute {
|
|
|
22
23
|
params?: unknown;
|
|
23
24
|
parent?: RuntimeRoute;
|
|
24
25
|
query?: unknown;
|
|
26
|
+
remountDeps?: RemountDeps<object, object>;
|
|
25
27
|
requestLoader?: (ctx: RequestLoaderContext) => Awaitable<object>;
|
|
26
28
|
revalidate?: number;
|
|
27
29
|
sourcePath?: string;
|
package/src/client/link.tsx
CHANGED
|
@@ -4,14 +4,14 @@ import {
|
|
|
4
4
|
findSearchDefaultsForRouteTarget,
|
|
5
5
|
type SearchParamsInput,
|
|
6
6
|
} from "../shared/search-params.ts";
|
|
7
|
-
import { CLIENT_FALLBACK_ROUTER, RouterContext,
|
|
7
|
+
import { CLIENT_FALLBACK_ROUTER, RouterContext, useRouterContext } from "./router/context.ts";
|
|
8
8
|
import {
|
|
9
9
|
applyLinkParams,
|
|
10
10
|
buildHref,
|
|
11
11
|
navigationHrefPolicy,
|
|
12
12
|
normalizeHref,
|
|
13
13
|
} from "./router/link-utils.ts";
|
|
14
|
-
import type { LinkProps, RouterContextValue, RouteTo } from "./router/types.ts";
|
|
14
|
+
import type { LinkBaseProps, LinkProps, RouterContextValue, RouteTo } from "./router/types.ts";
|
|
15
15
|
|
|
16
16
|
// biome-ignore lint/performance/noBarrelFile: re-exporting router symbols preserves backward compatibility for @teyik0/furin/link consumers
|
|
17
17
|
export * from "./router/index.ts";
|
|
@@ -40,7 +40,7 @@ function computeLinkView<To extends RouteTo>(
|
|
|
40
40
|
activeProps,
|
|
41
41
|
inactiveProps,
|
|
42
42
|
}: Pick<
|
|
43
|
-
|
|
43
|
+
LinkBaseProps<To>,
|
|
44
44
|
"to" | "params" | "search" | "hash" | "children" | "activeProps" | "inactiveProps"
|
|
45
45
|
>,
|
|
46
46
|
router: RouterContextValue
|
|
@@ -108,8 +108,8 @@ function LinkInteractive<To extends RouteTo>({
|
|
|
108
108
|
// @ts-expect-error: defensive strip of accidental href prop passed via spread
|
|
109
109
|
href: _href,
|
|
110
110
|
...anchorProps
|
|
111
|
-
}:
|
|
112
|
-
const router =
|
|
111
|
+
}: LinkBaseProps<To>): React.ReactElement {
|
|
112
|
+
const router = useRouterContext();
|
|
113
113
|
const anchorRef = useRef<HTMLAnchorElement>(null);
|
|
114
114
|
const intentTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
115
115
|
|
|
@@ -278,7 +278,7 @@ export const SSR_FALLBACK_ROUTER: RouterContextValue = {
|
|
|
278
278
|
* returning the final `<a>` element.
|
|
279
279
|
*/
|
|
280
280
|
function renderLinkElement<To extends RouteTo>(
|
|
281
|
-
props:
|
|
281
|
+
props: LinkBaseProps<To>,
|
|
282
282
|
router: RouterContextValue
|
|
283
283
|
): React.ReactElement {
|
|
284
284
|
const { href, isActive, resolvedChildren, extraProps } = computeLinkView(props, router);
|
|
@@ -347,8 +347,8 @@ export function Link<To extends RouteTo>(props: LinkProps<To>): React.ReactEleme
|
|
|
347
347
|
|
|
348
348
|
// ── Client rendering ──────────────────────────────────────────────────────
|
|
349
349
|
return createElement(
|
|
350
|
-
LinkInteractive as React.ComponentType<
|
|
351
|
-
props as
|
|
350
|
+
LinkInteractive as React.ComponentType<LinkBaseProps<RouteTo>>,
|
|
351
|
+
props as LinkBaseProps<RouteTo>
|
|
352
352
|
);
|
|
353
353
|
}
|
|
354
354
|
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { EdenFetchError } from "@elysia/eden";
|
|
2
|
+
import { useCallback, useRef, useState } from "react";
|
|
3
|
+
|
|
4
|
+
interface MutationResponse {
|
|
5
|
+
data: unknown;
|
|
6
|
+
error: unknown;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
type MutationMethod = (...args: never[]) => Promise<MutationResponse>;
|
|
10
|
+
type MutationData<Method extends MutationMethod> = Awaited<ReturnType<Method>>["data"];
|
|
11
|
+
type ErrorValue<Value> = Value extends { detail: string }
|
|
12
|
+
? Value
|
|
13
|
+
: Value extends readonly unknown[]
|
|
14
|
+
? { detail: string }
|
|
15
|
+
: Value extends object
|
|
16
|
+
? Omit<Value, "detail"> & { detail: string }
|
|
17
|
+
: { detail: string };
|
|
18
|
+
type ApiError<Failure> = Failure extends { status: infer Status extends number; value: infer Value }
|
|
19
|
+
? Value extends { detail: string }
|
|
20
|
+
? Failure
|
|
21
|
+
: EdenFetchError<Status, ErrorValue<Value>>
|
|
22
|
+
: never;
|
|
23
|
+
type MutationError<Method extends MutationMethod> =
|
|
24
|
+
| ApiError<NonNullable<Awaited<ReturnType<Method>>["error"]>>
|
|
25
|
+
| EdenFetchError<0, { detail: string }>;
|
|
26
|
+
|
|
27
|
+
function isApiError(value: unknown): value is { status: number; value: unknown } {
|
|
28
|
+
return (
|
|
29
|
+
typeof value === "object" &&
|
|
30
|
+
value !== null &&
|
|
31
|
+
"status" in value &&
|
|
32
|
+
typeof value.status === "number" &&
|
|
33
|
+
"value" in value
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function normalizeMutationError<Method extends MutationMethod>(
|
|
38
|
+
cause: unknown
|
|
39
|
+
): MutationError<Method> {
|
|
40
|
+
const exception =
|
|
41
|
+
isApiError(cause) && cause.status === 0 && cause.value instanceof Error ? cause.value : cause;
|
|
42
|
+
if (isApiError(exception)) {
|
|
43
|
+
const { value } = exception;
|
|
44
|
+
if (
|
|
45
|
+
typeof value === "object" &&
|
|
46
|
+
value !== null &&
|
|
47
|
+
"detail" in value &&
|
|
48
|
+
typeof value.detail === "string"
|
|
49
|
+
) {
|
|
50
|
+
return exception as MutationError<Method>;
|
|
51
|
+
}
|
|
52
|
+
const error = new EdenFetchError(exception.status, {
|
|
53
|
+
...(typeof value === "object" && value !== null && !Array.isArray(value) ? value : {}),
|
|
54
|
+
detail: typeof value === "string" && value ? value : "Mutation failed",
|
|
55
|
+
});
|
|
56
|
+
error.cause = exception;
|
|
57
|
+
return error as MutationError<Method>;
|
|
58
|
+
}
|
|
59
|
+
const error = new EdenFetchError(0, {
|
|
60
|
+
detail: exception instanceof Error ? exception.message : "Mutation failed",
|
|
61
|
+
});
|
|
62
|
+
error.cause = exception;
|
|
63
|
+
return error;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
interface MutationOptions<Method extends MutationMethod> {
|
|
67
|
+
onError?: (error: MutationError<Method>) => void | Promise<void>;
|
|
68
|
+
onSuccess?: (data: MutationData<Method>) => void | Promise<void>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function useMutation<Method extends MutationMethod>(
|
|
72
|
+
method: Method,
|
|
73
|
+
options?: MutationOptions<Method>
|
|
74
|
+
) {
|
|
75
|
+
const [state, setState] = useState<{
|
|
76
|
+
data: MutationData<Method> | undefined;
|
|
77
|
+
error: MutationError<Method> | null;
|
|
78
|
+
isPending: boolean;
|
|
79
|
+
}>({ data: undefined, error: null, isPending: false });
|
|
80
|
+
const latest = useRef<number>(0);
|
|
81
|
+
const pending = useRef<number>(0);
|
|
82
|
+
|
|
83
|
+
const mutateAsync = useCallback(
|
|
84
|
+
async (...args: Parameters<Method>): Promise<MutationData<Method>> => {
|
|
85
|
+
latest.current += 1;
|
|
86
|
+
const invocation = latest.current;
|
|
87
|
+
pending.current += 1;
|
|
88
|
+
setState({ data: undefined, error: null, isPending: true });
|
|
89
|
+
try {
|
|
90
|
+
const result = (await Reflect.apply(method, undefined, args)) as Awaited<
|
|
91
|
+
ReturnType<Method>
|
|
92
|
+
>;
|
|
93
|
+
if (result.error !== null) {
|
|
94
|
+
if (
|
|
95
|
+
"response" in result &&
|
|
96
|
+
result.response === undefined &&
|
|
97
|
+
result.error instanceof EdenFetchError &&
|
|
98
|
+
result.error.status === 503 &&
|
|
99
|
+
(result.error.value instanceof Error || typeof result.error.value === "string")
|
|
100
|
+
) {
|
|
101
|
+
throw result.error.value;
|
|
102
|
+
}
|
|
103
|
+
throw result.error;
|
|
104
|
+
}
|
|
105
|
+
await options?.onSuccess?.(result.data);
|
|
106
|
+
if (latest.current === invocation) {
|
|
107
|
+
setState((previous) => ({ ...previous, data: result.data, error: null }));
|
|
108
|
+
}
|
|
109
|
+
return result.data;
|
|
110
|
+
} catch (cause) {
|
|
111
|
+
const failure = normalizeMutationError<Method>(cause);
|
|
112
|
+
if (latest.current === invocation) {
|
|
113
|
+
setState((previous) => ({ ...previous, data: undefined, error: failure }));
|
|
114
|
+
}
|
|
115
|
+
await options?.onError?.(failure);
|
|
116
|
+
throw failure;
|
|
117
|
+
} finally {
|
|
118
|
+
pending.current -= 1;
|
|
119
|
+
setState((previous) => ({ ...previous, isPending: pending.current > 0 }));
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
[method, options]
|
|
123
|
+
);
|
|
124
|
+
const mutate = useCallback(
|
|
125
|
+
(...args: Parameters<Method>): void => {
|
|
126
|
+
mutateAsync(...args).catch(() => undefined);
|
|
127
|
+
},
|
|
128
|
+
[mutateAsync]
|
|
129
|
+
);
|
|
130
|
+
const reset = useCallback(() => {
|
|
131
|
+
latest.current += 1;
|
|
132
|
+
setState((previous) => ({ ...previous, data: undefined, error: null }));
|
|
133
|
+
}, []);
|
|
134
|
+
|
|
135
|
+
return { ...state, mutate, mutateAsync, reset };
|
|
136
|
+
}
|
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type QueryIdentity,
|
|
3
|
+
type QueryReadIdentity,
|
|
4
|
+
type QuerySeed,
|
|
5
|
+
queryTag,
|
|
6
|
+
queryUrl,
|
|
7
|
+
} from "../shared/sync-query.ts";
|
|
8
|
+
|
|
9
|
+
export const QUERY_REFERENCE = Symbol.for("furin.query.reference.v1");
|
|
10
|
+
|
|
11
|
+
export interface ReadResult {
|
|
12
|
+
data: unknown;
|
|
13
|
+
error: unknown;
|
|
14
|
+
identity?: QueryReadIdentity;
|
|
15
|
+
local?: boolean;
|
|
16
|
+
response?: Response;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface ReadReference {
|
|
20
|
+
client: QueryStore;
|
|
21
|
+
load: (options: unknown) => Promise<ReadResult>;
|
|
22
|
+
url: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type QueryMethod = ((...args: never[]) => Promise<ReadResult>) & {
|
|
26
|
+
readonly [QUERY_REFERENCE]: true;
|
|
27
|
+
};
|
|
28
|
+
export type QueryData<Method extends QueryMethod> = NonNullable<
|
|
29
|
+
Awaited<ReturnType<Method>>["data"]
|
|
30
|
+
>;
|
|
31
|
+
|
|
32
|
+
export interface QuerySnapshot {
|
|
33
|
+
data: unknown;
|
|
34
|
+
error: unknown;
|
|
35
|
+
isFetching: boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface QueryEntry {
|
|
39
|
+
base: unknown;
|
|
40
|
+
client?: QueryStore;
|
|
41
|
+
identity?: QueryReadIdentity;
|
|
42
|
+
listeners: Set<() => void>;
|
|
43
|
+
load?: () => Promise<ReadResult>;
|
|
44
|
+
local?: boolean;
|
|
45
|
+
promise?: Promise<void>;
|
|
46
|
+
snapshot: QuerySnapshot;
|
|
47
|
+
stale: boolean;
|
|
48
|
+
version: number;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface QueryProjection {
|
|
52
|
+
onRemove?: () => void;
|
|
53
|
+
pending: boolean;
|
|
54
|
+
transforms: Map<QueryEntry, (data: unknown) => unknown>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
interface QueryEnvironment {
|
|
58
|
+
onRead: () => void;
|
|
59
|
+
origin: string;
|
|
60
|
+
store: QueryStore;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const SERVER_ENVIRONMENT = Symbol.for("furin.query.environment.v1");
|
|
64
|
+
const environmentGlobal = globalThis as typeof globalThis & {
|
|
65
|
+
[SERVER_ENVIRONMENT]?: () => QueryEnvironment | undefined;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/** @internal Installed by the server loader context; no server imports enter the browser bundle. */
|
|
69
|
+
export function setServerQueryEnvironment(read: () => QueryEnvironment | undefined): void {
|
|
70
|
+
environmentGlobal[SERVER_ENVIRONMENT] = read;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function currentQueryEnvironment(): QueryEnvironment | undefined {
|
|
74
|
+
return environmentGlobal[SERVER_ENVIRONMENT]?.();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function readReference(method: QueryMethod): ReadReference {
|
|
78
|
+
return (method as unknown as { [QUERY_REFERENCE]: ReadReference })[QUERY_REFERENCE];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function readUrl(reference: ReadReference, options: unknown): string {
|
|
82
|
+
const url = new URL(reference.url);
|
|
83
|
+
const query = (options as { query?: { [key: string]: unknown } } | undefined)?.query;
|
|
84
|
+
const append = (key: string, value: unknown) => {
|
|
85
|
+
if (value === undefined || value === null) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
let serialized = String(value);
|
|
89
|
+
if (value instanceof Date) {
|
|
90
|
+
serialized = value.toISOString();
|
|
91
|
+
} else if (typeof value === "object") {
|
|
92
|
+
serialized = JSON.stringify(value);
|
|
93
|
+
}
|
|
94
|
+
url.searchParams.append(key, serialized);
|
|
95
|
+
};
|
|
96
|
+
for (const [key, value] of Object.entries(query ?? {})) {
|
|
97
|
+
if (Array.isArray(value)) {
|
|
98
|
+
for (const item of value) {
|
|
99
|
+
append(key, item);
|
|
100
|
+
}
|
|
101
|
+
} else {
|
|
102
|
+
append(key, value);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
url.searchParams.sort();
|
|
106
|
+
return url.href;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export class QueryStore {
|
|
110
|
+
private readonly entries = new Map<string, QueryEntry>();
|
|
111
|
+
private readonly listeners = new Set<() => void>();
|
|
112
|
+
private revisionValue = 0;
|
|
113
|
+
private readonly projections = new Set<QueryProjection>();
|
|
114
|
+
private session: string | undefined;
|
|
115
|
+
private epoch = 0;
|
|
116
|
+
private readonly origin: string | undefined;
|
|
117
|
+
|
|
118
|
+
constructor(origin: string | undefined) {
|
|
119
|
+
this.origin = origin;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
private key(url: string): string {
|
|
123
|
+
const absolute = new URL(url, this.origin).href;
|
|
124
|
+
return queryUrl(absolute, undefined);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
private entry(url: string): QueryEntry {
|
|
128
|
+
const key = this.key(url);
|
|
129
|
+
let entry = this.entries.get(key);
|
|
130
|
+
if (!entry) {
|
|
131
|
+
entry = {
|
|
132
|
+
base: undefined,
|
|
133
|
+
listeners: new Set(),
|
|
134
|
+
snapshot: { data: undefined, error: null, isFetching: false },
|
|
135
|
+
stale: true,
|
|
136
|
+
version: 0,
|
|
137
|
+
};
|
|
138
|
+
this.entries.set(key, entry);
|
|
139
|
+
}
|
|
140
|
+
return entry;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
private publish(entry: QueryEntry): void {
|
|
144
|
+
let data = entry.base;
|
|
145
|
+
for (const projection of this.projections) {
|
|
146
|
+
const transform = projection.transforms.get(entry);
|
|
147
|
+
if (transform && data !== undefined) {
|
|
148
|
+
data = transform(data);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
entry.snapshot = { ...entry.snapshot, data };
|
|
152
|
+
for (const listener of entry.listeners) {
|
|
153
|
+
listener();
|
|
154
|
+
}
|
|
155
|
+
this.revisionValue += 1;
|
|
156
|
+
for (const listener of this.listeners) {
|
|
157
|
+
listener();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
revision = (): number => this.revisionValue;
|
|
162
|
+
|
|
163
|
+
subscribeAll = (listener: () => void): (() => void) => {
|
|
164
|
+
this.listeners.add(listener);
|
|
165
|
+
return () => this.listeners.delete(listener);
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
observe(url: string, result: ReadResult, epoch: number, version?: number): void {
|
|
169
|
+
const existing = this.entries.get(this.key(url));
|
|
170
|
+
if (
|
|
171
|
+
epoch !== this.epoch ||
|
|
172
|
+
result.error !== null ||
|
|
173
|
+
(existing &&
|
|
174
|
+
(this.pending(existing) || (version !== undefined && existing.version !== version)))
|
|
175
|
+
) {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
const header = result.response?.headers.get("x-furin-query");
|
|
179
|
+
const identity =
|
|
180
|
+
result.identity ?? (header ? (JSON.parse(header) as QueryReadIdentity) : undefined);
|
|
181
|
+
if (identity) {
|
|
182
|
+
this.setSession(identity.session);
|
|
183
|
+
}
|
|
184
|
+
const entry = this.entry(url);
|
|
185
|
+
entry.base = result.data;
|
|
186
|
+
entry.version += 1;
|
|
187
|
+
entry.identity = identity;
|
|
188
|
+
entry.local = result.local ?? result.response?.url === "";
|
|
189
|
+
entry.stale = false;
|
|
190
|
+
entry.snapshot = { data: result.data, error: null, isFetching: false };
|
|
191
|
+
for (const projection of this.projections) {
|
|
192
|
+
if (!projection.pending) {
|
|
193
|
+
projection.transforms.delete(entry);
|
|
194
|
+
if (projection.transforms.size === 0) {
|
|
195
|
+
this.projections.delete(projection);
|
|
196
|
+
projection.onRemove?.();
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
this.publish(entry);
|
|
201
|
+
if (this.entries.size > 100) {
|
|
202
|
+
const oldest = [...this.entries].find(
|
|
203
|
+
([, item]) =>
|
|
204
|
+
item !== entry && item.listeners.size === 0 && !item.promise && !this.pending(item)
|
|
205
|
+
);
|
|
206
|
+
if (oldest) {
|
|
207
|
+
this.entries.delete(oldest[0]);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
private setSession(session: string): void {
|
|
213
|
+
if (this.session !== undefined && this.session !== session) {
|
|
214
|
+
this.epoch += 1;
|
|
215
|
+
for (const projection of this.projections) {
|
|
216
|
+
projection.onRemove?.();
|
|
217
|
+
}
|
|
218
|
+
this.projections.clear();
|
|
219
|
+
for (const entry of this.entries.values()) {
|
|
220
|
+
entry.base = undefined;
|
|
221
|
+
entry.identity = undefined;
|
|
222
|
+
entry.stale = true;
|
|
223
|
+
entry.version += 1;
|
|
224
|
+
entry.snapshot = { data: undefined, error: null, isFetching: false };
|
|
225
|
+
this.publish(entry);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
this.session = session;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
generation(): number {
|
|
232
|
+
return this.epoch;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
version(url: string): number {
|
|
236
|
+
return this.entry(url).version;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
snapshot(url: string): QuerySnapshot {
|
|
240
|
+
return this.entry(url).snapshot;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
subscribe(url: string, listener: () => void): () => void {
|
|
244
|
+
const entry = this.entry(url);
|
|
245
|
+
entry.listeners.add(listener);
|
|
246
|
+
return () => entry.listeners.delete(listener);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
bind(url: string, load: () => Promise<ReadResult>, client?: QueryStore): void {
|
|
250
|
+
const entry = this.entry(url);
|
|
251
|
+
if (client && entry.client && client !== entry.client) {
|
|
252
|
+
entry.base = undefined;
|
|
253
|
+
entry.identity = undefined;
|
|
254
|
+
entry.stale = true;
|
|
255
|
+
entry.version += 1;
|
|
256
|
+
entry.snapshot = { data: undefined, error: null, isFetching: false };
|
|
257
|
+
for (const projection of this.projections) {
|
|
258
|
+
projection.transforms.delete(entry);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
entry.client = client ?? entry.client;
|
|
262
|
+
entry.load = load;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
private pending(entry: QueryEntry): boolean {
|
|
266
|
+
return [...this.projections].some(
|
|
267
|
+
(projection) => projection.pending && projection.transforms.has(entry)
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
fetch(url: string): Promise<void> {
|
|
272
|
+
const entry = this.entry(url);
|
|
273
|
+
if (entry.promise) {
|
|
274
|
+
return entry.promise;
|
|
275
|
+
}
|
|
276
|
+
if (!(entry.stale && entry.load) || this.pending(entry)) {
|
|
277
|
+
return Promise.resolve();
|
|
278
|
+
}
|
|
279
|
+
const { version } = entry;
|
|
280
|
+
const { epoch } = this;
|
|
281
|
+
entry.snapshot = { ...entry.snapshot, isFetching: true };
|
|
282
|
+
this.publish(entry);
|
|
283
|
+
entry.promise = Promise.resolve()
|
|
284
|
+
.then(() => entry.load?.())
|
|
285
|
+
.then((result) => {
|
|
286
|
+
if (!result || epoch !== this.epoch || version !== entry.version || this.pending(entry)) {
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
if (result.error === null) {
|
|
290
|
+
this.observe(url, result, epoch);
|
|
291
|
+
} else {
|
|
292
|
+
entry.snapshot = { ...entry.snapshot, error: result.error };
|
|
293
|
+
}
|
|
294
|
+
})
|
|
295
|
+
.catch((error: unknown) => {
|
|
296
|
+
if (epoch === this.epoch && version === entry.version) {
|
|
297
|
+
entry.snapshot = { ...entry.snapshot, error };
|
|
298
|
+
}
|
|
299
|
+
})
|
|
300
|
+
.finally(() => {
|
|
301
|
+
entry.promise = undefined;
|
|
302
|
+
entry.snapshot = { ...entry.snapshot, isFetching: false };
|
|
303
|
+
this.publish(entry);
|
|
304
|
+
if (entry.stale && version !== entry.version && entry.listeners.size > 0) {
|
|
305
|
+
this.fetch(url);
|
|
306
|
+
}
|
|
307
|
+
});
|
|
308
|
+
return entry.promise;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
invalidate(identities: readonly QueryIdentity[]): boolean {
|
|
312
|
+
const tags = new Set(identities.map(queryTag));
|
|
313
|
+
let affected = false;
|
|
314
|
+
for (const [url, entry] of this.entries) {
|
|
315
|
+
if (entry.identity && tags.has(queryTag(entry.identity))) {
|
|
316
|
+
affected = true;
|
|
317
|
+
this.invalidateEntry(url, entry);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
return affected;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
private invalidateEntry(url: string, entry: QueryEntry): void {
|
|
324
|
+
entry.stale = true;
|
|
325
|
+
entry.version += 1;
|
|
326
|
+
if (entry.listeners.size > 0) {
|
|
327
|
+
this.fetch(url);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
invalidateAll(): void {
|
|
332
|
+
for (const [url, entry] of this.entries) {
|
|
333
|
+
this.invalidateEntry(url, entry);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
begin(): QueryProjection {
|
|
338
|
+
return { pending: true, transforms: new Map() };
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
update(projection: QueryProjection, url: string, transform: (data: unknown) => unknown): void {
|
|
342
|
+
const entry = this.entries.get(this.key(url));
|
|
343
|
+
if (!entry || entry.base === undefined) {
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
const previous = projection.transforms.get(entry);
|
|
347
|
+
projection.transforms.set(entry, previous ? (data) => transform(previous(data)) : transform);
|
|
348
|
+
entry.version += 1;
|
|
349
|
+
this.projections.add(projection);
|
|
350
|
+
this.publish(entry);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
finish(projection: QueryProjection, outcome: "success" | "error" | "ambiguous"): void {
|
|
354
|
+
projection.pending = false;
|
|
355
|
+
const entries = [...projection.transforms.keys()];
|
|
356
|
+
if (outcome === "error") {
|
|
357
|
+
this.projections.delete(projection);
|
|
358
|
+
projection.onRemove?.();
|
|
359
|
+
}
|
|
360
|
+
for (const entry of entries) {
|
|
361
|
+
this.publish(entry);
|
|
362
|
+
if (outcome !== "error") {
|
|
363
|
+
entry.stale = true;
|
|
364
|
+
}
|
|
365
|
+
const url = [...this.entries].find(([, candidate]) => candidate === entry)?.[0];
|
|
366
|
+
if (url && entry.stale) {
|
|
367
|
+
this.fetch(url);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
dehydrate(): QuerySeed[] {
|
|
373
|
+
return [...this.entries].flatMap(([url, entry]) =>
|
|
374
|
+
entry.identity && entry.base !== undefined
|
|
375
|
+
? [{ url, data: entry.base, identity: entry.identity, local: entry.local }]
|
|
376
|
+
: []
|
|
377
|
+
);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
private seedUrl(seed: QuerySeed, localOrigin: string | undefined): string {
|
|
381
|
+
const original = new URL(seed.url, this.origin);
|
|
382
|
+
return seed.local && localOrigin
|
|
383
|
+
? new URL(original.pathname + original.search, localOrigin).href
|
|
384
|
+
: original.href;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
captureHydration(): (seeds: readonly QuerySeed[], localOrigin: string | undefined) => void {
|
|
388
|
+
const { epoch } = this;
|
|
389
|
+
const versions = new Map([...this.entries].map(([key, entry]) => [key, entry.version]));
|
|
390
|
+
return (seeds, localOrigin) => {
|
|
391
|
+
if (epoch === this.epoch) {
|
|
392
|
+
this.hydrate(
|
|
393
|
+
seeds.filter((seed) => {
|
|
394
|
+
const url = this.seedUrl(seed, localOrigin);
|
|
395
|
+
return this.version(url) === (versions.get(this.key(url)) ?? 0);
|
|
396
|
+
}),
|
|
397
|
+
localOrigin
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
hydrate(seeds: readonly QuerySeed[], localOrigin?: string): void {
|
|
404
|
+
for (const seed of seeds) {
|
|
405
|
+
const url = this.seedUrl(seed, localOrigin);
|
|
406
|
+
this.observe(
|
|
407
|
+
url,
|
|
408
|
+
{
|
|
409
|
+
data: seed.data,
|
|
410
|
+
error: null,
|
|
411
|
+
identity: seed.identity,
|
|
412
|
+
local: seed.local ?? false,
|
|
413
|
+
},
|
|
414
|
+
this.epoch
|
|
415
|
+
);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Context,
|
|
3
|
+
createContext,
|
|
4
|
+
useCallback,
|
|
5
|
+
useContext,
|
|
6
|
+
useEffect,
|
|
7
|
+
useSyncExternalStore,
|
|
8
|
+
} from "react";
|
|
9
|
+
import {
|
|
10
|
+
type QueryData,
|
|
11
|
+
type QueryMethod,
|
|
12
|
+
type QueryStore,
|
|
13
|
+
readReference,
|
|
14
|
+
readUrl,
|
|
15
|
+
} from "./query-store.ts";
|
|
16
|
+
|
|
17
|
+
// Entrypoints are bundled separately; they must share the context, never the cached data.
|
|
18
|
+
const QUERY_CONTEXT = Symbol.for("furin.query.context.v1");
|
|
19
|
+
const contextGlobal = globalThis as typeof globalThis & {
|
|
20
|
+
[QUERY_CONTEXT]?: Context<QueryStore | null>;
|
|
21
|
+
};
|
|
22
|
+
export const QueryStoreContext = (contextGlobal[QUERY_CONTEXT] ??= createContext<QueryStore | null>(
|
|
23
|
+
null
|
|
24
|
+
));
|
|
25
|
+
|
|
26
|
+
type MethodOptions<Method extends QueryMethod> = NonNullable<Parameters<Method>[0]>;
|
|
27
|
+
type MethodError<Method extends QueryMethod> = Awaited<ReturnType<Method>>["error"];
|
|
28
|
+
type Options<Method extends QueryMethod, Selected> = MethodOptions<Method> & {
|
|
29
|
+
select?: (data: QueryData<Method>) => Selected;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export function useQuery<Method extends QueryMethod, Selected = QueryData<Method>>(
|
|
33
|
+
method: Method,
|
|
34
|
+
...args: object extends MethodOptions<Method>
|
|
35
|
+
? [options?: Options<Method, Selected>]
|
|
36
|
+
: [options: Options<Method, Selected>]
|
|
37
|
+
): { data: Selected | undefined; error: MethodError<Method> | null; isFetching: boolean } {
|
|
38
|
+
const reference = readReference(method);
|
|
39
|
+
if (!reference) {
|
|
40
|
+
throw new Error("[furin] useQuery requires a GET from withSync().");
|
|
41
|
+
}
|
|
42
|
+
const context = useContext(QueryStoreContext);
|
|
43
|
+
const store = context ?? reference.client;
|
|
44
|
+
const [options] = args;
|
|
45
|
+
const select = (options as { select?: (data: QueryData<Method>) => Selected } | undefined)
|
|
46
|
+
?.select;
|
|
47
|
+
const url = readUrl(reference, options);
|
|
48
|
+
store.bind(url, () => reference.load(options), reference.client);
|
|
49
|
+
const subscribe = useCallback(
|
|
50
|
+
(listener: () => void) => store.subscribe(url, listener),
|
|
51
|
+
[store, url]
|
|
52
|
+
);
|
|
53
|
+
const snapshot = useCallback(() => store.snapshot(url), [store, url]);
|
|
54
|
+
const state = useSyncExternalStore(subscribe, snapshot, snapshot);
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
store.fetch(url);
|
|
57
|
+
}, [store, url]);
|
|
58
|
+
let data: Selected | undefined;
|
|
59
|
+
if (state.data !== undefined) {
|
|
60
|
+
data = select ? select(state.data as QueryData<Method>) : (state.data as Selected);
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
data,
|
|
64
|
+
error: state.error as MethodError<Method> | null,
|
|
65
|
+
isFetching: state.isFetching,
|
|
66
|
+
};
|
|
67
|
+
}
|