Skip to content

Commit a7d271b

Browse files
committed
Added the comments
1 parent f882ad4 commit a7d271b

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

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

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ export class RealtimeHandler {
109109
});
110110
}
111111

112+
/**
113+
* Increase the backoff duration with a new end time based on Retry Interval.
114+
*/
112115
private async updateBackoffMetadataWithRetryInterval(
113116
retryIntervalSeconds: number
114117
): Promise<void> {
@@ -139,8 +142,9 @@ export class RealtimeHandler {
139142
};
140143

141144
/**
142-
* Stops the real-time HTTP connection by aborting the in-progress fetch request
143-
* and canceling the stream reader if they exist.
145+
* Closes the realtime HTTP connection.
146+
* Note: This method is designed to be called only once at a time.
147+
* If a call is already in progress, subsequent calls will be ignored.
144148
*/
145149
private async closeRealtimeHttpConnection(): Promise<void> {
146150
if (this.isClosingConnection) {
@@ -262,6 +266,11 @@ export class RealtimeHandler {
262266
this.isConnectionActive = connectionRunning;
263267
}
264268

269+
/**
270+
* Combines the check and set operations to prevent multiple asynchronous
271+
* calls from redundantly starting an HTTP connection. This ensures that
272+
* only one attempt is made at a time.
273+
*/
265274
private checkAndSetHttpConnectionFlagIfNotRunning(): boolean {
266275
const canMakeConnection = this.canEstablishStreamConnection();
267276
if (canMakeConnection) {
@@ -274,9 +283,12 @@ export class RealtimeHandler {
274283
fetchResponse: FetchResponse,
275284
lastKnownVersion: number
276285
): boolean {
286+
// If there is a config, make sure its version is >= the last known version.
277287
if (fetchResponse.config != null && fetchResponse.templateVersion) {
278288
return fetchResponse.templateVersion >= lastKnownVersion;
279289
}
290+
// If there isn't a config, return true if the fetch was successful and backend had no update.
291+
// Else, it returned an out of date config.
280292
return this.storageCache.getLastFetchStatus() === 'success';
281293
}
282294

@@ -302,6 +314,10 @@ export class RealtimeHandler {
302314
this.observers.forEach(observer => observer.next(configUpdate));
303315
}
304316

317+
/**
318+
* Compares two configuration objects and returns a set of keys that have changed.
319+
* A key is considered changed if it's new, removed, or has a different value.
320+
*/
305321
private getChangedParams(
306322
newConfig: FirebaseRemoteConfigObject,
307323
oldConfig: FirebaseRemoteConfigObject
@@ -420,6 +436,13 @@ export class RealtimeHandler {
420436
await this.fetchLatestConfig(remainingAttempts, targetVersion);
421437
}
422438

439+
/**
440+
* Processes a stream of real-time messages for configuration updates.
441+
* This method reassembles fragmented messages, validates and parses the JSON,
442+
* and automatically fetches a new config if a newer template version is available.
443+
* It also handles server-specified retry intervals and propagates errors for
444+
* invalid messages or when real-time updates are disabled.
445+
*/
423446
private async handleNotifications(
424447
reader: ReadableStreamDefaultReader
425448
): Promise<void> {
@@ -527,13 +550,6 @@ export class RealtimeHandler {
527550
'Real-time connection was closed due to an exception.'
528551
);
529552
}
530-
} finally {
531-
// Only need to close the reader, beginRealtimeHttpStream will disconnect
532-
// the connection
533-
if (this.reader) {
534-
void this.reader.cancel();
535-
this.reader = undefined;
536-
}
537553
}
538554
}
539555

@@ -680,6 +696,13 @@ export class RealtimeHandler {
680696
}
681697
}
682698

699+
/**
700+
* Handles changes to the application's visibility state, managing the real-time connection.
701+
*
702+
* When the application is moved to the background, this method closes the existing
703+
* real-time connection to save resources. When the application returns to the
704+
* foreground, it attempts to re-establish the connection.
705+
*/
683706
private async onVisibilityChange(visible: unknown): Promise<void> {
684707
this.isInBackground = !visible;
685708
if (!visible) {

0 commit comments

Comments
 (0)