Skip to content

Commit c96e607

Browse files
committed
simplify ws close handling
1 parent 43fd8c1 commit c96e607

1 file changed

Lines changed: 8 additions & 9 deletions

File tree

apps/playwright-browser-tunnel/src/PlaywrightBrowserTunnel.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// See LICENSE in the project root for license information.
33

44
import type { ChildProcess } from 'node:child_process';
5+
import { once } from 'node:events';
56

67
import type { BrowserServer, BrowserType, LaunchOptions } from 'playwright-core';
78
import { type RawData, WebSocket, type WebSocketServer } from 'ws';
@@ -122,15 +123,13 @@ export class PlaywrightTunnel {
122123

123124
public async waitForCloseAsync(): Promise<void> {
124125
const terminal: ITerminal = this._terminal;
125-
await new Promise<void>((resolve) => {
126-
void this._initWsPromise?.then((ws) => {
127-
ws.on('close', () => {
128-
terminal.writeLine('WebSocket connection closed. resolving init promise.');
129-
this._initWsPromise = undefined;
130-
resolve();
131-
});
132-
});
133-
});
126+
const initWsPromise: Promise<WebSocket> | undefined = this._initWsPromise;
127+
if (initWsPromise) {
128+
const ws: WebSocket = await initWsPromise;
129+
await once(ws, 'close');
130+
terminal.writeLine('WebSocket connection closed. resolving init promise.');
131+
this._initWsPromise = undefined;
132+
}
134133
}
135134

136135
public async startAsync(options: { keepRunning?: boolean } = {}): Promise<void> {

0 commit comments

Comments
 (0)