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:
This creates the Worky
global.
Usage
To understand this, you must know what web workers are.
window
)
Main thread (This is how you use Worky
in the main thread:
var worker = "some-worker.js";worker;worker;
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.
; var worker = ;worker;worker;
Documentation
EventEmitter()
Creates a new EventEmitter
instance. This is exposed via Worky.EventEmitter
.
Return
- EventEmitter The
EventEmitter
instance.
_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
EventEmitter
instance.
_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 wayworker; // Internally, this method is used:worker;
Params
- String|Worky.Message
ev
: The event name or aWorky.Message
object.
Return
- EventEmitter The
EventEmitter
instance.
Worky(script)
Creates or initializes a web worker. This is inherited from
the EventEmitter
class.
Usage:
// In the main thread (window)var worker = "some-worker.js"; // In the worker thread (some-worker.js)var worker = ;
Params
- String|Worker
script
: The worker script url or the worker object itself.
Return
- Worky The
Worky
instance.
Worky.Receiver()
Creates the onmessage
handler. This method is used internally.
Return
- Function The receiver handler which calls the core
_emit
function.
Worky.Emitter()
Creates the emit
handler. This method is used internally.
Return
- Function The emitter handler which calls the
postMessage
function.
Worky.Message(args)
Creates a new Message
instance
Params
- Arguments
args
: The arguments pseudo-array.
Return
- Worky.Message The
Message
instance 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.