-
Notifications
You must be signed in to change notification settings - Fork 529
/
RNAdMobInterstitial.js
46 lines (38 loc) · 1.2 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
import {
NativeModules,
NativeEventEmitter,
} from 'react-native';
import { createErrorFromErrorData } from './utils';
const RNAdMobInterstitial = NativeModules.RNAdMobInterstitial;
const adMobInterstitialEmitter = new NativeEventEmitter(RNAdMobInterstitial);
const eventHandlers = {};
const addEventListener = (type, handler) => {
eventHandlers[type] = eventHandlers[type] || new Map();
if (type === 'interstitialDidFailToLoad') {
eventHandlers[type].set(handler, adMobInterstitialEmitter.addListener(type, error => handler(createErrorFromErrorData(error))));
} else {
eventHandlers[type].set(handler, adMobInterstitialEmitter.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);
})
));
};
export default {
...RNAdMobInterstitial,
addEventListener,
removeEventListener,
removeAllListeners,
};