Skip to content

Commit acf25ee

Browse files
committed
Added the page Visibility handler and stopRealtime
1 parent 5f138c6 commit acf25ee

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

packages/remote-config/src/client/realtime_handler.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { ERROR_FACTORY, ErrorCode } from '../errors';
2121
import { _FirebaseInstallationsInternal } from '@firebase/installations';
2222
import { Storage } from '../storage/storage';
2323
import { calculateBackoffMillis, FirebaseError } from '@firebase/util';
24+
import { VisibilityMonitor } from '../../../database/src/core/util/VisibilityMonitor';
2425

2526
export class RealtimeHandler {
2627
constructor(
@@ -31,7 +32,9 @@ export class RealtimeHandler {
3132
private readonly projectId: string,
3233
private readonly apiKey: string,
3334
private readonly appId: string
34-
) { }
35+
) {
36+
VisibilityMonitor.getInstance().on('visible', this.onVisibilityChange_, this);
37+
}
3538

3639
private streamController?: AbortController;
3740
private observers: Set<ConfigUpdateObserver> = new Set<ConfigUpdateObserver>();
@@ -110,6 +113,16 @@ export class RealtimeHandler {
110113
return false;
111114
}
112115

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+
113126
private resetRetryCount(): void {
114127
this.retriesRemaining = ORIGINAL_RETRIES;
115128
}
@@ -147,8 +160,10 @@ export class RealtimeHandler {
147160
this.retryHttpConnectionWhenBackoffEnds();
148161
return;
149162
}
163+
150164
let response;
151165
try {
166+
console.log("into try");
152167
const [installationId, installationTokenResult] = await Promise.all([
153168
this.firebaseInstallations.getId(),
154169
this.firebaseInstallations.getToken(false)
@@ -159,7 +174,6 @@ export class RealtimeHandler {
159174
'If-None-Match': '*',
160175
'authentication-token': installationTokenResult
161176
};
162-
163177
const url = this.getRealtimeUrl();
164178
const requestBody = {
165179
project: this.projectId,
@@ -176,6 +190,7 @@ export class RealtimeHandler {
176190
body: JSON.stringify(requestBody)
177191
});
178192
if (response.status === 200 && response.body) {
193+
console.log("got the response");
179194
this.resetRetryCount();
180195
this.resetRealtimeBackoff();
181196
//code related to start StartAutofetch
@@ -200,7 +215,8 @@ export class RealtimeHandler {
200215
}
201216

202217
if (connectionFailed || statusCode === 200) {
203-
this.retryHttpConnectionWhenBackoffEnds();
218+
console.log("again");
219+
// this.retryHttpConnectionWhenBackoffEnds();
204220
} else {
205221
//still have to implement this part
206222
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 {
257273
return new URL(urlString);
258274
}
259275

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+
}
260293
}

packages/remote-config/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"extends": "../../config/tsconfig.base.json",
33
"compilerOptions": {
44
"outDir": "dist",
5+
"strict": false,
56
"resolveJsonModule": true
67
},
78
"exclude": ["dist/**/*"]

0 commit comments

Comments
 (0)