Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions waitgroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,43 @@
var WaitGroup = function(){
this.total = 0; // Number of total items
this.ready = 0; // Number of items ready
this.callbacks = [] // The wait callbacks
};

WaitGroup.prototype.add = function WaitGroupAdd(){
this.total++;
if (hasToEmit(this)) {
emitCallbacks(this);
}
};

WaitGroup.prototype.done = function WaitGroupDone(){
this.ready++;
if (hasToEmit(this)) {
emitCallbacks(this);
}
};

WaitGroup.prototype.wait = function(fn) {
var self = this;
setTimeout(function(){
if(self.ready == self.total) return fn();
self.wait(fn);
}, 0);
this.callbacks.push(fn);
if (hasToEmit(this)) {
emitCallbacks(this);
}
};

function hasToEmit(wg) {
return wg.ready === wg.total;
}

function emitCallbacks(wg) {
var callbacks = wg.callbacks;
for (var i = 0; i < callbacks.length; i++) {
var cb = callbacks[i];
cb();
}
wg.callbacks = [];
}

// Export to node.js
if(typeof(module) !== "undefined") {
module.exports = WaitGroup;
Expand Down