Skip to content

stepmuel/WorkerQueue

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 

WorkerQueue

A queue based web worker API to simplify multithreading in JavaScript.

This repository was created for my blog post which contains additional information.

The code was used for Map Miner, an interactive data visualizer to explore player positions within a minecraft world. There is also an interactive demo.

Usage

var wq = new workerQueue();

var job = function(arg) {
  return "hi " + arg.name;
};

var callback = function(result) {
  console.log(result);
};

wq.add(job, {name: "world"}, callback);

More advanced usage:

var wq = new workerQueue();

// configuration (default values)

// number of threads
wq.maxWorkers = 1;

// minimum delay between dispatches in ms
// (use for throttling)
wq.delay = 0;

// worker prototype (copied on initialization)
// define before first 'add' call!

wq.worker.foo = "bar";
wq.worker.bar = function () {
  this.foo = "baz";
};

// called after copying data 
wq.worker.init = function() {
  // access previously defined properties
  this.bar();
};

var job = function(arg, transferList) {
  var data = new Uint8Array(1024*32);
  var result = {};
  result.buffer = data.buffer;
  result.foo = this.foo;
  // add buffer to transfer list
  transferList.push(data.buffer);
  return result;
};

var callback = function(result, job) {
  console.log("foo: " + result.foo);
  // job sequence number
  console.log("foo: " + job.id);
  // execution time (use to update wq.delay)
  var dt = new Date().getTime() - job.start;
  console.log("dt: " + dt);
};

function update() {
  // remove pending jobs
  wq.queue.splice(0,wq.queue.length);
  wq.add(job, {}, callback);
};

About

A queue based web worker API to simplify multithreading in JavaScript

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published