{"_id":"@athilenius/simple-webworker-actors","name":"@athilenius/simple-webworker-actors","dist-tags":{"latest":"0.2.1"},"versions":{"0.2.1":{"name":"@athilenius/simple-webworker-actors","version":"0.2.1","license":"Dual-licensed MIT + APACHE V2.0","main":"dist/index.js","typings":"dist/index.d.ts","module":"dist/index.mjs","scripts":{"start":"tsup --watch --dts src/index.ts","build":"tsup --clean --sourcemap external --format cjs --format esm --dts src/index.ts src/index.ts"},"prettier":{"singleQuote":true},"devDependencies":{"prettier":"^2.6.2","tsup":"^5.12.7","typescript":"^4.6.4"},"gitHead":"1caed55514d19c2c97bd5e4e046e9a280d0de41c","description":"**Check out the `/examples` directory; run `cd example && yarn && yarn dev` to start the example.**","_id":"@athilenius/simple-webworker-actors@0.2.1","_nodeVersion":"14.18.2","_npmVersion":"6.14.15","dist":{"integrity":"sha512-uUloGf4Di5RJC8k/IqKCtr+ygQhziaDR3ZSr76AdmNmoKCr0K9FuhvRtRJEDzJPts0yO4j2TelznjHwFuZIMHg==","shasum":"d89ee1c66adcc7225c0cd0e9bf3c05c40edcd35e","tarball":"https://registry.npmjs.org/@athilenius/simple-webworker-actors/-/simple-webworker-actors-0.2.1.tgz","fileCount":9,"unpackedSize":33231,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDWNCAkWf5o4Jyi5t9Hu1U9KjLjhHj8CvPldJSsuR+QdgIgXY4mbySMNvgO6mv1T7a7U0eCCU+wvgi+E9H/WtrezK8="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJinOuXACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmptiw/+K35NzVVNMI8dFCvjVnDaPLNfPKfA8fasfRmjvWGNrQEUwMWJ\r\nbaLFiZTmrzGuLXwI2QzSQ0cbQOzSoHXWf7JQcK1BdRU8LvsqvKisEs12PKRe\r\nEhQuyVDSwv9DD0IL/sPlobFIETj1Vwgjr7aEMjh7Lw9lbWhR3S8WIJ8NoR9A\r\n7PsLXcLNDv8aAWVDj0KdbfiNgT7epV3307JJYfkqXZyzRtNPmQuEPOXyqlGO\r\nO+o1wK35EOFWA3JBD1IkqnBKpkp+MvnyMjM+MR3zXHsi/NUR56tFK3r6jSlg\r\nQCufVlUwLzNvR2LFqFZTHWGngdsoixQi2H5M4z2qw02Ff3th0lUdKjMkB6ZH\r\ndt/LVUtO09WT9Y/tbhJ15JYmsR61bAMgs+kvyNmUkwgFQ5zZaS9XWflg2oO/\r\nTVARuClt4OtYhfylb1Lf+mgykG5ZY8JTIhE8REQfO35MewhFF6Ghz9dCrU5S\r\n3x62zicqW9VSOe7aLu0yxY0DqYHWAr8cTBLxVslicvHDEDC4dUEbASvZ8EkS\r\n9H+HZ3r+jTI+hlPrJ692/Mf5r5l1VTjLdll/q9RdC+X47U1tJB9dHIpQP7U1\r\nqdX5rIQ9fE+U8fXh3/4mqjdoT/UDcUMWYRJzluSila0BUEZ6+xP36imJvn7z\r\n5hHGUSevgOYFkN6u7aKE02N8w221msmwA4Y=\r\n=ZFQJ\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"athilenius","email":"alec@thilenius.com"},"directories":{},"maintainers":[{"name":"athilenius","email":"alec@thilenius.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/simple-webworker-actors_0.2.1_1654451094933_0.3047857856599818"},"_hasShrinkwrap":false}},"time":{"created":"2022-06-05T17:44:54.842Z","0.2.1":"2022-06-05T17:44:55.096Z","modified":"2022-06-05T17:44:55.215Z"},"maintainers":[{"name":"athilenius","email":"alec@thilenius.com"}],"description":"**Check out the `/examples` directory; run `cd example && yarn && yarn dev` to start the example.**","license":"Dual-licensed MIT + APACHE V2.0","readme":"# Simple WebWorker Actors\n\n**Check out the `/examples` directory; run `cd example && yarn && yarn dev` to\nstart the example.**\n\n## A TS actor (multi-threading) library with the priorities...\n\n1. Simplicity above all else\n2. Strong, end-to-end typing\n3. Standard module syntax support via code-splitting\n4. Simple thread pool usage\n\n## Install\n\n```sh\nyarn add @athilenius/simple-webworker-actors\n```\n\n## Usage\n\nStart by defining the actor, which is just a wrapped function that returns an\nobject with named methods.\n\n`basic_actor.ts`\n\n```ts\nimport { Actor } from '@athilenius/simple-webworker-actors';\n\n// We can freely use imports. They will be included in the module used by\n// WebWorkers.\nimport _ from 'lodash';\n\nexport const BasicActor = Actor(\n  'basic-actor',\n  async (anyArgs: number, can: { go: string }, here: boolean) => {\n    let counter = 0;\n\n    return {\n      someActorMethod(messageArgs: number): string {\n        counter += _.add(anyArgs, can.go.length + (here ? messageArgs : -1));\n        return `Methods can optionally return values: ${counter}`;\n      },\n    };\n  }\n);\n```\n\nCreate a top-level 'module' file that imports all our Actors.\n\n`module.ts`\n\n```ts\nimport './basic_actor';\n```\n\nThen use them!\n\n`main.ts`\n\n```ts\nimport { createLocalThreadPool } from '@athilenius/simple-webworker-actors';\nimport { BasicActor } from './basic_actor';\n\n// Use Vite's code-splitting to create a module for the WebWorkers.\nimport WebworkerModule from './actors/module?worker';\n\nasync function main() {\n  createLocalThreadPool(WebworkerModule, navigator.hardwareConcurrency);\n  const actor = await BasicActor.spawnRemote(42, { go: '2' }, false);\n  console.log(await actor.someActorMethod(1));\n}\n\nvoid main();\n```\n\n---\n\n## Why? How?\n\nThere are a bunch of Actor libraries already out there for JavaScript, and one\nof them may very well suit your needs better than this library (which is\npurposefully feature-lite). However, none seemed to combine simplicity with\nstrong typing, or had limitations I was unwilling to live with like no module\nsupport, meaning no third-party library use. This library fills my own little\nniche need, and it seemed 'decent enough' to open source.\n\nThe **how** is pretty simple, it's 210 lines of TypeScript and much of that is\njust (admittedly complicated) typing definitions so I encourage just reading\nthrough the source. Factories are spawned in WebWorkers and Actors are spawned\n(invoked) by those factories via the standard message-passing mechanism.\nIndividual actor messages are then sent over a per-actor `MessageChannel`. The\nlocal 'remote control' object used to send messages to an actor is a simple ES6\nproxy.\n\n## How do I...\n\n> Terminate/free an actor?\n\nYou can't, they live for the lifetime of the WebWorker. Lifecycle events add\nmore complexity than I care to take on. Ie. dropping the local 'remote control'\nreference to the actor will not free the actor on the service worker thread, it\nwill simply be orphaned. However, locally spawned actors return the actor object\nitself, so are scoped like any other JS object.\n\n## Contributing\n\nPlease open an issue if you find a bug! And thank you ❤️ Feature requests are\nalso welcome, but do keep the #1 stated objective of this library in mind.\n\n## License\n\nDual-license under MIT or Apache V2, which ever you prefer.\n","readmeFilename":"README.md"}