/pseudo-worker

A tiny and mostly spec-compliant WebWorker polyfill

Primary LanguageJavaScriptApache License 2.0Apache-2.0

pseudo-worker Build Status Coverage Status

A tiny and mostly spec-compliant WebWorker polyfill, designed for browsers that don't support WebWorkers, or for browsers that don't support certain features of WebWorkers (e.g. IndexedDB in Safari).

This runs on the main thread, so you don't get any of the multithreading benefits of WebWorkers. However, it should be "good enough" for the less-compliant browsers.

The bundle size is very small: 964 bytes after uglify+gzip! Install

npm install pseudo-worker

Usage

var PseudoWorker = require('pseudo-worker');
var worker = new PseudoWorker('script.js');

Or as a polyfill:

require('pseudo-worker/polyfill');
// now window.Worker is polyfilled in older browsers

Instead of Browserify/Webpack, you can also use it directly as a script tag by downloading it from wzrd.in:

<script src="https://wzrd.in/standalone/pseudo-worker"></script>

Then it's available as window.pseudoWorker. To use it as a polyfill, just do:

if (typeof window.Worker === 'undefined') {
  window.Worker = window.pseudoWorker;
}

Supported APIS

  • onmessage
  • onerror
  • addEventListener
  • postMessage

Note: inside the worker, you must use the self variable instead of the implicit global object. I.e. do this:

self.onmessage = ...

Not this:

onmessage = ...

Supported browsers

The full list of browsers that are tested in CI are in .zuul.yml. But basically:

  • Chrome
  • Firefox
  • Safari 7+
  • IE 8+
  • iOS 7.0+
  • Android 4.0+

Node.js is not supported. Check out node-webworker instead.

Testing the library

First:

npm install

Then to test in Node (using an XHR shim):

npm test

Or to test manually in your browser of choice:

npm run test-local

Or to test in a browser using SauceLabs:

npm run test-browser

Or to test in PhantomJS:

npm run test-phantom

Or to test with coverage reports:

npm run coverage