Skip to content

Commit

Permalink
refactor: simplify types
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Dec 18, 2024
1 parent a93b207 commit d7079a3
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 66 deletions.
24 changes: 7 additions & 17 deletions react-server-dom-vite-example/src/entry.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ import React from "react";
import ReactDomClient from "react-dom/client";
import type { ServerPayload } from "./entry.rsc";
import type { CallServerFn } from "./types";
import { resolveClientReference } from "./utils/client-reference";
import { clientReferenceManifest } from "./utils/client-reference";
import { getFlightStreamBrowser } from "./utils/stream-script";

async function main() {
const manifest = {
resolveClientReference,
};

const callServer: CallServerFn = async (id, args) => {
const url = new URL(window.location.href);
url.searchParams.set("__rsc", id);
Expand All @@ -19,7 +15,7 @@ async function main() {
method: "POST",
body: await ReactClient.encodeReply(args),
}),
manifest,
clientReferenceManifest,
{ callServer },
);
setPayload(payload);
Expand All @@ -29,28 +25,22 @@ async function main() {
const initialPayload =
await ReactClient.createFromReadableStream<ServerPayload>(
getFlightStreamBrowser(),
manifest,
clientReferenceManifest,
{ callServer },
);

let setPayload: (v: ServerPayload) => void;

function BrowserRoot() {
const [payload, setPayload_] = React.useState(
initialPayload
);
const [payload, setPayload_] = React.useState(initialPayload);
const [_isPending, startTransition] = React.useTransition();
setPayload = (v) => startTransition(() => setPayload_(v));
return payload.root;
}

ReactDomClient.hydrateRoot(
document,
<BrowserRoot />,
{
formState: initialPayload.formState,
},
);
ReactDomClient.hydrateRoot(document, <BrowserRoot />, {
formState: initialPayload.formState,
});
}

main();
19 changes: 12 additions & 7 deletions react-server-dom-vite-example/src/entry.rsc.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import ReactServer from "@jacob-ebey/react-server-dom-vite/server";
import type { ReactFormState } from "react-dom/client";
import { App } from "./app";
import type { ServerReferenceManifest } from "./types";
import type {
ClientReferenceMetadataManifest,
ServerReferenceManifest,
} from "./types";
import { fromPipeableToWebReadable } from "./utils/fetch";

export interface RscHandlerResult {
Expand Down Expand Up @@ -57,12 +60,7 @@ export async function handler(
root: <App />,
returnValue,
},
{
resolveClientReferenceMetadata(metadata) {
// console.log("[debug:resolveClientReferenceMetadata]", { metadata }, Object.getOwnPropertyDescriptors(metadata));
return [metadata.$$id];
},
},
clientReferenceMetadataManifest,
{},
),
);
Expand Down Expand Up @@ -95,3 +93,10 @@ const serverReferenceManifest: ServerReferenceManifest = {
};
},
};

const clientReferenceMetadataManifest: ClientReferenceMetadataManifest = {
resolveClientReferenceMetadata(metadata) {
// console.log("[debug:resolveClientReferenceMetadata]", { metadata }, Object.getOwnPropertyDescriptors(metadata));
return metadata.$$id;
},
};
6 changes: 2 additions & 4 deletions react-server-dom-vite-example/src/entry.ssr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ReactClient from "@jacob-ebey/react-server-dom-vite/client";
import ReactDomServer from "react-dom/server";
import type { ModuleRunner } from "vite/module-runner";
import type { ServerPayload } from "./entry.rsc";
import { resolveClientReference } from "./utils/client-reference";
import { clientReferenceManifest } from "./utils/client-reference";
import {
createRequest,
fromPipeableToWebReadable,
Expand Down Expand Up @@ -35,9 +35,7 @@ export default async function handler(

const payload = await ReactClient.createFromNodeStream<ServerPayload>(
fromWebToNodeReadable(flightStream1),
{
resolveClientReference,
},
clientReferenceManifest,
);

const ssrAssets = await import("virtual:ssr-assets");
Expand Down
13 changes: 3 additions & 10 deletions react-server-dom-vite-example/src/types/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
declare module "@jacob-ebey/react-server-dom-vite/server" {
export function renderToPipeableStream<T>(
data: T,
manifest: {
// biome-ignore format:
resolveClientReferenceMetadata: import(".").ResolveClientReferenceMetadataFn;
},
manifest: import(".").ClientReferenceMetadataManifest,
opitons?: unknown,
): import("react-dom/server").PipeableStream;

Expand All @@ -27,16 +24,12 @@ declare module "@jacob-ebey/react-server-dom-vite/server" {
declare module "@jacob-ebey/react-server-dom-vite/client" {
export function createFromNodeStream<T>(
stream: import("node:stream").Readable,
manifest: {
resolveClientReference: import(".").ResolveClientReferenceFn;
},
manifest: import(".").ClientReferenceManifest,
): Promise<T>;

export function createFromReadableStream<T>(
stream: ReadableStream,
manifest: {
resolveClientReference: import(".").ResolveClientReferenceFn;
},
manifest: import(".").ClientReferenceManifest,
options: {
callServer: import(".").CallServerFn;
},
Expand Down
14 changes: 8 additions & 6 deletions react-server-dom-vite-example/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
export type ResolveClientReferenceMetadataFn = (metadata: { $$id: string }) => [
string,
];
export type ClientReferenceMetadataManifest = {
resolveClientReferenceMetadata(metadata: { $$id: string }): string;
};

export type ResolveClientReferenceFn = (reference: [string]) => {
preload(): Promise<void>;
get(): unknown;
export type ClientReferenceManifest = {
resolveClientReference(reference: string): {
preload(): Promise<void>;
get(): unknown;
};
};

export type ResolveServerReferenceFn = (reference: string) => {
Expand Down
47 changes: 25 additions & 22 deletions react-server-dom-vite-example/src/utils/client-reference.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
export function resolveClientReference(reference: [string]) {
// console.log("[debug:resolveClientReference]", { reference })
const [id, name] = reference[0].split("#");
let mod: Record<string, unknown>;
return {
async preload() {
// console.log("[debug:preload]", { id, name})
if (import.meta.env.DEV) {
mod ??= await import(/* @vite-ignore */ id);
} else {
const { default: references } = await import(
"virtual:build-client-references"
);
mod ??= await references[id]();
}
},
get() {
// console.log("[debug:get]", { id, name })
return mod[name];
},
};
}
import type { ClientReferenceManifest } from "../types";

export const clientReferenceManifest: ClientReferenceManifest = {
resolveClientReference(reference: string) {
const [id, name] = reference.split("#");
let resolved: unknown;
return {
async preload() {
let mod: Record<string, unknown>;
if (import.meta.env.DEV) {
mod = await import(/* @vite-ignore */ id);
} else {
const references = await import(
"virtual:build-client-references" as string
);
mod = references.default[id]();
}
resolved = mod[name];
},
get() {
return resolved;
},
};
},
};

0 comments on commit d7079a3

Please sign in to comment.