-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcommand_router.ts
36 lines (31 loc) · 1.26 KB
/
command_router.ts
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
import { Command, Response, isCommandSystem, isCommandUI, CommandUI, CommandSystem } from './types/commands'
import { CommandHandler, Bridge, VisualisationEngine } from './types/modules'
export default class CommandRouter implements CommandHandler {
bridge: Bridge
visualisationEngine: VisualisationEngine
constructor (bridge: Bridge, visualisationEngine: VisualisationEngine) {
this.bridge = bridge
this.visualisationEngine = visualisationEngine
}
async onCommand (command: Command): Promise<Response> {
return await new Promise<Response>((resolve, reject) => {
if (isCommandSystem(command)) {
this.onCommandSystem(command, resolve)
} else if (isCommandUI(command)) {
this.onCommandUI(command, resolve)
} else {
reject(new TypeError('Unknown command' + JSON.stringify(command)))
}
})
}
onCommandSystem (command: CommandSystem, resolve: (response: Response) => void): void {
this.bridge.send(command)
resolve({ __type__: 'Response', command, payload: { __type__: 'PayloadVoid', value: undefined } })
}
onCommandUI (command: CommandUI, reject: (reason?: any) => void): void {
this.visualisationEngine.render(command).then(
(response) => { reject(response) },
() => {}
)
}
}