Skip to content

Commit

Permalink
chore: add a separate class for messages
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jan 8, 2024
1 parent 1d84871 commit 7eb78a9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/vite/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ async function handleMessage(payload: HMRPayload) {
switch (payload.type) {
case 'connected':
console.debug(`[vite] connected.`)
hmrClient.flushMessageQueue()
hmrClient.messenger.flush()
// proxy(nginx, docker) hmr ws maybe caused timeout,
// so send ping package let ws keep alive.
setInterval(() => {
Expand Down
44 changes: 27 additions & 17 deletions packages/vite/src/shared/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface HotCallback {
fn: (modules: Array<ModuleNamespace | undefined>) => void
}

interface HMRConnection {
export interface HMRConnection {
/**
* Checked before sending messages to the client.
*/
Expand Down Expand Up @@ -146,7 +146,9 @@ export class HMRContext implements ViteHotContext {
}

send<T extends string>(event: T, data?: InferCustomEventPayload<T>): void {
this.hmrClient.sendMessage(JSON.stringify({ type: 'custom', event, data }))
this.hmrClient.messenger.send(
JSON.stringify({ type: 'custom', event, data }),
)
}

private acceptDeps(
Expand All @@ -165,6 +167,24 @@ export class HMRContext implements ViteHotContext {
}
}

class HMRMessenger {
constructor(private connection: HMRConnection) {}

private queue: string[] = []

public send(message: string): void {
this.queue.push(message)
this.flush()
}

public flush(): void {
if (this.connection.isReady()) {
this.queue.forEach((msg) => this.connection.send(msg))
this.queue = []
}
}
}

export class HMRClient {
public hotModulesMap = new Map<string, HotModule>()
public disposeMap = new Map<string, (data: any) => void | Promise<void>>()
Expand All @@ -173,14 +193,16 @@ export class HMRClient {
public customListenersMap: CustomListenersMap = new Map()
public ctxToListenersMap = new Map<string, CustomListenersMap>()

private messageQueue: string[] = []
public messenger: HMRMessenger

constructor(
public logger: Console,
private connection: HMRConnection,
connection: HMRConnection,
// This allows implementing reloading via different methods depending on the environment
private importUpdatedModule: (update: Update) => Promise<ModuleNamespace>,
) {}
) {
this.messenger = new HMRMessenger(connection)
}

public async notifyListeners<T extends string>(
event: T,
Expand All @@ -206,18 +228,6 @@ export class HMRClient {
})
}

public sendMessage(message: string): void {
this.messageQueue.push(message)
this.flushMessageQueue()
}

public flushMessageQueue(): void {
if (this.connection.isReady()) {
this.messageQueue.forEach((msg) => this.connection.send(msg))
this.messageQueue = []
}
}

protected warnFailedUpdate(err: Error, path: string | string[]): void {
if (!err.message.includes('fetch')) {
this.logger.error(err)
Expand Down

0 comments on commit 7eb78a9

Please sign in to comment.