1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-01-23 15:36:52 +00:00

Added support for asynchronous commands

This commit is contained in:
Jeremy Ruston 2012-05-02 17:27:33 +01:00
parent 0784d9754c
commit 95807ea75a

View File

@ -37,6 +37,7 @@ Commander.prototype.execute = function() {
Execute the next command in the sequence Execute the next command in the sequence
*/ */
Commander.prototype.executeNextCommand = function() { Commander.prototype.executeNextCommand = function() {
var self = this;
// Invoke the callback if there are no more commands // Invoke the callback if there are no more commands
if(this.nextToken >= this.commandTokens.length) { if(this.nextToken >= this.commandTokens.length) {
this.callback(null); this.callback(null);
@ -54,21 +55,36 @@ Commander.prototype.executeNextCommand = function() {
params.push(this.commandTokens[this.nextToken++]); params.push(this.commandTokens[this.nextToken++]);
} }
// Get the command info // Get the command info
var command = $tw.commands[commandName]; var command = $tw.commands[commandName],
c,err;
if(!command) { if(!command) {
this.callback("Unknown command: " + commandName); this.callback("Unknown command: " + commandName);
} else { } else {
if(command.info.synchronous) {
if(this.verbose) { if(this.verbose) {
this.streams.output.write("Executing command: " + commandName + " " + params.join(" ") + "\n"); this.streams.output.write("Executing command: " + commandName + " " + params.join(" ") + "\n");
} }
var c = new command.Command(params,this), if(command.info.synchronous) {
// Synchronous command
c = new command.Command(params,this);
err = c.execute(); err = c.execute();
if(err) { if(err) {
this.callback(err); this.callback(err);
} else { } else {
this.executeNextCommand(); this.executeNextCommand();
} }
} else {
// Asynchronous command
c = new command.Command(params,this,function(err) {
if(err) {
self.callback(err);
} else {
self.executeNextCommand();
}
});
err = c.execute();
if(err) {
this.callback(err);
}
} }
} }
} }