Skip to content

WorkerRunner

Worker-side primitives for worker-like runtimes.

A WorkerRunner receives messages tagged by a numeric port id, sends replies back through the same transport, and can expose disconnect notifications. The module also defines the small platform message shape and the WorkerRunnerPlatform service that starts a platform-specific runner.

3 exports Added in v4.0.0 Source

Models

PlatformMessage type

Added in v4.0.0 Source

Wire protocol message used by worker platforms: a request carrying input or a close signal.

Signature

type PlatformMessage<I> = readonly [request: 0, I] | readonly [close: 1];

WorkerRunner interface

Added in v4.0.0 Source

Platform-neutral worker runner that receives inbound messages by port ID, sends outbound messages, and optionally exposes disconnect notifications.

Signature

interface WorkerRunner<O = unknown, I = unknown> {
  readonly disconnects?: Dequeue<number, never>;
  readonly run: <A, E, R>(handler: (portId: number, message: I) => void | Effect<A, E, R>) => Effect<void, WorkerError, R>;
  readonly send: (portId: number, message: O, transfers?: readonly Array<unknown>) => Effect<void>;
  readonly sendUnsafe: (portId: number, message: O, transfers?: readonly Array<unknown>) => void;
}

Services

Context service that starts a platform-specific WorkerRunner.

Signature

declare class WorkerRunnerPlatform extends Shape<
  "effect/workers/WorkerRunner/WorkerRunnerPlatform",
  {
    readonly start: <O = unknown, I = unknown>() => Effect<WorkerRunner<O, I>, WorkerError>;
  },
  this
> {
  constructor(_: never);
}