@ch1/rpc-worker

JavaScript Remote Procedure Call (RPC) - Web Worker Adapter

Usage no npm install needed!

<script type="module">
  import ch1RpcWorker from 'https://cdn.skypack.dev/@ch1/rpc-worker';
</script>

README

CH1 RPC

CircleCI

This is not well maintained

This is not traditional RPC, but it is like it

Installation

yarn add @ch1/rpc-worker

Usage

Slightly easier API than in the raw @ch1/rpc

Main JS Script

import * as wrpc from '@ch1/rpc-worker';

const w = new Worker(TEST_FILE);
const rpc = wrpc.create({ worker: w });

rpc.ready
  .then(() => rpc.remote.foo())
  .then(result => {
    expect(result).toBe(7);
    done();
  })
  .catch(done);

Worker JS

import * as wrpc from '@ch1/rpc-worker';

wrpc.create(undefined, {
  foo: () => new Promise(resolve => resolve(7)),
});

Error Handling

  • Individual remote function calls handle their own errors through their own async interfaces
  • Global errors related specifically to the workers trigger onDestroy
  • Calling RPC<RemoteType>.destroy() will call terminate on the worker
import * as wrpc from '@ch1/rpc-worker';

const w = new Worker(TEST_FILE);
const rpc = wrpc.create({ worker: w });

rpc.ready
  .then(() => rpc.remote.foo())
  .then(result => {
    expect(result).toBe(7);
    done();
  })
  .catch(done);

// handle onDestroy here
rpc.onDestroy((reason?: string) => {
  // cleanup, restart, whatever
});

API

The worker extension is pretty much as described above. For more information on the RPC<RemoteType> object see the @ch1/rpc docs

License

LGPL