Skip to content

Commit

Permalink
refactored QueueItem as module
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-bromann committed Mar 21, 2013
1 parent dea2bd6 commit e55fc30
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 288 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ exports.remote = function(options) {

// make sure we have a default options if none are provided
options = options || {};
console.log(WebdriverJS);

if (options.singleton) {
if (!singletonInstance) {
singletonInstance = new WebdriverJS(options);
Expand Down
147 changes: 147 additions & 0 deletions lib/utils/QueueItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
var QueueItem = module.exports = function(commandName, method, scope, args) {

this.children = [];
this.commandName = commandName;
this.method = method;
this.scope = scope;
this.currentChildIndex = 0;
this.isDone = false;

var self = this;

// change callback
this.args = [];
var hasCallback = false;
for(var i = 0, ii = args.length; i < ii; i++) {

var currentArg = args[i];
if (typeof currentArg == "function" && i == (args.length - 1)) {
hasCallback = true;
this.args.push((function(method, methodName) {
return function(result)
{
// call the callback
method.call(scope, result);
// call the next item
self.next();
};
})(currentArg, commandName)
);
} else {
this.args.push(currentArg);
}
}

if (!hasCallback)
{
this.args.push((function(){
return function() {
// continue queue after callback
self.next();
};
})());
}
};

// queue item run command
QueueItem.prototype.run = function() {

// switch to the current queue item to make future addings to the correct queue item
this.scope.currentQueueItem = this;

// save the current length in case new items are added
var currentLength = this.children.length;

// run the command
this.method.apply(this.scope, this.args);

// if the command added new items to the queue, we make sure we run those commans
if (currentLength < this.children.length) {
this.next();
}
};

// add queue item to the current item
QueueItem.prototype.add = function(item)
{

// make a reference to its parent so we can travel back
item.parent = this;

// add the new item to this childrens list
this.children.push(item);

// if we arent running, its time now
if (!this.scope.queueIsRunning) {
// make sure we switch the running flag so that we dont run .next again when a new item is added.
this.scope.queueIsRunning = true;

// begin the que
this.next();
}
};

// go to next queu item
QueueItem.prototype.next = function() {
//printQueueNow();
// if we have more children, run the next
// otherwise tell the item we are done
if (this.currentChildIndex < this.children.length) {
this.children[this.currentChildIndex].run();
this.currentChildIndex++;
} else {
this.done();
}
};

// the done method has to check if we are done for real, ie all children are done.
// if not, we check what children are left to run
QueueItem.prototype.done = function(scope) {

if (!this.isDone) {
// get the next undone child
var checkDoneChildren = this.getNextChildToRun();

// if its null, we know all children are done and we dont need to go further
if (checkDoneChildren === null) {
// mark this as done
this.isDone = true;

// if we have a parent, run its next command, otherwise we are in the root and are totally finished
if (this.parent)
{
// console.log("PARENT DONE")
this.parent.next();
}
else
{
// and if we are finished we can turn off the running flag
this.scope.queueIsRunning = false;
}
}
} else {
// if we are done, we when wheter everything in the queue is done. if so, set the running flag to false
var nextToRun = this.scope.currentQueueItem.getNextChildToRun();
if (nextToRun === null) {
this.scope.queueIsRunning = false;
}
}
};

// recursive function to get the next undone item
QueueItem.prototype.getNextChildToRun = function()
{
var child = null;
for(var i = 0, ii = this.children.length; i < ii; i++) {
if (this.children[i] && !this.children[i].isDone)
{
return this.children[i];
}
else
{
child = this.children[i].getNextChildToRun();
}
}

return child;
};
Loading

0 comments on commit e55fc30

Please sign in to comment.