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.
Constructors
makePlatform
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
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
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
Internal worker platform protocol message: [0] signals readiness and [1, payload] carries data.
Signature
type PlatformMessage = readonly [ready: 0] | readonly [data: 1, unknown];Phantom identifier for the service that maps worker ids to platform-specific worker instances.
Signature
interface Spawner {
readonly _: typeof _;
}Function that creates or locates a platform-specific worker instance for a numeric worker id.
Signature
interface SpawnerFn<W = unknown> {
(id: number): W;
}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
Service tag for the worker SpawnerFn.
Signature
declare const Spawner: Service<Spawner, SpawnerFn<unknown>>;WorkerPlatform
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);
}
Creates a
WorkerPlatformfrom platform-specific setup and listen hooks, buffering sent messages until the worker is ready and scoping port cleanup to the worker run.