Skip to content

Instantly share code, notes, and snippets.

@kig
Created September 2, 2011 11:03
Show Gist options
  • Save kig/1188381 to your computer and use it in GitHub Desktop.
Save kig/1188381 to your computer and use it in GitHub Desktop.
WorkCrew - a WebWorker work queue library
/*
WorkCrew - a WebWorker work queue library
Usage:
// Create an 8 worker pool using worker.js.
var crew = new WorkCrew('worker.js', 8);
// Do something whenever a job is completed.
// The result object structure is
// {
// id: work unit ID,
// result: message received from worker
// }
crew.oncomplete = function(result) {
console.log(result.id, result.result);
};
// Add some work to the queue.
// The work unit is postMessaged to one of
// the workers.
var workId = crew.addWork(myWorkUnit);
// Add an onfinish event handler.
// Fired when the queue is empty and all workers
// are free.
crew.onfinish = function() {
console.log('All work in queue finished!');
};
*/
WorkCrew = function(filename, count) {
this.filename = filename;
this.count = count || 4;
this.queue = [];
this.results = [];
this.pool = [];
this.working = {};
this.uuid = 0;
this.fillPool();
};
WorkCrew.prototype.onfinish = function() {};
WorkCrew.prototype.oncomplete = function(res) {
return [res.id, res.result];
};
WorkCrew.prototype.addWork = function(work) {
var id = this.uuid++;
this.queue.push({id: id, work: work});
this.processQueue();
return id;
};
WorkCrew.prototype.processQueue = function() {
if (this.queue.length == 0 && this.pool.length == this.count) {
if (this.onfinish)
this.onfinish();
} else {
while (this.queue.length > 0 && this.pool.length > 0) {
var unit = this.queue.shift();
var worker = this.pool.shift();
worker.id = unit.id;
this.working[worker.id] = worker;
worker.postMessage(unit.work);
}
}
};
WorkCrew.prototype.addWorker = function() {
var w = new Worker(this.filename);
var self = this;
w.onmessage = function(res) {
var id = this.id;
delete self.working[this.id];
this.id = null;
self.pool.push(this);
try {
self.oncomplete({id: id, result: res});
} catch(e) {
console.log(e);
}
self.processQueue();
};
this.pool.push(w);
};
WorkCrew.prototype.fillPool = function() {
for (var i=0; i<this.count; i++) {
this.addWorker();
}
};
@rrichter
Copy link

rrichter commented Jun 2, 2015

The time spent within the while loop is minimal and limited by the time it takes to serve your workers (this.pool.length) with tasks.

The returning workers as well as incoming tasks will trigger the processQueue() method again.

@jared201
Copy link

I'm getting DataCloneError on IE11. thanks!

@gpolyn
Copy link

gpolyn commented May 1, 2018

working does nothing : )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment