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

thrafe

v0.0.13

Published

A typescript library to help you get the most out of multithreading in the browser using web workers

Downloads

4

Readme

Thrafe

Pronounced like the name Rafe, as in:

My uncle, Thrafe lives in the Cayman Islands.

This is a library that aims to make it simple and straightforward to make typesafe, multithreaded web applications (using Typescript and web workers) -- hence the annoying name, Thrafe: threading + typesafe.

Why?

When working on my CommunicativeCode webapp, I found it tricky to use both Typescript and web workers for reasons I outline further below.

Additionally, existing awesome worker libraries like Comlink and workway didn't make it easy to accomplish the twoway message passing I needed (i.e. in addition to the main thread talking to the web worker, I also wanted the worker code to be able to talk to the main thread at will*):

MainThread ⇄ Worker

_*= This is probably also possible through passing callbacks using a 'proxy' mechanism in one of these libraries, but that seemed like more indirection than I wanted

A little terminology

Usage

Using thrafe requires you to do 4 things (3 development steps, and 1 build step):

  1. Define your Threading Architecture using types
  2. Instantiate a Context in your threaded code
  3. Instantiate a Thread in your client / main thread code
  4. Add the thrafe generation step in your application's build steps

Check out the specifics of each step below.

Architecture Definition

This can go in its own .ts file, or at the top of the worker file, or wherever you want really! It's just a type definition, so will be transpiled away.

An architecture consists of 6 things:

1. Thread Name:

A string literal type definition that defines what this thread should be referred to as, and utlimately the name of the file that is created by the thrafe generator.

For example:

type MyVerySpecialThreadName = "myWorkerThread";

2. To Worker Thread Events Enum:

const enum defining the events where the MAIN THREAD calls into a worker thread

For example:

const enum ToMyVerySpecialThreadEvents {
  DoSomeWork,
  FactorTheseNumbers
}

3. To Worker Thread Message Structure Definition:

blah blah blah

4. To Main Thread Events Enum:

const enum defining the events where the WORKER THREAD calls into the main thread

5. To Main Thread Message Structure Definition:

blah blah blah

5. Thread Architecture Defintion

Worker File

Main Thread

Worker code

Component code

Svelte

React

...TBD...

Vue

...TBD...

How it works

...TBD...

State of things

Why? (cont.)

Where / how should types fit it in?

Bungling Bundling

On one side, it's completely unobvious how best to compile & bundle a web worker written in typescript down to a single javascript file so that it can then be executed as a worker in the browser, e.g.:

const worker = new Worker('https://www.my-example-website.com/static/worker.js');

Bundlers are supporting this in different ways, which I think is already confusing.

Vite's solution seems pretty neat, webpack seems to be working on it (not immediately clear to me how this works with typescript), but regardless I don't like how any of the solutions I've seen.

Two-way communication

Anatomy of a Thrafe Implementation