0.1.0 • Published 7 months ago
@ngocro208/easy-worker v0.1.0
Easy Worker
A lightweight, zero-dependency TypeScript library to create and manage Web Workers from inline functions, eliminating the need for separate worker files. Ideal for simplifying parallelism in web applications.
Features
- No Separate Worker File: Define worker logic directly in your main script.
- TypeScript Native: Written in TypeScript with full type support.
- Async/Await Friendly: Returns Promises for easy integration.
- Dependency Imports: Supports importing external scripts into the worker using
importScripts(). - Simple API:
createInlineWorkerfunction to get started quickly. - Fallback: Gracefully handles environments where Web Workers are not supported by running tasks on the main thread asynchronously.
- Zero Dependencies: The core library has no runtime dependencies.
Installation
npm install easy-worker
# or
yarn add easy-workerUsage
01-basic-execution.ts: Basic synchronous and asynchronous function execution.02-error-handling.ts: Demonstrates how errors from worker functions are propagated.03-multiple-workers.ts: Shows creating and using multiple worker instances.index.html: External dependencies imports. HTML file uses easy-worker via ESM module since Bun JS engine does not haveimportScripts
See Examples
API
createInlineWorker<TFunc>(func: TFunc, options?: CreateInlineWorkerOptions): InlineWorker<TFunc>
Creates and returns an InlineWorker.
funcThe function to execute in the Web Worker.- Important: This function is serialized using
toString(). It must be self-contained and cannot rely on lexical closures from its original scope unless variables are simple constants or globally available in a worker context (e.g.,Math,JSON, or imported viadependencies). Pass all dynamic data as arguments. thiscontext inside the worker function will be the worker's global scope (self) orundefinedin strict mode.
- Important: This function is serialized using
options(optional):dependencies?: string[]: An array of script URLs forimportScripts().
InlineWorker<TFunc>
An executable function that is also an object with methods:
(...args: Parameters<TFunc>): Promise<ReturnType>: Call the worker.terminate(): void: Terminates the worker.getWorkerInstance(): Worker | null: Returns the rawWorkerobject (ornullin fallback mode).
Limitations
- Closures: True lexical closures are not transferred. Functions must be self-contained or rely on passed arguments / importScripts.
- DOM Access: Workers cannot directly access the main thread's DOM.
- Function Serialization: The function passed to createInlineWorker is converted to a string. This has implications for complex functions, this context, and prototypes. Standard arrow functions, function expressions, and named function declarations work best. Object method shorthands might require wrapping.
Contributing
Contributions are welcome! Please open an issue or submit a pull request. (Remember to add contribution guidelines if you wish).
0.1.0
7 months ago