Skip to content

Commit

Permalink
refactor(types): share hot context type (#7475)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Mar 26, 2022
1 parent dd33d9c commit 64ddff0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 50 deletions.
16 changes: 8 additions & 8 deletions packages/vite/src/client/client.ts
Expand Up @@ -6,6 +6,7 @@ import type {
Update,
UpdatePayload
} from 'types/hmrPayload'
import type { ViteHotContext } from 'types/hot'
import type { CustomEventName } from 'types/customEvent'
import { ErrorOverlay, overlayId } from './overlay'
// eslint-disable-next-line node/no-missing-import
Expand Down Expand Up @@ -391,9 +392,7 @@ const ctxToListenersMap = new Map<
Map<string, ((data: any) => void)[]>
>()

// Just infer the return type for now
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const createHotContext = (ownerPath: string) => {
export function createHotContext(ownerPath: string): ViteHotContext {
if (!dataMap.has(ownerPath)) {
dataMap.set(ownerPath, {})
}
Expand Down Expand Up @@ -434,12 +433,12 @@ export const createHotContext = (ownerPath: string) => {
hotModulesMap.set(ownerPath, mod)
}

const hot = {
const hot: ViteHotContext = {
get data() {
return dataMap.get(ownerPath)
},

accept(deps: any, callback?: any) {
accept(deps?: any, callback?: any) {
if (typeof deps === 'function' || !deps) {
// self-accept: hot.accept(() => {})
acceptDeps([ownerPath], ([mod]) => deps && deps(mod))
Expand All @@ -460,10 +459,11 @@ export const createHotContext = (ownerPath: string) => {
)
},

dispose(cb: (data: any) => void) {
dispose(cb) {
disposeMap.set(ownerPath, cb)
},

// @ts-expect-error untyped
prune(cb: (data: any) => void) {
pruneMap.set(ownerPath, cb)
},
Expand All @@ -479,7 +479,7 @@ export const createHotContext = (ownerPath: string) => {
},

// custom events
on: (event: string, cb: (data: any) => void) => {
on(event, cb) {
const addToMap = (map: Map<string, any[]>) => {
const existing = map.get(event) || []
existing.push(cb)
Expand All @@ -489,7 +489,7 @@ export const createHotContext = (ownerPath: string) => {
addToMap(newListeners)
},

send: (event: string, data?: any) => {
send(event, data) {
messageBuffer.push(JSON.stringify({ type: 'custom', event, data }))
sendMessageBuffer()
}
Expand Down
43 changes: 43 additions & 0 deletions packages/vite/types/hot.d.ts
@@ -0,0 +1,43 @@
import type {
ErrorPayload,
FullReloadPayload,
PrunePayload,
UpdatePayload
} from './hmrPayload'

export interface ViteHotContext {
readonly data: any

accept(): void
accept(cb: (mod: any) => void): void
accept(dep: string, cb: (mod: any) => void): void
accept(deps: readonly string[], cb: (mods: any[]) => void): void

/**
* @deprecated
*/
acceptDeps(): never

dispose(cb: (data: any) => void): void
decline(): void
invalidate(): void

on: {
(event: 'vite:beforeUpdate', cb: (payload: UpdatePayload) => void): void
(event: 'vite:beforePrune', cb: (payload: PrunePayload) => void): void
(
event: 'vite:beforeFullReload',
cb: (payload: FullReloadPayload) => void
): void
(event: 'vite:error', cb: (payload: ErrorPayload) => void): void
(event: string, cb: (data: any) => void): void
}

send(event: string, data?: any): void
}

// See https://stackoverflow.com/a/63549561.
export type CustomEventName<T extends string> = (T extends `vite:${T}`
? never
: T) &
(`vite:${T}` extends T ? never : T)
43 changes: 1 addition & 42 deletions packages/vite/types/importMeta.d.ts
Expand Up @@ -20,48 +20,7 @@ interface GlobOptions {
interface ImportMeta {
url: string

readonly hot?: {
readonly data: any

accept(): void
accept(cb: (mod: any) => void): void
accept(dep: string, cb: (mod: any) => void): void
accept(deps: readonly string[], cb: (mods: any[]) => void): void

/**
* @deprecated
*/
acceptDeps(): never

dispose(cb: (data: any) => void): void
decline(): void
invalidate(): void

on: {
(
event: 'vite:beforeUpdate',
cb: (payload: import('./hmrPayload').UpdatePayload) => void
): void
(
event: 'vite:beforePrune',
cb: (payload: import('./hmrPayload').PrunePayload) => void
): void
(
event: 'vite:beforeFullReload',
cb: (payload: import('./hmrPayload').FullReloadPayload) => void
): void
(
event: 'vite:error',
cb: (payload: import('./hmrPayload').ErrorPayload) => void
): void
<T extends string>(
event: import('./customEvent').CustomEventName<T>,
cb: (data: any) => void
): void
}

send(event: string, data?: any): void
}
readonly hot?: import('./hot').ViteHotContext

readonly env: ImportMetaEnv

Expand Down

0 comments on commit 64ddff0

Please sign in to comment.