-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
67 lines (55 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
55
56
57
58
59
60
61
62
63
64
65
66
67
import {NativeModules, NativeEventEmitter} from 'react-native'
const {ARArgyleSdk} = NativeModules
const callbacks = [
'onAccountCreated',
'onAccountConnected',
'onAccountRemoved',
'onAccountError',
'onDDSSuccess',
'onDDSError',
'onUIEvent',
'onError',
'onClose',
'onCantFindItemClicked',
'onExitIntroClicked',
'onFormSubmitted',
'onDocumentsSubmitted'
]
export class ArgyleLink {
static eventsEmitter = new NativeEventEmitter(ARArgyleSdk)
static listeners = {}
static start(config) {
const {sandbox, userToken} = config
if (sandbox === undefined || userToken === undefined) {
throw '[ArgyleLink] userToken and sandbox must be defined.'
}
callbacks.forEach(name => {
ArgyleLink.addListener(name, payload => {
config[name]?.(payload)
})
})
ArgyleLink.addListener('onTokenExpired', () => {
config?.onTokenExpired(newToken => {
ARArgyleSdk.updateToken(newToken)
})
})
ARArgyleSdk.start(config)
}
static close() {
ARArgyleSdk.close()
}
static removeListenerIfAdded(key) {
const listener = ArgyleLink.listeners[key]
if (listener) {
listener.remove()
delete ArgyleLink.listeners[key]
}
}
static addListener(key, callback) {
ArgyleLink.removeListenerIfAdded(key)
ArgyleLink.listeners[key] = ArgyleLink.eventsEmitter.addListener(
key,
callback
)
}
}