forked from bsy/react-native-pollfish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pollfish.js
58 lines (52 loc) · 1.77 KB
/
Pollfish.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
56
57
58
import { NativeModules, NativeEventEmitter } from 'react-native';
const RNPollfish = NativeModules.RNPollfish;
const PollfishEventEmitter = new NativeEventEmitter(RNPollfish);
const eventHandlers = {
surveyReceived: new Map(),
surveyCompleted: new Map(),
userNotEligible: new Map(),
surveyNotAvailable: new Map(),
surveyOpened: new Map(),
surveyClosed: new Map()
};
const addEventListener = (type, handler) => {
switch (type) {
case 'surveyReceived':
case 'surveyCompleted':
case 'userNotEligible':
case 'surveyNotAvailable':
case 'surveyOpened':
case 'surveyClosed':
eventHandlers[type].set(handler, PollfishEventEmitter.addListener(type, handler));
break;
default:
console.log(`Event with type ${type} does not exist.`);
}
}
const removeEventListener = (type, handler) => {
if (!eventHandlers[type].has(handler)) {
return;
}
eventHandlers[type].get(handler).remove();
eventHandlers[type].delete(handler);
}
const removeAllListeners = () => {
PollfishEventEmitter.removeAllListeners('surveyReceived');
PollfishEventEmitter.removeAllListeners('surveyCompleted');
PollfishEventEmitter.removeAllListeners('userNotEligible');
PollfishEventEmitter.removeAllListeners('surveyNotAvailable');
PollfishEventEmitter.removeAllListeners('surveyOpened');
PollfishEventEmitter.removeAllListeners('surveyClosed');
};
module.exports = {
...RNPollfish,
initialize: (key, releaseMode, customMode, offerWallMode, userId) =>
RNPollfish.initialize(key, releaseMode, customMode, offerWallMode, userId),
show: () => RNPollfish.show(),
hide: () => RNPollfish.hide(),
destroy: () => RNPollfish.destroy(),
surveyAvailable: () => RNPollfish.surveyAvailable(),
addEventListener,
removeEventListener,
removeAllListeners
};