npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

uman

v1.0.3

Published

Units manager javascript library to orchestrate web workers

Downloads

91

Readme

Units Manager

A javascript library to split your code with web workers

GitHub Workflow Status David npm bundle size npm

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.