Skip to content

Commit

Permalink
refactor: make internal invoke event to use the same interface with `…
Browse files Browse the repository at this point in the history
…handleInvoke` (#18902)
  • Loading branch information
sapphi-red authored Dec 16, 2024
1 parent 30f256e commit 27f691b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
14 changes: 4 additions & 10 deletions packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export const normalizeHotChannel = (
) => {
if (!invokeHandlers) {
return {
e: {
error: {
name: 'TransportError',
message: 'invokeHandlers is not set',
stack: new Error().stack,
Expand All @@ -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,
Expand Down Expand Up @@ -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') {
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/shared/invokeMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ export type InvokeResponseData<
/** 'response' is for responses without an id */
id: 'response' | `response:${string}`
data:
| { r: Awaited<ReturnType<InvokeMethods[T]>>; e?: undefined }
| { r?: undefined; e: any }
| { result: Awaited<ReturnType<InvokeMethods[T]>>; error?: undefined }
| { result?: undefined; error: any }
}

export type InvokeMethods = {
Expand Down
8 changes: 4 additions & 4 deletions packages/vite/src/shared/moduleRunnerTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 27f691b

Please sign in to comment.