[config] Standardize ios & android

This commit is contained in:
Elliot Hesp 2017-05-23 14:08:59 +01:00
parent 037de1f77b
commit 25e8bb6d5a
4 changed files with 81 additions and 63 deletions

View File

@ -63,8 +63,8 @@ public class RNFirebaseRemoteConfig extends ReactContextBaseJavaModule {
} }
@ReactMethod @ReactMethod
public void fetchWithExpirationDuration(long expirationDuration, final Promise promise) { public void fetchWithExpirationDuration(double expirationDuration, final Promise promise) {
fetchInternal(promise, true, expirationDuration); fetchInternal(promise, true, (long) expirationDuration);
} }
@ReactMethod @ReactMethod
@ -74,13 +74,13 @@ public class RNFirebaseRemoteConfig extends ReactContextBaseJavaModule {
} }
@ReactMethod @ReactMethod
public void configValueForKey(String key, final Promise promise) { public void getValue(String key, final Promise promise) {
FirebaseRemoteConfigValue value = FirebaseRemoteConfig.getInstance().getValue(key); FirebaseRemoteConfigValue value = FirebaseRemoteConfig.getInstance().getValue(key);
promise.resolve(convertRemoteConfigValue(value)); promise.resolve(convertRemoteConfigValue(value));
} }
@ReactMethod @ReactMethod
public void configValuesForKeys(ReadableArray keys, final Promise promise) { public void getValues(ReadableArray keys, final Promise promise) {
WritableArray array = Arguments.createArray(); WritableArray array = Arguments.createArray();
List<Object> keysList = Utils.recursivelyDeconstructReadableArray(keys); List<Object> keysList = Utils.recursivelyDeconstructReadableArray(keys);
@ -93,7 +93,7 @@ public class RNFirebaseRemoteConfig extends ReactContextBaseJavaModule {
} }
@ReactMethod @ReactMethod
public void keysWithPrefix(String prefix, final Promise promise) { public void getKeysByPrefix(String prefix, final Promise promise) {
Set<String> keys = FirebaseRemoteConfig.getInstance().getKeysByPrefix(prefix); Set<String> keys = FirebaseRemoteConfig.getInstance().getKeysByPrefix(prefix);
WritableArray array = Arguments.createArray(); WritableArray array = Arguments.createArray();

View File

@ -23,10 +23,12 @@ Any values in the defaults but not on Firebase will be untouched.
}); });
``` ```
### fetch(): `Promise<String>` ### fetch(duration?: `number`): `Promise<String>`
Fetches the remote config data from Firebase, defined in the dashboard. To have fetched data available in the active config, use Fetches the remote config data from Firebase, defined in the dashboard.
`activateFetched`. If duration is defined (seconds), data will be locally cached for this duration.
The default duration is 43200 seconds (12 hours). To force a cache refresh call the method with a duration of 0.
Thrown errors can be one of the following: Thrown errors can be one of the following:
* config/failure - Config fetch failed. * config/failure - Config fetch failed.
@ -111,6 +113,18 @@ Gets multiple values by key. Returns an object of keys with the same object retu
.catch(console.error); .catch(console.error);
``` ```
### getKeysByPrefix(prefix?: `String`): `Promise <Array<String>>`
Returns all keys as an array by a prefix. If no prefix is defined all keys are returned.
```js
firebase.config()
.getKeysByPrefix()
.then((keys) => {
});
```
## Usage ## Usage
```js ```js

View File

