Worker.ts
Worker.ts overview
Section titled “Worker.ts overview”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
Exports Grouped by Category
Section titled “Exports Grouped by Category”constructors
Section titled “constructors”makePlatform
Section titled “makePlatform”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"]Since v4.0.0
layers
Section titled “layers”layerSpawner
Section titled “layerSpawner”Creates a layer that provides a worker Spawner service from a SpawnerFn.
Signature
declare const layerSpawner: <W = unknown>(spawner: SpawnerFn<W>) => Layer.Layer<Spawner>Since v4.0.0
models
Section titled “models”PlatformMessage (type alias)
Section titled “PlatformMessage (type alias)”Internal worker platform protocol message: [0] signals readiness and
[1, payload] carries data.
Signature
type PlatformMessage = readonly [ready: 0] | readonly [data: 1, unknown]Since v4.0.0
Spawner (interface)
Section titled “Spawner (interface)”Phantom identifier for the service that maps worker ids to platform-specific worker instances.
Signature
export interface Spawner { readonly _: unique symbol}Since v4.0.0
SpawnerFn (interface)
Section titled “SpawnerFn (interface)”Function that creates or locates a platform-specific worker instance for a numeric worker id.
Signature
export interface SpawnerFn<W = unknown> { (id: number): W}Since v4.0.0
Worker (interface)
Section titled “Worker (interface)”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>}Since v4.0.0
WorkerPlatform (class)
Section titled “WorkerPlatform (class)”Service that spawns effect Worker instances for numeric worker ids using
the configured Spawner.
Signature
declare class WorkerPlatformSince v4.0.0
makeUnsafe
Section titled “makeUnsafe”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>Since v4.0.0
services
Section titled “services”Spawner
Section titled “Spawner”Service tag for the worker SpawnerFn.
Signature
declare const Spawner: Context.Service<Spawner, SpawnerFn<unknown>>Since v4.0.0