2017-05-19 07:49:49 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
|
|
|
*/
|
2017-06-30 16:23:32 +00:00
|
|
|
import ModuleBase from './../../utils/ModuleBase';
|
2017-05-19 07:49:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @class Config
|
|
|
|
*/
|
2017-06-30 16:23:32 +00:00
|
|
|
export default class RemoteConfig extends ModuleBase {
|
|
|
|
constructor(firebaseApp: Object, options: Object = {}) {
|
2017-07-12 14:49:33 +00:00
|
|
|
super(firebaseApp, options, 'RemoteConfig');
|
2017-05-19 07:49:49 +00:00
|
|
|
this.developerModeEnabled = false;
|
|
|
|
}
|
|
|
|
|
2017-05-23 13:08:59 +00:00
|
|
|
/**
|
|
|
|
* Converts a native map to single JS value
|
|
|
|
* @param nativeValue
|
|
|
|
* @returns {*}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_nativeValueToJS(nativeValue) {
|
|
|
|
return {
|
|
|
|
source: nativeValue.source,
|
|
|
|
val() {
|
|
|
|
if (nativeValue.boolValue !== null && (nativeValue.stringValue === 'true' || nativeValue.stringValue === 'false' || nativeValue.stringValue === null)) return nativeValue.boolValue;
|
|
|
|
if (nativeValue.numberValue !== null && (nativeValue.stringValue == null || nativeValue.stringValue === '' || `${nativeValue.numberValue}` === nativeValue.stringValue)) return nativeValue.numberValue;
|
|
|
|
if (nativeValue.dataValue !== nativeValue.stringValue && (nativeValue.stringValue == null || nativeValue.stringValue === '')) return nativeValue.dataValue;
|
|
|
|
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() {
|
|
|
|
if (!this.developerModeEnabled) {
|
|
|
|
this.log.debug('Enabled developer mode');
|
2017-06-30 16:23:32 +00:00
|
|
|
this._native.enableDeveloperMode();
|
2017-05-23 13:08:59 +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>}:
|
|
|
|
*/
|
2017-05-23 13:08:59 +00:00
|
|
|
fetch(expiration?: number) {
|
|
|
|
if (expiration !== undefined) {
|
|
|
|
this.log.debug(`Fetching remote config data with expiration ${expiration.toString()}`);
|
2017-06-30 16:23:32 +00:00
|
|
|
return this._native.fetchWithExpirationDuration(expiration);
|
2017-05-23 13:08:59 +00:00
|
|
|
}
|
2017-05-19 07:49:49 +00:00
|
|
|
this.log.debug('Fetching remote config data');
|
2017-06-30 16:23:32 +00:00
|
|
|
return this._native.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.
|
|
|
|
*/
|
|
|
|
activateFetched() {
|
|
|
|
this.log.debug('Activating remote config');
|
2017-06-30 16:23:32 +00:00
|
|
|
return this._native.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)
|
|
|
|
* }
|
|
|
|
*/
|
2017-05-23 13:08:59 +00:00
|
|
|
getValue(key: String) {
|
2017-06-30 16:23:32 +00:00
|
|
|
return this._native
|
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)
|
|
|
|
* }
|
|
|
|
*/
|
2017-05-23 13:08:59 +00:00
|
|
|
getValues(keys: Array<String>) {
|
2017-06-30 16:23:32 +00:00
|
|
|
return this._native
|
2017-05-23 13:08:59 +00:00
|
|
|
.getValues(keys || [])
|
|
|
|
.then((nativeValues) => {
|
2017-06-30 16:23:32 +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>>}
|
|
|
|
*/
|
2017-05-23 13:08:59 +00:00
|
|
|
getKeysByPrefix(prefix?: String) {
|
2017-06-30 16:23:32 +00:00
|
|
|
return this._native.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.
|
|
|
|
*/
|
|
|
|
setDefaults(defaults: Object) {
|
2017-06-30 16:23:32 +00:00
|
|
|
this._native.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
|
|
|
*/
|
2017-05-23 13:53:19 +00:00
|
|
|
setDefaultsFromResource(resource: String | number) {
|
2017-06-30 16:23:32 +00:00
|
|
|
this._native.setDefaultsFromResource(resource);
|
2017-05-19 07:49:49 +00:00
|
|
|
}
|
|
|
|
}
|