command-pattern
v0.1.2
Published
Abstract implementation of the Command pattern.
Downloads
5
Readme
Command
Usage
var Command = require("command-pattern");
// What commands will do is up to you! But, first, they'll need a name...
var command = new Command("welcome");
command.implementation = function execute(/*parameters..., */callback) {
// Here, `this` is the command itself.
callback(null, "Hello!");
};
Inheritance
Surely, you can inherit from Command
to craft your own commands:
function WelcomeCommand() {
Command.call(this, "welcome");
}
inherit(WelcomeCommand, Command);
WelcomeCommand.prototype.implementation = function(/*parameters...*/) {
return "Hello!";
};
Asynchronous Mode
When executed with a callback as the last parameter the command is considered to be asynchronous.
If it fails, it should invoke the callback with an error as the first parameter while while if it succeeds, the first parameter should be null
.
In any case, execution results should be passed in subsequent parameters.
Synchronous Mode
When executed without any callback the command is considered to be synchronous and should return the results immediately. If it fails it should throw an error.