Repository navigation
Expand file tree
/
Copy pathlistener.tsx
More file actions
142 lines (122 loc) · 4.09 KB
/
Copy pathlistener.tsx
File metadata and controls
142 lines (122 loc) · 4.09 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import type * as Framed from 'framed-msgpack-rpc'
import {getEngine} from './require'
import {RPCError} from '@/util/errors'
import {printOutstandingRPCs} from '@/local-debug'
import type {CommonResponseHandler} from './types'
import {wrapErrors} from '@/util/debug'
type WaitingKey = string | Array<string>
// Wraps a response to update the waiting state
const makeWaitingResponse = (_r?: Partial<CommonResponseHandler>, waitingKey?: WaitingKey) => {
const r = _r
if (!r || !waitingKey) {
return r
}
const response: Partial<CommonResponseHandler> = {}
if (r.error) {
response.error = (e: Framed.ErrorType) => {
// Waiting on the server again
if (waitingKey) {
getEngine().dispatchWaitingAction(waitingKey, true)
}
r.error?.(e)
}
}
if (r.result) {
response.result = (...args: Array<unknown>) => {
// Waiting on the server again
if (waitingKey) {
getEngine().dispatchWaitingAction(waitingKey, true)
}
r.result?.(...args)
}
}
return response
}
// TODO could have a mechanism to ensure only one is in flight at a time. maybe by some key or something
async function listener(p: {
method: string
params?: Object
incomingCallMap?: {[K in string]: (params: unknown) => Promise<void>}
customResponseIncomingCallMap?: {
[K in string]: (params: unknown, response: Partial<CommonResponseHandler>) => Promise<void>
}
waitingKey?: WaitingKey
}) {
return new Promise((resolve, reject) => {
const {method, params, waitingKey} = p
const incomingCallMap = p.incomingCallMap || {}
const customResponseIncomingCallMap = p.customResponseIncomingCallMap || {}
// custom and normal incomingCallMaps
const bothCallMaps = [
...Object.keys(incomingCallMap).map(method => ({custom: false, method})),
...Object.keys(customResponseIncomingCallMap).map(method => ({custom: true, method})),
]
// Waiting on the server
if (waitingKey) {
getEngine().dispatchWaitingAction(waitingKey, true)
}
const callMap = bothCallMaps.reduce((map: {[key: string]: unknown}, {method, custom}) => {
map[method] = (params: unknown, _response: CommonResponseHandler) => {
// No longer waiting on the server
if (waitingKey) {
getEngine().dispatchWaitingAction(waitingKey, false)
}
let response = makeWaitingResponse(_response, waitingKey)
if (__DEV__) {
if (incomingCallMap[method] && customResponseIncomingCallMap[method]) {
throw new Error('Invalid method in both incomingCallMap and customResponseIncomingCallMap ')
}
}
if (!custom) {
if (response) {
response.result?.()
response = undefined
}
}
// defer to process network first
setTimeout(() => {
const invokeAndDispatch = wrapErrors(async () => {
if (response) {
const cb = customResponseIncomingCallMap[method]
await cb?.(params, response)
} else {
const cb = incomingCallMap[method]
await cb?.(params)
}
}, method)
invokeAndDispatch()
.then(() => {})
.catch(() => {})
}, 5)
}
return map
}, {})
// Make the actual call
let outstandingIntervalID: ReturnType<typeof setInterval>
if (printOutstandingRPCs) {
outstandingIntervalID = setInterval(() => {
console.log('Engine/Listener with a still-alive eventChannel for method:', method)
}, 2000)
}
getEngine()._rpcOutgoing({
callback: (error?: RPCError, params?: unknown) => {
if (printOutstandingRPCs) {
clearInterval(outstandingIntervalID)
}
if (waitingKey) {
// No longer waiting
getEngine().dispatchWaitingAction(waitingKey, false, error instanceof RPCError ? error : undefined)
}
if (error) {
reject(error)
} else {
resolve(params)
}
},
incomingCallMap: callMap,
method,
params,
})
})
}
export default listener