@ch1/rpc-worker
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

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

Package Sidebar

Install

npm i @ch1/rpc-worker

Weekly Downloads

2

Version

1.1.0

License

LGPL-3.0

Unpacked Size

29.2 kB

Total Files

25

Last publish

Collaborators

  • bennett000