Web Worker Proxies is a tiny (< 850 byte) implementation of RPC-style calls to web-workers.
This script is an brief exploration of the question, what if we used web workers to host more of our objects?
It's designed to be simple, interesting, and handy.
This example, which is running in your browser right now, is comprised of two scripts, a web worker and browser script.
The worker script references web-worker-proxies.js and defines a proxy object. The proxy script takes care of listening to messages and routing them to methods in the proxy definition.
The browser script defines a worker and calls proxy() on it. This returns a proxy object that can be used to remotely invoke methods on the web worker. Here, add is called with parameters 2 and 3.
We also pass a Node-style callback. If an error occurs in the remote web worker, error will be a string representing that error. The second argument in the callback, result, just returns the result of the function as evaluated by the web worker.
In this example, once we get the result back from, we bind the result to the our DOM element. You can run this code by pressing the 'Show Me' button below.
You can experiment with making calls to the web worker on this page by copy-and-pasting the following code into your dev console. Change the parameters or method name to see what happens.
Just reference web-worker-proxy.js or web-worker-proxy.min.js in your scripts to get started.