Skip to content

Commit 3fb4a7e

Browse files
committed
Tracking pollInterval now in the class to clear it on stop.
1 parent f9cb069 commit 3fb4a7e

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,16 @@ export class PlaywrightTunnel {
8888
private readonly _onStatusChange: (status: TunnelStatus) => void;
8989
private readonly _onBeforeLaunch?: (handshake: IHandshake) => Promise<boolean> | boolean;
9090
private readonly _playwrightBrowsersInstalled: Set<string> = new Set();
91-
private readonly _wsEndpoint?: string;
92-
private readonly _listenPort?: number;
91+
private readonly _wsEndpoint: string | undefined;
92+
private readonly _listenPort: number | undefined;
9393
private readonly _playwrightInstallPath: string;
9494
private _status: TunnelStatus = 'stopped';
9595
private _initWsPromise?: Promise<WebSocket>;
9696
private _keepRunning: boolean = false;
9797
private _ws?: WebSocket;
9898
private _mode: TunnelMode;
9999
private _pendingConnectionAttempt?: Promise<WebSocket>;
100+
private _pollInterval?: NodeJS.Timeout;
100101

101102
public constructor(options: IPlaywrightTunnelOptions) {
102103
const { mode, terminal, onStatusChange, playwrightInstallPath, onBeforeLaunch } = options;
@@ -143,7 +144,7 @@ export class PlaywrightTunnel {
143144
if (initWsPromise) {
144145
const ws: WebSocket = await initWsPromise;
145146
await once(ws, 'close');
146-
terminal.writeLine('WebSocket connection closed. resolving init promise.');
147+
terminal.writeDebugLine('WebSocket connection closed. resolving init promise.');
147148
this._initWsPromise = undefined;
148149
}
149150
}
@@ -164,6 +165,10 @@ export class PlaywrightTunnel {
164165

165166
public async stopAsync(): Promise<void> {
166167
this._keepRunning = false;
168+
if (this._pollInterval) {
169+
clearInterval(this._pollInterval);
170+
this._pollInterval = undefined;
171+
}
167172
await this._initWsPromise?.finally(() => {
168173
this._ws?.close();
169174
});
@@ -244,7 +249,7 @@ export class PlaywrightTunnel {
244249
private async _tryConnectAsync(): Promise<WebSocket> {
245250
const wsEndpoint: string | undefined = this._wsEndpoint;
246251
if (!wsEndpoint) {
247-
return Promise.reject(new Error('WebSocket endpoint is not defined'));
252+
throw new Error('WebSocket endpoint is not defined');
248253
}
249254
return await new Promise<WebSocket>((resolve, reject) => {
250255
const ws: WebSocket = new WebSocket(wsEndpoint);
@@ -262,16 +267,17 @@ export class PlaywrightTunnel {
262267
// Need to support multiple simultaneous connections for parallel tests.
263268
private async _pollConnectionAsync(): Promise<WebSocket> {
264269
this._terminal.writeLine(`Waiting for WebSocket connection`);
265-
return new Promise((resolve, reject) => {
266-
const interval: NodeJS.Timeout = setInterval(() => {
270+
return await new Promise((resolve, reject) => {
271+
this._pollInterval = setInterval(() => {
267272
if (this._pendingConnectionAttempt) {
268273
return; // Skip if a connection attempt is already in progress
269274
}
270275
const connectionPromise: Promise<WebSocket> = this._tryConnectAsync();
271276
this._pendingConnectionAttempt = connectionPromise;
272277
connectionPromise
273278
.then((ws: WebSocket) => {
274-
clearInterval(interval);
279+
clearInterval(this._pollInterval);
280+
this._pollInterval = undefined;
275281
ws.removeAllListeners();
276282
this._pendingConnectionAttempt = undefined;
277283
resolve(ws);

0 commit comments

Comments
 (0)