From 27f691b0c7dca2259108fe6b79583b459429bf7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Mon, 16 Dec 2024 20:42:09 +0900 Subject: [PATCH] refactor: make internal invoke event to use the same interface with `handleInvoke` (#18902) --- packages/vite/src/node/server/hmr.ts | 14 ++++---------- packages/vite/src/shared/invokeMethods.ts | 4 ++-- packages/vite/src/shared/moduleRunnerTransport.ts | 8 ++++---- 3 files changed, 10 insertions(+), 16 deletions(-) diff --git a/packages/vite/src/node/server/hmr.ts b/packages/vite/src/node/server/hmr.ts index b4993bd31c6b7e..8683f9a7b37c24 100644 --- a/packages/vite/src/node/server/hmr.ts +++ b/packages/vite/src/node/server/hmr.ts @@ -193,7 +193,7 @@ export const normalizeHotChannel = ( ) => { if (!invokeHandlers) { return { - e: { + error: { name: 'TransportError', message: 'invokeHandlers is not set', stack: new Error().stack, @@ -207,10 +207,10 @@ export const normalizeHotChannel = ( const invokeHandler = invokeHandlers[name] // @ts-expect-error `invokeHandler` is `InvokeMethods[T]`, so passing the args is fine const result = await invokeHandler(...args) - return { r: result } + return { result } } catch (error) { return { - e: { + error: { name: error.name, message: error.message, stack: error.stack, @@ -301,13 +301,7 @@ export const normalizeHotChannel = ( } channel.on?.('vite:invoke', listenerForInvokeHandler) }, - handleInvoke: async (payload) => { - const data = await handleInvoke(payload) - if (data.e) { - return { error: data.e } - } - return { result: data.r } - }, + handleInvoke, send: (...args: any[]) => { let payload: HotPayload if (typeof args[0] === 'string') { diff --git a/packages/vite/src/shared/invokeMethods.ts b/packages/vite/src/shared/invokeMethods.ts index b37adacc0cb3ef..7a6f3f02ef8e04 100644 --- a/packages/vite/src/shared/invokeMethods.ts +++ b/packages/vite/src/shared/invokeMethods.ts @@ -72,8 +72,8 @@ export type InvokeResponseData< /** 'response' is for responses without an id */ id: 'response' | `response:${string}` data: - | { r: Awaited>; e?: undefined } - | { r?: undefined; e: any } + | { result: Awaited>; error?: undefined } + | { result?: undefined; error: any } } export type InvokeMethods = { diff --git a/packages/vite/src/shared/moduleRunnerTransport.ts b/packages/vite/src/shared/moduleRunnerTransport.ts index 23b91c7a5bf50d..e70adb3067413a 100644 --- a/packages/vite/src/shared/moduleRunnerTransport.ts +++ b/packages/vite/src/shared/moduleRunnerTransport.ts @@ -96,11 +96,11 @@ const createInvokeableTransport = ( rpcPromises.delete(invokeId) - const { e, r } = data.data - if (e) { - promise.reject(e) + const { error, result } = data.data + if (error) { + promise.reject(error) } else { - promise.resolve(r) + promise.resolve(result) } return }