Highway.js
A flexible pub/sub event emitter for web-worker cross-communication with fallback support.
Usage
Include Highway.js on both client side and worker side.
Client:
Worker:
Initialize:
// Client sidevar Host = selfWorker ? 'worker.bundle.js' : selfselfHW = selfHW || Host// Worker sideselfHW = selfHW ||
Note that you cannot use importScripts
in case you want proper fallback when there is no web-worker support.
In that case you need to include a bundle in new Worker('worker.bundle.js')
and the bundle should also include Highway.
This way you can include your worker bundle on the client side after no worker support is detected.
// On client sideif !selfWorker var script = document script documentbody
In most cases you want to initialize client side stuff after the worker is loaded. Use your own event for this. For example you need your data model from the backend before rendering the site.
// Client sideselfHW// Worker sideselfHW
API
sub
Subscribe to an event.
selfHWselfHW
pub
Publish an event.
selfHWselfHWselfHW
one
Subscribe to an event, unsubscribe once it is called.
selfHW
exe
Execute a function on the other side. Your function will be executed within self
context.
selfHW
off
Unsubscribe from an event.
// Unsubscribe from ALL events associated with MyOwnEventselfHW// Unsubscribe only a specific functionselfHW// Unsubscribe from ALL events associated with MyOwnEvent and event the deep ones. eg: MyOwnEvent->DeepEvent tooselfHW
destroy
Destroy the Highway instance.
selfHW