This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (47 loc) · 1.66 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
/* @flow */
import { NativeEventEmitter, NativeModules } from 'react-native';
const { ReactNativeSocketMobile } = NativeModules;
// Exported only for test purposes
export const emitter = new NativeEventEmitter(ReactNativeSocketMobile);
const DECODED_DATA_LISTENER = 'DecodedData';
const STATUS_DEVICE_LISTENER = 'StatusDeviceChanged';
export const STATUS_WAITING = 'Waiting for a scanner...';
type ParamsType = {
bundleId: string,
developerId: string,
appKey: string,
};
type StatusType = 'connected' | 'disconnected';
const safe = (cb) => {
if (!ReactNativeSocketMobile) {
return () => Promise.reject('UNDEFINED_NATIVE_LIBRARY');
}
return cb;
};
const start = (params: ParamsType) => {
const { bundleId, developerId, appKey } = params;
return ReactNativeSocketMobile.start(bundleId, developerId, appKey);
};
const stop = () => ReactNativeSocketMobile.stop();
const updateStatusFromDevices = () =>
ReactNativeSocketMobile.updateStatusFromDevices();
const setDataListener = (callback: (result: { data: string }) => void) => {
emitter.addListener(DECODED_DATA_LISTENER, callback);
};
const setDeviceStatusListener = (callback: (status: StatusType) => void) => {
emitter.addListener(STATUS_DEVICE_LISTENER, ({ status }) => {
callback(status);
});
};
const clearAllListeners = () => {
emitter.removeAllListeners(DECODED_DATA_LISTENER);
emitter.removeAllListeners(STATUS_DEVICE_LISTENER);
};
export default {
clearAllListeners: safe(clearAllListeners),
start: safe(start),
stop: safe(stop),
setDataListener: safe(setDataListener),
setDeviceStatusListener: safe(setDeviceStatusListener),
updateStatusFromDevices: safe(updateStatusFromDevices),
};