-
Notifications
You must be signed in to change notification settings - Fork 15
/
RNUnityAds.js
77 lines (68 loc) · 2.27 KB
/
RNUnityAds.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
import {
NativeModules,
DeviceEventEmitter,
} from 'react-native';
const RNUnityAds = NativeModules.RNUnityAds;
const eventHandlers = {
onReady: new Map(),
onStart: new Map(),
onFinish: new Map(),
onError: new Map(),
};
const addEventListener = (type, handler) => {
switch (type) {
case 'onReady':
eventHandlers[type].set(handler, DeviceEventEmitter.addListener(type, event => { handler(event.placementId); }));
break;
case 'onStart':
eventHandlers[type].set(handler, DeviceEventEmitter.addListener(type, event => { handler(event.placementId); }));
break;
case 'onFinish':
eventHandlers[type].set(handler, DeviceEventEmitter.addListener(type, event => { handler(event.placementId, event.result); }));
break;
case 'onError':
eventHandlers[type].set(handler, DeviceEventEmitter.addListener(type, event => { handler(event.error, event.message); }));
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('onReady');
DeviceEventEmitter.removeAllListeners('onError');
DeviceEventEmitter.removeAllListeners('onStart');
DeviceEventEmitter.removeAllListeners('onFinish');
};
module.exports = {
...RNUnityAds,
getState: function getState(placementId, callback) {
if (arguments.length === 1) {
callback = placementId;
placementId = 'DEFAULT';
}
RNUnityAds.getState(placementId, callback);
},
isReady: function isReady(placementId, callback) {
if (arguments.length === 1) {
callback = placementId;
placementId = 'DEFAULT';
}
RNUnityAds.isReady(placementId, callback);
},
show: function show(placementId) {
if (arguments.length === 0) {
placementId = 'DEFAULT';
}
RNUnityAds.show(placementId);
},
addEventListener,
removeEventListener,
removeAllListeners
};