https://github.com/pulsejet/webworker-typed
Dead simple type-safe Web Workers.
https://github.com/pulsejet/webworker-typed
Last synced: 17 days ago
JSON representation
Dead simple type-safe Web Workers.
- Host: GitHub
- URL: https://github.com/pulsejet/webworker-typed
- Owner: pulsejet
- License: mit
- Created: 2023-10-31T00:41:52.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-08-19T05:58:26.000Z (8 months ago)
- Last Synced: 2025-03-24T10:38:01.833Z (about 1 month ago)
- Language: TypeScript
- Size: 10.7 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# webworker-typed
Dead simple type-safe Web Workers.
## Installation
```bash
npm install webworker-typed
```## Usage
Create a worker file and export the functions you want to expose:
```ts
// my-worker.ts
import { exportWorker } from 'webworker-typed';function foo() { return 'bar'; }
async function asyncFoo() { return 'bar'; }
export default exportWorker({
foo,
asyncFoo,
inline: () => 'bar',
});
```From your main thread, import the worker and call the functions:
```ts
// main.ts
import { importWorker } from 'webworker-typed';
import type MyWorker from './my-worker.ts';const untypedWorker = new Worker(new URL('./my-worker.ts', import.meta.url)); // example with webpack
const worker = importWorker(untypedWorker);(async () => {
// all imported methods are async
console.assert(await worker.foo() === 'bar');
console.assert(await worker.asyncFoo() === 'bar');
console.assert(await worker.inline() === 'bar');
})();
```## License
This project is licensed under the MIT License.