@react-native-firebase/remote-config 25.1.0 → 26.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +38 -0
- package/RNFBRemoteConfig.podspec +9 -7
- package/android/build.gradle +19 -0
- package/android/src/reactnative/java/io/invertase/firebase/config/{ReactNativeFirebaseConfigModule.java → NativeRNFBTurboConfig.java} +53 -40
- package/android/src/reactnative/java/io/invertase/firebase/config/ReactNativeFirebaseConfigPackage.java +1 -1
- package/android/src/reactnative/java/io/invertase/firebase/config/generated/java/com/facebook/fbreact/specs/NativeRNFBTurboConfigSpec.java +115 -0
- package/android/src/reactnative/java/io/invertase/firebase/config/generated/jni/CMakeLists.txt +36 -0
- package/android/src/reactnative/java/io/invertase/firebase/config/generated/jni/RNFBRemoteConfigTurboModules-generated.cpp +98 -0
- package/android/src/reactnative/java/io/invertase/firebase/config/generated/jni/RNFBRemoteConfigTurboModules.h +31 -0
- package/android/src/reactnative/java/io/invertase/firebase/config/generated/jni/react/renderer/components/RNFBRemoteConfigTurboModules/RNFBRemoteConfigTurboModulesJSI-generated.cpp +110 -0
- package/android/src/reactnative/java/io/invertase/firebase/config/generated/jni/react/renderer/components/RNFBRemoteConfigTurboModules/RNFBRemoteConfigTurboModulesJSI.h +471 -0
- package/dist/module/index.js +499 -5
- package/dist/module/index.js.map +1 -1
- package/dist/module/version.js +1 -1
- package/dist/typescript/lib/index.d.ts +83 -5
- package/dist/typescript/lib/index.d.ts.map +1 -1
- package/dist/typescript/lib/types/internal.d.ts +25 -24
- package/dist/typescript/lib/types/internal.d.ts.map +1 -1
- package/dist/typescript/lib/types/remote-config.d.ts +24 -3
- package/dist/typescript/lib/types/remote-config.d.ts.map +1 -1
- package/dist/typescript/lib/version.d.ts +1 -1
- package/ios/RNFBConfig/RNFBConfigHelper.h +89 -0
- package/ios/RNFBConfig/{RNFBConfigModule.m → RNFBConfigHelper.m} +176 -165
- package/ios/RNFBConfig/RNFBConfigModule.h +3 -2
- package/ios/RNFBConfig/RNFBConfigModule.mm +134 -0
- package/ios/RNFBConfig.xcodeproj/project.pbxproj +8 -8
- package/ios/generated/RCTAppDependencyProvider.h +25 -0
- package/ios/generated/RCTAppDependencyProvider.mm +55 -0
- package/ios/generated/RCTModulesConformingToProtocolsProvider.h +18 -0
- package/ios/generated/RCTModulesConformingToProtocolsProvider.mm +33 -0
- package/ios/generated/RCTThirdPartyComponentsProvider.h +16 -0
- package/ios/generated/RCTThirdPartyComponentsProvider.mm +23 -0
- package/ios/generated/RNFBRemoteConfigTurboModules/RNFBRemoteConfigTurboModules-generated.mm +121 -0
- package/ios/generated/RNFBRemoteConfigTurboModules/RNFBRemoteConfigTurboModules.h +172 -0
- package/ios/generated/RNFBRemoteConfigTurboModulesJSI-generated.cpp +110 -0
- package/ios/generated/RNFBRemoteConfigTurboModulesJSI.h +471 -0
- package/ios/generated/ReactAppDependencyProvider.podspec +34 -0
- package/lib/index.ts +683 -8
- package/lib/types/internal.ts +25 -44
- package/lib/types/remote-config.ts +28 -3
- package/lib/version.ts +1 -1
- package/package.json +23 -5
- package/react-native.config.js +10 -0
- package/specs/NativeRNFBTurboConfig.ts +60 -0
- package/typedoc.json +1 -1
- package/dist/module/modular.js +0 -155
- package/dist/module/modular.js.map +0 -1
- package/dist/module/namespaced.js +0 -400
- package/dist/module/namespaced.js.map +0 -1
- package/dist/module/types/namespaced.js +0 -24
- package/dist/module/types/namespaced.js.map +0 -1
- package/dist/typescript/lib/modular.d.ts +0 -76
- package/dist/typescript/lib/modular.d.ts.map +0 -1
- package/dist/typescript/lib/namespaced.d.ts +0 -11
- package/dist/typescript/lib/namespaced.d.ts.map +0 -1
- package/dist/typescript/lib/types/namespaced.d.ts +0 -124
- package/dist/typescript/lib/types/namespaced.d.ts.map +0 -1
- package/lib/modular.ts +0 -197
- package/lib/namespaced.ts +0 -552
- package/lib/types/namespaced.ts +0 -163
package/lib/index.ts
CHANGED
|
@@ -15,11 +15,686 @@
|
|
|
15
15
|
*
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
18
|
+
import {
|
|
19
|
+
hasOwnProperty,
|
|
20
|
+
isFunction,
|
|
21
|
+
isIOS,
|
|
22
|
+
isNumber,
|
|
23
|
+
isObject,
|
|
24
|
+
isString,
|
|
25
|
+
isUndefined,
|
|
26
|
+
parseListenerOrObserver,
|
|
27
|
+
} from '@react-native-firebase/app/dist/module/common';
|
|
28
|
+
import type { FirebaseApp } from '@react-native-firebase/app';
|
|
29
|
+
import {
|
|
30
|
+
FirebaseModule,
|
|
31
|
+
getOrCreateModularInstance,
|
|
32
|
+
} from '@react-native-firebase/app/dist/module/internal';
|
|
33
|
+
import type { ModuleConfig } from '@react-native-firebase/app/dist/module/internal';
|
|
34
|
+
import NativeFirebaseError from '@react-native-firebase/app/dist/module/internal/NativeFirebaseError';
|
|
35
|
+
import { setReactNativeModule } from '@react-native-firebase/app/dist/module/internal/nativeModule';
|
|
36
|
+
import type { ReactNativeFirebase } from '@react-native-firebase/app';
|
|
37
|
+
import RemoteConfigValue from './RemoteConfigValue';
|
|
38
|
+
import { LastFetchStatus, ValueSource } from './statics';
|
|
39
|
+
import type {
|
|
40
|
+
ConfigUpdate,
|
|
41
|
+
ConfigUpdateObserver,
|
|
42
|
+
CustomSignals,
|
|
43
|
+
FetchStatus,
|
|
44
|
+
LogLevel,
|
|
45
|
+
RemoteConfig,
|
|
46
|
+
RemoteConfigOptions,
|
|
47
|
+
RemoteConfigSettings,
|
|
48
|
+
Unsubscribe,
|
|
49
|
+
Value,
|
|
50
|
+
} from './types/remote-config';
|
|
51
|
+
import type {
|
|
52
|
+
CallbackOrObserver,
|
|
53
|
+
ConfigSettingsStateInternal,
|
|
54
|
+
NativeRemoteConfigConstants,
|
|
55
|
+
NativeRemoteConfigResult,
|
|
56
|
+
OnConfigUpdatedListenerCallback,
|
|
57
|
+
RemoteConfigInternal,
|
|
58
|
+
RemoteConfigUpdateErrorEventInternal,
|
|
59
|
+
RemoteConfigUpdateErrorInternal,
|
|
60
|
+
RemoteConfigUpdateSuccessEventInternal,
|
|
61
|
+
StoredConfigValueInternal,
|
|
62
|
+
} from './types/internal';
|
|
63
|
+
import { version } from './version';
|
|
64
|
+
import fallBackModule from './web/RNFBConfigModule';
|
|
65
|
+
import './types/internal';
|
|
66
|
+
|
|
67
|
+
type ConfigDefaults = Record<string, string | number | boolean>;
|
|
68
|
+
type ConfigSettings = {
|
|
69
|
+
minimumFetchIntervalMillis?: number;
|
|
70
|
+
fetchTimeMillis?: number;
|
|
71
|
+
fetchTimeoutMillis?: number;
|
|
72
|
+
};
|
|
73
|
+
type ConfigValues = Record<string, Value>;
|
|
74
|
+
|
|
75
|
+
const namespace = 'remoteConfig';
|
|
76
|
+
const nativeModuleName = 'NativeRNFBTurboConfig' as const;
|
|
77
|
+
|
|
78
|
+
function isSuccessEvent(
|
|
79
|
+
event: RemoteConfigUpdateSuccessEventInternal | RemoteConfigUpdateErrorEventInternal,
|
|
80
|
+
): event is RemoteConfigUpdateSuccessEventInternal {
|
|
81
|
+
return event.resultType === 'success';
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function toConfigUpdate(updatedKeys: string[]): ConfigUpdate {
|
|
85
|
+
return {
|
|
86
|
+
getUpdatedKeys: () => new Set(updatedKeys),
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function toNativeFirebaseError(
|
|
91
|
+
errorEvent: RemoteConfigUpdateErrorInternal,
|
|
92
|
+
): ReactNativeFirebase.NativeFirebaseError {
|
|
93
|
+
return NativeFirebaseError.fromEvent(
|
|
94
|
+
errorEvent,
|
|
95
|
+
namespace,
|
|
96
|
+
) as ReactNativeFirebase.NativeFirebaseError;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function rc(remoteConfig: RemoteConfig): RemoteConfigInternal {
|
|
100
|
+
return remoteConfig as RemoteConfigInternal;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
class FirebaseConfigModule extends FirebaseModule<typeof nativeModuleName> {
|
|
104
|
+
private _settings: ConfigSettingsStateInternal;
|
|
105
|
+
private _lastFetchTime: number;
|
|
106
|
+
private _lastFetchStatus: FetchStatus;
|
|
107
|
+
private _values: Record<string, StoredConfigValueInternal>;
|
|
108
|
+
private _configUpdateListenerCount: number;
|
|
109
|
+
private _nativeMutationQueue: Promise<void>;
|
|
110
|
+
|
|
111
|
+
constructor(
|
|
112
|
+
app: ReactNativeFirebase.FirebaseAppBase,
|
|
113
|
+
config: ModuleConfig,
|
|
114
|
+
customUrlOrRegion?: string | null,
|
|
115
|
+
) {
|
|
116
|
+
super(app, config, customUrlOrRegion);
|
|
117
|
+
this._settings = {
|
|
118
|
+
// defaults to 1 minute.
|
|
119
|
+
fetchTimeoutMillis: 60000,
|
|
120
|
+
// defaults to 12 hours.
|
|
121
|
+
minimumFetchIntervalMillis: 43200000,
|
|
122
|
+
};
|
|
123
|
+
this._lastFetchTime = -1;
|
|
124
|
+
this._lastFetchStatus = 'no_fetch_yet';
|
|
125
|
+
this._values = {};
|
|
126
|
+
this._configUpdateListenerCount = 0;
|
|
127
|
+
this._nativeMutationQueue = Promise.resolve();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
get defaultConfig(): ConfigDefaults {
|
|
131
|
+
const updatedDefaultConfig: ConfigDefaults = {};
|
|
132
|
+
Object.keys(this._values).forEach(key => {
|
|
133
|
+
// Need to make it an object with key and literal value. Not `Value` instance.
|
|
134
|
+
const configValue = this._values[key];
|
|
135
|
+
if (configValue) {
|
|
136
|
+
updatedDefaultConfig[key] = configValue.value;
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
return updatedDefaultConfig;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
set defaultConfig(defaults: ConfigDefaults) {
|
|
144
|
+
if (!isObject(defaults)) {
|
|
145
|
+
throw new Error("firebase.remoteConfig().defaultConfig: 'defaults' must be an object.");
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// To make Firebase web v9 API compatible, we update the config first so it immediately
|
|
149
|
+
// updates defaults on the instance. We then pass to underlying SDK to update. We do this because
|
|
150
|
+
// there is no way to "await" a setter.
|
|
151
|
+
const nonDefaultValues = Object.fromEntries(
|
|
152
|
+
Object.entries(this._values).filter(([, configValue]) => configValue?.source !== 'default'),
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
this._values = Object.freeze({
|
|
156
|
+
...Object.fromEntries(
|
|
157
|
+
Object.entries(defaults).map(([key, value]) => [
|
|
158
|
+
key,
|
|
159
|
+
{ value, source: 'default' as const },
|
|
160
|
+
]),
|
|
161
|
+
),
|
|
162
|
+
...nonDefaultValues,
|
|
163
|
+
});
|
|
164
|
+
void this.setDefaults(defaults, true);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
get settings(): RemoteConfigSettings & { fetchTimeMillis: number } {
|
|
168
|
+
return {
|
|
169
|
+
minimumFetchIntervalMillis: this._settings.minimumFetchIntervalMillis,
|
|
170
|
+
fetchTimeoutMillis: this._settings.fetchTimeoutMillis,
|
|
171
|
+
fetchTimeMillis: this._settings.fetchTimeoutMillis,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
set settings(settings: ConfigSettings | RemoteConfigSettings) {
|
|
176
|
+
// To make Firebase web v9 API compatible, we update the settings first so it immediately
|
|
177
|
+
// updates settings on the instance. We then pass to underlying SDK to update. We do this because
|
|
178
|
+
// there is no way to "await" a setter. We can't delegate to `setConfigSettings()` as it is setup
|
|
179
|
+
// for native.
|
|
180
|
+
this._settings = {
|
|
181
|
+
minimumFetchIntervalMillis:
|
|
182
|
+
settings.minimumFetchIntervalMillis ?? this._settings.minimumFetchIntervalMillis,
|
|
183
|
+
fetchTimeoutMillis:
|
|
184
|
+
('fetchTimeoutMillis' in settings ? settings.fetchTimeoutMillis : undefined) ??
|
|
185
|
+
('fetchTimeMillis' in settings ? settings.fetchTimeMillis : undefined) ??
|
|
186
|
+
this._settings.fetchTimeoutMillis,
|
|
187
|
+
};
|
|
188
|
+
void this.setConfigSettings(settings, true);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
getValue(key: string): Value {
|
|
192
|
+
if (!isString(key)) {
|
|
193
|
+
throw new Error("firebase.remoteConfig().getValue(): 'key' must be a string value.");
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (typeof this._values === 'undefined' || !hasOwnProperty(this._values, key)) {
|
|
197
|
+
return new RemoteConfigValue({
|
|
198
|
+
value: '',
|
|
199
|
+
source: 'static',
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const configValue = this._values[key]!;
|
|
204
|
+
return new RemoteConfigValue({ value: `${configValue.value}`, source: configValue.source });
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
getBoolean(key: string): boolean {
|
|
208
|
+
return this.getValue(key).asBoolean();
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
getNumber(key: string): number {
|
|
212
|
+
return this.getValue(key).asNumber();
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
getString(key: string): string {
|
|
216
|
+
return this.getValue(key).asString();
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
getAll(): ConfigValues {
|
|
220
|
+
const values: ConfigValues = {};
|
|
221
|
+
Object.keys(this._values).forEach(key => {
|
|
222
|
+
values[key] = this.getValue(key);
|
|
223
|
+
});
|
|
224
|
+
return values;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
get fetchTimeMillis(): number {
|
|
228
|
+
// android returns -1 if no fetch yet and iOS returns 0
|
|
229
|
+
return this._lastFetchTime;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
get lastFetchStatus(): FetchStatus {
|
|
233
|
+
return this._lastFetchStatus;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Deletes all activated, fetched and defaults configs and resets all Firebase Remote Config settings.
|
|
238
|
+
* @returns {Promise<void>}
|
|
239
|
+
*/
|
|
240
|
+
reset(): Promise<void> {
|
|
241
|
+
if (isIOS) {
|
|
242
|
+
return Promise.resolve();
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return this._enqueueNativeMutation(() => this._promiseWithConstants(this.native.reset()));
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
setConfigSettings(
|
|
249
|
+
settings: ConfigSettings | RemoteConfigSettings,
|
|
250
|
+
fromSettingsSetter = false,
|
|
251
|
+
): Promise<void> {
|
|
252
|
+
const updatedSettings: {
|
|
253
|
+
fetchTimeout: number;
|
|
254
|
+
minimumFetchInterval: number;
|
|
255
|
+
} = {
|
|
256
|
+
fetchTimeout: this._settings.fetchTimeoutMillis / 1000,
|
|
257
|
+
minimumFetchInterval: this._settings.minimumFetchIntervalMillis / 1000,
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
const apiCalled = fromSettingsSetter ? 'settings' : 'setConfigSettings';
|
|
261
|
+
if (!isObject(settings)) {
|
|
262
|
+
throw new Error(`firebase.remoteConfig().${apiCalled}(*): settings must set an object.`);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (hasOwnProperty(settings, 'minimumFetchIntervalMillis')) {
|
|
266
|
+
if (!isNumber(settings.minimumFetchIntervalMillis)) {
|
|
267
|
+
throw new Error(
|
|
268
|
+
`firebase.remoteConfig().${apiCalled}(): 'settings.minimumFetchIntervalMillis' must be a number type in milliseconds.`,
|
|
269
|
+
);
|
|
270
|
+
} else {
|
|
271
|
+
updatedSettings.minimumFetchInterval = settings.minimumFetchIntervalMillis / 1000;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (hasOwnProperty(settings, 'fetchTimeMillis')) {
|
|
276
|
+
if (!isNumber(settings.fetchTimeMillis)) {
|
|
277
|
+
throw new Error(
|
|
278
|
+
`firebase.remoteConfig().${apiCalled}(): 'settings.fetchTimeMillis' must be a number type in milliseconds.`,
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
updatedSettings.fetchTimeout = settings.fetchTimeMillis / 1000;
|
|
283
|
+
} else if (hasOwnProperty(settings, 'fetchTimeoutMillis')) {
|
|
284
|
+
if (!isNumber(settings.fetchTimeoutMillis)) {
|
|
285
|
+
throw new Error(
|
|
286
|
+
`firebase.remoteConfig().${apiCalled}(): 'settings.fetchTimeoutMillis' must be a number type in milliseconds.`,
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
updatedSettings.fetchTimeout = settings.fetchTimeoutMillis / 1000;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const nextSettings = {
|
|
294
|
+
fetchTimeoutMillis: updatedSettings.fetchTimeout * 1000,
|
|
295
|
+
minimumFetchIntervalMillis: updatedSettings.minimumFetchInterval * 1000,
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
// Keep JS reads in sync immediately. Native can report stale settings constants
|
|
299
|
+
// for this call because native setConfigSettingsAsync completes after the bridge resolves.
|
|
300
|
+
this._settings = nextSettings;
|
|
301
|
+
|
|
302
|
+
return this._enqueueNativeMutation(() =>
|
|
303
|
+
this.native.setConfigSettings(updatedSettings).then(({ result, constants }) => {
|
|
304
|
+
// Preserve the eagerly computed settings above and only refresh the rest of the cache.
|
|
305
|
+
this._updateFromConstants({
|
|
306
|
+
...constants,
|
|
307
|
+
fetchTimeout: undefined,
|
|
308
|
+
minimumFetchInterval: undefined,
|
|
309
|
+
});
|
|
310
|
+
return result;
|
|
311
|
+
}),
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Activates the Fetched RemoteConfig, so that the fetched key-values take effect.
|
|
317
|
+
* @returns {Promise<boolean>}
|
|
318
|
+
*/
|
|
319
|
+
activate(): Promise<boolean> {
|
|
320
|
+
return this._promiseWithConstants(this.native.activate());
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Fetches parameter values for your app.
|
|
325
|
+
*
|
|
326
|
+
* @param expirationDurationSeconds
|
|
327
|
+
* @returns {Promise}
|
|
328
|
+
*/
|
|
329
|
+
fetch(expirationDurationSeconds?: number): Promise<void> {
|
|
330
|
+
if (!isUndefined(expirationDurationSeconds) && !isNumber(expirationDurationSeconds)) {
|
|
331
|
+
throw new Error(
|
|
332
|
+
"firebase.remoteConfig().fetch(): 'expirationDurationSeconds' must be a number value.",
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
return this._promiseWithConstants(
|
|
337
|
+
this.native.fetch(expirationDurationSeconds !== undefined ? expirationDurationSeconds : -1),
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
fetchAndActivate(): Promise<boolean> {
|
|
342
|
+
return this._promiseWithConstants(this.native.fetchAndActivate());
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
ensureInitialized(): Promise<void> {
|
|
346
|
+
return this._promiseWithConstants(this.native.ensureInitialized());
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Sets defaults.
|
|
351
|
+
*
|
|
352
|
+
* @param defaults
|
|
353
|
+
*/
|
|
354
|
+
setDefaults(defaults: ConfigDefaults, fromDefaultConfigSetter = false): Promise<null> {
|
|
355
|
+
const apiCalled = fromDefaultConfigSetter ? 'defaultConfig' : 'setDefaults';
|
|
356
|
+
if (!isObject(defaults)) {
|
|
357
|
+
throw new Error(`firebase.remoteConfig().${apiCalled}(): 'defaults' must be an object.`);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
return this._enqueueNativeMutation(() =>
|
|
361
|
+
this._promiseWithConstants(this.native.setDefaults(defaults)),
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Sets defaults based on resource.
|
|
367
|
+
* @param resourceName
|
|
368
|
+
*/
|
|
369
|
+
setDefaultsFromResource(resourceName: string): Promise<null> {
|
|
370
|
+
if (!isString(resourceName)) {
|
|
371
|
+
throw new Error(
|
|
372
|
+
"firebase.remoteConfig().setDefaultsFromResource(): 'resourceName' must be a string value.",
|
|
373
|
+
);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
return this._enqueueNativeMutation(() =>
|
|
377
|
+
this._promiseWithConstants(this.native.setDefaultsFromResource(resourceName)),
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Registers an observer to changes in the configuration.
|
|
383
|
+
*
|
|
384
|
+
* @param observer - The observer to be notified of config updates.
|
|
385
|
+
* @returns An unsubscribe function to remove the listener.
|
|
386
|
+
*/
|
|
387
|
+
onConfigUpdate(observer: ConfigUpdateObserver): Unsubscribe {
|
|
388
|
+
if (!isObject(observer) || !isFunction(observer.next) || !isFunction(observer.error)) {
|
|
389
|
+
throw new Error("'observer' expected an object with 'next' and 'error' functions.");
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
// We maintaine our pre-web-support native interface but bend it to match
|
|
393
|
+
// the official JS SDK API by assuming the callback is an Observer, and sending it a ConfigUpdate
|
|
394
|
+
// compatible parameter that implements the `getUpdatedKeys` method
|
|
395
|
+
let unsubscribed = false;
|
|
396
|
+
const subscription = this.emitter.addListener(
|
|
397
|
+
this.eventNameForApp('on_config_updated'),
|
|
398
|
+
(event: RemoteConfigUpdateSuccessEventInternal | RemoteConfigUpdateErrorEventInternal) => {
|
|
399
|
+
if (isSuccessEvent(event)) {
|
|
400
|
+
observer.next(toConfigUpdate(event.updatedKeys));
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
observer.error(toNativeFirebaseError(event));
|
|
405
|
+
},
|
|
406
|
+
);
|
|
407
|
+
|
|
408
|
+
if (this._configUpdateListenerCount === 0) {
|
|
409
|
+
this.native.onConfigUpdated();
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
this._configUpdateListenerCount++;
|
|
413
|
+
|
|
414
|
+
return () => {
|
|
415
|
+
if (unsubscribed) {
|
|
416
|
+
// there is no harm in calling this multiple times to unsubscribe,
|
|
417
|
+
// but anything after the first call is a no-op
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
unsubscribed = true;
|
|
422
|
+
subscription.remove();
|
|
423
|
+
this._configUpdateListenerCount--;
|
|
424
|
+
|
|
425
|
+
if (this._configUpdateListenerCount === 0) {
|
|
426
|
+
this.native.removeConfigUpdateRegistration();
|
|
427
|
+
}
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Registers a listener to changes in the configuration.
|
|
433
|
+
*
|
|
434
|
+
* @param listenerOrObserver - function called on config change
|
|
435
|
+
* @returns unsubscribe listener
|
|
436
|
+
* @deprecated use official firebase-js-sdk onConfigUpdate now that web supports realtime
|
|
437
|
+
*/
|
|
438
|
+
onConfigUpdated(listenerOrObserver: unknown): Unsubscribe {
|
|
439
|
+
const listener = parseListenerOrObserver(
|
|
440
|
+
listenerOrObserver as CallbackOrObserver<OnConfigUpdatedListenerCallback>,
|
|
441
|
+
) as (event?: { updatedKeys: string[] }, error?: RemoteConfigUpdateErrorInternal) => void;
|
|
442
|
+
let unsubscribed = false;
|
|
443
|
+
const subscription = this.emitter.addListener(
|
|
444
|
+
this.eventNameForApp('on_config_updated'),
|
|
445
|
+
(event: RemoteConfigUpdateSuccessEventInternal | RemoteConfigUpdateErrorEventInternal) => {
|
|
446
|
+
if (isSuccessEvent(event)) {
|
|
447
|
+
listener({ updatedKeys: event.updatedKeys }, undefined);
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
listener(undefined, {
|
|
452
|
+
code: event.code,
|
|
453
|
+
message: event.message,
|
|
454
|
+
nativeErrorMessage: event.nativeErrorMessage,
|
|
455
|
+
});
|
|
456
|
+
},
|
|
457
|
+
);
|
|
458
|
+
|
|
459
|
+
if (this._configUpdateListenerCount === 0) {
|
|
460
|
+
this.native.onConfigUpdated();
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
this._configUpdateListenerCount++;
|
|
464
|
+
|
|
465
|
+
return () => {
|
|
466
|
+
if (unsubscribed) {
|
|
467
|
+
// there is no harm in calling this multiple times to unsubscribe,
|
|
468
|
+
// but anything after the first call is a no-op
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
unsubscribed = true;
|
|
473
|
+
subscription.remove();
|
|
474
|
+
this._configUpdateListenerCount--;
|
|
475
|
+
|
|
476
|
+
if (this._configUpdateListenerCount === 0) {
|
|
477
|
+
this.native.removeConfigUpdateRegistration();
|
|
478
|
+
}
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
private _updateFromConstants(constants: NativeRemoteConfigConstants): void {
|
|
483
|
+
// Wrapped this as we update using sync getters initially for `defaultConfig` & `settings`
|
|
484
|
+
if (constants.lastFetchTime !== undefined) {
|
|
485
|
+
this._lastFetchTime = constants.lastFetchTime;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
// Wrapped this as we update using sync getters initially for `defaultConfig` & `settings`
|
|
489
|
+
if (constants.lastFetchStatus !== undefined) {
|
|
490
|
+
this._lastFetchStatus = constants.lastFetchStatus;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
if (constants.fetchTimeout !== undefined && constants.minimumFetchInterval !== undefined) {
|
|
494
|
+
this._settings = {
|
|
495
|
+
fetchTimeoutMillis: constants.fetchTimeout * 1000,
|
|
496
|
+
minimumFetchIntervalMillis: constants.minimumFetchInterval * 1000,
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
if (constants.values !== undefined) {
|
|
501
|
+
this._values = Object.freeze(constants.values);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
private _promiseWithConstants<T>(promise: Promise<NativeRemoteConfigResult<T>>): Promise<T> {
|
|
506
|
+
return promise.then(({ result, constants }) => {
|
|
507
|
+
this._updateFromConstants(constants);
|
|
508
|
+
return result;
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
private _enqueueNativeMutation<T>(task: () => Promise<T>): Promise<T> {
|
|
513
|
+
// Some callers (like property setters) discard the returned promise; serialize all mutations
|
|
514
|
+
// so later writes like reset() cannot be overwritten by an earlier async completion.
|
|
515
|
+
const next = this._nativeMutationQueue.then(task, task);
|
|
516
|
+
this._nativeMutationQueue = next.then(
|
|
517
|
+
() => undefined,
|
|
518
|
+
() => undefined,
|
|
519
|
+
);
|
|
520
|
+
return next;
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
const config: ModuleConfig = {
|
|
525
|
+
namespace,
|
|
526
|
+
nativeModuleName,
|
|
527
|
+
nativeEvents: ['on_config_updated'],
|
|
528
|
+
hasMultiAppSupport: true,
|
|
529
|
+
hasCustomUrlOrRegionSupport: false,
|
|
530
|
+
turboModule: true,
|
|
531
|
+
};
|
|
532
|
+
|
|
533
|
+
export { LastFetchStatus, ValueSource };
|
|
534
|
+
export const SDK_VERSION = version;
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* Returns a RemoteConfig instance for the given app.
|
|
538
|
+
* @param app - FirebaseApp. Optional.
|
|
539
|
+
*/
|
|
540
|
+
export function getRemoteConfig(app?: FirebaseApp, options?: RemoteConfigOptions): RemoteConfig {
|
|
541
|
+
void options;
|
|
542
|
+
return getOrCreateModularInstance(FirebaseConfigModule, config, app) as unknown as RemoteConfig;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* Returns a Boolean which resolves to true if the current call
|
|
547
|
+
* activated the fetched configs.
|
|
548
|
+
*/
|
|
549
|
+
export function activate(remoteConfig: RemoteConfig): Promise<boolean> {
|
|
550
|
+
return rc(remoteConfig).activate();
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* Ensures the last activated config are available to the getters.
|
|
555
|
+
*/
|
|
556
|
+
export function ensureInitialized(remoteConfig: RemoteConfig): Promise<void> {
|
|
557
|
+
return rc(remoteConfig).ensureInitialized();
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
/**
|
|
561
|
+
* Performs a fetch and returns a Boolean which resolves to true
|
|
562
|
+
* if the current call activated the fetched configs.
|
|
563
|
+
*/
|
|
564
|
+
export function fetchAndActivate(remoteConfig: RemoteConfig): Promise<boolean> {
|
|
565
|
+
return rc(remoteConfig).fetchAndActivate();
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/**
|
|
569
|
+
* Fetches and caches configuration from the Remote Config service.
|
|
570
|
+
*/
|
|
571
|
+
export function fetchConfig(remoteConfig: RemoteConfig): Promise<void> {
|
|
572
|
+
return rc(remoteConfig).fetch();
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
/**
|
|
576
|
+
* Gets all config.
|
|
577
|
+
*/
|
|
578
|
+
export function getAll(remoteConfig: RemoteConfig): Record<string, Value> {
|
|
579
|
+
return rc(remoteConfig).getAll();
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* Gets the value for the given key as a boolean.
|
|
584
|
+
*/
|
|
585
|
+
export function getBoolean(remoteConfig: RemoteConfig, key: string): boolean {
|
|
586
|
+
return rc(remoteConfig).getBoolean(key);
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* Gets the value for the given key as a number.
|
|
591
|
+
*/
|
|
592
|
+
export function getNumber(remoteConfig: RemoteConfig, key: string): number {
|
|
593
|
+
return rc(remoteConfig).getNumber(key);
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
/**
|
|
597
|
+
* Gets the value for the given key as a string.
|
|
598
|
+
*/
|
|
599
|
+
export function getString(remoteConfig: RemoteConfig, key: string): string {
|
|
600
|
+
return rc(remoteConfig).getString(key);
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
/**
|
|
604
|
+
* Gets the value for the given key.
|
|
605
|
+
*/
|
|
606
|
+
export function getValue(remoteConfig: RemoteConfig, key: string): Value {
|
|
607
|
+
return rc(remoteConfig).getValue(key);
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/**
|
|
611
|
+
* Defines the log level to use.
|
|
612
|
+
*/
|
|
613
|
+
export function setLogLevel(remoteConfig: RemoteConfig, logLevel: LogLevel): void {
|
|
614
|
+
void remoteConfig;
|
|
615
|
+
void logLevel;
|
|
616
|
+
// Intentionally ignored on native. The modular API matches the JS SDK and returns void.
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
/**
|
|
620
|
+
* Checks two different things.
|
|
621
|
+
* 1. Check if IndexedDB exists in the browser environment.
|
|
622
|
+
* 2. Check if the current browser context allows IndexedDB open() calls.
|
|
623
|
+
*/
|
|
624
|
+
export function isSupported(): Promise<boolean> {
|
|
625
|
+
// always return "true" for now. Web only.
|
|
626
|
+
return Promise.resolve(true);
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
/**
|
|
630
|
+
* Deletes all activated, fetched and defaults configs and
|
|
631
|
+
* resets all Firebase Remote Config settings.
|
|
632
|
+
*
|
|
633
|
+
* @remarks **Android only.** iOS does not clear activated, fetched, or default configs.
|
|
634
|
+
*/
|
|
635
|
+
export function reset(remoteConfig: RemoteConfig): Promise<void> {
|
|
636
|
+
return rc(remoteConfig).reset();
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
/**
|
|
640
|
+
* Sets defaults based on a native resource file.
|
|
641
|
+
*
|
|
642
|
+
* @remarks Loads defaults from a platform resource — iOS `.plist` or Android XML — identified
|
|
643
|
+
* by `resourceName`. No firebase-js-sdk modular equivalent.
|
|
644
|
+
*/
|
|
645
|
+
export function setDefaultsFromResource(
|
|
646
|
+
remoteConfig: RemoteConfig,
|
|
647
|
+
resourceName: string,
|
|
648
|
+
): Promise<null> {
|
|
649
|
+
return rc(remoteConfig).setDefaultsFromResource(resourceName);
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
/**
|
|
653
|
+
* Registers a listener to changes in the configuration.
|
|
654
|
+
*
|
|
655
|
+
*/
|
|
656
|
+
export function onConfigUpdate(
|
|
657
|
+
remoteConfig: RemoteConfig,
|
|
658
|
+
observer: ConfigUpdateObserver,
|
|
659
|
+
): Unsubscribe {
|
|
660
|
+
return rc(remoteConfig).onConfigUpdate(observer);
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
/**
|
|
664
|
+
* Sets the custom signals for the app instance.
|
|
665
|
+
*/
|
|
666
|
+
export async function setCustomSignals(
|
|
667
|
+
remoteConfig: RemoteConfig,
|
|
668
|
+
customSignals: CustomSignals,
|
|
669
|
+
): Promise<void> {
|
|
670
|
+
for (const [key, value] of Object.entries(customSignals)) {
|
|
671
|
+
if (typeof value !== 'string' && typeof value !== 'number' && value !== null) {
|
|
672
|
+
throw new Error(
|
|
673
|
+
`firebase.remoteConfig().setCustomSignals(): Invalid type for custom signal '${key}': ${typeof value}. Expected 'string', 'number', or 'null'.`,
|
|
674
|
+
);
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
return rc(remoteConfig)._promiseWithConstants(
|
|
679
|
+
rc(remoteConfig).native.setCustomSignals(customSignals),
|
|
680
|
+
);
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
export type {
|
|
684
|
+
ConfigUpdate,
|
|
685
|
+
ConfigUpdateObserver,
|
|
686
|
+
CustomSignals,
|
|
687
|
+
FetchResponse,
|
|
688
|
+
FetchStatus,
|
|
689
|
+
FirebaseExperimentDescription,
|
|
690
|
+
FirebaseRemoteConfigObject,
|
|
691
|
+
LogLevel,
|
|
692
|
+
RemoteConfig,
|
|
693
|
+
RemoteConfigOptions,
|
|
694
|
+
RemoteConfigSettings,
|
|
695
|
+
Unsubscribe,
|
|
696
|
+
Value,
|
|
697
|
+
} from './types/remote-config';
|
|
698
|
+
|
|
699
|
+
// Register the interop module for non-native platforms.
|
|
700
|
+
setReactNativeModule(nativeModuleName, fallBackModule as unknown as Record<string, unknown>);
|