2017-05-19 07:49:49 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
2017-12-04 12:07:41 +00:00
|
|
|
* Remote Config representation wrapper
|
2017-05-19 07:49:49 +00:00
|
|
|
*/
|
2017-12-22 15:24:31 +00:00
|
|
|
import { getLogger } from '../../utils/log';
|
2018-01-05 17:20:02 +00:00
|
|
|
import ModuleBase from '../../utils/ModuleBase';
|
|
|
|
import { getNativeModule } from '../../utils/native';
|
2017-05-19 07:49:49 +00:00
|
|
|
|
2018-02-14 13:00:19 +00:00
|
|
|
import type App from '../core/app';
|
2017-12-04 12:07:41 +00:00
|
|
|
|
|
|
|
type NativeValue = {
|
|
|
|
stringValue?: string,
|
|
|
|
numberValue?: number,
|
|
|
|
dataValue?: Object,
|
|
|
|
boolValue?: boolean,
|
2018-01-25 18:25:39 +00:00
|
|
|
source:
|
|
|
|
| 'remoteConfigSourceRemote'
|
|
|
|
| 'remoteConfigSourceDefault'
|
|
|
|
| ' remoteConfigSourceStatic',
|
|
|
|
};
|
2017-12-04 12:07:41 +00:00
|
|
|
|
2018-01-03 20:00:38 +00:00
|
|
|
export const MODULE_NAME = 'RNFirebaseRemoteConfig';
|
|
|
|
export const NAMESPACE = 'config';
|
|
|
|
|
2018-06-19 06:15:51 +00:00
|
|
|
type ConfigSnapshot = {
|
|
|
|
source: string,
|
|
|
|
val(): any,
|
|
|
|
};
|
|
|
|
|
2017-05-19 07:49:49 +00:00
|
|
|
/**
|
|
|
|
* @class Config
|
|
|
|
*/
|
2017-06-30 16:23:32 +00:00
|
|
|
export default class RemoteConfig extends ModuleBase {
|
2017-12-04 12:07:41 +00:00
|
|
|
_developerModeEnabled: boolean;
|
|
|
|
|
2018-01-05 17:20:02 +00:00
|
|
|
constructor(app: App) {
|
|
|
|
super(app, {
|
2018-01-03 20:00:38 +00:00
|
|
|
moduleName: MODULE_NAME,
|
2018-01-09 17:31:00 +00:00
|
|
|
multiApp: false,
|
2017-11-21 23:37:05 +00:00
|
|
|
hasShards: false,
|
2018-01-03 20:00:38 +00:00
|
|
|
namespace: NAMESPACE,
|
|
|
|
});
|
2017-12-04 12:07:41 +00:00
|
|
|
this._developerModeEnabled = false;
|
2017-05-19 07:49:49 +00:00
|
|
|
}
|
|
|
|
|
2017-05-23 13:08:59 +00:00
|
|
|
/**
|
|
|
|
* Converts a native map to single JS value
|
|
|
|
* @param nativeValue
|
|
|
|
* @returns {*}
|
|
|
|
* @private
|
|
|
|
*/
|
2018-06-19 06:15:51 +00:00
|
|
|
_nativeValueToJS(nativeValue: NativeValue): ConfigSnapshot {
|
2017-05-23 13:08:59 +00:00
|
|
|
return {
|
|
|
|
source: nativeValue.source,
|
|
|
|
val() {
|
2018-01-25 18:25:39 +00:00
|
|
|
if (
|
|
|
|
nativeValue.boolValue !== null &&
|
|
|
|
(nativeValue.stringValue === 'true' ||
|
|
|
|
nativeValue.stringValue === 'false' ||
|
|
|
|
nativeValue.stringValue === null)
|
|
|
|
)
|
|
|
|
return nativeValue.boolValue;
|
|
|
|
if (
|
|
|
|
nativeValue.numberValue !== null &&
|
|
|
|
nativeValue.numberValue !== undefined &&
|
|
|
|
(nativeValue.stringValue == null ||
|
|
|
|
nativeValue.stringValue === '' ||
|
|
|
|
nativeValue.numberValue.toString() === nativeValue.stringValue)
|
|
|
|
)
|
|
|
|
return nativeValue.numberValue;
|
|
|
|
if (
|
|
|
|
nativeValue.dataValue !== nativeValue.stringValue &&
|
|
|
|
(nativeValue.stringValue == null || nativeValue.stringValue === '')
|
|
|
|
)
|
|
|
|
return nativeValue.dataValue;
|
2017-05-23 13:08:59 +00:00
|
|
|
return nativeValue.stringValue;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-05-19 07:49:49 +00:00
|
|
|
/**
|
2017-05-23 13:53:19 +00:00
|
|
|
* Enable Remote Config developer mode to allow for frequent refreshes of the cache
|
|
|
|
*/
|
2017-05-19 07:49:49 +00:00
|
|
|
enableDeveloperMode() {
|
2017-12-04 12:07:41 +00:00
|
|
|
if (!this._developerModeEnabled) {
|
2017-12-22 15:24:31 +00:00
|
|
|
getLogger(this).debug('Enabled developer mode');
|
2018-01-05 17:20:02 +00:00
|
|
|
getNativeModule(this).enableDeveloperMode();
|
2017-12-04 12:07:41 +00:00
|
|
|
this._developerModeEnabled = true;
|
2017-05-19 07:49:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches Remote Config data
|
|
|
|
* Call activateFetched to make fetched data available in app
|
|
|
|
* @returns {*|Promise.<String>}:
|
|
|
|
*/
|
2018-06-19 06:15:51 +00:00
|
|
|
fetch(expiration?: number): Promise<string> {
|
2017-05-23 13:08:59 +00:00
|
|
|
if (expiration !== undefined) {
|
2018-01-25 18:25:39 +00:00
|
|
|
getLogger(this).debug(
|
|
|
|
`Fetching remote config data with expiration ${expiration.toString()}`
|
|
|
|
);
|
2018-01-05 17:20:02 +00:00
|
|
|
return getNativeModule(this).fetchWithExpirationDuration(expiration);
|
2017-05-23 13:08:59 +00:00
|
|
|
}
|
2017-12-22 15:24:31 +00:00
|
|
|
getLogger(this).debug('Fetching remote config data');
|
2018-01-05 17:20:02 +00:00
|
|
|
return getNativeModule(this).fetch();
|
2017-05-19 07:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies Fetched Config data to the Active Config
|
|
|
|
* @returns {*|Promise.<Bool>}
|
|
|
|
* resolves if there was a Fetched Config, and it was activated,
|
|
|
|
* rejects if no Fetched Config was found, or the Fetched Config was already activated.
|
|
|
|
*/
|
2018-06-19 06:15:51 +00:00
|
|
|
activateFetched(): Promise<boolean> {
|
2017-12-22 15:24:31 +00:00
|
|
|
getLogger(this).debug('Activating remote config');
|
2018-01-05 17:20:02 +00:00
|
|
|
return getNativeModule(this).activateFetched();
|
2017-05-19 07:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the config value of the default namespace.
|
|
|
|
* @param key: Config key
|
|
|
|
* @returns {*|Promise.<Object>}, will always resolve
|
|
|
|
* Object looks like
|
|
|
|
* {
|
|
|
|
* "stringValue" : stringValue,
|
|
|
|
* "numberValue" : numberValue,
|
|
|
|
* "dataValue" : dataValue,
|
|
|
|
* "boolValue" : boolValue,
|
|
|
|
* "source" : OneOf<String>(remoteConfigSourceRemote|remoteConfigSourceDefault|remoteConfigSourceStatic)
|
|
|
|
* }
|
|
|
|
*/
|
2018-06-19 06:15:51 +00:00
|
|
|
getValue(key: string): Promise<ConfigSnapshot> {
|
2018-01-05 17:20:02 +00:00
|
|
|
return getNativeModule(this)
|
2017-05-23 13:08:59 +00:00
|
|
|
.getValue(key || '')
|
|
|
|
.then(this._nativeValueToJS);
|
2017-05-19 07:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the config value of the default namespace.
|
2017-05-23 13:08:59 +00:00
|
|
|
* @param keys: Config key
|
2017-05-19 07:49:49 +00:00
|
|
|
* @returns {*|Promise.<Object>}, will always resolve.
|
|
|
|
* Result will be a dictionary of key and config objects
|
|
|
|
* Object looks like
|
|
|
|
* {
|
|
|
|
* "stringValue" : stringValue,
|
|
|
|
* "numberValue" : numberValue,
|
|
|
|
* "dataValue" : dataValue,
|
|
|
|
* "boolValue" : boolValue,
|
|
|
|
* "source" : OneOf<String>(remoteConfigSourceRemote|remoteConfigSourceDefault|remoteConfigSourceStatic)
|
|
|
|
* }
|
|
|
|
*/
|
2018-06-19 06:15:51 +00:00
|
|
|
getValues(keys: Array<string>): Promise<{ [string]: ConfigSnapshot }> {
|
2018-01-05 17:20:02 +00:00
|
|
|
return getNativeModule(this)
|
2017-05-23 13:08:59 +00:00
|
|
|
.getValues(keys || [])
|
2018-01-25 18:25:39 +00:00
|
|
|
.then(nativeValues => {
|
2018-03-09 04:16:40 +00:00
|
|
|
const values: { [string]: Object } = {};
|
2017-05-23 13:08:59 +00:00
|
|
|
for (let i = 0, len = keys.length; i < len; i++) {
|
|
|
|
values[keys[i]] = this._nativeValueToJS(nativeValues[i]);
|
|
|
|
}
|
|
|
|
return values;
|
|
|
|
});
|
2017-05-19 07:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the set of parameter keys that start with the given prefix, from the default namespace
|
|
|
|
* @param prefix: The key prefix to look for. If prefix is nil or empty, returns all the keys.
|
|
|
|
* @returns {*|Promise.<Array<String>>}
|
|
|
|
*/
|
2018-06-19 06:15:51 +00:00
|
|
|
getKeysByPrefix(prefix?: string): Promise<string[]> {
|
2018-01-05 17:20:02 +00:00
|
|
|
return getNativeModule(this).getKeysByPrefix(prefix);
|
2017-05-19 07:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets config defaults for parameter keys and values in the default namespace config.
|
|
|
|
* @param defaults: A dictionary mapping a String key to a Object values.
|
|
|
|
*/
|
2018-06-19 06:15:51 +00:00
|
|
|
setDefaults(defaults: Object): void {
|
2018-01-05 17:20:02 +00:00
|
|
|
getNativeModule(this).setDefaults(defaults);
|
2017-05-19 07:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets default configs from plist for default namespace;
|
2017-05-23 13:53:19 +00:00
|
|
|
* @param resource: The plist file name or resource ID
|
2017-05-19 07:49:49 +00:00
|
|
|
*/
|
2018-06-19 06:15:51 +00:00
|
|
|
setDefaultsFromResource(resource: string | number): void {
|
2018-01-05 17:20:02 +00:00
|
|
|
getNativeModule(this).setDefaultsFromResource(resource);
|
2017-05-19 07:49:49 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-23 17:29:40 +00:00
|
|
|
|
|
|
|
export const statics = {};
|