(In development)
This is a open source JavaScript library that seeks to help developers by simplifying the use of Web Workers API.
We have a example project on github ChimeraJSPOC.
But basically, here is where you can get started:
chimera-js
into your JavaScript code: import Chimera from 'chimera-js';
We have two ways to use a parallel web worker using chimera-js
;
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
});
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
});
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);
});
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);
});
});
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.
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 toolThe files inside the src folder are structured in that way:
index.js
: End-point and modules exportchimera.js
: Main module to expose chimera-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 with meruem.js
through the chimeraWorker
object.