-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.js
55 lines (44 loc) · 1.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import {
Platform,
NativeModules,
NativeEventEmitter,
} from 'react-native';
const { RNLaunchDarkly } = NativeModules;
class LaunchDarkly {
constructor () {
this.emitter = new NativeEventEmitter(RNLaunchDarkly);
this.listeners = {};
}
configure (apiKey, options) {
RNLaunchDarkly.configure(apiKey, options);
}
boolVariation (featureName, callback) {
RNLaunchDarkly.boolVariation(featureName, callback);
}
stringVariation (featureName, fallback, callback) {
RNLaunchDarkly.stringVariation(featureName, fallback, callback);
}
addFeatureFlagChangeListener (featureName, callback) {
if (Platform.OS === 'android') {
RNLaunchDarkly.addFeatureFlagChangeListener(featureName);
}
if (this.listeners[featureName]) {
return;
}
this.listeners[featureName] = this.emitter.addListener(
'FeatureFlagChanged',
({ flagName }) => {
if (flagName === featureName) {
callback(flagName);
}
},
);
}
unsubscribe () {
Object.keys(this.listeners).forEach((featureName) => {
this.listeners[featureName].remove();
});
this.listeners = {};
}
}
export default new LaunchDarkly();