-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathswitch.ts
48 lines (41 loc) · 1.36 KB
/
switch.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
47
48
// The Switch (Invoker) Class.
import ICommand from './icommand'
export default class Switch {
#commands: { [id: string]: ICommand }
#history: [number, string][]
constructor() {
this.#commands = {}
this.#history = []
}
showHistory(): void {
// Print the history of each time a command was invoked"
this.#history.forEach((row) => {
console.log(`${row[0]} : ${row[1]}`)
})
}
register(commandName: string, command: ICommand): void {
// Register commands in the Invoker
this.#commands[commandName] = command
}
execute(commandName: string): void {
// Execute any registered commands
if (commandName in this.#commands) {
this.#commands[commandName].execute()
this.#history.push([Date.now(), commandName])
} else {
console.log(`Command [${commandName}] not recognised`)
}
}
replayLast(numberOfCommands: number): void {
// Replay the last N commands
const commands = this.#history.slice(
this.#history.length - numberOfCommands,
this.#history.length
)
commands.forEach((command) => {
this.#commands[command[1]].execute()
// or if you wanted to also record this replay in history
// this.execute(command[1])
})
}
}