forked from squaretwo/react-native-iron-source
-
Notifications
You must be signed in to change notification settings - Fork 37
/
RNIronSourceRewardedVideo.js
72 lines (66 loc) · 3.25 KB
/
RNIronSourceRewardedVideo.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
60
61
62
63
64
65
66
67
68
69
70
71
72
import { NativeModules, NativeEventEmitter } from 'react-native';
const RNIronSourceRewardedVideo = NativeModules.RNIronSourceRewardedVideo;
const IronSourceRewardedVideoEventEmitter = new NativeEventEmitter(RNIronSourceRewardedVideo);
const eventHandlers = {
ironSourceRewardedVideoAvailable: new Map(),
ironSourceRewardedVideoUnavailable: new Map(),
ironSourceRewardedVideoDidOpen: new Map(),
ironSourceRewardedVideoDidStart: new Map(),
ironSourceRewardedVideoClosedByUser: new Map(),
ironSourceRewardedVideoClosedByError: new Map(),
ironSourceRewardedVideoAdStarted: new Map(),
ironSourceRewardedVideoAdEnded: new Map(),
ironSourceRewardedVideoAdRewarded:new Map()
};
const addEventListener = (type, handler) => {
switch (type) {
case 'ironSourceRewardedVideoAvailable':
case 'ironSourceRewardedVideoUnavailable':
case 'ironSourceRewardedVideoDidOpen':
case 'ironSourceRewardedVideoDidStart':
case 'ironSourceRewardedVideoClosedByError':
case 'ironSourceRewardedVideoAdStarted':
case 'ironSourceRewardedVideoAdEnded':
case 'ironSourceRewardedVideoAdRewarded':
eventHandlers[type].set(handler, IronSourceRewardedVideoEventEmitter.addListener(type, handler));
break;
case 'ironSourceRewardedVideoClosedByUser':
eventHandlers[type].set(handler, IronSourceRewardedVideoEventEmitter.addListener(type, () => {
// This is a dirty hack that is required by some Ad Networks (Vungle, UnityAds)
// It makes 'ironSourceRewardedVideoClosedByUser' and 'ironSourceRewardedVideoAdRewarded'
// events order match with all other networks
setTimeout(handler, 250);
}));
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 = () => {
IronSourceRewardedVideoEventEmitter.removeAllListeners('ironSourceRewardedVideoAvailable');
IronSourceRewardedVideoEventEmitter.removeAllListeners('ironSourceRewardedVideoUnavailable');
IronSourceRewardedVideoEventEmitter.removeAllListeners('ironSourceRewardedVideoDidOpen');
IronSourceRewardedVideoEventEmitter.removeAllListeners('ironSourceRewardedVideoDidStart');
IronSourceRewardedVideoEventEmitter.removeAllListeners('ironSourceRewardedVideoClosedByUser');
IronSourceRewardedVideoEventEmitter.removeAllListeners('ironSourceRewardedVideoClosedByError');
IronSourceRewardedVideoEventEmitter.removeAllListeners('ironSourceRewardedVideoAdStarted');
IronSourceRewardedVideoEventEmitter.removeAllListeners('ironSourceRewardedVideoAdEnded');
IronSourceRewardedVideoEventEmitter.removeAllListeners('ironSourceRewardedVideoAdRewarded');
};
module.exports = {
...RNIronSourceRewardedVideo,
initializeRewardedVideo: () => RNIronSourceRewardedVideo.initializeRewardedVideo(),
showRewardedVideo: (placementName) => RNIronSourceRewardedVideo.showRewardedVideo(placementName),
isRewardedVideoAvailable: () => RNIronSourceRewardedVideo.isRewardedVideoAvailable(),
setDynamicUserId: (userId) => RNIronSourceRewardedVideo.setDynamicUserId(userId),
addEventListener,
removeEventListener,
removeAllListeners
};