Skip to content

Commit fce5596

Browse files
author
Kevin Elko
committed
Update API to accept initial config as a part of getRemoteConfig options
1 parent 18d1fd8 commit fce5596

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

packages/remote-config/src/api.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,23 @@ export function getRemoteConfig(app: FirebaseApp = getApp(), options: RemoteConf
5454
throw ERROR_FACTORY.create(ErrorCode.ALREADY_INITIALIZED);
5555
}
5656
rcProvider.initialize({ options });
57-
return rcProvider.getImmediate();
57+
const rc = rcProvider.getImmediate() as RemoteConfigImpl;
58+
59+
if (options.initialFetchResponse) {
60+
//
61+
rc._initializePromise = Promise.all([
62+
rc._storage.setLastSuccessfulFetchResponse(options.initialFetchResponse),
63+
rc._storage.setActiveConfigEtag(options.initialFetchResponse?.eTag || ''),
64+
rc._storageCache.setLastSuccessfulFetchTimestampMillis(Date.now()),
65+
rc._storageCache.setLastFetchStatus('success'),
66+
rc._storageCache.setActiveConfig(options.initialFetchResponse?.config || {})
67+
]).then();
68+
// The storageCache methods above set their in-memory fields sycnhronously, so it's
69+
// safe to declare our initialization complete at this point.
70+
rc._isInitializationComplete = true;
71+
}
72+
73+
return rc;
5874
}
5975

6076
/**
@@ -153,24 +169,6 @@ export async function fetchConfig(remoteConfig: RemoteConfig): Promise<void> {
153169
}
154170
}
155171

156-
/**
157-
* Manually hydrates the config state without making an async fetch request.
158-
* @param remoteConfig - The {@link RemoteConfig} instance.
159-
* @param fetchResponse - The fetchResponse containing the config values and eTag
160-
* with which to hydrate the internal state.
161-
*/
162-
export async function setConfigState(remoteConfig: RemoteConfig, fetchResponse: FetchResponse) {
163-
const rc = getModularInstance(remoteConfig) as RemoteConfigImpl;
164-
await Promise.all([
165-
rc._storage.setLastSuccessfulFetchResponse(fetchResponse),
166-
rc._storageCache.setLastSuccessfulFetchTimestampMillis(Date.now()),
167-
rc._storageCache.setLastFetchStatus('success'),
168-
// TODO - maybe we just call activate() here?
169-
rc._storage.setActiveConfigEtag(fetchResponse.eTag || ''),
170-
rc._storageCache.setActiveConfig(fetchResponse.config || {}),
171-
]);
172-
}
173-
174172
/**
175173
* Gets all config.
176174
*

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ export interface FetchRequest {
115115
* <p>Modeled after the native {@link Response} interface, but simplified for Remote Config's
116116
* use case.
117117
*/
118-
// TODO - should we move this public_types.ts?
119118
export interface FetchResponse {
120119
/**
121120
* The HTTP status, which is useful for differentiating success responses with data from

packages/remote-config/src/public_types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
import { FirebaseApp } from '@firebase/app';
19+
import { FetchResponse } from './client/remote_config_fetch_client';
1920

2021
/**
2122
* Options for Remote Config initialization.
@@ -24,9 +25,14 @@ import { FirebaseApp } from '@firebase/app';
2425
*/
2526
export interface RemoteConfigOptions {
2627
/**
27-
* The ID of the template to use. If not provided, defaults to "firebase"
28+
* The ID of the template to use. If not provided, defaults to "firebase".
2829
*/
2930
templateId?: string;
31+
32+
/**
33+
* Hydrates the state with an initial fetch response.
34+
*/
35+
initialFetchResponse?: FetchResponse;
3036
}
3137

3238
/**

0 commit comments

Comments
 (0)