Repository navigation
Expand file tree
/
Copy pathsession-impl.tsx
More file actions
208 lines (184 loc) · 6.11 KB
/
Copy pathsession-impl.tsx
File metadata and controls
208 lines (184 loc) · 6.11 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import {
StatusCode,
type CustomResponseIncomingCallMap,
type IncomingCallMapType,
} from '@/constants/types/rpc-gen'
import {rpcLog, type InvokeType} from './index.platform'
import {IncomingRequest, OutgoingRequest} from './request'
import {RPCError} from '@/util/errors'
import {getEngine} from './require'
import type {SessionID, ResponseType, EndHandlerType, MethodKey} from './types'
type WaitingKey = string | Array<string>
// A session is a series of calls back and forth tied together with a single sessionID
class Session {
// Our id
_id: SessionID
// Map of methods => callbacks
_incomingCallMap: IncomingCallMapType
// Map of methods => callbacks
_customResponseIncomingCallMap: CustomResponseIncomingCallMap
// Let the outside know we're waiting
_waitingKey: WaitingKey
// Tell engine we're done
_endHandler: EndHandlerType | undefined
// Sequence IDs we've seen. Value is true if we've responded (often we get cancel after we've replied)
_seqIDResponded: {[K in string]: boolean} = {}
// If you want to know about being cancelled
_cancelHandler: CancelHandlerType | undefined
// If true this session exists forever
_dangling: boolean
// Name of the start method, just to help debug
_startMethod: MethodKey | undefined
// Start callback so we can cancel our own callback
_startCallback: ((err?: RPCError, ...args: Array<unknown>) => void) | undefined
// Allow us to make calls
_invoke: InvokeType
// Outstanding requests
_outgoingRequests: Array<{method: string}> = []
_incomingRequests: Array<{method: string}> = []
constructor(p: {
sessionID: SessionID
incomingCallMap?: IncomingCallMapType
customResponseIncomingCallMap?: CustomResponseIncomingCallMap
waitingKey?: WaitingKey
invoke: InvokeType
endHandler: EndHandlerType
cancelHandler?: CancelHandlerType
dangling?: boolean
}) {
this._id = p.sessionID
this._incomingCallMap = p.incomingCallMap || {}
this._customResponseIncomingCallMap = p.customResponseIncomingCallMap || {}
this._waitingKey = p.waitingKey || ''
this._invoke = p.invoke
this._endHandler = p.endHandler
this._cancelHandler = p.cancelHandler
this._dangling = p.dangling || false
}
setId(_: SessionID) {
throw new Error("Can't set sessionID")
}
getId(): SessionID {
return this._id
}
getDangling(): boolean {
return this._dangling
}
// Make a waiting handler for the request. We add additional data before calling the parent waitingHandler
// and do internal bookkeeping if the request is done
_makeWaitingHandler(isOutgoing: boolean, method: MethodKey, seqid?: number) {
return (waiting: boolean, err: RPCError | undefined) => {
rpcLog({
extra: {
id: this.getId(),
seqid,
this: this,
waiting,
},
method,
reason: `[${waiting ? '+' : '-'}waiting]`,
type: 'engineInternal',
})
if (this._waitingKey) {
getEngine().dispatchWaitingAction(this._waitingKey, waiting, err)
}
// Request is finished, do cleanup
if (!waiting) {
const requests = isOutgoing ? this._outgoingRequests : this._incomingRequests
const idx = requests.findIndex(r => r.method === method)
if (idx !== -1) {
// Mark us as responded
if (seqid) {
this._seqIDResponded[String(seqid)] = true
}
// Remove from our list
requests.splice(idx, 1)
}
}
}
}
cancel() {
if (this._cancelHandler) {
this._cancelHandler(this)
} else if (this._startCallback) {
this._startCallback(new RPCError('Received RPC cancel for session', StatusCode.sccanceled))
}
this.end()
}
end() {
this._endHandler?.(this)
}
// Start the session normally. Tells engine we're done at the end
start(method: MethodKey, param: Object, callback: (() => void) | undefined) {
this._startMethod = method
this._startCallback = callback
// When this request is done the session is done
const wrappedCallback = (err: RPCError | undefined, ...args: Array<unknown>) => {
this._startCallback?.(err, ...args)
this._startCallback = undefined
this.end()
}
// Add the sessionID
const wrappedParam = {
...param,
sessionID: this.getId(),
}
rpcLog({
extra: {id: this.getId(), this: this},
method,
reason: '[+session]',
type: 'engineInternal',
})
const outgoingRequest = new OutgoingRequest(
method,
wrappedParam,
wrappedCallback,
this._makeWaitingHandler(true, method),
this._invoke
)
this._outgoingRequests.push(outgoingRequest)
outgoingRequest.send()
}
// We have an incoming call tied to a sessionID, called only by engine
incomingCall(method: keyof IncomingCallMapType, param: Object, response?: ResponseType): boolean {
rpcLog({
extra: {
id: this.getId(),
response,
this: this,
},
method,
reason: '[-calling:session]',
type: 'engineInternal',
})
let handler = this._incomingCallMap[method] as
| undefined
| ((param: Object | undefined, request: ResponseType) => void)
if (!handler) {
const c = this._customResponseIncomingCallMap as {[key: string]: typeof handler}
handler = c[method]
}
if (!handler) {
return false
}
if (response?.seqid) {
this._seqIDResponded[String(response.seqid)] = false
}
const waitingHandler = this._makeWaitingHandler(false, method, response?.seqid)
const incomingRequest = new IncomingRequest(method, param, response, waitingHandler, handler as any)
this._incomingRequests.push(incomingRequest)
incomingRequest.handle()
return true
}
// Tell engine if we can handle the cancelled call
hasSeqID(seqID: number) {
if (__DEV__) {
if (Object.hasOwn(this._seqIDResponded, String(seqID))) {
console.log('Cancelling seqid found, current session state', this)
}
}
return Object.hasOwn(this._seqIDResponded, String(seqID))
}
}
export type CancelHandlerType = (session: Session) => void
export default Session