From 95807ea75aadd913474e32bc760dfb8eb3fdfef7 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Wed, 2 May 2012 17:27:33 +0100 Subject: [PATCH] Added support for asynchronous commands --- rabbithole/core/modules/commander.js | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/rabbithole/core/modules/commander.js b/rabbithole/core/modules/commander.js index d6b433de8..9aa939e71 100644 --- a/rabbithole/core/modules/commander.js +++ b/rabbithole/core/modules/commander.js @@ -37,6 +37,7 @@ Commander.prototype.execute = function() { Execute the next command in the sequence */ Commander.prototype.executeNextCommand = function() { + var self = this; // Invoke the callback if there are no more commands if(this.nextToken >= this.commandTokens.length) { this.callback(null); @@ -54,21 +55,36 @@ Commander.prototype.executeNextCommand = function() { params.push(this.commandTokens[this.nextToken++]); } // Get the command info - var command = $tw.commands[commandName]; + var command = $tw.commands[commandName], + c,err; if(!command) { this.callback("Unknown command: " + commandName); } else { + if(this.verbose) { + this.streams.output.write("Executing command: " + commandName + " " + params.join(" ") + "\n"); + } if(command.info.synchronous) { - if(this.verbose) { - this.streams.output.write("Executing command: " + commandName + " " + params.join(" ") + "\n"); - } - var c = new command.Command(params,this), - err = c.execute(); + // Synchronous command + c = new command.Command(params,this); + err = c.execute(); if(err) { this.callback(err); } else { 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); + } } } }