jarvice
TypeScript icon, indicating that this package has built-in type declarations

0.7.0 • Public • Published

jarvice

Small yet mighty library for multithreaded browser applications

Github license npm Downloads npm

About

jarvice is a tiny library that helps you develop maintainable multithreaded browser applications and simplifies the code written atop the Channel Messaging API.

Features

  • supports TypeScript as a first-class citizen
  • supports module bundlers like webpack and parcel
  • zero dependencies

Usage

jarvice is based on a creation of nodes. Each node represents a separate JavaScript thread (currently Window or a Web Worker) and exposes it's own external API for other nodes and threads. The nodes' connections can be passed down the spawn tree so for example the nested workers can still access the main thread.

Using methods:

worker.ts

import { node } from 'jarvice';

node()
  .method('greetPromise', async ([who]) => `Hello ${who}!`)
  .method('greetCallback', ([who], callback) => callback(`Hey ${who}!`));

window.ts

import { connect } from 'jarvice';

const worker = connect(new Worker('./worker.ts'));

const main = async () => {
  // 'Hello Dave!'
  console.log(await worker.methods.greetPromise('Dave'));
  // 'Hey David!'
  console.log(await worker.methods.greetCallback('David'));
};

main();

Using topics:

worker.ts

import { node } from 'jarvice';

const { publish } = node().topic('SECOND');

let i = -1;
setInterval(() => publish('SECOND', (i += 1)), 1000);

window.ts

import { connect } from 'jarvice';

const worker = connect(new Worker('./worker.ts'));

const unsubscribe = worker.topics.SECOND.subscribe((i) => {
  // 0, 1, 2, 3...
  console.log(i);
});

Passing down the nodes connections:

In this example we create a topic, pass down the app node interface to the worker and begin to listen on user's input on another thread.

window.ts

import { node, connect } from 'jarvice';

const app = node().topic('USER_INPUT');
const worker = connect(new Worker('./worker.ts'));

document.body.querySelector('#input').addEventListener((event) => {
  app.publish('USER_INPUT', event.currentTarget.value);
});

worker.connect('app', app);

worker.ts

import { node } from 'jarvice';

const worker = node();

const main = async () => {
  const app = await worker.getConnectedNode('app');

  const unsubscribe = app.topics.USER_INPUT.subscribe(console.log);
};

main();

Terminating nodes:

Nodes can be terminated by using terminate method available in the connected threads.

window.ts

import { node, connect } from 'jarvice';

const worker = connect(new Worker('./worker.ts'));

setTimeout(() => worker.terminate(), 1000);

Using typings schemas:

This library provides helper typings so you can benefit from TypeScript IDEs and type checking.

schemas.ts

import { NodeMethodDefinition, NodeSchema, NodeTopicDefinition } from 'jarvice';

// First we define methods, then topics, then connectable nodes schemas.
export type WorkerNodeSchema = NodeSchema<
  { formatTime: NodeMethodDefinition<[number, string], string> },
  { FORMATTED_TIME: NodeTopicDefinition<string> }
>;

worker.ts

Depending on the IDE you use, as you type the following code you should see names suggestions and typing errors highlighting.

import { node } from 'jarvice';
import { WorkerNodeSchema } from './schemas';
import { format } from 'date-fns';

const worker = node<WorkerNodeSchema>()
  .method('formatTime', async ([millis, pattern]) => format(millis, pattern))
  .topic('FORMATTED_TIME');

setInterval(
  () => worker.publish('FORMATTED_TIME', format(Date.now(), '')),
  1000,
);

app.ts

Same as in the above snippet, the schemas can be passed to the nodes interfaces.

import { connect } from 'jarvice';

import { WorkerNodeSchema } from './schemas';

const worker = connect<WorkerNodeSchema>(new Worker('./worker.ts'));

const main = async () => {
  const result = await worker.methods.formatTime(Date.now(), 'YYYY-MM-DD');
};

main();

Package Sidebar

Install

npm i jarvice

Weekly Downloads

0

Version

0.7.0

License

MIT

Unpacked Size

297 kB

Total Files

71

Last publish

Collaborators

  • mchyb