Units Manager
A javascript library to split your code with web workers
About
Being a small but robust javascript library, the Uman lets easily split your code by separarted modules - units. Even more, you may define units as web workers to have pure multithreading way of programming and don't think about communication between workers.
Features
- ES6+
- small size
- no dependency
- units lazy loading
- code splitting support
- transferable objects accepted
- easy communication between units
- pure multithreading with web workers
- dedicated, shared and service workers support
Why?
Javascript is single threaded. The browser freezes UI and other operations if task eats a lot of resources and time to do things.
The best choice to avoid that is web workers to run your code in background threads independently from the main thread. This also gives you pure multithreading approach.
To start code with worker you usually do the following:
main.js
// ask worker to do thingsworker;// sometime or neverworker { // check if it's for you // do things};
worker.js
// reply { // check it's for you // do things, reply ;};
It looks nice for a task.
But!
What if you have more than one? What if you need to run tasks in separate workers? How to communicate between them and the main thread? How to pass objects with methods to workers or back? How to avoid code duplication?
With the Uman everything is as simple as if you code in asynchronous way:
index.js
// ask worker to do thingsconst result = await unit;// do things with the result// or catch an error
unit.js
class { // do things, reply return result; } ;
To run tasks in separate workers and communicate between them:
index.js
const main = Manager; // set up unitsmain; // domainunitsone;
one.js
class async { const two six ten = thisunits; // ask other workers to do things const result = await Promiseall two six ; // ask "ten" to do things, and reply return await ten; } ;
two.js
, six.js
or ten.js
class async { // do things, reply return await something... } ;
Examples
There are some working examples to test the Uman.
Clone repository and:
npm i
npm run dev
Then open browser with http://loclahost:8080.
Getting started
Install the Uman with npm i -D uman
and use it with import
.
TODO
- node.js worker support
- communication with server units
Contacts
Please feel free to contact me if you have questions or open an issue.
License
Copyright © 2019-2020 G. Schurovski
Licensed under the Apache-2.0 license.