forked from MammothMedia/react-native-iron-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRNIronSourceBanner.js
58 lines (50 loc) · 1.72 KB
/
RNIronSourceBanner.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
import { NativeModules, NativeEventEmitter } from 'react-native';
const RNIronSourceBanner = NativeModules.RNIronSourceBanner;
const IronSourceBannerEventEmitter = new NativeEventEmitter(RNIronSourceBanner);
const supportedEvents = [
'ironSourceBannerDidLoad',
'ironSourceBannerDidFailToLoadWithError',
'ironSourceBannerDidDismissScreen',
'ironSourceBannerWillLeaveApplication',
'ironSourceBannerWillPresentScreen',
'ironSourceDidClickBanner',
]
const loadBannerDefaultOptions = {
position: 'bottom',
scaleToFitWidth: false,
};
const eventHandlers = supportedEvents.reduce((acc, eventName) => {
acc[eventName] = new Map();
return acc;
}, {});
const addEventListener = (type, handler) => {
if (supportedEvents.includes(type)) {
eventHandlers[type].set(handler, IronSourceBannerEventEmitter.addListener(type, handler));
} else {
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 = () => {
supportedEvents.map((eventType) => IronSourceBannerEventEmitter.removeAllListeners(eventType));
};
module.exports = {
...RNIronSourceBanner,
initializeBanner: () => {}, // Deprecated. Here for backwards compatibility with 2.5.3
loadBanner: (size = 'BANNER', options) => RNIronSourceBanner.loadBanner(size, {
...loadBannerDefaultOptions,
...options,
}),
showBanner: () => RNIronSourceBanner.showBanner(),
hideBanner: () => RNIronSourceBanner.hideBanner(),
destroyBanner: () => RNIronSourceBanner.destroyBanner(),
addEventListener,
removeEventListener,
removeAllListeners
};