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

rodosha

v1.2.0

Published

Rodosha: multi-threading using workers and proxy objects

Downloads

7

Readme

USRZ Rodosha API

Rodosha (労働者 - or "worker" in Japanese) are an easier way to deal with multi-threading in the browser by using Web Workers

The implementation is wrapped in a rodosha Esquire module which provides a create(...) method. This method will return a Promise which will be resolved with the instance of the Rodosha or rejected with a failure:

esquire.inject(['rodosha'], function(rodoshaFactory) {
  rodoshaFactory.create().then(function(rodosha) {
    // foo! do something...
  });
})

Rodoshas operate mainly of Esquire modules which can be imported directly in a remote worker:

rodosha.import('module-a', 'module-b').then(function(imported) {
  // the modules (and all their dipendencies) were imported...
});

Object proxies

After those modules are imported, local proxy objects pointing to their instances in the worker can be created quite trivially:

rodosha.proxy('module-a').then(function(proxy) {
  // the "proxy" variable is a local object proxying an instance in the worker
})

Any method and variable in objects being proxied will be replaced with a Promise, and method execution, or value retrieval will trigger a message, be executed in the Worker and its result will resolve or reject the promise.

So if a module defines a method called foo() and a variable bar like:

{
  foo: function(arg) {
    return "Called with " + arg;
  },
  bar: "hello, world"
}

It's proxy will return promises for both:

proxy.foo("my value").then(result) {
  // result will be "Called with my value"
}
proxy.bar.then(value) {
  // value will be "hello world"
}

What will happen under the covers is that messages will be sent to the Worker asking for the method to be executed in its remote native thread (or the variable's value to be evaluated) and once a response is received locally the returned promises will be resolved or rejected.

Proxies from functions

A special note for function calls is that their return values can also be retrieved as a proxy object by invoking the asProxy() method on the returned promise.

So, for example, if a function is defined as:

function gimmeAnObject() {
  // this will return a complex object, with functions and properties
}

Locally its result can be used through a proxy (henceforth, its methods will still be invoked - and variables evaluated - in the Worker):

proxy.gimmeAnObject().asProxy().then(function(newProxy) {
  // newProxy will be a proxy object to what's returned...
})

Cleaning up

Proxies can (should) be discarded when no longer needed, freeing up memory in the Worker:

rodosha.destroy(proy);

The Worker itself can be closed gracefully

rodosha.close().then(function() {
  // nicely closed
});

or terminated abruptly calling rodosha.terminate().