-
Notifications
You must be signed in to change notification settings - Fork 0
/
commandmap.ts
46 lines (39 loc) · 1.31 KB
/
commandmap.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
37
38
39
40
41
42
43
44
45
46
/**
* Acts as a map of commands names to callbacks
*/
namespace BLECommands {
export class CommandCallbacks {
private commandNames: string[]
private argCommandNames: string[]
private callbacks: (() => void)[]
private argCallbacks: ((value: number) => void)[]
public constructor() {
this.commandNames = []
this.argCommandNames = []
this.callbacks = []
this.argCallbacks = []
}
public addCommandCallback(commandName: string, callback: () => void) {
this.commandNames.push(commandName.toLowerCase())
this.callbacks.push(callback)
}
public addCommandCallbackwithArg(commandName: string, callback: (value: number) => void) {
this.argCommandNames.push(commandName.toLowerCase())
this.argCallbacks.push(callback)
}
public fireCallbackFor(commandName: string) {
const callbackIndex = this.commandNames.indexOf(commandName.toLowerCase())
if (callbackIndex == -1) {
return
}
this.callbacks[callbackIndex]()
}
public fireCallbackwithArgFor(commandName: string, arg: number) {
const callbackIndex = this.argCommandNames.indexOf(commandName.toLowerCase())
if (callbackIndex == -1) {
return
}
this.argCallbacks[callbackIndex](arg)
}
}
}