Skip to content

BlackAsLight/thread

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Thread

Thread is a small module that wraps around the Web Worker API to offer a more promise like experience to pass values to and receive values from web workers.

Example main.ts

import { assertEquals } from "jsr:@std/assert";
import { Thread } from "jsr:@doctor/thread";

const thread = new Thread<[number[], number]>(
  new URL("./worker.ts", import.meta.url).href,
);

assertEquals(await thread.send([1, 2, 3]), 6);
thread.terminate();

Example worker.ts

import { listen } from "jsr:@doctor/thread/worker";

listen<[number[], number]>(function (input) {
  return [input.reduce((x, y) => x + y)];
});