Skip to main content
Version: Next 🚧

InlineWorker

InlineWorker creates a Web Worker inline from a plain function, without requiring a separate worker file. It serializes the function to a Blob URL and runs it in a background thread.

API​

new InlineWorker(func: Function)
NameTypeRequiredDescription
funcFunction✓The function to run inside the worker.

postMessage()​

Sends a message to the worker.

postMessage(data: unknown): void
NameTypeRequiredDescription
dataunknown✓The data to send to the worker.

onmessage()​

Registers a handler for messages received from the worker.

onmessage(handler: (data: MessageEvent) => void): InlineWorker
NameTypeRequiredDescription
handler(data: MessageEvent) => void✓Callback invoked with each message from the worker.

Returns InlineWorker — the instance, for chaining.

onerror()​

Registers a handler for worker errors.

onerror(handler: (data: ErrorEvent) => void): InlineWorker
NameTypeRequiredDescription
handler(data: ErrorEvent) => void✓Callback invoked on worker errors.

Returns InlineWorker — the instance, for chaining.

terminate()​

Stops the worker.

terminate(): void

Example​

example.ts
import { InlineWorker } from '@sinequa/atomic-angular';

const worker = new InlineWorker(() => {
self.onmessage = (e: MessageEvent) => {
const result = e.data * 2;
(self as unknown as Worker).postMessage(result);
};
});

worker
.onmessage((e) => console.log('result:', e.data))
.onerror((e) => console.error(e));

worker.postMessage(21); // logs: result: 42