@react-native-firebase/remote-config 24.1.0 → 25.0.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 +33 -0
- package/android/src/reactnative/java/io/invertase/firebase/config/ReactNativeFirebaseConfigModule.java +11 -5
- package/dist/module/RemoteConfigValue.js +53 -0
- package/dist/module/RemoteConfigValue.js.map +1 -0
- package/dist/module/index.js +27 -0
- package/dist/module/index.js.map +1 -0
- package/dist/module/modular.js +155 -0
- package/dist/module/modular.js.map +1 -0
- package/dist/module/namespaced.js +400 -0
- package/dist/module/namespaced.js.map +1 -0
- package/dist/module/package.json +1 -0
- package/{lib → dist/module}/polyfills.js +26 -11
- package/dist/module/polyfills.js.map +1 -0
- package/dist/module/polyfills.web.js +3 -0
- package/dist/module/polyfills.web.js.map +1 -0
- package/{lib → dist/module}/statics.js +5 -3
- package/dist/module/statics.js.map +1 -0
- package/dist/module/types/internal.js +4 -0
- package/dist/module/types/internal.js.map +1 -0
- package/dist/module/types/namespaced.js +24 -0
- package/dist/module/types/namespaced.js.map +1 -0
- package/dist/module/types/remote-config.js +4 -0
- package/dist/module/types/remote-config.js.map +1 -0
- package/dist/module/version.js +5 -0
- package/dist/module/version.js.map +1 -0
- package/dist/module/web/RNFBConfigModule.android.js +6 -0
- package/dist/module/web/RNFBConfigModule.android.js.map +1 -0
- package/dist/module/web/RNFBConfigModule.ios.js +6 -0
- package/dist/module/web/RNFBConfigModule.ios.js.map +1 -0
- package/{lib → dist/module}/web/RNFBConfigModule.js +69 -77
- package/dist/module/web/RNFBConfigModule.js.map +1 -0
- package/dist/typescript/lib/RemoteConfigValue.d.ts +16 -0
- package/dist/typescript/lib/RemoteConfigValue.d.ts.map +1 -0
- package/dist/typescript/lib/index.d.ts +6 -0
- package/dist/typescript/lib/index.d.ts.map +1 -0
- package/dist/typescript/lib/modular.d.ts +76 -0
- package/dist/typescript/lib/modular.d.ts.map +1 -0
- package/dist/typescript/lib/namespaced.d.ts +11 -0
- package/dist/typescript/lib/namespaced.d.ts.map +1 -0
- package/dist/typescript/lib/polyfills.d.ts +2 -0
- package/dist/typescript/lib/polyfills.d.ts.map +1 -0
- package/dist/typescript/lib/polyfills.web.d.ts +1 -0
- package/dist/typescript/lib/polyfills.web.d.ts.map +1 -0
- package/dist/typescript/lib/statics.d.ts +12 -0
- package/dist/typescript/lib/statics.d.ts.map +1 -0
- package/dist/typescript/lib/types/internal.d.ts +94 -0
- package/dist/typescript/lib/types/internal.d.ts.map +1 -0
- package/dist/typescript/lib/types/namespaced.d.ts +124 -0
- package/dist/typescript/lib/types/namespaced.d.ts.map +1 -0
- package/dist/typescript/lib/types/remote-config.d.ts +37 -0
- package/dist/typescript/lib/types/remote-config.d.ts.map +1 -0
- package/dist/typescript/lib/version.d.ts +2 -0
- package/dist/typescript/lib/version.d.ts.map +1 -0
- package/dist/typescript/lib/web/RNFBConfigModule.android.d.ts +3 -0
- package/dist/typescript/lib/web/RNFBConfigModule.android.d.ts.map +1 -0
- package/dist/typescript/lib/web/RNFBConfigModule.d.ts +17 -0
- package/dist/typescript/lib/web/RNFBConfigModule.d.ts.map +1 -0
- package/dist/typescript/lib/web/RNFBConfigModule.ios.d.ts +3 -0
- package/dist/typescript/lib/web/RNFBConfigModule.ios.d.ts.map +1 -0
- package/dist/typescript/package.json +1 -0
- package/ios/RNFBConfig/RNFBConfigModule.m +10 -4
- package/lib/RemoteConfigValue.ts +66 -0
- package/lib/index.ts +25 -0
- package/lib/modular.ts +197 -0
- package/lib/namespaced.ts +552 -0
- package/lib/polyfills.ts +54 -0
- package/lib/statics.ts +27 -0
- package/lib/types/internal.ts +165 -0
- package/lib/types/namespaced.ts +163 -0
- package/lib/types/remote-config.ts +63 -0
- package/lib/version.ts +2 -0
- package/lib/web/RNFBConfigModule.android.ts +4 -0
- package/lib/web/RNFBConfigModule.ios.ts +4 -0
- package/lib/web/RNFBConfigModule.ts +254 -0
- package/package.json +42 -9
- package/tsconfig.json +21 -0
- package/typedoc.json +2 -3
- package/lib/RemoteConfigValue.js +0 -51
- package/lib/index.d.ts +0 -659
- package/lib/index.js +0 -400
- package/lib/modular/index.d.ts +0 -265
- package/lib/modular/index.js +0 -289
- package/lib/version.js +0 -2
- package/lib/web/RNFBConfigModule.android.js +0 -2
- package/lib/web/RNFBConfigModule.ios.js +0 -2
- /package/lib/{polyfills.web.js → polyfills.web.ts} +0 -0
|
@@ -0,0 +1,552 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2016-present Invertase Limited & Contributors
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this library except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*
|
|
16
|
+
*/
|
|
17
|
+
|
|
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 {
|
|
29
|
+
createModuleNamespace,
|
|
30
|
+
FirebaseModule,
|
|
31
|
+
getFirebaseRoot,
|
|
32
|
+
type ModuleConfig,
|
|
33
|
+
} 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
|
+
FetchStatus,
|
|
43
|
+
RemoteConfigSettings,
|
|
44
|
+
} from './types/remote-config';
|
|
45
|
+
import type { FirebaseRemoteConfigTypes } from './types/namespaced';
|
|
46
|
+
import type {
|
|
47
|
+
ConfigSettingsStateInternal,
|
|
48
|
+
NativeRemoteConfigConstants,
|
|
49
|
+
NativeRemoteConfigResult,
|
|
50
|
+
RemoteConfigUpdateErrorEventInternal,
|
|
51
|
+
RemoteConfigUpdateErrorInternal,
|
|
52
|
+
RemoteConfigUpdateSuccessEventInternal,
|
|
53
|
+
StoredConfigValueInternal,
|
|
54
|
+
} from './types/internal';
|
|
55
|
+
import { version } from './version';
|
|
56
|
+
import fallBackModule from './web/RNFBConfigModule';
|
|
57
|
+
|
|
58
|
+
type ConfigDefaults = FirebaseRemoteConfigTypes.ConfigDefaults;
|
|
59
|
+
type ConfigSettings = FirebaseRemoteConfigTypes.ConfigSettings;
|
|
60
|
+
type ConfigValue = FirebaseRemoteConfigTypes.ConfigValue;
|
|
61
|
+
type ConfigValues = FirebaseRemoteConfigTypes.ConfigValues;
|
|
62
|
+
type LastFetchStatusType = FirebaseRemoteConfigTypes.LastFetchStatusType;
|
|
63
|
+
|
|
64
|
+
function isSuccessEvent(
|
|
65
|
+
event: RemoteConfigUpdateSuccessEventInternal | RemoteConfigUpdateErrorEventInternal,
|
|
66
|
+
): event is RemoteConfigUpdateSuccessEventInternal {
|
|
67
|
+
return event.resultType === 'success';
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function toConfigUpdate(updatedKeys: string[]): ConfigUpdate {
|
|
71
|
+
return {
|
|
72
|
+
getUpdatedKeys: () => new Set(updatedKeys),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function toNativeFirebaseError(
|
|
77
|
+
errorEvent: RemoteConfigUpdateErrorInternal,
|
|
78
|
+
): ReactNativeFirebase.NativeFirebaseError {
|
|
79
|
+
return NativeFirebaseError.fromEvent(
|
|
80
|
+
errorEvent,
|
|
81
|
+
namespace,
|
|
82
|
+
) as ReactNativeFirebase.NativeFirebaseError;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const statics = {
|
|
86
|
+
LastFetchStatus,
|
|
87
|
+
ValueSource,
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const namespace = 'remoteConfig';
|
|
91
|
+
const nativeModuleName = 'RNFBConfigModule' as const;
|
|
92
|
+
|
|
93
|
+
class FirebaseConfigModule extends FirebaseModule<typeof nativeModuleName> {
|
|
94
|
+
private _settings: ConfigSettingsStateInternal;
|
|
95
|
+
private _lastFetchTime: number;
|
|
96
|
+
private _lastFetchStatus: FetchStatus;
|
|
97
|
+
private _values: Record<string, StoredConfigValueInternal>;
|
|
98
|
+
private _configUpdateListenerCount: number;
|
|
99
|
+
private _nativeMutationQueue: Promise<void>;
|
|
100
|
+
|
|
101
|
+
constructor(
|
|
102
|
+
app: ReactNativeFirebase.FirebaseAppBase,
|
|
103
|
+
config: ModuleConfig,
|
|
104
|
+
customUrlOrRegion?: string | null,
|
|
105
|
+
) {
|
|
106
|
+
super(app, config, customUrlOrRegion);
|
|
107
|
+
this._settings = {
|
|
108
|
+
// defaults to 1 minute.
|
|
109
|
+
fetchTimeoutMillis: 60000,
|
|
110
|
+
// defaults to 12 hours.
|
|
111
|
+
minimumFetchIntervalMillis: 43200000,
|
|
112
|
+
};
|
|
113
|
+
this._lastFetchTime = -1;
|
|
114
|
+
this._lastFetchStatus = 'no_fetch_yet';
|
|
115
|
+
this._values = {};
|
|
116
|
+
this._configUpdateListenerCount = 0;
|
|
117
|
+
this._nativeMutationQueue = Promise.resolve();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
get defaultConfig(): ConfigDefaults {
|
|
121
|
+
const updatedDefaultConfig: ConfigDefaults = {};
|
|
122
|
+
Object.keys(this._values).forEach(key => {
|
|
123
|
+
// Need to make it an object with key and literal value. Not `Value` instance.
|
|
124
|
+
const configValue = this._values[key];
|
|
125
|
+
if (configValue) {
|
|
126
|
+
updatedDefaultConfig[key] = configValue.value;
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
return updatedDefaultConfig;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
set defaultConfig(defaults: ConfigDefaults) {
|
|
134
|
+
if (!isObject(defaults)) {
|
|
135
|
+
throw new Error("firebase.remoteConfig().defaultConfig: 'defaults' must be an object.");
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// To make Firebase web v9 API compatible, we update the config first so it immediately
|
|
139
|
+
// updates defaults on the instance. We then pass to underlying SDK to update. We do this because
|
|
140
|
+
// there is no way to "await" a setter.
|
|
141
|
+
const nonDefaultValues = Object.fromEntries(
|
|
142
|
+
Object.entries(this._values).filter(([, configValue]) => configValue?.source !== 'default'),
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
this._values = Object.freeze({
|
|
146
|
+
...Object.fromEntries(
|
|
147
|
+
Object.entries(defaults).map(([key, value]) => [
|
|
148
|
+
key,
|
|
149
|
+
{ value, source: 'default' as const },
|
|
150
|
+
]),
|
|
151
|
+
),
|
|
152
|
+
...nonDefaultValues,
|
|
153
|
+
});
|
|
154
|
+
void this.setDefaults(defaults, true);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
get settings(): RemoteConfigSettings & { fetchTimeMillis: number } {
|
|
158
|
+
return {
|
|
159
|
+
minimumFetchIntervalMillis: this._settings.minimumFetchIntervalMillis,
|
|
160
|
+
fetchTimeoutMillis: this._settings.fetchTimeoutMillis,
|
|
161
|
+
fetchTimeMillis: this._settings.fetchTimeoutMillis,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
set settings(settings: ConfigSettings | RemoteConfigSettings) {
|
|
166
|
+
// To make Firebase web v9 API compatible, we update the settings first so it immediately
|
|
167
|
+
// updates settings on the instance. We then pass to underlying SDK to update. We do this because
|
|
168
|
+
// there is no way to "await" a setter. We can't delegate to `setConfigSettings()` as it is setup
|
|
169
|
+
// for native.
|
|
170
|
+
this._settings = {
|
|
171
|
+
minimumFetchIntervalMillis:
|
|
172
|
+
settings.minimumFetchIntervalMillis ?? this._settings.minimumFetchIntervalMillis,
|
|
173
|
+
fetchTimeoutMillis:
|
|
174
|
+
('fetchTimeoutMillis' in settings ? settings.fetchTimeoutMillis : undefined) ??
|
|
175
|
+
('fetchTimeMillis' in settings ? settings.fetchTimeMillis : undefined) ??
|
|
176
|
+
this._settings.fetchTimeoutMillis,
|
|
177
|
+
};
|
|
178
|
+
void this.setConfigSettings(settings, true);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
getValue(key: string): ConfigValue {
|
|
182
|
+
if (!isString(key)) {
|
|
183
|
+
throw new Error("firebase.remoteConfig().getValue(): 'key' must be a string value.");
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (typeof this._values === 'undefined' || !hasOwnProperty(this._values, key)) {
|
|
187
|
+
return new RemoteConfigValue({
|
|
188
|
+
value: '',
|
|
189
|
+
source: 'static',
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const configValue = this._values[key]!;
|
|
194
|
+
return new RemoteConfigValue({ value: `${configValue.value}`, source: configValue.source });
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
getBoolean(key: string): boolean {
|
|
198
|
+
return this.getValue(key).asBoolean();
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
getNumber(key: string): number {
|
|
202
|
+
return this.getValue(key).asNumber();
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
getString(key: string): string {
|
|
206
|
+
return this.getValue(key).asString();
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
getAll(): ConfigValues {
|
|
210
|
+
const values: ConfigValues = {};
|
|
211
|
+
Object.keys(this._values).forEach(key => {
|
|
212
|
+
values[key] = this.getValue(key);
|
|
213
|
+
});
|
|
214
|
+
return values;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
get fetchTimeMillis(): number {
|
|
218
|
+
// android returns -1 if no fetch yet and iOS returns 0
|
|
219
|
+
return this._lastFetchTime;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
get lastFetchStatus(): LastFetchStatusType {
|
|
223
|
+
return this._lastFetchStatus;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Deletes all activated, fetched and defaults configs and resets all Firebase Remote Config settings.
|
|
228
|
+
* @returns {Promise<void>}
|
|
229
|
+
*/
|
|
230
|
+
reset(): Promise<void> {
|
|
231
|
+
if (isIOS) {
|
|
232
|
+
return Promise.resolve();
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return this._enqueueNativeMutation(() => this._promiseWithConstants(this.native.reset()));
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
setConfigSettings(
|
|
239
|
+
settings: ConfigSettings | RemoteConfigSettings,
|
|
240
|
+
fromSettingsSetter = false,
|
|
241
|
+
): Promise<void> {
|
|
242
|
+
const updatedSettings: {
|
|
243
|
+
fetchTimeout: number;
|
|
244
|
+
minimumFetchInterval: number;
|
|
245
|
+
} = {
|
|
246
|
+
fetchTimeout: this._settings.fetchTimeoutMillis / 1000,
|
|
247
|
+
minimumFetchInterval: this._settings.minimumFetchIntervalMillis / 1000,
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
const apiCalled = fromSettingsSetter ? 'settings' : 'setConfigSettings';
|
|
251
|
+
if (!isObject(settings)) {
|
|
252
|
+
throw new Error(`firebase.remoteConfig().${apiCalled}(*): settings must set an object.`);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (hasOwnProperty(settings, 'minimumFetchIntervalMillis')) {
|
|
256
|
+
if (!isNumber(settings.minimumFetchIntervalMillis)) {
|
|
257
|
+
throw new Error(
|
|
258
|
+
`firebase.remoteConfig().${apiCalled}(): 'settings.minimumFetchIntervalMillis' must be a number type in milliseconds.`,
|
|
259
|
+
);
|
|
260
|
+
} else {
|
|
261
|
+
updatedSettings.minimumFetchInterval = settings.minimumFetchIntervalMillis / 1000;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (hasOwnProperty(settings, 'fetchTimeMillis')) {
|
|
266
|
+
if (!isNumber(settings.fetchTimeMillis)) {
|
|
267
|
+
throw new Error(
|
|
268
|
+
`firebase.remoteConfig().${apiCalled}(): 'settings.fetchTimeMillis' must be a number type in milliseconds.`,
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
updatedSettings.fetchTimeout = settings.fetchTimeMillis / 1000;
|
|
273
|
+
} else if (hasOwnProperty(settings, 'fetchTimeoutMillis')) {
|
|
274
|
+
if (!isNumber(settings.fetchTimeoutMillis)) {
|
|
275
|
+
throw new Error(
|
|
276
|
+
`firebase.remoteConfig().${apiCalled}(): 'settings.fetchTimeoutMillis' must be a number type in milliseconds.`,
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
updatedSettings.fetchTimeout = settings.fetchTimeoutMillis / 1000;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const nextSettings = {
|
|
284
|
+
fetchTimeoutMillis: updatedSettings.fetchTimeout * 1000,
|
|
285
|
+
minimumFetchIntervalMillis: updatedSettings.minimumFetchInterval * 1000,
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
// Keep JS reads in sync immediately. Native can report stale settings constants
|
|
289
|
+
// for this call because native setConfigSettingsAsync completes after the bridge resolves.
|
|
290
|
+
this._settings = nextSettings;
|
|
291
|
+
|
|
292
|
+
return this._enqueueNativeMutation(() =>
|
|
293
|
+
this.native.setConfigSettings(updatedSettings).then(({ result, constants }) => {
|
|
294
|
+
// Preserve the eagerly computed settings above and only refresh the rest of the cache.
|
|
295
|
+
this._updateFromConstants({
|
|
296
|
+
...constants,
|
|
297
|
+
fetchTimeout: undefined,
|
|
298
|
+
minimumFetchInterval: undefined,
|
|
299
|
+
});
|
|
300
|
+
return result;
|
|
301
|
+
}),
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Activates the Fetched RemoteConfig, so that the fetched key-values take effect.
|
|
307
|
+
* @returns {Promise<boolean>}
|
|
308
|
+
*/
|
|
309
|
+
activate(): Promise<boolean> {
|
|
310
|
+
return this._promiseWithConstants(this.native.activate());
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Fetches parameter values for your app.
|
|
315
|
+
*
|
|
316
|
+
* @param expirationDurationSeconds
|
|
317
|
+
* @returns {Promise}
|
|
318
|
+
*/
|
|
319
|
+
fetch(expirationDurationSeconds?: number): Promise<void> {
|
|
320
|
+
if (!isUndefined(expirationDurationSeconds) && !isNumber(expirationDurationSeconds)) {
|
|
321
|
+
throw new Error(
|
|
322
|
+
"firebase.remoteConfig().fetch(): 'expirationDurationSeconds' must be a number value.",
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
return this._promiseWithConstants(
|
|
327
|
+
this.native.fetch(expirationDurationSeconds !== undefined ? expirationDurationSeconds : -1),
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
fetchAndActivate(): Promise<boolean> {
|
|
332
|
+
return this._promiseWithConstants(this.native.fetchAndActivate());
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
ensureInitialized(): Promise<void> {
|
|
336
|
+
return this._promiseWithConstants(this.native.ensureInitialized());
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Sets defaults.
|
|
341
|
+
*
|
|
342
|
+
* @param defaults
|
|
343
|
+
*/
|
|
344
|
+
setDefaults(defaults: ConfigDefaults, fromDefaultConfigSetter = false): Promise<null> {
|
|
345
|
+
const apiCalled = fromDefaultConfigSetter ? 'defaultConfig' : 'setDefaults';
|
|
346
|
+
if (!isObject(defaults)) {
|
|
347
|
+
throw new Error(`firebase.remoteConfig().${apiCalled}(): 'defaults' must be an object.`);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
return this._enqueueNativeMutation(() =>
|
|
351
|
+
this._promiseWithConstants(this.native.setDefaults(defaults)),
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Sets defaults based on resource.
|
|
357
|
+
* @param resourceName
|
|
358
|
+
*/
|
|
359
|
+
setDefaultsFromResource(resourceName: string): Promise<null> {
|
|
360
|
+
if (!isString(resourceName)) {
|
|
361
|
+
throw new Error(
|
|
362
|
+
"firebase.remoteConfig().setDefaultsFromResource(): 'resourceName' must be a string value.",
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
return this._enqueueNativeMutation(() =>
|
|
367
|
+
this._promiseWithConstants(this.native.setDefaultsFromResource(resourceName)),
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Registers an observer to changes in the configuration.
|
|
373
|
+
*
|
|
374
|
+
* @param observer - The observer to be notified of config updates.
|
|
375
|
+
* @returns An unsubscribe function to remove the listener.
|
|
376
|
+
*/
|
|
377
|
+
onConfigUpdate(observer: ConfigUpdateObserver): () => void {
|
|
378
|
+
if (!isObject(observer) || !isFunction(observer.next) || !isFunction(observer.error)) {
|
|
379
|
+
throw new Error("'observer' expected an object with 'next' and 'error' functions.");
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// We maintaine our pre-web-support native interface but bend it to match
|
|
383
|
+
// the official JS SDK API by assuming the callback is an Observer, and sending it a ConfigUpdate
|
|
384
|
+
// compatible parameter that implements the `getUpdatedKeys` method
|
|
385
|
+
let unsubscribed = false;
|
|
386
|
+
const subscription = this.emitter.addListener(
|
|
387
|
+
this.eventNameForApp('on_config_updated'),
|
|
388
|
+
(event: RemoteConfigUpdateSuccessEventInternal | RemoteConfigUpdateErrorEventInternal) => {
|
|
389
|
+
if (isSuccessEvent(event)) {
|
|
390
|
+
observer.next(toConfigUpdate(event.updatedKeys));
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
observer.error(toNativeFirebaseError(event));
|
|
395
|
+
},
|
|
396
|
+
);
|
|
397
|
+
|
|
398
|
+
if (this._configUpdateListenerCount === 0) {
|
|
399
|
+
this.native.onConfigUpdated();
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
this._configUpdateListenerCount++;
|
|
403
|
+
|
|
404
|
+
return () => {
|
|
405
|
+
if (unsubscribed) {
|
|
406
|
+
// there is no harm in calling this multiple times to unsubscribe,
|
|
407
|
+
// but anything after the first call is a no-op
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
unsubscribed = true;
|
|
412
|
+
subscription.remove();
|
|
413
|
+
this._configUpdateListenerCount--;
|
|
414
|
+
|
|
415
|
+
if (this._configUpdateListenerCount === 0) {
|
|
416
|
+
this.native.removeConfigUpdateRegistration();
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Registers a listener to changes in the configuration.
|
|
423
|
+
*
|
|
424
|
+
* @param listenerOrObserver - function called on config change
|
|
425
|
+
* @returns unsubscribe listener
|
|
426
|
+
* @deprecated use official firebase-js-sdk onConfigUpdate now that web supports realtime
|
|
427
|
+
*/
|
|
428
|
+
onConfigUpdated(listenerOrObserver: unknown): () => void {
|
|
429
|
+
const listener = parseListenerOrObserver(
|
|
430
|
+
listenerOrObserver as FirebaseRemoteConfigTypes.CallbackOrObserver<FirebaseRemoteConfigTypes.OnConfigUpdatedListenerCallback>,
|
|
431
|
+
) as (event?: { updatedKeys: string[] }, error?: RemoteConfigUpdateErrorInternal) => void;
|
|
432
|
+
let unsubscribed = false;
|
|
433
|
+
const subscription = this.emitter.addListener(
|
|
434
|
+
this.eventNameForApp('on_config_updated'),
|
|
435
|
+
(event: RemoteConfigUpdateSuccessEventInternal | RemoteConfigUpdateErrorEventInternal) => {
|
|
436
|
+
if (isSuccessEvent(event)) {
|
|
437
|
+
listener({ updatedKeys: event.updatedKeys }, undefined);
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
listener(undefined, {
|
|
442
|
+
code: event.code,
|
|
443
|
+
message: event.message,
|
|
444
|
+
nativeErrorMessage: event.nativeErrorMessage,
|
|
445
|
+
});
|
|
446
|
+
},
|
|
447
|
+
);
|
|
448
|
+
|
|
449
|
+
if (this._configUpdateListenerCount === 0) {
|
|
450
|
+
this.native.onConfigUpdated();
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
this._configUpdateListenerCount++;
|
|
454
|
+
|
|
455
|
+
return () => {
|
|
456
|
+
if (unsubscribed) {
|
|
457
|
+
// there is no harm in calling this multiple times to unsubscribe,
|
|
458
|
+
// but anything after the first call is a no-op
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
unsubscribed = true;
|
|
463
|
+
subscription.remove();
|
|
464
|
+
this._configUpdateListenerCount--;
|
|
465
|
+
|
|
466
|
+
if (this._configUpdateListenerCount === 0) {
|
|
467
|
+
this.native.removeConfigUpdateRegistration();
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
private _updateFromConstants(constants: NativeRemoteConfigConstants): void {
|
|
473
|
+
// Wrapped this as we update using sync getters initially for `defaultConfig` & `settings`
|
|
474
|
+
if (constants.lastFetchTime !== undefined) {
|
|
475
|
+
this._lastFetchTime = constants.lastFetchTime;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
// Wrapped this as we update using sync getters initially for `defaultConfig` & `settings`
|
|
479
|
+
if (constants.lastFetchStatus !== undefined) {
|
|
480
|
+
this._lastFetchStatus = constants.lastFetchStatus;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
if (constants.fetchTimeout !== undefined && constants.minimumFetchInterval !== undefined) {
|
|
484
|
+
this._settings = {
|
|
485
|
+
fetchTimeoutMillis: constants.fetchTimeout * 1000,
|
|
486
|
+
minimumFetchIntervalMillis: constants.minimumFetchInterval * 1000,
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
if (constants.values !== undefined) {
|
|
491
|
+
this._values = Object.freeze(constants.values);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
private _promiseWithConstants<T>(promise: Promise<NativeRemoteConfigResult<T>>): Promise<T> {
|
|
496
|
+
return promise.then(({ result, constants }) => {
|
|
497
|
+
this._updateFromConstants(constants);
|
|
498
|
+
return result;
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
private _enqueueNativeMutation<T>(task: () => Promise<T>): Promise<T> {
|
|
503
|
+
// Some callers (like property setters) discard the returned promise; serialize all mutations
|
|
504
|
+
// so later writes like reset() cannot be overwritten by an earlier async completion.
|
|
505
|
+
const next = this._nativeMutationQueue.then(task, task);
|
|
506
|
+
this._nativeMutationQueue = next.then(
|
|
507
|
+
() => undefined,
|
|
508
|
+
() => undefined,
|
|
509
|
+
);
|
|
510
|
+
return next;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// import { SDK_VERSION } from '@react-native-firebase/remote-config';
|
|
515
|
+
export const SDK_VERSION = version;
|
|
516
|
+
|
|
517
|
+
const remoteConfigNamespace = createModuleNamespace({
|
|
518
|
+
statics,
|
|
519
|
+
version,
|
|
520
|
+
namespace,
|
|
521
|
+
nativeModuleName,
|
|
522
|
+
nativeEvents: ['on_config_updated'],
|
|
523
|
+
hasMultiAppSupport: true,
|
|
524
|
+
hasCustomUrlOrRegionSupport: false,
|
|
525
|
+
ModuleClass: FirebaseConfigModule,
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
type RemoteConfigNamespace = ReactNativeFirebase.FirebaseModuleWithStaticsAndApp<
|
|
529
|
+
FirebaseRemoteConfigTypes.Module,
|
|
530
|
+
FirebaseRemoteConfigTypes.Statics
|
|
531
|
+
> & {
|
|
532
|
+
firebase: ReactNativeFirebase.Module;
|
|
533
|
+
app(name?: string): ReactNativeFirebase.FirebaseApp;
|
|
534
|
+
};
|
|
535
|
+
|
|
536
|
+
// import remoteConfig from '@react-native-firebase/remote-config';
|
|
537
|
+
// remoteConfig().X(...);
|
|
538
|
+
export default remoteConfigNamespace as unknown as RemoteConfigNamespace;
|
|
539
|
+
|
|
540
|
+
// import remoteConfig, { firebase } from '@react-native-firebase/remote-config';
|
|
541
|
+
// remoteConfig().X(...);
|
|
542
|
+
// firebase.remoteConfig().X(...);
|
|
543
|
+
export const firebase =
|
|
544
|
+
getFirebaseRoot() as unknown as ReactNativeFirebase.FirebaseNamespacedExport<
|
|
545
|
+
'remoteConfig',
|
|
546
|
+
FirebaseRemoteConfigTypes.Module,
|
|
547
|
+
FirebaseRemoteConfigTypes.Statics,
|
|
548
|
+
false
|
|
549
|
+
>;
|
|
550
|
+
|
|
551
|
+
// Register the interop module for non-native platforms.
|
|
552
|
+
setReactNativeModule(nativeModuleName, fallBackModule as unknown as Record<string, unknown>);
|
package/lib/polyfills.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2024 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
const { polyfillGlobal } = require('react-native/Libraries/Utilities/PolyfillFunctions') as {
|
|
18
|
+
polyfillGlobal: <T>(name: string, getValue: () => T) => void;
|
|
19
|
+
};
|
|
20
|
+
const { fetch, Headers, Request, Response } = require('react-native-fetch-api') as {
|
|
21
|
+
fetch: typeof globalThis.fetch;
|
|
22
|
+
Headers: typeof globalThis.Headers;
|
|
23
|
+
Request: typeof globalThis.Request;
|
|
24
|
+
Response: typeof globalThis.Response;
|
|
25
|
+
};
|
|
26
|
+
const { ReadableStream } = require('web-streams-polyfill/dist/ponyfill') as {
|
|
27
|
+
ReadableStream: typeof globalThis.ReadableStream;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
type PolyfilledFetch = typeof fetch;
|
|
31
|
+
type PolyfilledFetchArgs = Parameters<PolyfilledFetch>;
|
|
32
|
+
type StreamingFetchInit = NonNullable<PolyfilledFetchArgs[1]> & {
|
|
33
|
+
reactNative?: {
|
|
34
|
+
textStreaming?: boolean;
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const fetchWithTextStreaming: PolyfilledFetch = (...args) => {
|
|
39
|
+
const [input, init] = args;
|
|
40
|
+
const nextInit: StreamingFetchInit = {
|
|
41
|
+
...(init as StreamingFetchInit | undefined),
|
|
42
|
+
reactNative: { textStreaming: true },
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
return fetch(input, nextInit);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
polyfillGlobal('fetch', (): PolyfilledFetch => fetchWithTextStreaming);
|
|
49
|
+
polyfillGlobal('Headers', () => Headers);
|
|
50
|
+
polyfillGlobal('Request', () => Request);
|
|
51
|
+
polyfillGlobal('Response', () => Response);
|
|
52
|
+
polyfillGlobal('ReadableStream', () => ReadableStream);
|
|
53
|
+
|
|
54
|
+
import 'text-encoding';
|
package/lib/statics.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ValueSource as ValueSourceType } from './types/remote-config';
|
|
2
|
+
|
|
3
|
+
interface LastFetchStatusStatics {
|
|
4
|
+
SUCCESS: 'success';
|
|
5
|
+
FAILURE: 'failure';
|
|
6
|
+
THROTTLED: 'throttled';
|
|
7
|
+
NO_FETCH_YET: 'no_fetch_yet';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface ValueSourceStatics {
|
|
11
|
+
REMOTE: ValueSourceType;
|
|
12
|
+
DEFAULT: ValueSourceType;
|
|
13
|
+
STATIC: ValueSourceType;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const LastFetchStatus = {
|
|
17
|
+
SUCCESS: 'success',
|
|
18
|
+
FAILURE: 'failure',
|
|
19
|
+
THROTTLED: 'throttled',
|
|
20
|
+
NO_FETCH_YET: 'no_fetch_yet',
|
|
21
|
+
} as const satisfies LastFetchStatusStatics;
|
|
22
|
+
|
|
23
|
+
export const ValueSource = {
|
|
24
|
+
REMOTE: 'remote',
|
|
25
|
+
DEFAULT: 'default',
|
|
26
|
+
STATIC: 'static',
|
|
27
|
+
} as const satisfies ValueSourceStatics;
|