1.0.0 • Published 10 years ago

worky v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
10 years ago

worky

worky

An EventEmitter like interface for web-workers.

Demo

Click here or the image below to check out a live example.

Worky Demo

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 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 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 a Worky.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 = 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 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.

1.0.0

10 years ago

0.0.0

10 years ago

0.2.1

11 years ago

0.2.0

11 years ago

0.1.1

11 years ago

0.1.0

11 years ago