/
githubmirror
/
Motrix
Обзор
Документация
Войти
/
githubmirror
/
Motrix
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/renderer/components/CommandManager/index.js
46 строк
971 B
Dr_rOot
refactor: code format
06 май 2023, 13:16
06 май 2023, 13:16
1f033d1
Код
Авторство
О чём код?
import { EventEmitter } from 'node:events' export default class CommandManager extends EventEmitter { constructor () { super() this.commands = {} } register (id, fn) { if (this.commands[id]) { console.log('[Motrix] Attempting to register an already-registered command: ' + id) return null } if (!id || !fn) { console.error('[Motrix] Attempting to register a command with a missing id, or command function.') return null } this.commands[id] = fn this.emit('commandRegistered', id) } unregister (id) { if (this.commands[id]) { delete this.commands[id] this.emit('commandUnregistered', id) } } execute (id, ...args) { const fn = this.commands[id] if (fn) { try { this.emit('beforeExecuteCommand', id) } catch (err) { console.error(err) } const result = fn(...args) return result } else { return false } } }