worky v1.0.0
worky
An EventEmitter like interface for web-workers.
Demo
Click here or the image below to check out a live example.
Installation
Include the worky.min.js (or worky.js) into your page:
<script src="path/to/worky.min.js"></script>This creates the Worky global.
Usage
To understand this, you must know what web workers are.
Main thread (window)
This is how you use Worky in the main thread:
var worker = new Worky("some-worker.js");
worker.on("eventName", function (some, data) {
/* do something with data coming from the worker thread */
});
worker.emit("someEvent", and, some, data);Basically, you can emit and listen (for) things to/from the worker.
Worker thread
When you are inside of the worker thread, you have to import the worky.js script.
importScript("path/to/worky.js");
var worker = Worky();
worker.on("someEvent", function (and, some, data) {
/* do something with data coming from the main thread*/
});
worker.emit("eventName", some, data);Documentation
EventEmitter()
Creates a new EventEmitter instance. This is exposed via Worky.EventEmitter.
Return
- EventEmitter The
EventEmitterinstance.
_on(ev, fn)
The core on method. By default on is the same with _on.
However, on can be rewritten, but _on is still the same.
Params
- String
ev: The event name. - Function
fn: The listener function.
Return
- EventEmitter The
EventEmitterinstance.
_emit(ev)
Emits the passed arguments. By default emit is the same with _emit.
However, emit can be rewritten, but _emit is still the same.
Usage:
// Using arguments - this is the convenient way
worker.emit("eventName", 42, { some: "object" }, "foo");
// Internally, this method is used:
worker.emit({
event: "eventName"
, args: [42, { some: "object" }, "foo"]
});Params
- String|Worky.Message
ev: The event name or aWorky.Messageobject.
Return
- EventEmitter The
EventEmitterinstance.
Worky(script)
Creates or initializes a web worker. This is inherited from
the EventEmitter class.
Usage:
// In the main thread (window)
var worker = new Worky("some-worker.js");
// In the worker thread (some-worker.js)
var worker = new Worky();Params
- String|Worker
script: The worker script url or the worker object itself.
Return
- Worky The
Workyinstance.
Worky.Receiver()
Creates the onmessage handler. This method is used internally.
Return
- Function The receiver handler which calls the core
_emitfunction.
Worky.Emitter()
Creates the emit handler. This method is used internally.
Return
- Function The emitter handler which calls the
postMessagefunction.
Worky.Message(args)
Creates a new Message instance
Params
- Arguments
args: The arguments pseudo-array.
Return
- Worky.Message The
Messageinstance containing the following fields: event(String): The event name.args(Array): An array of elements representing the event data.
How to contribute
Have an idea? Found a bug? See how to contribute.
License
See the LICENSE file.

