@@ -70,10 +70,11 @@ export class RealtimeHandler {
7070 private isConnectionActive : boolean = false ;
7171 private isRealtimeDisabled : boolean = false ;
7272 private controller ?: AbortController ;
73- private reader : ReadableStreamDefaultReader | undefined ;
73+ private reader : ReadableStreamDefaultReader < Uint8Array > | undefined ;
7474 private httpRetriesRemaining : number = ORIGINAL_RETRIES ;
7575 private isInBackground : boolean = false ;
7676 private readonly decoder = new TextDecoder ( 'utf-8' ) ;
77+ private isClosingConnection : boolean = false ;
7778
7879 private async setRetriesRemaining ( ) : Promise < void > {
7980 // Retrieve number of remaining retries from last session. The minimum retry count being one.
@@ -142,16 +143,22 @@ export class RealtimeHandler {
142143 * and canceling the stream reader if they exist.
143144 */
144145 private closeRealtimeHttpConnection ( ) : void {
145- // Aborting only when the controller is not null and tab is in foreground.
146- if ( this . controller && ! this . isInBackground ) {
147- this . controller . abort ( ) ;
148- this . controller = undefined ;
146+ if ( this . isClosingConnection ) {
147+ return ;
149148 }
150-
149+ this . isClosingConnection = true ;
150+
151151 if ( this . reader ) {
152152 void this . reader . cancel ( ) ;
153153 this . reader = undefined ;
154154 }
155+
156+ if ( this . controller ) {
157+ this . controller . abort ( ) ;
158+ this . controller = undefined ;
159+ }
160+
161+ this . isClosingConnection = false ;
155162 }
156163
157164 private async resetRealtimeBackoff ( ) : Promise < void > {
@@ -493,7 +500,7 @@ export class RealtimeHandler {
493500 ) ;
494501 }
495502 } catch ( e : unknown ) {
496- this . logger . error ( 'Unable to parse latest config update message.' , e ) ;
503+ this . logger . debug ( 'Unable to parse latest config update message.' , e ) ;
497504 const errorMessage = e instanceof Error ? e . message : String ( e ) ;
498505 this . propagateError (
499506 ERROR_FACTORY . create ( ErrorCode . CONFIG_UPDATE_MESSAGE_INVALID , {
@@ -565,6 +572,7 @@ export class RealtimeHandler {
565572 this . resetRetryCount ( ) ;
566573 await this . resetRealtimeBackoff ( ) ;
567574 const reader = response . body . getReader ( ) ;
575+ this . reader = reader ;
568576 // Start listening for realtime notifications.
569577 await this . listenForNotifications ( reader ) ;
570578 }
@@ -583,11 +591,11 @@ export class RealtimeHandler {
583591 }
584592 } finally {
585593 // Close HTTP connection and associated streams.
586- this . closeRealtimeHttpConnection ( ) ;
594+ await this . closeRealtimeHttpConnection ( ) ;
587595 this . setIsHttpConnectionRunning ( false ) ;
588596
589597 // Update backoff metadata if the connection failed in the foreground.
590- const connectionFailed =
598+ const connectionFailed = ! this . isInBackground &&
591599 responseCode == null || this . isStatusCodeRetryable ( responseCode ) ;
592600
593601 if ( connectionFailed ) {
@@ -673,9 +681,8 @@ export class RealtimeHandler {
673681
674682 private async onVisibilityChange ( visible : unknown ) : Promise < void > {
675683 this . isInBackground = ! visible ;
676- if ( ! visible && this . controller ) {
677- this . controller . abort ( ) ;
678- this . controller = undefined ;
684+ if ( ! visible ) {
685+ await this . closeRealtimeHttpConnection ( ) ;
679686 } else if ( visible ) {
680687 await this . beginRealtime ( ) ;
681688 }
0 commit comments