MCPcopy
hub / github.com/vitejs/vite / HMRContext

Class HMRContext

packages/vite/src/shared/hmr.ts:24–168  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

22}
23
24export class HMRContext implements ViteHotContext {
25 private newListeners: CustomListenersMap
26
27 constructor(
28 private hmrClient: HMRClient,
29 private ownerPath: string,
30 ) {
31 if (!hmrClient.dataMap.has(ownerPath)) {
32 hmrClient.dataMap.set(ownerPath, {})
33 }
34
35 // when a file is hot updated, a new context is created
36 // clear its stale callbacks
37 const mod = hmrClient.hotModulesMap.get(ownerPath)
38 if (mod) {
39 mod.callbacks = []
40 }
41
42 // clear stale custom event listeners
43 const staleListeners = hmrClient.ctxToListenersMap.get(ownerPath)
44 if (staleListeners) {
45 for (const [event, staleFns] of staleListeners) {
46 const listeners = hmrClient.customListenersMap.get(event)
47 if (listeners) {
48 hmrClient.customListenersMap.set(
49 event,
50 listeners.filter((l) => !staleFns.includes(l)),
51 )
52 }
53 }
54 }
55
56 this.newListeners = new Map()
57 hmrClient.ctxToListenersMap.set(ownerPath, this.newListeners)
58 }
59
60 get data(): any {
61 return this.hmrClient.dataMap.get(this.ownerPath)
62 }
63
64 accept(deps?: any, callback?: any): void {
65 if (typeof deps === 'function' || !deps) {
66 // self-accept: hot.accept(() => {})
67 this.acceptDeps([this.ownerPath], ([mod]) => deps?.(mod))
68 } else if (typeof deps === 'string') {
69 // explicit deps
70 this.acceptDeps([deps], ([mod]) => callback?.(mod))
71 } else if (Array.isArray(deps)) {
72 this.acceptDeps(deps, callback)
73 } else {
74 throw new Error(`invalid hot.accept() usage.`)
75 }
76 }
77
78 // export names (first arg) are irrelevant on the client side, they're
79 // extracted in the server for propagation
80 acceptExports(
81 _: string | readonly string[],

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected