An EventEmitter like interface for web-workers.
Click here or the image below to check out a live example.
Include the worky.min.js
(or worky.js
) into your page:
<script src="path/to/worky.min.js"></script>
This creates the Worky
global.
To understand this, you must know what web workers are.
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.
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);
Creates a new EventEmitter
instance. This is exposed via Worky.EventEmitter
.
- EventEmitter The
EventEmitter
instance.
The core on
method. By default on
is the same with _on
.
However, on
can be rewritten, but _on
is still the same.
- String
ev
: The event name. - Function
fn
: The listener function.
- EventEmitter The
EventEmitter
instance.
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"]
});
- String|Worky.Message
ev
: The event name or aWorky.Message
object.
- EventEmitter The
EventEmitter
instance.
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();
- String|Worker
script
: The worker script url or the worker object itself.
- Worky The
Worky
instance.
Creates the onmessage
handler. This method is used internally.
- Function The receiver handler which calls the core
_emit
function.
Creates the emit
handler. This method is used internally.
- Function The emitter handler which calls the
postMessage
function.
Creates a new Message
instance
- Arguments
args
: The arguments pseudo-array.
- Worky.Message The
Message
instance containing the following fields: event
(String): The event name.args
(Array): An array of elements representing the event data.
Have an idea? Found a bug? See how to contribute.
See the LICENSE file.