Skip to content

Commit

Permalink
chore: use object style send
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Mar 26, 2022
1 parent c26627f commit 661f7d2
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
5 changes: 4 additions & 1 deletion docs/guide/api-plugin.md
Expand Up @@ -537,7 +537,10 @@ To send events from the client to the server, we can use [`hot.send`](/guide/api
```ts
// client side
if (import.meta.hot) {
import.meta.hot.send('from-client', { msg: 'Hey!' })
import.meta.hot.send({
event: 'from-client',
data: { msg: 'Hey!' }
})
}
```
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/hmr/hmr.js
Expand Up @@ -59,7 +59,7 @@ if (import.meta.hot) {
})

// send custom event to server to calculate 1 + 2
import.meta.hot.send('remote-add', { a: 1, b: 2 })
import.meta.hot.send({ event: 'remote-add', data: { a: 1, b: 2 } })
import.meta.hot.on('remote-add-result', ({ result }) => {
text('.custom-communication', result)
})
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/client/client.ts
Expand Up @@ -6,7 +6,7 @@ import type {
Update,
UpdatePayload
} from 'types/hmrPayload'
import type { CustomEventName } from 'types/customEvent'
import type { CustomEventName, CustomEventPayload } from 'types/customEvent'
import { ErrorOverlay, overlayId } from './overlay'
// eslint-disable-next-line node/no-missing-import
import '@vite/env'
Expand Down Expand Up @@ -489,8 +489,8 @@ export const createHotContext = (ownerPath: string) => {
addToMap(newListeners)
},

send: (event: string, data?: unknown) => {
messageBuffer.push(JSON.stringify({ type: 'custom', event, data }))
send: (payload: CustomEventPayload) => {
messageBuffer.push(JSON.stringify({ type: 'custom', ...payload }))
sendMessageBuffer()
}
}
Expand Down
6 changes: 6 additions & 0 deletions packages/vite/types/customEvent.d.ts
Expand Up @@ -3,3 +3,9 @@ export type CustomEventName<T extends string> = (T extends `vite:${T}`
? never
: T) &
(`vite:${T}` extends T ? never : T)

export interface CustomEventPayload {
type?: 'custom'
event: string
data?: any
}
2 changes: 1 addition & 1 deletion packages/vite/types/importMeta.d.ts
Expand Up @@ -60,7 +60,7 @@ interface ImportMeta {
): void
}

send(event: string, data?: unknown): void
send(payload: import('./customEvent').CustomEventPayload): void
}

readonly env: ImportMetaEnv
Expand Down

0 comments on commit 661f7d2

Please sign in to comment.