@@ -21,6 +21,7 @@ import { ERROR_FACTORY, ErrorCode } from '../errors';
21
21
import { _FirebaseInstallationsInternal } from '@firebase/installations' ;
22
22
import { Storage } from '../storage/storage' ;
23
23
import { calculateBackoffMillis , FirebaseError } from '@firebase/util' ;
24
+ import { VisibilityMonitor } from '../../../database/src/core/util/VisibilityMonitor' ;
24
25
25
26
export class RealtimeHandler {
26
27
constructor (
@@ -31,7 +32,9 @@ export class RealtimeHandler {
31
32
private readonly projectId : string ,
32
33
private readonly apiKey : string ,
33
34
private readonly appId : string
34
- ) { }
35
+ ) {
36
+ VisibilityMonitor . getInstance ( ) . on ( 'visible' , this . onVisibilityChange_ , this ) ;
37
+ }
35
38
36
39
private streamController ?: AbortController ;
37
40
private observers : Set < ConfigUpdateObserver > = new Set < ConfigUpdateObserver > ( ) ;
@@ -110,6 +113,16 @@ export class RealtimeHandler {
110
113
return false ;
111
114
}
112
115
116
+ private stopRealtime ( ) : void {
117
+ console . log ( "page Visibility changes: connection aborted" ) ;
118
+ if ( this . scheduledConnectionTimeoutId ) {
119
+ clearTimeout ( this . scheduledConnectionTimeoutId ) ;
120
+ this . scheduledConnectionTimeoutId = undefined ;
121
+ }
122
+ this . streamController ?. abort ( ) ;
123
+ this . isConnectionActive = false ;
124
+ }
125
+
113
126
private resetRetryCount ( ) : void {
114
127
this . retriesRemaining = ORIGINAL_RETRIES ;
115
128
}
@@ -147,8 +160,10 @@ export class RealtimeHandler {
147
160
this . retryHttpConnectionWhenBackoffEnds ( ) ;
148
161
return ;
149
162
}
163
+
150
164
let response ;
151
165
try {
166
+ console . log ( "into try" ) ;
152
167
const [ installationId , installationTokenResult ] = await Promise . all ( [
153
168
this . firebaseInstallations . getId ( ) ,
154
169
this . firebaseInstallations . getToken ( false )
@@ -159,7 +174,6 @@ export class RealtimeHandler {
159
174
'If-None-Match' : '*' ,
160
175
'authentication-token' : installationTokenResult
161
176
} ;
162
-
163
177
const url = this . getRealtimeUrl ( ) ;
164
178
const requestBody = {
165
179
project : this . projectId ,
@@ -176,6 +190,7 @@ export class RealtimeHandler {
176
190
body : JSON . stringify ( requestBody )
177
191
} ) ;
178
192
if ( response . status === 200 && response . body ) {
193
+ console . log ( "got the response" ) ;
179
194
this . resetRetryCount ( ) ;
180
195
this . resetRealtimeBackoff ( ) ;
181
196
//code related to start StartAutofetch
@@ -200,7 +215,8 @@ export class RealtimeHandler {
200
215
}
201
216
202
217
if ( connectionFailed || statusCode === 200 ) {
203
- this . retryHttpConnectionWhenBackoffEnds ( ) ;
218
+ console . log ( "again" ) ;
219
+ // this.retryHttpConnectionWhenBackoffEnds();
204
220
} else {
205
221
//still have to implement this part
206
222
let errorMessage = `Unable to connect to the server. Try again in a few minutes. HTTP status code: ${ statusCode } ` ;
@@ -257,4 +273,21 @@ export class RealtimeHandler {
257
273
return new URL ( urlString ) ;
258
274
}
259
275
276
+ private onVisibilityChange_ ( visible : unknown ) {
277
+ console . log ( "inside2" ) ;
278
+ const wasInBackground = this . isInBackground ;
279
+ this . isInBackground = ! visible ;
280
+
281
+ if ( wasInBackground !== this . isInBackground ) {
282
+ if ( this . isInBackground ) {
283
+ // The page just became hidden.
284
+ console . log ( "stopoping because of backgrouhnd" ) ;
285
+ this . stopRealtime ( ) ;
286
+ } else {
287
+ console . log ( "starting because of foreground" ) ;
288
+ // The page just became visible.
289
+ this . beginRealtime ( ) ;
290
+ }
291
+ }
292
+ }
260
293
}
0 commit comments