@@ -70,10 +70,11 @@ export class RealtimeHandler {
70
70
private isConnectionActive : boolean = false ;
71
71
private isRealtimeDisabled : boolean = false ;
72
72
private controller ?: AbortController ;
73
- private reader : ReadableStreamDefaultReader | undefined ;
73
+ private reader : ReadableStreamDefaultReader < Uint8Array > | undefined ;
74
74
private httpRetriesRemaining : number = ORIGINAL_RETRIES ;
75
75
private isInBackground : boolean = false ;
76
76
private readonly decoder = new TextDecoder ( 'utf-8' ) ;
77
+ private isClosingConnection : boolean = false ;
77
78
78
79
private async setRetriesRemaining ( ) : Promise < void > {
79
80
// Retrieve number of remaining retries from last session. The minimum retry count being one.
@@ -142,16 +143,22 @@ export class RealtimeHandler {
142
143
* and canceling the stream reader if they exist.
143
144
*/
144
145
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 ;
149
148
}
150
-
149
+ this . isClosingConnection = true ;
150
+
151
151
if ( this . reader ) {
152
152
void this . reader . cancel ( ) ;
153
153
this . reader = undefined ;
154
154
}
155
+
156
+ if ( this . controller ) {
157
+ this . controller . abort ( ) ;
158
+ this . controller = undefined ;
159
+ }
160
+
161
+ this . isClosingConnection = false ;
155
162
}
156
163
157
164
private async resetRealtimeBackoff ( ) : Promise < void > {
@@ -493,7 +500,7 @@ export class RealtimeHandler {
493
500
) ;
494
501
}
495
502
} 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 ) ;
497
504
const errorMessage = e instanceof Error ? e . message : String ( e ) ;
498
505
this . propagateError (
499
506
ERROR_FACTORY . create ( ErrorCode . CONFIG_UPDATE_MESSAGE_INVALID , {
@@ -565,6 +572,7 @@ export class RealtimeHandler {
565
572
this . resetRetryCount ( ) ;
566
573
await this . resetRealtimeBackoff ( ) ;
567
574
const reader = response . body . getReader ( ) ;
575
+ this . reader = reader ;
568
576
// Start listening for realtime notifications.
569
577
await this . listenForNotifications ( reader ) ;
570
578
}
@@ -583,11 +591,11 @@ export class RealtimeHandler {
583
591
}
584
592
} finally {
585
593
// Close HTTP connection and associated streams.
586
- this . closeRealtimeHttpConnection ( ) ;
594
+ await this . closeRealtimeHttpConnection ( ) ;
587
595
this . setIsHttpConnectionRunning ( false ) ;
588
596
589
597
// Update backoff metadata if the connection failed in the foreground.
590
- const connectionFailed =
598
+ const connectionFailed = ! this . isInBackground &&
591
599
responseCode == null || this . isStatusCodeRetryable ( responseCode ) ;
592
600
593
601
if ( connectionFailed ) {
@@ -673,9 +681,8 @@ export class RealtimeHandler {
673
681
674
682
private async onVisibilityChange ( visible : unknown ) : Promise < void > {
675
683
this . isInBackground = ! visible ;
676
- if ( ! visible && this . controller ) {
677
- this . controller . abort ( ) ;
678
- this . controller = undefined ;
684
+ if ( ! visible ) {
685
+ await this . closeRealtimeHttpConnection ( ) ;
679
686
} else if ( visible ) {
680
687
await this . beginRealtime ( ) ;
681
688
}
0 commit comments