chimera-js
v0.1.17
Published
Javascript library to facilitate the use of the web workers api.
Downloads
24
Maintainers
Readme
chimera-js
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 methodexportToWorker
. OBS: In version0.1.17
you can't export arrow functions in a single line. You need to use the parentheses and the braces when using the methodexportToWorker
.
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 methodsetWorker
.
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 fileparallelExample.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 parameterresult
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 objectworker
inside the objectWorkerGlobalScope
.
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 utilspackage.json
: The project dependecies and scriptswebpack.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 exportchimera.js
: Main module to exposechimera-js
API methodsmeruem.js
: Module responsible for create and manage the worker object that is created by chimera-js and runned in the web workerneferpitou.worker.js
: Default web worker that runs the parallel code and expose public functions to be used in the main thread. That one talks withmeruem.js
through thechimeraWorker
object.