Skip to content

Commit bbda349

Browse files
committed
Added the api definition and required interfaces also added the addObserver and removeObserver methods.
1 parent e59cd7d commit bbda349

File tree

5 files changed

+106
-6
lines changed

5 files changed

+106
-6
lines changed

packages/remote-config/src/api.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ import {
2222
LogLevel as RemoteConfigLogLevel,
2323
RemoteConfig,
2424
Value,
25-
RemoteConfigOptions
25+
RemoteConfigOptions,
26+
ConfigUpdateObserver,
27+
Unsubscribe,
28+
ConfigUpdate
2629
} from './public_types';
2730
import { RemoteConfigAbortSignal } from './client/remote_config_fetch_client';
2831
import {
@@ -351,3 +354,22 @@ export async function setCustomSignals(
351354
);
352355
}
353356
}
357+
/**
358+
* Registers a real-time listener for Remote Config updates.
359+
*
360+
* @param remoteConfig - The {@link RemoteConfig} instance.
361+
* @param observer - The {@link ConfigUpdateObserver} to be notified of config updates.
362+
* @returns An {@link Unsubscribe} function to remove the listener.
363+
*
364+
* @public
365+
*/
366+
export function onConfigUpdate(
367+
remoteConfig: RemoteConfig,
368+
observer: ConfigUpdateObserver
369+
): Unsubscribe {
370+
const rc = getModularInstance(remoteConfig) as RemoteConfigImpl;
371+
rc._realtimeHandler.addObserver(observer);
372+
return () => {
373+
rc._realtimeHandler.removeObserver(observer);
374+
}
375+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
import { ConfigUpdateObserver } from "../public_types";
3+
export class RealtimeHandler {
4+
constructor() {
5+
6+
}
7+
private observers: Set<ConfigUpdateObserver> = new Set<ConfigUpdateObserver>();
8+
begineRealtime(){
9+
//if observers are present, start the realtime updates
10+
if (this.observers.size > 0) {
11+
12+
}
13+
}
14+
/**
15+
* Adds an observer to the realtime updates.
16+
* @param observer The observer to add.
17+
*/
18+
addObserver(observer: ConfigUpdateObserver) {
19+
this.observers.add(observer);
20+
console.log("observer added:", this.observers);
21+
22+
this.begineRealtime();
23+
}
24+
/**
25+
* Removes an observer from the realtime updates.
26+
* @param observer The observer to remove.
27+
* @returns true if the observer was removed, false if it was not found.
28+
*/
29+
removeObserver(observer: ConfigUpdateObserver) {
30+
if (this.observers.has(observer)) {
31+
console.log("observer remove:", this.observers);
32+
return this.observers.delete(observer);
33+
}
34+
}
35+
}

packages/remote-config/src/public_types.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { FirebaseApp } from '@firebase/app';
19-
18+
import { FirebaseApp, FirebaseError } from '@firebase/app';
2019
/**
2120
* The Firebase Remote Config service interface.
2221
*
@@ -217,3 +216,39 @@ declare module '@firebase/component' {
217216
'remote-config': RemoteConfig;
218217
}
219218
}
219+
220+
/**
221+
* Observer interface for receiving real-time Remote Config update notifications.
222+
*
223+
* @public
224+
*/
225+
export interface ConfigUpdateObserver {
226+
// Called when a new ConfigUpdate is available.
227+
next: (configUpdate: ConfigUpdate) => void;
228+
229+
// Called if an error occurs during the stream.
230+
error: (error: FirebaseError) => void;
231+
232+
// Called when the stream is gracefully terminated.
233+
complete: () => void;
234+
}
235+
236+
/**
237+
* A function that unsubscribes from a real-time event stream.
238+
*
239+
* @public
240+
*/
241+
export type Unsubscribe = () => void;
242+
243+
/**
244+
* Contains information about which keys have been updated.
245+
*
246+
* @public
247+
*/
248+
export interface ConfigUpdate {
249+
/**
250+
* Parameter keys whose values have been updated from the currently activated values.
251+
* Includes keys that are added, deleted, or whose value, value source, or metadata has changed.
252+
*/
253+
readonly updatedKeys: string[]
254+
}

packages/remote-config/src/register.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { ErrorCode, ERROR_FACTORY } from './errors';
3737
import { RemoteConfig as RemoteConfigImpl } from './remote_config';
3838
import { IndexedDbStorage, InMemoryStorage } from './storage/storage';
3939
import { StorageCache } from './storage/storage_cache';
40+
import { RealtimeHandler } from './client/realtime_handler';
4041
// This needs to be in the same file that calls `getProvider()` on the component
4142
// or it will get tree-shaken out.
4243
import '@firebase/installations';
@@ -107,12 +108,15 @@ export function registerRemoteConfig(): void {
107108
logger
108109
);
109110

111+
const realtimehandler = new RealtimeHandler();
112+
110113
const remoteConfigInstance = new RemoteConfigImpl(
111114
app,
112115
cachingClient,
113116
storageCache,
114117
storage,
115-
logger
118+
logger,
119+
realtimehandler
116120
);
117121

118122
// Starts warming cache.

packages/remote-config/src/remote_config.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { StorageCache } from './storage/storage_cache';
2525
import { RemoteConfigFetchClient } from './client/remote_config_fetch_client';
2626
import { Storage } from './storage/storage';
2727
import { Logger } from '@firebase/logger';
28-
28+
import { RealtimeHandler } from './client/realtime_handler';
2929
const DEFAULT_FETCH_TIMEOUT_MILLIS = 60 * 1000; // One minute
3030
const DEFAULT_CACHE_MAX_AGE_MILLIS = 12 * 60 * 60 * 1000; // Twelve hours.
3131

@@ -83,6 +83,10 @@ export class RemoteConfig implements RemoteConfigType {
8383
/**
8484
* @internal
8585
*/
86-
readonly _logger: Logger
86+
readonly _logger: Logger,
87+
/**
88+
* @internal
89+
*/
90+
readonly _realtimeHandler: RealtimeHandler
8791
) {}
8892
}

0 commit comments

Comments
 (0)