forked from bubjavier/react-native-dfp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRNDFPInterstitial.js
More file actions
65 lines (57 loc) · 1.51 KB
/
RNDFPInterstitial.js
File metadata and controls
65 lines (57 loc) · 1.51 KB
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
import { NativeModules, NativeEventEmitter, DeviceEventEmitter, Platform } from 'react-native';
const { RNDFPInterstitial } = NativeModules
const eventTypes = {
onAdLoaded: 'onAdLoaded',
onAdOpened: 'onAdOpened',
onAdLeftApplication: 'onAdLeftApplication',
onAdClosed: 'onAdClosed',
onAdFailedToLoad: 'onAdFailedToLoad'
}
let subscriptions = [];
const interstitialEventEmitter = Platform.OS === 'ios' ? new NativeEventEmitter(RNDFPInterstitial) : DeviceEventEmitter;
export default class Interstitial {
constructor(adUnitId, customTargeting) {
this.adUnitId = adUnitId;
this.customTargeting = customTargeting;
for (let i = 0, len = subscriptions.length; i < len; i++) {
subscriptions[i].remove();
}
subscriptions = [];
}
/**
* Load an ad with an instance of AdRequest
* @returns {*}
*/
loadAd() {
RNDFPInterstitial.loadAdFromAdUnitId(this.adUnitId, this.customTargeting);
}
/**
* Show the advert - will only show if loaded
* @returns {*}
*/
show() {
RNDFPInterstitial.showAd();
}
/**
* Listen to an Ad event
* @param eventType
* @param listenerCb
* @returns {null}
*/
on(eventType, listenerCb) {
if (!eventTypes[eventType]) {
console.warn(
`Invalid event type provided, must be one of: ${Object.keys(
eventTypes
).join(', ')}`
);
return null;
}
const sub = interstitialEventEmitter.addListener(
eventType,
listenerCb
);
subscriptions.push(sub);
return sub;
}
}