0.1.17 • Published 7 years ago

chimera-js v0.1.17

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

chimera-js

Build Status Coverage Status

What Is This?

(In development)

This is a open source JavaScript library that seeks to help developers by simplifying the use of Web Workers API.

Examples

We have a example project on github ChimeraJSPOC.

But basically, here is where you can get started:

  • First you will need to add the library chimera-js into your JavaScript code:
  import Chimera from 'chimera-js';

We have two ways to use a parallel web worker using chimera-js;

  • The first one is to use the Chimera object to specify a object with all the functions that you want to run in parallel through the method exportToWorker. OBS: In version 0.1.17 you can't export arrow functions in a single line. You need to use the parentheses and the braces when using the method exportToWorker.
  const _fibonacci = (n) => {
    (n < 2) ? 1 : _fibonacci(n-2) + _fibonacci(n-1);
  };

  Chimera.exportToWorker({ fibonacci: _fibonacci }).then((worker) => {
    // Do your code here
  });
  • The other one is that you can use the Chimera object to specify another JavaScript file to run in parallel (Inside a web worker) through the method setWorker.
  Chimera.setWorker('./parallelExample.js').then((worker) => {
    // Do your code here
  });
  • Either way the executed method will return a Promise and the callback function executed by that promise will receive a object (worker) that you can use to call a function defined in the file parallelExample.js or a function defined in the object exported.
  Chimera.exportToWorker({ fibonacci: _fibonacci }).then((worker) => {
    // Do your code here
  });
  -------------------------------------------------------------
  Chimera.setWorker('./parallelExample.js').then((worker) => {
    worker.executeFibonacci(42);
  });
  • All functions of the code executed in parallel that have a return will return a Promise and the callback function executed by that promise will receive a parameter result that is the result returned by the executed function.
  Chimera.setWorker('./parallelExample.js').then((worker) => {
    worker.executeFibonacci(42).then(result => {
        console.log(result);
    });
  });
  • To specify what functions will be available in your main thread you need to specify in the JavaScript code executed in parallel (in that case in the parallelExample.js file) by adding the public functions of your JavaScript code in the object worker inside the object WorkerGlobalScope.
  const _fibonacci = n => (n < 2) ? 1 : _fibonacci(n-2) + _fibonacci(n-1);

  WorkerGlobalScope.chimeraWorker = {
    executeFibonacci: _fibonacci
  }

This example will calculate the fibonacci of 42, process which normally blocks the JavaScript main thread and all it DOM, but thanks to the power of Web Workers that will not happen and thanks to the API of the library chimera-js we developers can do that in a much easier way.

Project structure

The files of the project

When you clone or download the project you will see the following files:

src/
test/
package.json
webpack.config.js
.eslintrc.json
.travis.yml
  • src: Source code of the library, with its main modules (Chimera, Meruem, Neferpitou)
  • test: The unit tests utils
  • package.json: The project dependecies and scripts
  • webpack.prod.config: The webpack configuration for transpile the source code
  • .eslintrc.json: The rulos of linting
  • .travis.yml: The configuration of Travis our CI tool

Inside the SRC folder

The files inside the src folder are structured in that way:

  • index.js: End-point and modules export
  • chimera.js: Main module to expose chimera-js API methods
  • meruem.js: Module responsible for create and manage the worker object that is created by chimera-js and runned in the web worker
  • neferpitou.worker.js: Default web worker that runs the parallel code and expose public functions to be used in the main thread. That one talks with meruem.js through the chimeraWorker object.
0.1.17

7 years ago

0.1.16

7 years ago

0.1.15

7 years ago

0.1.14

7 years ago

0.1.13

7 years ago

0.1.12

7 years ago

0.1.11

7 years ago

0.1.10

7 years ago

0.1.9

7 years ago

0.1.8

7 years ago

0.1.7

7 years ago

0.1.6

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago