forked from sbugert/react-native-admob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RNAdMobRewarded.js
70 lines (63 loc) · 2.34 KB
/
RNAdMobRewarded.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
'use strict';
import {
NativeModules,
DeviceEventEmitter,
} from 'react-native';
const RNAdMobRewarded = NativeModules.RNAdMobRewarded;
const eventHandlers = {
rewardedVideoDidRewardUser: new Map(),
rewardedVideoDidLoad: new Map(),
rewardedVideoDidFailToLoad: new Map(),
rewardedVideoDidOpen: new Map(),
rewardedVideoDidClose: new Map(),
rewardedVideoWillLeaveApplication: new Map(),
};
const addEventListener = (type, handler) => {
switch (type) {
case 'rewardedVideoDidRewardUser':
eventHandlers[type].set(handler, DeviceEventEmitter.addListener(type, (type, amount) => {
handler(type, amount);
}));
break;
case 'rewardedVideoDidLoad':
eventHandlers[type].set(handler, DeviceEventEmitter.addListener(type, handler));
break;
case 'rewardedVideoDidFailToLoad':
eventHandlers[type].set(handler, DeviceEventEmitter.addListener(type, (error) => { handler(error); }));
break;
case 'rewardedVideoDidOpen':
eventHandlers[type].set(handler, DeviceEventEmitter.addListener(type, handler));
break;
case 'rewardedVideoDidClose':
eventHandlers[type].set(handler, DeviceEventEmitter.addListener(type, handler));
break;
case 'rewardedVideoWillLeaveApplication':
eventHandlers[type].set(handler, DeviceEventEmitter.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 = () => {
DeviceEventEmitter.removeAllListeners('rewardedVideoDidRewardUser');
DeviceEventEmitter.removeAllListeners('rewardedVideoDidLoad');
DeviceEventEmitter.removeAllListeners('rewardedVideoDidFailToLoad');
DeviceEventEmitter.removeAllListeners('rewardedVideoDidOpen');
DeviceEventEmitter.removeAllListeners('rewardedVideoDidClose');
DeviceEventEmitter.removeAllListeners('rewardedVideoWillLeaveApplication');
};
module.exports = {
...RNAdMobRewarded,
requestAd: (cb = () => {}) => RNAdMobRewarded.requestAd(cb), // requestAd callback is optional
showAd: (cb = () => {}) => RNAdMobRewarded.showAd(cb), // showAd callback is optional
addEventListener,
removeEventListener,
removeAllListeners,
};