Giter Club home page Giter Club logo

session-typed-worker's Introduction

session-typed-worker

npm version

A deadlock-free communication API for web workers based on (a subset of) session types.

Features

  • Type-safe & deadlock-free
  • Zero-dependency
  • Integration with webpack & parcel

Getting Started

Install

Also install worker-loader for importScripts().

You can use worker-plugin or parcel instead of worker-loader. In this case, the following .d.ts setting is unnecessary. Complete examples is here.

$ npm i -D worker-loader session-typed-worker

Then, set worker-loader's integrating with TypeScript.

// typings/custom.d.ts
declare module "worker-loader!*";

Note: If "esModuleInterop": true is not set, please write .d.ts as follows.

// typings/custom.d.ts
declare module "worker-loader!*" {
  const worker: any;
  export default worker;
}

Writing a protocol

Write as a type a communication procedure and the kinds of values ​​to handle. Sending from the main script to the worker is C2W, and the reverse is W2C. The following CheckNumbersEquality protocol shows the operation of sending a numbers twice to the worker, receiving a boolean from the worker.

// protocols.d.ts
import { C2W, W2C, Fin } from "session-typed-worker";

type CheckNumbersEquality = C2W<number, C2W<number, W2C<boolean, Fin>>>;

export { CheckNumbersEquality };

Writing code

The type representing communication on the main script side is taken out by giving ["client"] to the protocol.

// index.ts
import { send, recv } from "session-typed-worker";
import * as proto from "./protocols";
import Worker from "worker-loader!./worker";

const p: proto.CheckNumbersEquality["client"] = new Worker();

(async () => {
  const p1 = send(p, 42);
  const p2 = send(p1, 42);
  const [v, _] = await recv(p2);
  console.log(v); // true
})();

Here the type of p is Send<number, Send<number, Recv<boolean, Close>>>. (If you are using VSCode, you can check this with a mouseover.) This type means that you first need to send a value of type number twice with send and then receive a value of type boolean with recv. If you actually do send once, the type of the return value (i.e. p1) changes to Send<number, Recv<boolean, Close>>. Even if you write recv instead of send or apply a value of type string instead of a value of type number, you can detect it by type checking.

In TypeScript, shadowing of local variables is not allowed. Therefore, please note that it is necessary to change the variable name of the type value representing the communication operation like p1 and p2.

Just like the main script side, the type representing communication on the worker side is taken out by giving ["worker"] to the protocol.

// worker.ts
import { send, recv } from "session-typed-worker";
import * as proto from "./protocols";

const p: proto.CheckNumbersEquality["worker"] = self as any;

(async () => {
  const [v1, p1] = await recv(p);
  const [v2, p2] = await recv(p1);
  send(p2, v1 === v2);
})();

At this time, the type of p is Recv<number, Recv<number, Send<boolean, Close>>>, which is opposite to the type of p in the main script. In other words, if you sending on one side, you can guarantee that the other side is sure to be receiving and you can write code that will not cause deadlock.

Complete examples including tsconfig.json and webpack.config.js are in the examples directory.

License

MIT

session-typed-worker's People

Contributors

ahuglajbclajep avatar renovate-bot avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

session-typed-worker's Issues

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: undefined. Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/main.yaml
  • actions/checkout v2
  • actions/setup-node v2
  • actions/cache v2
npm
package.json
  • @babel/core ^7.14.8
  • @babel/preset-env ^7.14.9
  • @rollup/plugin-babel ^5.3.1
  • babel-plugin-transform-async-to-promises ^0.8.18
  • dts-jest ^23.3.0
  • jest ^26.6.3
  • rollup ^2.55.1
  • rollup-plugin-typescript2 ^0.30.0
  • typescript ^4.3.5

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.