Skip to content

Commit

Permalink
chore: use transport.setInvokeHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Oct 26, 2024
1 parent 9d4c2ad commit a86f681
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 53 deletions.
30 changes: 18 additions & 12 deletions examples/child-process/src/lib/vite/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { webToNodeHandler } from "@hiogawa/utils-node";
import {
DevEnvironment,
type DevEnvironmentOptions,
type HotChannelInvokeHandler,
type HotPayload,
} from "vite";
import type { BridgeClientOptions } from "./types";
Expand All @@ -20,6 +21,7 @@ export class ChildProcessFetchDevEnvironment extends DevEnvironment {
public bridgeUrl!: string;
public child!: childProcess.ChildProcess;
public childUrl!: string;
public invokeHandler!: HotChannelInvokeHandler;

static createFactory(options: {
runtime: "node" | "bun";
Expand All @@ -32,18 +34,27 @@ export class ChildProcessFetchDevEnvironment extends DevEnvironment {
options.conditions ? ["--conditions", ...options.conditions] : [],
join(import.meta.dirname, `./runtime/${options.runtime}.js`),
].flat();
return new ChildProcessFetchDevEnvironment({ command }, name, config, {
// TODO
hot: false,
});
return new ChildProcessFetchDevEnvironment({ command }, name, config);
};
}

constructor(
public extraOptions: { command: string[] },
...args: ConstructorParameters<typeof DevEnvironment>
arg0: ConstructorParameters<typeof DevEnvironment>[0],
arg1: ConstructorParameters<typeof DevEnvironment>[1],
) {
super(...args);
let invokeHandler!: HotChannelInvokeHandler;
super(arg0, arg1, {
hot: false,
transport: {
setInvokeHandler: (invokeHandler_) => {
if (invokeHandler_) {
invokeHandler = invokeHandler_;
}
},
},
});
this.invokeHandler = invokeHandler;
}

override init: DevEnvironment["init"] = async (...args) => {
Expand All @@ -52,8 +63,6 @@ export class ChildProcessFetchDevEnvironment extends DevEnvironment {
// protect bridge rpc
const key = Math.random().toString(36).slice(2);

const invokeHandlers = this.getInvokeHandlers();

const listener = webToNodeHandler(async (request) => {
const url = new URL(request.url);
// TODO: other than json?
Expand All @@ -65,10 +74,7 @@ export class ChildProcessFetchDevEnvironment extends DevEnvironment {
if (reqKey !== key) {
return Response.json({ message: "invalid key" }, { status: 400 });
}
assert(payload.type === "custom");
const handler = invokeHandlers[payload.event];
assert(handler);
const result = await handler(payload.data);
const result = await this.invokeHandler(payload);
return Response.json(result);
}
return undefined;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"tsup": "^8.1.2",
"tsx": "^4.16.2",
"typescript": "^5.5.3",
"vite": "file:./vite-6.0.0-beta.3.tgz",
"vite": "file:./vite-6.0.0-beta.5.tgz",
"vitest": "^2.0.3",
"wrangler": "^3.79.0"
},
Expand Down
80 changes: 40 additions & 40 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file removed vite-6.0.0-beta.3.tgz
Binary file not shown.
Binary file added vite-6.0.0-beta.5.tgz
Binary file not shown.

0 comments on commit a86f681

Please sign in to comment.