forked from sbugert/react-native-admob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RNAdMobInterstitial.js
82 lines (73 loc) · 2.59 KB
/
RNAdMobInterstitial.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
73
74
75
76
77
78
79
80
81
82
'use strict';
import {
NativeModules,
DeviceEventEmitter,
} from 'react-native';
const RNAdMobInterstitial = NativeModules.RNAdMobInterstitial;
const eventHandlers = {
interstitialDidLoad: new Map(),
interstitialDidFailToLoad: new Map(),
interstitialDidOpen: new Map(),
interstitialDidClose: new Map(),
interstitialWillLeaveApplication: new Map(),
};
const addEventListener = (type, handler) => {
switch (type) {
case 'interstitialDidLoad':
eventHandlers[type].set(handler, DeviceEventEmitter.addListener(type, handler));
break;
case 'interstitialDidFailToLoad':
eventHandlers[type].set(handler, DeviceEventEmitter.addListener(type, (error) => { handler(error); }));
break;
case 'interstitialDidOpen':
eventHandlers[type].set(handler, DeviceEventEmitter.addListener(type, handler));
break;
case 'interstitialDidClose':
eventHandlers[type].set(handler, DeviceEventEmitter.addListener(type, handler));
break;
case 'interstitialWillLeaveApplication':
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('interstitialDidLoad');
DeviceEventEmitter.removeAllListeners('interstitialDidFailToLoad');
DeviceEventEmitter.removeAllListeners('interstitialDidOpen');
DeviceEventEmitter.removeAllListeners('interstitialDidClose');
DeviceEventEmitter.removeAllListeners('interstitialWIllLeaveApplication');
};
// replaces deprecated API
const tryShowNewInterstitial = (testID) => {
console.warn(`tryShowNewInterstitial method is deprecated and will be removed in the next major release, please use requestAd() and showAd() directly.\n\nExample: AdMobInterstitial.requestAd(AdMobInterstitial.showAd)`);
if (testID) {
RNAdMobInterstitial.setTestDeviceID(testID);
}
RNAdMobInterstitial.isReady((isReady) => {
if (isReady) {
RNAdMobInterstitial.showAd();
}
});
};
module.exports = {
...RNAdMobInterstitial,
requestAd: RNAdMobInterstitial.requestAd,
showAd: RNAdMobInterstitial.showAd,
tryShowNewInterstitial,
addEventListener,
removeEventListener,
removeAllListeners,
setAdUnitId: (id) => {
RNAdMobInterstitial.setAdUnitID(id);
console.warn(`setAdUnitId will be deprecated soon. Please use setAdUnitID instead.`);
},
};