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-01-05 17:20:02 +00:00
|
|
|
import type App from '../core/firebase-app';
|
2017-12-04 12:07:41 +00:00
|
|
|
|
|
|
|
type NativeValue = {
|
|
|
|
stringValue?: string,
|
|
|
|
numberValue?: number,
|
|
|
|
dataValue?: Object,
|
|
|
|
boolValue?: boolean,
|
|
|
|
source: 'remoteConfigSourceRemote' | 'remoteConfigSourceDefault' | ' remoteConfigSourceStatic',
|
|
|
|
}
|
|
|
|
|
2018-01-03 20:00:38 +00:00
|
|
|
export const MODULE_NAME = 'RNFirebaseRemoteConfig';
|
|
|
|
export const NAMESPACE = 'config';
|
|
|
|
|
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,
|
|
|
|
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
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
_nativeValueToJS(nativeValue: NativeValue) {
|
2017-05-23 13:08:59 +00:00
|
|
|
return {
|
|
|
|
source: nativeValue.source,
|
|
|
|
val() {
|
|
|
|
if (nativeValue.boolValue !== null && (nativeValue.stringValue === 'true' || nativeValue.stringValue === 'false' || nativeValue.stringValue === null)) return nativeValue.boolValue;
|
2018-01-05 17:20:02 +00:00
|
|
|
if (nativeValue.numberValue !== null && nativeValue.numberValue !== undefined && (nativeValue.stringValue == null || nativeValue.stringValue === '' || nativeValue.numberValue.toString() === nativeValue.stringValue)) return nativeValue.numberValue;
|
2017-05-23 13:08:59 +00:00
|
|
|
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() {
|
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>}:
|
|
|
|
*/
|
2017-05-23 13:08:59 +00:00
|
|
|
fetch(expiration?: number) {
|
|
|
|
if (expiration !== undefined) {
|
2017-12-22 15:24:31 +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.
|
|
|
|
*/
|
|
|
|
activateFetched() {
|
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)
|
|
|
|
* }
|
|
|
|
*/
|
2017-05-23 13:08:59 +00:00
|
|
|
getValue(key: String) {
|
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)
|
|
|
|
* }
|
|
|
|
*/
|
2017-05-23 13:08:59 +00:00
|
|
|
getValues(keys: Array<String>) {
|
2018-01-05 17:20:02 +00:00
|
|
|
return getNativeModule(this)
|
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) {
|
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.
|
|
|
|
*/
|
|
|
|
setDefaults(defaults: Object) {
|
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
|
|
|
*/
|
2017-05-23 13:53:19 +00:00
|
|
|
setDefaultsFromResource(resource: String | number) {
|
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 = {};
|