-
Notifications
You must be signed in to change notification settings - Fork 529
/
RNAdMobRewarded.js
46 lines (38 loc) · 1.23 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
'use strict';
import {
Platform,
NativeModules,
NativeEventEmitter,
} from 'react-native';
const RNAdMobRewarded = NativeModules.RNAdMobRewarded;
const adMobRewardedEmitter = new NativeEventEmitter(RNAdMobRewarded);
const eventHandlers = {};
const addEventListener = (type, handler) => {
eventHandlers[type] = eventHandlers[type] || new Map();
eventHandlers[type].set(handler, adMobRewardedEmitter.addListener(type, handler));
}
const removeEventListener = (type, handler) => {
if (!eventHandlers[type].has(handler)) {
return;
}
eventHandlers[type].get(handler).remove();
eventHandlers[type].delete(handler);
}
const removeAllListeners = () => {
const types = Object.keys(eventHandlers);
types.forEach(type => (
eventHandlers[type].forEach((subscription, key, map) => {
subscription.remove();
map.delete(key);
})
));
};
module.exports = {
...RNAdMobRewarded,
requestAd: (cb = () => {}) => RNAdMobRewarded.requestAd(cb), // requestAd callback is optional
showAd: (cb = () => {}) => RNAdMobRewarded.showAd(cb), // showAd callback is optional
addEventListener,
removeEventListener,
removeAllListeners,
simulatorId: Platform.OS === 'android' ? 'EMULATOR' : RNAdMobRewarded.simulatorId,
};