Web Workers Integration
1. Ensure TypeScript knows about worker APIs
{
"compilerOptions": {
"lib": ["dom", "dom.iterable", "esnext", "webworker"],
"types": []
}
}2. Create a worker entry file
// src/workers/fibonacci.worker.ts
/// <reference lib="webworker" />
function fibonacci(n: number): number {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
self.onmessage = (event: MessageEvent<number>) => {
const input = event.data;
const result = fibonacci(input);
self.postMessage(result);
};3. Wrap worker creation for DI-friendly usage
4. Declare a module type for worker imports (optional)
5. Use the worker inside an Aurelia component
6. Sharing types between main thread and worker
7. Debugging tips
Last updated
Was this helpful?