forked from eddieowens/react-native-boundary
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
72 lines (55 loc) · 1.67 KB
/
index.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
import {NativeEventEmitter, NativeModules, AppRegistry} from 'react-native';
const {RNBoundary} = NativeModules;
const TAG = "RNBoundary";
const boundaryEventEmitter = new NativeEventEmitter(RNBoundary);
const Events = {
EXIT: "onExit",
ENTER: "onEnter",
};
export {
Events
}
const HeadlessBoundaryEventTask = async ({event, ids}) => {
console.log(event, ids);
boundaryEventEmitter.emit(event, ids)
};
AppRegistry.registerHeadlessTask('OnBoundaryEvent', () => HeadlessBoundaryEventTask);
export default {
add: boundary => {
if (!boundary || (boundary.constructor !== Array && typeof boundary !== 'object')) {
throw TAG + ': a boundary must be an array or non-null object';
}
return new Promise((resolve, reject) => {
if (typeof boundary === 'object' && !boundary.id) {
reject(TAG + ': an id is required')
}
RNBoundary.add(boundary)
.then(id => resolve(id))
.catch(e => reject(e))
})
},
on: (event, callback) => {
if (typeof callback !== 'function') {
throw TAG + ': callback function must be provided';
}
if (!Object.values(Events).find(e => e === event)) {
throw TAG + ': invalid event';
}
return boundaryEventEmitter.addListener(event, callback);
},
off: (event) => {
if (!Object.values(Events).find(e => e === event)) {
throw TAG + ': invalid event';
}
return boundaryEventEmitter.removeAllListeners(event);
},
removeAll: () => {
return RNBoundary.removeAll();
},
remove: id => {
if (!id || (id.constructor !== Array && typeof id !== 'string')) {
throw TAG + ': id must be a string';
}
return RNBoundary.remove(id);
}
}