uman
v1.0.3
Published
Units manager javascript library to orchestrate web workers
Downloads
91
Maintainers
Readme
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 things
worker.postMessage(message);
// sometime or never
worker.onmessage = event => {
// check if it's for you
// do things
};
worker.js
// reply
onmessage = event => {
// check it's for you
// do things, reply
postMessage(message);
};
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 things
const result = await unit.dothings(...args);
// do things with the result
// or catch an error
unit.js
export default Unit(
class {
dothings(...args) {
// do things, reply
return result;
}
}
);
To run tasks in separate workers and communicate between them:
index.js
const main = new Unit(Manager)();
// set up units
main.add({
// as a worker thread
// with lazy loading
one: () => new Worker("one.js"),
two: () => new SharedWorker("two.js"),
// ...
// main thread unit
// with lazy loading
six: () => import("six.js"),
// as a service worker
// from source "ten.js"
// has to be activated
ten: () => window.navigator.serviceWorker
});
// do
main.units.one.task(...args);
one.js
export default Unit(
class {
async task(...args) {
const { two, six, ten } = this.units;
// ask other workers to do things
const result = await Promise.all([
two.dothings(...args),
six.dothings(...args)
]);
// ask "ten" to do things, and reply
return await ten.dothings(result);
}
}
);
two.js
, six.js
or ten.js
export default Unit(
class {
async dothings(...args) {
// 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.