Skip to content

tinylibs/tinylet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

37 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿšง Under construction ๐Ÿ‘ทโ€โ™‚๏ธ

tinylet

๐ŸŽจ redlet() and greenlet() threading helpers for Node.js, Deno, and the browser

โฑ Run an async function synchronously with redlet()
๐Ÿƒโ€โ™‚๏ธ Offload a heavy function to a Worker with greenlet()
๐ŸŒณ Fully tree-shakable
๐Ÿฆ• Supports Deno!
๐Ÿ’ป Works in the browser
โš ๏ธ redlet() requires enabling SharedArrayBuffer in the browser

Installation

npm Yarn pnpm jsDelivr

You can install this package using npm, Yarn, or pnpm:

npm install tinylet

If you're using Deno, you can import this package from an npm CDN like ESM>CDN or jsDelivr. You can also use Deno's new npm: specifier to import this package directly from npm!

import {} from "https://esm.run/tinylet";
import {} from "npm:tinylet";

If you're in the browser using a <script type="module"> tag, you can import this package straight from an npm CDN like ESM>CDN or jsDelivr!

<script type="module">
  import {} from "https://esm.run/tinylet";
</script>

Usage

Node.js Deno Browser

๐Ÿ“š Find more examples and docs on the documentation website!

You can use greenlet() to run a function asynchronously in a web worker! This is great for offloading complicated synchronous work (like image processing) to a web worker so that it doesn't block the main thread.

import { greenlet } from "tinylet";

// Runs asynchronously in a worker thread.
const f = greenlet((a, b) => {
  let n = 0;
  for (let i = 0; i < 1000000000; i++) {
    if (i % 5 === 0) n += a;
    if (i % 10 === 0) n -= i;
    if (i % 60 === 0) n *= 2;
    if (i % 75 === 0) n /= b;
  }
  return n;
});
// Takes ~3 seconds to run, but doesn't block the main thread!
console.log(await f(1, 200));
//=> -2066010092.990183

If you want to go the other way and run an async function in a worker thread, but still get the result back synchronously in the current thread, you can use redlet()! This is useful when you absolutely need something to be synchronous (like for WASM interop) but the underlying web API is asynchronous.

import { redlet } from "tinylet";

// Runs in a worker thread and uses Atomics.wait() to block the current thread.
const f = redlet(async (u) => {
  const response = await fetch(u);
  return await response.json();
});
// Takes 1 second to run and BLOCKS the current thread!
console.log(f("https://jsonplaceholder.typicode.com/todos/1"));
//=> { "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false }

โš ๏ธ redlet() works in browsers, only if you've enabled SharedArrayBuffer. Even then, you can't call redlet() on the main thread; it only works in worker threads. This is because browsers don't allow Atomics.wait() to be called on the main thread.

โœ… redlet() will always work in Node.js and other server-side environments like Deno. Those contexts all enable SharedArrayBuffer by default, and support Atomics.wait() on the main thread! ๐ŸŽ‰

Development

TODO: Add development blurb

About

๐ŸŽจ redlet(), greenlet(), bluelet(), and more threading helpers for web Workers

Resources

License

Stars

Watchers

Forks

Packages

No packages published