@ -17,29 +17,25 @@ NSString *convertFIRRemoteConfigFetchStatusToNSString(FIRRemoteConfigFetchStatus
{ {
switch(value){ switch(value){
case FIRRemoteConfigFetchStatusNoFetchYet: case FIRRemoteConfigFetchStatusNoFetchYet:
return @"remoteConfigFetchStatusNoFetchYet"; return @"config/no_fetch_yet";
case FIRRemoteConfigFetchStatusSuccess:
return @"remoteConfigFetchStatusSuccess";
case FIRRemoteConfigFetchStatusFailure: case FIRRemoteConfigFetchStatusFailure:
return @"remoteConfigFetchStatusFailure"; return @"config/failure";
case FIRRemoteConfigFetchStatusThrottled: case FIRRemoteConfigFetchStatusThrottled:
return @"remoteConfigFetchStatusThrottled"; return @"config/throttled";
default: default:
return @"remoteConfigFetchStatusFailure"; return @"config/failure";
} }
} }
NSString *convertFIRRemoteConfigSourceToNSString(FIRRemoteConfigSource value) NSString *convertFIRRemoteConfigSourceToNSString(FIRRemoteConfigSource value)
{ {
switch(value) { switch(value) {
case FIRRemoteConfigSourceRemote:
return @"remoteConfigSourceRemote";
case FIRRemoteConfigSourceDefault: case FIRRemoteConfigSourceDefault:
return @"remoteConfigSourceDefault"; return @"default";
case FIRRemoteConfigSourceStatic: case FIRRemoteConfigSourceRemote:
return @"remoteConfigSourceStatic"; return @"remote";
default: default:
return @"remoteConfigSourceStatic"; return @"static";
} }
} }
@ -112,7 +108,7 @@ RCT_EXPORT_METHOD(activateFetched:(RCTPromiseResolveBlock)resolve
resolve(@(status)); resolve(@(status));
} }
RCT_EXPORT_METHOD(configValueForKey:(NSString *)key RCT_EXPORT_METHOD(getValue:(NSString *)key
resolver:(RCTPromiseResolveBlock)resolve resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) rejecter:(RCTPromiseRejectBlock)reject)
{ {
@ -120,28 +116,28 @@ RCT_EXPORT_METHOD(configValueForKey:(NSString *)key
resolve(convertFIRRemoteConfigValueToNSDictionary(value)); resolve(convertFIRRemoteConfigValueToNSDictionary(value));
} }
RCT_EXPORT_METHOD(configValuesForKeys:(NSArray *)keys RCT_EXPORT_METHOD(getValues:(NSArray *)keys
resolver:(RCTPromiseResolveBlock)resolve resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) rejecter:(RCTPromiseRejectBlock)reject)
{ {
NSMutableDictionary *res = [[NSMutableDictionary alloc] init]; NSMutableArray *valuesArray = [[NSMutableArray alloc] init];
for (NSString *key in keys) { for (NSString *key in keys) {
FIRRemoteConfigValue *value = [self.remoteConfig configValueForKey:key]; FIRRemoteConfigValue *value = [self.remoteConfig configValueForKey:key];
res[key] = convertFIRRemoteConfigValueToNSDictionary(value); [valuesArray addObject:convertFIRRemoteConfigValueToNSDictionary(value)];
} }
resolve(res); resolve(valuesArray);
} }
RCT_EXPORT_METHOD(keysWithPrefix:(NSString *)prefix RCT_EXPORT_METHOD(getKeysByPrefix:(NSString *)prefix
resolver:(RCTPromiseResolveBlock)resolve resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) rejecter:(RCTPromiseRejectBlock)reject)
{ {
NSSet *keys = [self.remoteConfig keysWithPrefix:prefix]; NSSet *keys = [self.remoteConfig keysWithPrefix:prefix];
if (keys.count) { NSMutableArray *keysArray = [[NSMutableArray alloc] init];
resolve(keys); for (NSString *key in keys) {
} else { [keysArray addObject:key];
reject(@"no_keys_matching_prefix", @"There are no keys that match that prefix", nil);
} }
resolve(keysArray);
} }
RCT_EXPORT_METHOD(setDefaults:(NSDictionary *)defaults) RCT_EXPORT_METHOD(setDefaults:(NSDictionary *)defaults)

View File

@ -19,6 +19,24 @@ export default class RemoteConfig extends Base {
this.developerModeEnabled = false; this.developerModeEnabled = false;
} }
/**
* 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;
},
};
}
/** /**
* Enable Remote Config developer mode to allow for frequent refreshes of the cache * Enable Remote Config developer mode to allow for frequent refreshes of the cache
*/ */
@ -26,7 +44,7 @@ export default class RemoteConfig extends Base {
if (!this.developerModeEnabled) { if (!this.developerModeEnabled) {
this.log.debug('Enabled developer mode'); this.log.debug('Enabled developer mode');
FirebaseRemoteConfig.enableDeveloperMode(); FirebaseRemoteConfig.enableDeveloperMode();
this.developerModeEnabled = true this.developerModeEnabled = true;
} }
} }
@ -34,36 +52,16 @@ export default class RemoteConfig extends Base {
* Fetches Remote Config data * Fetches Remote Config data
* Call activateFetched to make fetched data available in app * Call activateFetched to make fetched data available in app
* @returns {*|Promise.<String>}: * @returns {*|Promise.<String>}:
* One of
* - remoteConfitFetchStatusSuccess
* - remoteConfitFetchStatusFailure
* - remoteConfitFetchStatusThrottled
* rejects on remoteConfitFetchStatusFailure and remoteConfitFetchStatusThrottled
* resolves on remoteConfitFetchStatusSuccess
*/ */
fetch() { fetch(expiration?: number) {
if (expiration !== undefined) {
this.log.debug(`Fetching remote config data with expiration ${expiration.toString()}`);
return FirebaseRemoteConfig.fetchWithExpirationDuration(expiration);
}
this.log.debug('Fetching remote config data'); this.log.debug('Fetching remote config data');
return FirebaseRemoteConfig.fetch(); return FirebaseRemoteConfig.fetch();
} }
/**
* Fetches Remote Config data and sets a duration that specifies how long config data lasts.
* Call activateFetched to make fetched data available
* @param expiration: Duration that defines how long fetched config data is available, in
* seconds. When the config data expires, a new fetch is required.
* @returns {*|Promise.<Bool>}
* One of
* - remoteConfitFetchStatusSuccess
* - remoteConfitFetchStatusFailure
* - remoteConfitFetchStatusThrottled
* rejects on remoteConfitFetchStatusFailure and remoteConfitFetchStatusThrottled
* resolves on remoteConfitFetchStatusSuccess
*/
fetchWithExpirationDuration(expiration: Number) {
this.log.debug(`Fetching remote config data with expiration ${expiration.toString()}`);
return FirebaseRemoteConfig.fetchWithExpirationDuration(expiration);
}
/** /**
* Applies Fetched Config data to the Active Config * Applies Fetched Config data to the Active Config
* @returns {*|Promise.<Bool>} * @returns {*|Promise.<Bool>}
@ -88,13 +86,15 @@ export default class RemoteConfig extends Base {
* "source" : OneOf<String>(remoteConfigSourceRemote|remoteConfigSourceDefault|remoteConfigSourceStatic) * "source" : OneOf<String>(remoteConfigSourceRemote|remoteConfigSourceDefault|remoteConfigSourceStatic)
* } * }
*/ */
configValueForKey(key: String) { getValue(key: String) {
return FirebaseRemoteConfig.configValueForKey(key); return FirebaseRemoteConfig
.getValue(key || '')
.then(this._nativeValueToJS);
} }
/** /**
* Gets the config value of the default namespace. * Gets the config value of the default namespace.
* @param key: Config key * @param keys: Config key
* @returns {*|Promise.<Object>}, will always resolve. * @returns {*|Promise.<Object>}, will always resolve.
* Result will be a dictionary of key and config objects * Result will be a dictionary of key and config objects
* Object looks like * Object looks like
@ -106,8 +106,16 @@ export default class RemoteConfig extends Base {
* "source" : OneOf<String>(remoteConfigSourceRemote|remoteConfigSourceDefault|remoteConfigSourceStatic) * "source" : OneOf<String>(remoteConfigSourceRemote|remoteConfigSourceDefault|remoteConfigSourceStatic)
* } * }
*/ */
configValuesForKeys(keys: Array<String>) { getValues(keys: Array<String>) {
return FirebaseRemoteConfig.configValuesForKeys(keys); return FirebaseRemoteConfig
.getValues(keys || [])
.then((nativeValues) => {
const values = {};
for (let i = 0, len = keys.length; i < len; i++) {
values[keys[i]] = this._nativeValueToJS(nativeValues[i]);
}
return values;
});
} }
/** /**
@ -115,8 +123,8 @@ export default class RemoteConfig extends Base {
* @param prefix: The key prefix to look for. If prefix is nil or empty, returns all the keys. * @param prefix: The key prefix to look for. If prefix is nil or empty, returns all the keys.
* @returns {*|Promise.<Array<String>>} * @returns {*|Promise.<Array<String>>}
*/ */
keysWithPrefix(prefix: String) { getKeysByPrefix(prefix?: String) {
return FirebaseRemoteConfig.keysWithPrefix(prefix); return FirebaseRemoteConfig.getKeysByPrefix(prefix);
} }
/** /**