From 4de2cee008ee74c131bf782ee5779a5fee736b41 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sun, 6 Oct 2024 13:31:05 +0900 Subject: [PATCH] refactor: BridgeServerDevEnvironment --- examples/child-process/vite.config.ts | 47 +++++++++++++++------------ 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/examples/child-process/vite.config.ts b/examples/child-process/vite.config.ts index 19cfa9ed..5f1d2a11 100644 --- a/examples/child-process/vite.config.ts +++ b/examples/child-process/vite.config.ts @@ -77,24 +77,11 @@ export default defineConfig((_env) => ({ }, })); -// TODO -// can we abstract away child process? FetchBridgeDevEnvironment? -// multiple children per env (like Vitest)? need different API? -export class ChildProcessFetchDevEnvironment extends DevEnvironment { +class BridgeServerDevEnvironment extends DevEnvironment { public bridge!: http.Server; public bridgeUrl!: string; - public child!: childProcess.ChildProcess; - public childUrl!: string; - public childUrlPromise!: PromiseWithResolvers; - - constructor( - public extraOptions: { command: string[] }, - ...args: ConstructorParameters - ) { - super(...args); - } - override init: DevEnvironment["init"] = async (...args) => { + override async init(...args: Parameters) { await super.init(...args); const listener = webToNodeHandler(async (request) => { @@ -130,8 +117,29 @@ export class ChildProcessFetchDevEnvironment extends DevEnvironment { reject(e); }); }); + } + + override async close(...args: Parameters) { + await super.close(...args); + this.bridge?.close(); + } +} + +export class ChildProcessFetchDevEnvironment extends BridgeServerDevEnvironment { + public child!: childProcess.ChildProcess; + public childUrl!: string; + public childUrlPromise!: PromiseWithResolvers; + + constructor( + public extraOptions: { command: string[] }, + ...args: ConstructorParameters + ) { + super(...args); + } + + override async init(...args: Parameters) { + await super.init(...args); - // TODO: separate child process concern? this.childUrlPromise = PromiseWithReoslvers(); const command = this.extraOptions.command; const child = childProcess.spawn( @@ -161,13 +169,12 @@ export class ChildProcessFetchDevEnvironment extends DevEnvironment { bridgeUrl: this.bridgeUrl, childUrl: this.childUrl, }); - }; + } - override close: DevEnvironment["close"] = async (...args) => { + override async close(...args: Parameters) { await super.close(...args); this.child?.kill(); - this.bridge?.close(); - }; + } async dispatchFetch(entry: string, request: Request): Promise { const headers = new Headers(request.headers);