npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

session-typed-worker

v1.0.3

Published

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

Downloads

3

Readme

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