radio/magnus/core/commands.js

86 lines
2.5 KiB
JavaScript
Raw Permalink Normal View History

2018-08-04 21:46:25 +00:00
"use strict";
var ModelVocabulary = require("../lib/wModel/vocabulary");
var Vocabulary = require("../lib/wType/vocabulary");
var String = require("../lib/wType/string");
var Commands = ModelVocabulary.inherit({
"className": "Commands",
"constructor": function(address) {
ModelVocabulary.fn.constructor.call(this, address);
this._commands = global.Object.create(null);
},
"destructor": function() {
for (var key in this._commands) {
var cmd = this._commands[key];
if (cmd.enabled) {
this._removeHandler(cmd.handler);
}
cmd.name.destructor();
cmd.handler.destructor();
cmd.arguments.destructor();
delete this._commands[key];
}
ModelVocabulary.fn.destructor.call(this);
},
"addCommand": function(key, handler, args) {
if (this._commands[key]) {
throw new Error("Command with this key already exist");
}
this._commands[key] = {
name: new String(key),
handler: handler,
arguments: args,
enabled: false
}
},
"_disableCommand": function(cmd) {
this._removeHandler(cmd.handler);
cmd.enabled = false;
this.erase(cmd.name.toString());
},
"enableCommand": function(key, value) {
var cmd = this._commands[key];
if (!cmd) {
throw new Error("An attempt to access non existing command: " + key);
}
if (cmd.enabled !== value) {
if (value) {
this._enableCommand(cmd);
} else {
this._disableCommand(cmd);
}
}
},
"_enableCommand": function(cmd) {
this._addHandler(cmd.handler);
cmd.enabled = true;
var vc = new Vocabulary();
vc.insert("address", cmd.handler.address.clone());
vc.insert("arguments", cmd.arguments.clone());
this.insert(cmd.name.toString(), vc);
},
"removeCommand": function(name) {
var cmd = this._commands[name];
if (cmd === undefined) {
throw new Error("An attempt to access non existing command: " + key);
}
if (cmd.enabled) {
this._disableCommand(cmd);
}
cmd.name.destructor();
cmd.handler.destructor();
cmd.arguments.destructor();
delete this._commands[name];
}
});
module.exports = Commands;