Skip to content

Commit 2211693

Browse files
committed
Explicitly close any unclosed WebChannel instances on terminate
1 parent 799de59 commit 2211693

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

packages/firestore/src/platform/browser/webchannel_connection.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ export class WebChannelConnection extends RestConnection {
5959
private readonly useFetchStreams: boolean;
6060
private readonly longPollingOptions: ExperimentalLongPollingOptions;
6161

62+
/** A collection of open WebChannel instances */
63+
private webChannels: WebChannel[] = [];
64+
6265
constructor(info: DatabaseInfo) {
6366
super(info);
6467
this.forceLongPolling = info.forceLongPolling;
@@ -239,6 +242,7 @@ export class WebChannelConnection extends RestConnection {
239242
request
240243
);
241244
const channel = webchannelTransport.createWebChannel(url, request);
245+
this.trackWebChannel(channel);
242246

243247
// WebChannel supports sending the first message with the handshake - saving
244248
// a network round trip. However, it will have to call send in the same
@@ -321,6 +325,7 @@ export class WebChannelConnection extends RestConnection {
321325
`RPC '${rpcName}' stream ${streamId} transport closed`
322326
);
323327
streamBridge.callOnClose();
328+
this.untrackWebChannel(channel);
324329
}
325330
});
326331

@@ -427,4 +432,22 @@ export class WebChannelConnection extends RestConnection {
427432
}, 0);
428433
return streamBridge;
429434
}
435+
436+
/**
437+
* Closes and cleans up any resources associated with the connection.
438+
*/
439+
terminate(): void {
440+
this.webChannels.forEach(webChannel => webChannel.close());
441+
this.webChannels = [];
442+
}
443+
444+
trackWebChannel(webChannel: WebChannel): void {
445+
this.webChannels.push(webChannel);
446+
}
447+
448+
untrackWebChannel(webChannel: WebChannel): void {
449+
this.webChannels = this.webChannels.filter(
450+
instance => instance === webChannel
451+
);
452+
}
430453
}

0 commit comments

Comments
 (0)