Giter Club home page Giter Club logo

worky's Introduction

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.

worky's People

Contributors

ionicabizau avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.