Skip to content

Worker.ts

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.

Since v4.0.0



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 const makePlatform: <W>() => <
P extends { readonly postMessage: (message: any, transfers?: any | undefined) => void }
>(options: {
readonly setup: (options: { readonly worker: W; readonly scope: Scope.Scope }) => Effect.Effect<P, WorkerError>
readonly listen: (options: {
readonly port: P
readonly emit: (data: any) => void
readonly deferred: Deferred.Deferred<never, WorkerError>
readonly scope: Scope.Scope
}) => Effect.Effect<void>
}) => WorkerPlatform["Service"]

Source

Since v4.0.0

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

Signature

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

Source

Since v4.0.0

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

Signature

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

Source

Since v4.0.0

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

Signature

export interface Spawner {
readonly _: unique symbol
}

Source

Since v4.0.0

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

Signature

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

Source

Since v4.0.0

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

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

Source

Since v4.0.0

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

Signature

declare class WorkerPlatform

Source

Since v4.0.0

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 const makeUnsafe: (options: {
readonly send: (message: unknown, transfers?: ReadonlyArray<unknown>) => Effect.Effect<void, WorkerError>
readonly run: <A, E, R>(
handler: (message: PlatformMessage) => Effect.Effect<A, E, R>
) => Effect.Effect<never, E | WorkerError, R>
}) => Worker<any, any>

Source

Since v4.0.0

Service tag for the worker SpawnerFn.

Signature

declare const Spawner: Context.Service<Spawner, SpawnerFn<unknown>>

Source

Since v4.0.0