Skip to content

Worker

Client-side worker primitives shared by browser, Node, and Bun adapters.

This module defines the platform-neutral Worker client, the WorkerPlatform service that creates workers by numeric id, and the Spawner service used to find platform-specific worker instances. makePlatform wraps platform setup and listen hooks into a WorkerPlatform, buffers outgoing messages until the worker is ready, runs incoming messages with Effect handlers, and ties worker cleanup to scope lifetime.

9 exports Added in v4.0.0 Source

Constructors

makePlatform

Added in v4.0.0 Source

Creates a WorkerPlatform from platform-specific setup and listen hooks, buffering sent messages until the worker is ready and scoping port cleanup to the worker run.

Signature

declare function makePlatform<W>(): <
  P extends {
    readonly postMessage: (message: any, transfers?: any) => void;
  },
>(options: {
  readonly listen: (options: {
    readonly deferred: Deferred<never, WorkerError>;
    readonly emit: (data: any) => void;
    readonly port: P;
    readonly scope: Scope;
  }) => Effect<void>;
  readonly setup: (options: {
    readonly scope: Scope;
    readonly worker: W;
  }) => Effect<P, WorkerError>;
}) => {
  readonly spawn: <O = unknown, I = unknown>(
    id: number,
  ) => Effect<Worker<O, I>, WorkerError, Spawner>;
};

makeUnsafe

Added in v4.0.0 Source

Wraps platform-specific send and run functions into a Worker, translating platform ready/data messages and running the optional onSpawn effect when the worker reports readiness.

Signature

declare function makeUnsafe(options: {
  readonly run: <A, E, R>(handler: (message: PlatformMessage) => Effect<A, E, R>) => Effect<never, WorkerError | E, R>;
  readonly send: (message: unknown, transfers?: readonly Array<unknown>) => Effect<void, WorkerError>;
}): Worker<any, any>

Layers

layerSpawner

Added in v4.0.0 Source

Creates a layer that provides a worker Spawner service from a SpawnerFn.

Signature

declare const layerSpawner: <W = unknown>(spawner: SpawnerFn<W>) => Layer.Layer<Spawner>;

Models

PlatformMessage type

Added in v4.0.0 Source

Internal worker platform protocol message: [0] signals readiness and [1, payload] carries data.

Signature

type PlatformMessage = readonly [ready: 0] | readonly [data: 1, unknown];

Spawner interface

Added in v4.0.0 Source

Phantom identifier for the service that maps worker ids to platform-specific worker instances.

Signature

interface Spawner {
  readonly _: typeof _;
}

SpawnerFn interface

Added in v4.0.0 Source

Function that creates or locates a platform-specific worker instance for a numeric worker id.

Signature

interface SpawnerFn<W = unknown> {
  (id: number): W;
}

Worker interface

Added in v4.0.0 Source

Effect-based worker abstraction that can send input messages and run a long-lived handler for output messages, failing with WorkerError or handler errors.

Signature

interface Worker<O = unknown, I = unknown> {
  readonly run: <A, E, R>(handler: (message: O) => Effect<A, E, R>, options?: {
    readonly onSpawn?: Effect<void, never, never>;
  }) => Effect<never, WorkerError | E, R>;
  readonly send: (message: I, transfers?: readonly Array<unknown>) => Effect<void, WorkerError>;
}

Services

Spawner

Added in v4.0.0 Source

Service tag for the worker SpawnerFn.

Signature

declare const Spawner: Service<Spawner, SpawnerFn<unknown>>;

Service that spawns effect Worker instances for numeric worker ids using the configured Spawner.

Signature

declare class WorkerPlatform extends Shape<
  "effect/workers/Worker/WorkerPlatform",
  {
    readonly spawn: <O = unknown, I = unknown>(
      id: number,
    ) => Effect<Worker<O, I>, WorkerError, Spawner>;
  },
  this
> {
  constructor(_: never);
}