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

@athilenius/simple-webworker-actors

v0.2.1

Published

**Check out the `/examples` directory; run `cd example && yarn && yarn dev` to start the example.**

Downloads

5

Readme

Simple WebWorker Actors

Check out the /examples directory; run cd example && yarn && yarn dev to start the example.

A TS actor (multi-threading) library with the priorities...

  1. Simplicity above all else
  2. Strong, end-to-end typing
  3. Standard module syntax support via code-splitting
  4. Simple thread pool usage

Install

yarn add @athilenius/simple-webworker-actors

Usage

Start by defining the actor, which is just a wrapped function that returns an object with named methods.

basic_actor.ts

import { Actor } from '@athilenius/simple-webworker-actors';

// We can freely use imports. They will be included in the module used by
// WebWorkers.
import _ from 'lodash';

export const BasicActor = Actor(
  'basic-actor',
  async (anyArgs: number, can: { go: string }, here: boolean) => {
    let counter = 0;

    return {
      someActorMethod(messageArgs: number): string {
        counter += _.add(anyArgs, can.go.length + (here ? messageArgs : -1));
        return `Methods can optionally return values: ${counter}`;
      },
    };
  }
);

Create a top-level 'module' file that imports all our Actors.

module.ts

import './basic_actor';

Then use them!

main.ts

import { createLocalThreadPool } from '@athilenius/simple-webworker-actors';
import { BasicActor } from './basic_actor';

// Use Vite's code-splitting to create a module for the WebWorkers.
import WebworkerModule from './actors/module?worker';

async function main() {
  createLocalThreadPool(WebworkerModule, navigator.hardwareConcurrency);
  const actor = await BasicActor.spawnRemote(42, { go: '2' }, false);
  console.log(await actor.someActorMethod(1));
}

void main();

Why? How?

There are a bunch of Actor libraries already out there for JavaScript, and one of them may very well suit your needs better than this library (which is purposefully feature-lite). However, none seemed to combine simplicity with strong typing, or had limitations I was unwilling to live with like no module support, meaning no third-party library use. This library fills my own little niche need, and it seemed 'decent enough' to open source.

The how is pretty simple, it's 210 lines of TypeScript and much of that is just (admittedly complicated) typing definitions so I encourage just reading through the source. Factories are spawned in WebWorkers and Actors are spawned (invoked) by those factories via the standard message-passing mechanism. Individual actor messages are then sent over a per-actor MessageChannel. The local 'remote control' object used to send messages to an actor is a simple ES6 proxy.

How do I...

Terminate/free an actor?

You can't, they live for the lifetime of the WebWorker. Lifecycle events add more complexity than I care to take on. Ie. dropping the local 'remote control' reference to the actor will not free the actor on the service worker thread, it will simply be orphaned. However, locally spawned actors return the actor object itself, so are scoped like any other JS object.

Contributing

Please open an issue if you find a bug! And thank you ❤️ Feature requests are also welcome, but do keep the #1 stated objective of this library in mind.

License

Dual-license under MIT or Apache V2, which ever you prefer.