Repository navigation
Expand file tree
/
Copy pathindex-impl.tsx
More file actions
308 lines (277 loc) · 9.44 KB
/
Copy pathindex-impl.tsx
File metadata and controls
308 lines (277 loc) · 9.44 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Handles sending requests to the daemon
import Session, {type CancelHandlerType} from './session'
import engineListener from './listener'
import logger from '@/logger'
import {debugWarning} from '@/util/debug-warning'
import throttle from 'lodash/throttle'
import type {CustomResponseIncomingCallMapType, IncomingCallMapType, BatchParams} from '.'
import type {SessionID, SessionIDKey, MethodKey} from './types'
import {initEngine, initEngineListener} from './require'
import {isMobile} from '@/constants/platform'
import {printOutstandingRPCs} from '@/local-debug'
import {resetClient, createClient, rpcLog, type CreateClientType, type PayloadType} from './index.platform'
import {type RPCError, convertToError} from '@/util/errors'
import type * as EngineGen from '../actions/engine-gen-gen'
import type {_useState as UseStateType} from '@/constants/engine'
// delay incoming to stop react from queueing too many setState calls and stopping rendering
// only while debugging for now
const DEFER_INCOMING_DURING_DEBUG = __DEV__ && (false as boolean)
if (DEFER_INCOMING_DURING_DEBUG) {
debugWarning('DEFER_INCOMING_DURING_DEBUG is On')
}
type WaitingKey = string | ReadonlyArray<string>
function capitalize(s: string) {
return s.charAt(0).toUpperCase() + s.slice(1)
}
class Engine {
_onConnectedCB: (c: boolean) => void
// Tracking outstanding sessions
_sessionsMap: {[K in SessionIDKey]: Session} = {}
// Helper we delegate actual calls to
_rpcClient: CreateClientType
// Set which actions we don't auto respond with so listeners can themselves
_customResponseAction: {[K in MethodKey]: true} = {
'keybase.1.rekeyUI.delegateRekeyUI': true,
'keybase.1.secretUi.getPassphrase': true,
...(isMobile ? {'chat.1.chatUi.chatWatchPosition': true} : {'keybase.1.logsend.prepareLogsend': true}),
}
// We generate sessionIDs monotonically
_nextSessionID: number = 123
// We call onDisconnect handlers only if we've actually disconnected (ie connected once)
_hasConnected: boolean = isMobile // mobile is always connected
// App tells us when the listeners are done loading so we can start emitting events
_listenersAreReady: boolean = false
_emitWaiting: (changes: BatchParams) => void
_queuedChanges: Array<{error?: RPCError; increment: boolean; key: WaitingKey}> = []
dispatchWaitingAction = (key: WaitingKey, waiting: boolean, error?: RPCError) => {
this._queuedChanges.push({error, increment: waiting, key})
this._throttledDispatchWaitingAction()
}
_throttledDispatchWaitingAction = throttle(() => {
const changes = this._queuedChanges
this._queuedChanges = []
this._emitWaiting(changes)
}, 500)
constructor(
emitWaiting: (changes: BatchParams) => void,
onConnected: (c: boolean) => void,
allowIncomingCalls = true
) {
this._onConnectedCB = onConnected
// the node engine doesn't do this and we don't want to pull in any reqs
if (allowIncomingCalls) {
this._engineConstantsIncomingCall = (
require('@/constants/engine') as {_useState: typeof UseStateType}
)._useState.getState().dispatch.onEngineIncoming
}
this._emitWaiting = emitWaiting
this._rpcClient = createClient(
payload => this._rpcIncoming(payload),
() => this._onConnected(),
() => this._onDisconnect()
)
this._setupDebugging()
}
_setupDebugging() {
if (!__DEV__) {
return
}
global.DEBUGEngine = this
// Print out any alive sessions periodically
if (printOutstandingRPCs) {
setInterval(() => {
if (Object.keys(this._sessionsMap).filter(k => !this._sessionsMap[k]?.getDangling()).length) {
logger.localLog('outstandingSessionDebugger: ', this._sessionsMap)
}
}, 10 * 1000)
}
}
_onDisconnect() {
// tell renderer we're disconnected
this._onConnectedCB(false)
}
// We want to dispatch the connect action but only after listeners boot up
listenersAreReady = () => {
this._listenersAreReady = true
if (this._hasConnected) {
this._onConnectedCB(true)
}
}
// Called when we reconnect to the server. This only happens in node in the electron side.
// We proxy the stuff over the mainWindowDispatch
_onConnected() {
this._hasConnected = true
this._onConnectedCB(true)
}
// Create and return the next unique session id
_generateSessionID() {
this._nextSessionID++
return this._nextSessionID
}
// Got a cancelled sequence id
_handleCancel(seqid: number) {
const cancelledSessionID = Object.keys(this._sessionsMap).find(key =>
this._sessionsMap[key]?.hasSeqID(seqid)
)
if (cancelledSessionID) {
const s = this._sessionsMap[cancelledSessionID]
rpcLog({
extra: {cancelledSessionID},
method: s?._startMethod || 'unknown',
reason: '[cancel]',
type: 'engineInternal',
})
s?.cancel()
} else {
rpcLog({
extra: {cancelledSessionID},
method: 'unknown',
reason: '[cancel?]',
type: 'engineInternal',
})
}
}
// An incoming rpc call
_rpcIncoming(payload: PayloadType) {
const {method, param: incomingParam, response} = payload
const param = incomingParam.length ? incomingParam[0] || {} : {}
const {seqid, cancelled} = response || {cancelled: false, seqid: 0}
const {sessionID} = param
if (cancelled) {
this._handleCancel(seqid)
} else {
const session = this._sessionsMap[String(sessionID)]
if (session?.incomingCall(method, param, response)) {
// Part of a session?
} else {
// Dispatch as an action
const extra: {response?: unknown} = {}
if (this._customResponseAction[method]) {
extra.response = response
} else {
// Not a custom response so we auto handle it
response?.result?.()
}
const type = method
.replace(/'/g, '')
.split('.')
.map((p, idx) => (idx ? capitalize(p) : p))
.join('')
const act = {payload: {params: param, ...extra}, type: `engine-gen:${type}`}
this._engineConstantsIncomingCall(act as EngineGen.Actions)
}
}
}
_engineConstantsIncomingCall = (_a: EngineGen.Actions): void => {
logger.error('_engineConstantsIncomingCall not overriden')
throw Error('needs override')
}
// An outgoing call. ONLY called by the flow-type rpc helpers
_rpcOutgoing(p: {
method: string
params: Object
callback: (...args: Array<any>) => void
incomingCallMap?: IncomingCallMapType
customResponseIncomingCallMap?: CustomResponseIncomingCallMapType
waitingKey?: WaitingKey
}) {
const {customResponseIncomingCallMap, incomingCallMap, waitingKey} = p
const {method, params, callback} = p
// Make a new session and start the request
const session = this.createSession({
customResponseIncomingCallMap,
incomingCallMap,
waitingKey,
})
session.start(method, params, callback)
return session.getId()
}
// Make a new session. If the session hangs around forever set dangling to true
createSession(p: {
incomingCallMap?: IncomingCallMapType
customResponseIncomingCallMap?: CustomResponseIncomingCallMapType
cancelHandler?: CancelHandlerType
dangling?: boolean
waitingKey?: WaitingKey
}): Session {
const {customResponseIncomingCallMap, incomingCallMap, cancelHandler, dangling = false, waitingKey} = p
const sessionID = this._generateSessionID()
const session = new Session({
cancelHandler,
customResponseIncomingCallMap,
dangling,
endHandler: (session: Session) => this._sessionEnded(session),
incomingCallMap,
invoke: (method, param, cb) => {
const callback =
(method: string) =>
(...args: Array<unknown>) => {
// If first argument is set, convert it to an Error type
if (args.length > 0 && !!args[0]) {
args[0] = convertToError(args[0], method)
}
cb(...args)
}
this._rpcClient.invoke(method, param || [{}], callback(method))
},
sessionID,
waitingKey,
})
this._sessionsMap[String(sessionID)] = session
return session
}
// Cancel a session maybe deprecate, not used
cancelSession(sessionID: SessionID) {
const session = this._sessionsMap[String(sessionID)]
if (session) {
session.cancel()
}
}
// Cleanup a session that ended
_sessionEnded(session: Session) {
rpcLog({
extra: {
sessionID: session.getId(),
},
method: session._startMethod || 'unknown',
reason: '[-session]',
type: 'engineInternal',
})
delete this._sessionsMap[String(session.getId())] // eslint-disable-line
}
// Reset the engine
reset() {
if (isMobile) {
return
}
resetClient(this._rpcClient)
}
}
// don't overwrite this on HMR
let engine: Engine | undefined
if (__DEV__) {
engine = global.DEBUGEngine as Engine
}
const makeEngine = (
emitWaiting: (b: BatchParams) => void,
onConnected: (c: boolean) => void,
allowIncomingCalls = true
) => {
if (__DEV__ && engine) {
logger.warn('makeEngine called multiple times')
}
if (!engine) {
engine = new Engine(emitWaiting, onConnected, allowIncomingCalls)
initEngine(engine)
initEngineListener(engineListener)
}
return engine
}
const getEngine = (): Engine => {
if (!engine) {
throw new Error('Engine needs to be initialized first')
}
return engine
}
export default getEngine
export {getEngine, makeEngine, Engine}