Skip to content
This repository has been archived by the owner. It is now read-only.
Human-Friendly Web Worker wrapper.
JavaScript HTML
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
demo
lib
spec
src
.gitignore
.npmignore
.travis.yml
.ucompilerrc
LICENSE
README.md
exchange.js
package.json

README.md

WorkerExchange

WorkerExchange is an extremely lightweight wrapper for HTML5 WebWorker and SharedWorker. It provides a consistent API across both type of workers, So you won't have to replace nothing to change between the types. You can even use the same js file as both Dedicated and Shared Worker, an example of it can be found in the Demo Folder.

It automatically declares Exchange variable in Both Worker and Host scope. It automatically creates an Exchange instance in worker scope with the name exchange.

Hello World

// Host
const worker = Exchange.create('Worker.js');
// or
const worker = Exchange.createShared('Worker.js');

worker.request('some-job', {Key: 'value'}).then(function(response){
  console.log(response); // Pong
});
// Worker.js
importScripts('/path/to/exchange.js');
exchange.onRequest('some-job', function(data, message){
  console.log(data); // {"Key": "Value"}
  message.response = "Pong";
});

Check out the Online Demo. (Check your browser console)

Installation

npm install --save worker-exchange

API

// Browser
export class Exchange {
  constructor(worker: Object)
  request(name: String, data: Mixed)
  onRequest(name: String, callback: Function)
  terminate() // <-- also disposes it
  dispose()
  static create(filePath): Exchange
  static createShared(filePath): Exchange
}

// Worker
export class Exchange {
  constructor()
  forEach(callback: Function) /// <-- iterates over ports
  observe(callback: Function): Disposable
  onRequest(name: String, callback: Function): Disposable
  onDidPortAdd(callback: Function): Disposable
  onDidPortClose(callback: Function): Disposable
  dispose()
}
class ExchangePort {
  request(name: String, data: Mixed)
  onRequest(name: String, callback: Function): Disposable
  onDidClose(callback: Function): Disposable
  dispose()
}

License

This project is licensed under the terms of MIT License. See the LICENSE file for more info.

You can’t perform that action at this time.