-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
54 lines (41 loc) · 1.4 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
/* @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 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,
start,
stop,
setDataListener,
setDeviceStatusListener,
updateStatusFromDevices,
};