[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
public void fetchWithExpirationDuration(long expirationDuration, final Promise promise) {
fetchInternal(promise, true, expirationDuration);
public void fetchWithExpirationDuration(double expirationDuration, final Promise promise) {
fetchInternal(promise, true, (long) expirationDuration);
}
@ReactMethod
@ -74,13 +74,13 @@ public class RNFirebaseRemoteConfig extends ReactContextBaseJavaModule {
}
@ReactMethod
public void configValueForKey(String key, final Promise promise) {
public void getValue(String key, final Promise promise) {
FirebaseRemoteConfigValue value = FirebaseRemoteConfig.getInstance().getValue(key);
promise.resolve(convertRemoteConfigValue(value));
}
@ReactMethod
public void configValuesForKeys(ReadableArray keys, final Promise promise) {
public void getValues(ReadableArray keys, final Promise promise) {
WritableArray array = Arguments.createArray();
List<Object> keysList = Utils.recursivelyDeconstructReadableArray(keys);
@ -93,7 +93,7 @@ public class RNFirebaseRemoteConfig extends ReactContextBaseJavaModule {
}
@ReactMethod
public void keysWithPrefix(String prefix, final Promise promise) {
public void getKeysByPrefix(String prefix, final Promise promise) {
Set<String> keys = FirebaseRemoteConfig.getInstance().getKeysByPrefix(prefix);
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
`activateFetched`.
Fetches the remote config data from Firebase, defined in the dashboard.
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:
* 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);
```
### 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
```js

View File

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

View File

@ -19,6 +19,24 @@ export default class RemoteConfig extends Base {
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
*/
@ -26,7 +44,7 @@ export default class RemoteConfig extends Base {
if (!this.developerModeEnabled) {
this.log.debug('Enabled developer mode');
FirebaseRemoteConfig.enableDeveloperMode();
this.developerModeEnabled = true
this.developerModeEnabled = true;
}
}
@ -34,35 +52,15 @@ export default class RemoteConfig extends Base {
* Fetches Remote Config data
* Call activateFetched to make fetched data available in app
* @returns {*|Promise.<String>}:
* One of
* - remoteConfitFetchStatusSuccess
* - remoteConfitFetchStatusFailure
* - remoteConfitFetchStatusThrottled
* rejects on remoteConfitFetchStatusFailure and remoteConfitFetchStatusThrottled
* resolves on remoteConfitFetchStatusSuccess
*/
fetch() {
this.log.debug('Fetching remote config data');
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) {
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');
return FirebaseRemoteConfig.fetch();
}
/**
* Applies Fetched Config data to the Active Config
@ -88,13 +86,15 @@ export default class RemoteConfig extends Base {
* "source" : OneOf<String>(remoteConfigSourceRemote|remoteConfigSourceDefault|remoteConfigSourceStatic)
* }
*/
configValueForKey(key: String) {
return FirebaseRemoteConfig.configValueForKey(key);
getValue(key: String) {
return FirebaseRemoteConfig
.getValue(key || '')
.then(this._nativeValueToJS);
}
/**
* Gets the config value of the default namespace.
* @param key: Config key
* @param keys: Config key
* @returns {*|Promise.<Object>}, will always resolve.
* Result will be a dictionary of key and config objects
* Object looks like
@ -106,8 +106,16 @@ export default class RemoteConfig extends Base {
* "source" : OneOf<String>(remoteConfigSourceRemote|remoteConfigSourceDefault|remoteConfigSourceStatic)
* }
*/
configValuesForKeys(keys: Array<String>) {
return FirebaseRemoteConfig.configValuesForKeys(keys);
getValues(keys: Array<String>) {
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.
* @returns {*|Promise.<Array<String>>}
*/
keysWithPrefix(prefix: String) {
return FirebaseRemoteConfig.keysWithPrefix(prefix);
getKeysByPrefix(prefix?: String) {
return FirebaseRemoteConfig.getKeysByPrefix(prefix);
}
/**