USRZ Rodosha API
It also works in Node.JS.
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
Rodosha
s operate mainly of Esquire modules which can be imported directly
in a remote worker
:
rodosha;
Object proxies
After those modules are imported, local proxy objects pointing to their instances in the worker can be created quite trivially:
rodosha
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:
{ return "Called with " + arg; } bar: "hello, world"
It's proxy will return promises for both:
proxy // result will be "Called with my value"proxybar // 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:
{ // 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
Cleaning up
Proxies can (should) be discarded when no longer needed, freeing up memory
in the Worker
:
rodosha;
The Worker
itself can be closed gracefully
rodosha;
or terminated abruptly calling rodosha.terminate()
.
Further reading
Licensed under the Apache Software License 2.0
The full API documentation is avaiblable here.