Skip to content
easy-web-worker

API reference

defineWorker, createWorker, unwrap, the configuration, the message and the EasyWebWorker class, in one place.

API referenceTypeScriptTry an example

defineWorker

ts
import { defineWorker } from 'easy-web-worker/defineWorker';

const worker = defineWorker(({ onMessage }) => ({
  method: (payload, message, event) => result,
  inferred: onMessage((payload, message, event) => result),
  later: onMessage<Payload, Result>().handle((message, event) => {}),
}));

export type MyWorker = typeof worker;

Defines the methods of a worker file. Call it once, inside the worker. The builder receives { onMessage } and returns an object: each key is a method.

Returns a definition whose type describes the worker. Pass that type to createWorker.

createWorker

ts
import { createWorker } from 'easy-web-worker/createWorker';

const worker = createWorker<MyWorker>(source, config);

Creates a worker on the main thread and returns an object with its methods. Each method returns a CancelablePromise.

CallThe methods come from
createWorker<MyWorker>(source)The type of a definition, typeof worker
createWorker<{ ... }>(source)A plain description of the methods
createWorker((helpers, context) => …)The function itself, inferred
createWorker([fn, fn])Every function, merged; the last one wins

source is a string, a URL, a Worker, a Worker[], a function, or an array of functions. A function receives { onMessage, easyWorker } and the scope of the worker.

unwrap

ts
import { unwrap } from 'easy-web-worker/createWorker';
Called withReturns
The result of createWorkerThe EasyWebWorker instance
The result of defineWorkerThe worker-side controls: onMessage, close, importScripts

The methods of a worker never collide with the library, because the controls live behind unwrap.

Configuration

Second argument of createWorker, createEasyWebWorker and new EasyWebWorker(...).

OptionDefaultPurpose
maxWorkers1Maximum number of native Workers
warmUpWorkerstrue for one WorkerCreates the pool during initialization
keepAliveSame as warmUpWorkersKeeps idle Workers alive
terminationDelay1000 msWait before terminating idle Workers
workerOptions{}Native WorkerOptions, such as { type: 'module' }
onWorkerErrorRethrowsReceives the uncaught errors of the worker
scripts[]Scripts imported into a runtime worker
primitiveParameters[]Static values for a runtime worker, in context

The call

Every method returns a CancelablePromise<TResult>.

MemberPurpose
then, catch, finallyThe same as a native promise
cancel(reason?)Cancels the call and tells the worker
onProgress((percentage, payload) => …)Receives what the worker reports
onCancel(callback)Runs when the call is canceled

The message

Received inside the worker by onMessage callbacks, by .handle(...) and as second parameter of a method.

MemberPurpose
payloadValue sent from the main thread
messageId, methodIdentifier and method name of the call
resolve(result?, transfer?)Resolves the call
reject(reason?, transfer?)Rejects the call
cancel(reason?, transfer?)Cancels the call from inside the worker
reportProgress(percentage, payload?, transfer?)Reports progress and keeps the call pending
onCancel, onResolve, onReject, onProgressSubscribe to each outcome; return unsubscribe
onFinalizeRuns once the call ends, whatever the outcome
isPending(), getStatus()Current state of the call

EasyWebWorker

The class behind every worker. createEasyWebWorker(source, config) is a factory for it, and unwrap(worker) returns it for typed workers.

MethodPurpose
send(payload?, transfer?)Sends a message to the default handler
sendToMethod<TResult, TPayload>(method, payload?, transfer?)Sends a message to a named method
cancelAll(reason?, { force? })Cancels every pending call; force restarts instead of waiting
override(payload?, reason?, { force? })Cancels the pending calls and sends a new message
overrideAfterCurrent(payload?, reason?)Keeps the current call, cancels the rest, sends a new message
reboot(reason?)Terminates the Workers, rejects pending calls, starts again
dispose()Cancels pending calls and releases the Workers

cancelAll and reboot return a CancelablePromise<void> that settles when every call is canceled.

StaticEasyWebWorker

ts
import { createStaticEasyWebWorker } from 'easy-web-worker/createStaticEasyWebWorker';

const { onMessage, importScripts, close } =
  createStaticEasyWebWorker(defaultHandler);

The worker side of the message API. defineWorker is built on it.

Types

TypeDescribes
WorkerProxy<TWorker>What createWorker returns
WorkerDefinition<TMethods>What defineWorker returns
WorkerMethodMessage<TPayload>The message received by a method that returns its result
IEasyWebWorkerMessage<P, R>The message received by .handle(...) and the message API
IWorkerConfigThe configuration object
EasyWebWorkerBody<P, R>A function used as source by createEasyWebWorker
Documentation
↑↓ Navigate ↵ Open Esc Closeeasy-web-worker docs
Get started

Real Web Workers. Typed methods.
Cancelable promises.