|
17 | 17 |
|
18 | 18 | import { _getProvider, FirebaseApp, getApp } from '@firebase/app';
|
19 | 19 | import {
|
| 20 | + CustomSignals, |
20 | 21 | LogLevel as RemoteConfigLogLevel,
|
21 | 22 | RemoteConfig,
|
22 | 23 | Value
|
23 | 24 | } from './public_types';
|
24 | 25 | import { RemoteConfigAbortSignal } from './client/remote_config_fetch_client';
|
25 |
| -import { RC_COMPONENT_NAME } from './constants'; |
| 26 | +import { |
| 27 | + RC_COMPONENT_NAME, |
| 28 | + RC_CUSTOM_SIGNAL_KEY_MAX_LENGTH, |
| 29 | + RC_CUSTOM_SIGNAL_VALUE_MAX_LENGTH |
| 30 | +} from './constants'; |
26 | 31 | import { ErrorCode, hasErrorCode } from './errors';
|
27 | 32 | import { RemoteConfig as RemoteConfigImpl } from './remote_config';
|
28 | 33 | import { Value as ValueImpl } from './value';
|
@@ -114,11 +119,18 @@ export async function fetchConfig(remoteConfig: RemoteConfig): Promise<void> {
|
114 | 119 | abortSignal.abort();
|
115 | 120 | }, rc.settings.fetchTimeoutMillis);
|
116 | 121 |
|
| 122 | + const customSignals = rc._storageCache.getCustomSignals(); |
| 123 | + if (customSignals) { |
| 124 | + rc._logger.debug( |
| 125 | + `Fetching config with custom signals: ${JSON.stringify(customSignals)}` |
| 126 | + ); |
| 127 | + } |
117 | 128 | // Catches *all* errors thrown by client so status can be set consistently.
|
118 | 129 | try {
|
119 | 130 | await rc._client.fetch({
|
120 | 131 | cacheMaxAgeMillis: rc.settings.minimumFetchIntervalMillis,
|
121 |
| - signal: abortSignal |
| 132 | + signal: abortSignal, |
| 133 | + customSignals |
122 | 134 | });
|
123 | 135 |
|
124 | 136 | await rc._storageCache.setLastFetchStatus('success');
|
@@ -258,3 +270,51 @@ export function setLogLevel(
|
258 | 270 | function getAllKeys(obj1: {} = {}, obj2: {} = {}): string[] {
|
259 | 271 | return Object.keys({ ...obj1, ...obj2 });
|
260 | 272 | }
|
| 273 | + |
| 274 | +/** |
| 275 | + * Sets the custom signals for the app instance. |
| 276 | + * |
| 277 | + * @param remoteConfig - The {@link RemoteConfig} instance. |
| 278 | + * @param customSignals - Map (key, value) of the custom signals to be set for the app instance. If |
| 279 | + * a key already exists, the value is overwritten. Setting the value of a custom signal to null |
| 280 | + * unsets the signal. The signals will be persisted locally on the client. |
| 281 | + * |
| 282 | + * @public |
| 283 | + */ |
| 284 | +export async function setCustomSignals( |
| 285 | + remoteConfig: RemoteConfig, |
| 286 | + customSignals: CustomSignals |
| 287 | +): Promise<void> { |
| 288 | + const rc = getModularInstance(remoteConfig) as RemoteConfigImpl; |
| 289 | + if (Object.keys(customSignals).length === 0) { |
| 290 | + return; |
| 291 | + } |
| 292 | + |
| 293 | + // eslint-disable-next-line guard-for-in |
| 294 | + for (const key in customSignals) { |
| 295 | + if (key.length > RC_CUSTOM_SIGNAL_KEY_MAX_LENGTH) { |
| 296 | + rc._logger.error( |
| 297 | + `Custom signal key ${key} is too long, max allowed length is ${RC_CUSTOM_SIGNAL_KEY_MAX_LENGTH}.` |
| 298 | + ); |
| 299 | + return; |
| 300 | + } |
| 301 | + const value = customSignals[key]; |
| 302 | + if ( |
| 303 | + typeof value === 'string' && |
| 304 | + value.length > RC_CUSTOM_SIGNAL_VALUE_MAX_LENGTH |
| 305 | + ) { |
| 306 | + rc._logger.error( |
| 307 | + `Value supplied for custom signal ${key} is too long, max allowed length is ${RC_CUSTOM_SIGNAL_VALUE_MAX_LENGTH}.` |
| 308 | + ); |
| 309 | + return; |
| 310 | + } |
| 311 | + } |
| 312 | + |
| 313 | + try { |
| 314 | + await rc._storageCache.setCustomSignals(customSignals); |
| 315 | + } catch (error) { |
| 316 | + rc._logger.error( |
| 317 | + `Error encountered while setting custom signals: ${error}` |
| 318 | + ); |
| 319 | + } |
| 320 | +} |
0 commit comments