Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Commit

Permalink
Prepare 2.1.0 release (#30) (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
LaunchDarklyCI authored Dec 24, 2019
1 parent abd9d2e commit d643d0a
Show file tree
Hide file tree
Showing 8 changed files with 541 additions and 38 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LaunchDarkly overview
Supported versions
-------------------------

This SDK is compatible with React Native 0.59.9 and Xcode 10.2.1 and is tested in Android 27 and iOS 12.2. Earlier versions of this SDK are compatible with prior versions of React Native, Android, and iOS.
This SDK is compatible with React Native 0.61.2 and Xcode 10.2.1 and is tested in Android 27 and iOS 12.2. Earlier versions of this SDK are compatible with prior versions of React Native, Android, and iOS.

Getting started
---------------
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ allprojects {

dependencies {
implementation 'com.facebook.react:react-native:+'
implementation 'com.launchdarkly:launchdarkly-android-client-sdk:2.8.5'
implementation 'com.launchdarkly:launchdarkly-android-client-sdk:2.9.0'
implementation 'com.jakewharton.timber:timber:4.7.1'
implementation "com.google.code.gson:gson:2.8.5"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.facebook.react.bridge.WritableNativeArray;
import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
Expand All @@ -29,6 +30,11 @@
import com.launchdarkly.android.LDConfig;
import com.launchdarkly.android.LDCountryCode;
import com.launchdarkly.android.LDUser;
import com.launchdarkly.android.ConnectionInformation;
import com.launchdarkly.android.LDStatusListener;
import com.launchdarkly.android.LDAllFlagsListener;
import com.launchdarkly.android.EvaluationDetail;
import com.launchdarkly.android.LDFailure;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand All @@ -37,6 +43,7 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.List;
import java.util.concurrent.ExecutionException;

import timber.log.Timber;
Expand Down Expand Up @@ -154,6 +161,8 @@ void loadFromMap(ReadableMap map, LDUser.Builder builder, Set<String> privateAtt
private LDClient ldClient;
// Current feature flag listeners
private Map<String, FeatureFlagChangeListener> listeners = new HashMap<>();
private Map<String, LDStatusListener> connectionModeListeners = new HashMap<>();
private Map<String, LDAllFlagsListener> allFlagsListeners = new HashMap<>();

public LaunchdarklyReactNativeClientModule(ReactApplicationContext reactContext) {
super(reactContext);
Expand All @@ -178,7 +187,9 @@ public String getName() {
private static final String ERROR_UNKNOWN = "E_UNKNOWN";

// Prefix for events sent over the React Native event bridge
private static final String EVENT_PREFIX = "LaunchDarkly--";
private static final String FLAG_PREFIX = "LaunchDarkly-Flag-";
private static final String ALL_FLAGS_PREFIX = "LaunchDarkly-All-Flags-";
private static final String CONNECTION_MODE_PREFIX = "LaunchDarkly-Connection-Mode-";


/**
Expand All @@ -190,7 +201,9 @@ public String getName() {
@Override
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
constants.put("EVENT_PREFIX", EVENT_PREFIX);
constants.put("FLAG_PREFIX", FLAG_PREFIX);
constants.put("ALL_FLAGS_PREFIX", ALL_FLAGS_PREFIX);
constants.put("CONNECTION_MODE_PREFIX", CONNECTION_MODE_PREFIX);
return constants;
}

Expand Down Expand Up @@ -546,6 +559,92 @@ public void jsonVariationObject(String flagKey, ReadableMap fallback, Promise pr
jsonVariationBase(flagKey, toJsonObject(fallback), promise);
}

@ReactMethod
public void boolVariationDetail(String flagKey, Promise promise) {
boolVariationDetailFallback(flagKey, null, promise);
}

@ReactMethod
public void boolVariationDetailFallback(String flagKey, Boolean fallback, Promise promise) {
try {
promise.resolve(ldClient.boolVariationDetail(flagKey, fallback));
} catch (Exception e) {
promise.resolve(fallback);
}
}

@ReactMethod
public void intVariationDetail(String flagKey, Promise promise) {
intVariationDetailFallback(flagKey, null, promise);
}

@ReactMethod
public void intVariationDetailFallback(String flagKey, Integer fallback, Promise promise) {
try {
promise.resolve(ldClient.intVariationDetail(flagKey, fallback));
} catch (Exception e) {
promise.resolve(fallback);
}
}

@ReactMethod
public void floatVariationDetail(String flagKey, Promise promise) {
floatVariationDetailFallback(flagKey, null, promise);
}

@ReactMethod
public void floatVariationDetailFallback(String flagKey, Float fallback, Promise promise) {
try {
promise.resolve(ldClient.floatVariationDetail(flagKey, fallback));
} catch (Exception e) {
promise.resolve(fallback);
}
}

@ReactMethod
public void stringVariationDetail(String flagKey, Promise promise) {
stringVariationDetailFallback(flagKey, null, promise);
}

@ReactMethod
public void stringVariationDetailFallback(String flagKey, String fallback, Promise promise) {
try {
promise.resolve(ldClient.stringVariationDetail(flagKey, fallback));
} catch (Exception e) {
promise.resolve(fallback);
}
}

@ReactMethod
public void jsonVariationDetailNone(String flagKey, Promise promise) {
jsonVariationDetailBase(flagKey, null, promise);
}

@ReactMethod
public void jsonVariationDetailNumber(String flagKey, Double fallback, Promise promise) {
jsonVariationDetailBase(flagKey, new JsonPrimitive(fallback), promise);
}

@ReactMethod
public void jsonVariationDetailBool(String flagKey, Boolean fallback, Promise promise) {
jsonVariationDetailBase(flagKey, new JsonPrimitive(fallback), promise);
}

@ReactMethod
public void jsonVariationDetailString(String flagKey, String fallback, Promise promise) {
jsonVariationDetailBase(flagKey, new JsonPrimitive(fallback), promise);
}

@ReactMethod
public void jsonVariationDetailArray(String flagKey, ReadableArray fallback, Promise promise) {
jsonVariationDetailBase(flagKey, toJsonArray(fallback), promise);
}

@ReactMethod
public void jsonVariationDetailObject(String flagKey, ReadableMap fallback, Promise promise) {
jsonVariationDetailBase(flagKey, toJsonObject(fallback), promise);
}

/**
* Helper for jsonVariation methods.
*
Expand All @@ -562,6 +661,15 @@ private void jsonVariationBase(String flagKey, JsonElement fallback, Promise pro
}
}

private void jsonVariationDetailBase(String flagKey, JsonElement fallback, Promise promise) {
try {
EvaluationDetail<JsonElement> jsonElementDetail = ldClient.jsonVariationDetail(flagKey, fallback);
resolveJsonElementDetail(promise, jsonElementDetail);
} catch (Exception e) {
resolveJsonElement(promise, fallback);
}
}


/**
* Converts the jsonElement to a React Native bridge compatible type and resolves the promise
Expand Down Expand Up @@ -589,6 +697,11 @@ private void resolveJsonElement(Promise promise, JsonElement jsonElement) {
}
}

private void resolveJsonElementDetail(Promise promise, EvaluationDetail<JsonElement> jsonElementDetail) {
JsonElement jsonElement = jsonElementDetail.getValue();
resolveJsonElement(promise, jsonElement);
}

/**
* Gets a object mapping of all flags and their values.
*
Expand Down Expand Up @@ -729,6 +842,36 @@ public void track(String eventName) {
ldClient.track(eventName);
}

@ReactMethod
public void trackNumberMetricValue(String eventName, Double data, Double metricValue) {
ldClient.track(eventName, new JsonPrimitive(data), metricValue);
}

@ReactMethod
public void trackBoolMetricValue(String eventName, Boolean data, Double metricValue) {
ldClient.track(eventName, new JsonPrimitive(data), metricValue);
}

@ReactMethod
public void trackStringMetricValue(String eventName, String data, Double metricValue) {
ldClient.track(eventName, new JsonPrimitive(data), metricValue);
}

@ReactMethod
public void trackArrayMetricValue(String eventName, ReadableArray data, Double metricValue) {
ldClient.track(eventName, toJsonArray(data), metricValue);
}

@ReactMethod
public void trackObjectMetricValue(String eventName, ReadableMap data, Double metricValue) {
ldClient.track(eventName, toJsonObject(data), metricValue);
}

@ReactMethod
public void trackMetricValue(String eventName, Double metricValue) {
ldClient.track(eventName, new JsonPrimitive(""), metricValue);
}

/**
* Shuts down any network connections maintained by the client and puts the client in offline
* mode.
Expand Down Expand Up @@ -842,6 +985,16 @@ public void isDisableBackgroundPolling(Promise promise) {
}
}

@ReactMethod
public void getConnectionInformation(Promise promise) {
try {
ConnectionInformation result = ldClient.getConnectionInformation();
promise.resolve(result);
} catch (Exception e) {
promise.reject(ERROR_UNKNOWN, e);
}
}

@ReactMethod
public void registerFeatureFlagListener(String flagKey) {
FeatureFlagChangeListener listener = new FeatureFlagChangeListener() {
Expand All @@ -852,7 +1005,7 @@ public void onFeatureFlagChange(String flagKey) {

getReactApplicationContext()
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(EVENT_PREFIX, result);
.emit(FLAG_PREFIX, result);
}
};

Expand All @@ -868,6 +1021,61 @@ public void unregisterFeatureFlagListener(String flagKey) {
}
}

@ReactMethod
public void registerCurrentConnectionModeListener(String listenerId) {
LDStatusListener listener = new LDStatusListener() {
@Override
public void onConnectionModeChanged(ConnectionInformation connectionInfo) {
WritableMap result = Arguments.createMap();
result.putString("connectionMode", new Gson().toJson(connectionInfo));

getReactApplicationContext()
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(CONNECTION_MODE_PREFIX, result);
}

@Override
public void onInternalFailure(LDFailure ldFailure) {}
};

ldClient.registerStatusListener(listener);
connectionModeListeners.put(listenerId, listener);
}

@ReactMethod
public void unregisterCurrentConnectionModeListener(String listenerId) {
if (connectionModeListeners.containsKey(listenerId)) {
ldClient.unregisterStatusListener(connectionModeListeners.get(listenerId));
connectionModeListeners.remove(listenerId);
}
}

@ReactMethod
public void registerAllFlagsListener(String listenerId) {
LDAllFlagsListener listener = new LDAllFlagsListener() {
@Override
public void onChange(List<String> flagKeys) {
WritableMap result = Arguments.createMap();
result.putString("flagKeys", new Gson().toJson(flagKeys));

getReactApplicationContext()
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(ALL_FLAGS_PREFIX, result);
}
};

ldClient.registerAllFlagsListener(listener);
allFlagsListeners.put(listenerId, listener);
}

@ReactMethod
public void unregisterAllFlagsListener(String listenerId) {
if (allFlagsListeners.containsKey(listenerId)) {
ldClient.unregisterAllFlagsListener(allFlagsListeners.get(listenerId));
allFlagsListeners.remove(listenerId);
}
}

/**
* Convert a ReadableMap into a JsonObject
* <p>
Expand Down
Loading

0 comments on commit d643d0a

Please sign in to comment.