@@ -59,6 +59,9 @@ export class WebChannelConnection extends RestConnection {
59
59
private readonly useFetchStreams : boolean ;
60
60
private readonly longPollingOptions : ExperimentalLongPollingOptions ;
61
61
62
+ /** A collection of open WebChannel instances */
63
+ private openWebChannels : WebChannel [ ] = [ ] ;
64
+
62
65
constructor ( info : DatabaseInfo ) {
63
66
super ( info ) ;
64
67
this . forceLongPolling = info . forceLongPolling ;
@@ -239,6 +242,7 @@ export class WebChannelConnection extends RestConnection {
239
242
request
240
243
) ;
241
244
const channel = webchannelTransport . createWebChannel ( url , request ) ;
245
+ this . addOpenWebChannel ( channel ) ;
242
246
243
247
// WebChannel supports sending the first message with the handshake - saving
244
248
// a network round trip. However, it will have to call send in the same
@@ -321,6 +325,7 @@ export class WebChannelConnection extends RestConnection {
321
325
`RPC '${ rpcName } ' stream ${ streamId } transport closed`
322
326
) ;
323
327
streamBridge . callOnClose ( ) ;
328
+ this . removeOpenWebChannel ( channel ) ;
324
329
}
325
330
} ) ;
326
331
@@ -427,4 +432,32 @@ export class WebChannelConnection extends RestConnection {
427
432
} , 0 ) ;
428
433
return streamBridge ;
429
434
}
435
+
436
+ /**
437
+ * Closes and cleans up any resources associated with the connection.
438
+ */
439
+ terminate ( ) : void {
440
+ // If the Firestore instance is terminated, we will explicitly
441
+ // close any remaining open WebChannel instances.
442
+ this . openWebChannels . forEach ( webChannel => webChannel . close ( ) ) ;
443
+ this . openWebChannels = [ ] ;
444
+ }
445
+
446
+ /**
447
+ * Add a WebChannel instance to the collection of open instances.
448
+ * @param webChannel
449
+ */
450
+ addOpenWebChannel ( webChannel : WebChannel ) : void {
451
+ this . openWebChannels . push ( webChannel ) ;
452
+ }
453
+
454
+ /**
455
+ * Remove a WebChannel instance from the collection of open instances.
456
+ * @param webChannel
457
+ */
458
+ removeOpenWebChannel ( webChannel : WebChannel ) : void {
459
+ this . openWebChannels = this . openWebChannels . filter (
460
+ instance => instance === webChannel
461
+ ) ;
462
+ }
430
463
}
0 commit comments