forked from marktext/marktext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
53 lines (44 loc) · 1.24 KB
/
index.js
File metadata and controls
53 lines (44 loc) · 1.24 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
import COMMAND_CONSTANTS from 'common/commands/constants'
import { loadFileCommands } from './file'
import { loadTabCommands } from './tab'
export const COMMANDS = COMMAND_CONSTANTS
export const loadDefaultCommands = commandManager => {
loadFileCommands(commandManager)
loadTabCommands(commandManager)
}
class CommandManager {
constructor () {
this._commands = new Map()
}
add (id, callback) {
const { _commands } = this
if (_commands.has(id)) {
throw new Error(`Command with id="${id}" already exists.`)
}
_commands.set(id, callback)
}
remove (id) {
return this._commands.delete(id)
}
has (id) {
return this._commands.has(id)
}
execute (id, ...args) {
const command = this._commands.get(id)
if (!command) {
throw new Error(`No command found with id="${id}".`)
}
return command(...args)
}
__verifyDefaultCommands () {
const { _commands } = this
Object.keys(COMMANDS).forEach(propertyName => {
const id = COMMANDS[propertyName]
if (!_commands.has(id)) {
console.error(`[DEBUG] Default command with id="${id}" isn't available!`)
}
})
}
}
const commandManagerInstance = new CommandManager()
export { commandManagerInstance as CommandManager }