-
Notifications
You must be signed in to change notification settings - Fork 11
/
RNFyberRewardedVideo.js
59 lines (53 loc) · 2.23 KB
/
RNFyberRewardedVideo.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
59
import { NativeModules, NativeEventEmitter } from 'react-native';
const RNFyberRewardedVideo = NativeModules.RNFyberRewardedVideo;
const RNFyberRewardedVideoEventEmitter = new NativeEventEmitter(RNFyberRewardedVideo);
const eventHandlers = {
rewardedVideoReceived: new Map(),
rewardedVideoFailedToLoad: new Map(),
rewardedVideoDidStart: new Map(),
rewardedVideoClosedByUser: new Map(),
rewardedVideoClosedByError: new Map()
};
const addEventListener = (type, handler) => {
switch (type) {
case 'rewardedVideoReceived':
eventHandlers[type].set(handler, RNFyberRewardedVideoEventEmitter.addListener(type, handler));
break;
case 'rewardedVideoFailedToLoad':
eventHandlers[type].set(handler, RNFyberRewardedVideoEventEmitter.addListener(type, (error) => { handler(error); }));
break;
case 'rewardedVideoDidStart':
eventHandlers[type].set(handler, RNFyberRewardedVideoEventEmitter.addListener(type, handler));
break;
case 'rewardedVideoClosedByUser':
eventHandlers[type].set(handler, RNFyberRewardedVideoEventEmitter.addListener(type, handler));
break;
case 'rewardedVideoClosedByError':
eventHandlers[type].set(handler, RNFyberRewardedVideoEventEmitter.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 = () => {
RNFyberRewardedVideoEventEmitter.removeAllListeners('rewardedVideoReceived');
RNFyberRewardedVideoEventEmitter.removeAllListeners('rewardedVideoFailedToLoad');
RNFyberRewardedVideoEventEmitter.removeAllListeners('rewardedVideoDidStart');
RNFyberRewardedVideoEventEmitter.removeAllListeners('rewardedVideoClosedByUser');
RNFyberRewardedVideoEventEmitter.removeAllListeners('rewardedVideoClosedByError');
};
module.exports = {
...RNFyberRewardedVideo,
requestRewardedVideo: (cb = () => {}) => RNFyberRewardedVideo.requestRewardedVideo(cb),
showRewardedVideo: () => RNFyberRewardedVideo.showRewardedVideo(),
addEventListener,
removeEventListener,
removeAllListeners
};