Skip to main content Link Search Menu Expand Document (external link)

Worker overview

Added in v1.0.0


Table of contents


constructors

makeManager

Signature

export declare const makeManager: Effect.Effect<WorkerManager, never, PlatformWorker>

Added in v1.0.0

makePool

Signature

export declare const makePool: <I, O, E>(
  options: WorkerPool.Options<I>
) => Effect.Effect<WorkerPool<I, O, E>, WorkerError, WorkerManager | Spawner | Scope.Scope>

Added in v1.0.0

makePoolLayer

Signature

export declare const makePoolLayer: <Tag, I, O, E>(
  tag: Context.Tag<Tag, WorkerPool<I, O, E>>,
  options: WorkerPool.Options<I>
) => Layer.Layer<Tag, WorkerError, WorkerManager | Spawner>

Added in v1.0.0

makePoolSerialized

Signature

export declare const makePoolSerialized: <I extends Schema.TaggedRequest.All>(
  options: SerializedWorkerPool.Options<I>
) => Effect.Effect<SerializedWorkerPool<I>, WorkerError, WorkerManager | Spawner | Scope.Scope>

Added in v1.0.0

makeSerialized

Signature

export declare const makeSerialized: <I extends Schema.TaggedRequest.All>(
  options: SerializedWorker.Options<I>
) => Effect.Effect<SerializedWorker<I>, WorkerError, WorkerManager | Spawner | Scope.Scope>

Added in v1.0.0

layers

layerManager

Signature

export declare const layerManager: Layer.Layer<WorkerManager, never, PlatformWorker>

Added in v1.0.0

layerSpawner

Signature

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

Added in v1.0.0

makePoolSerializedLayer

Signature

export declare const makePoolSerializedLayer: <Tag, I extends Schema.TaggedRequest.All>(
  tag: Context.Tag<Tag, SerializedWorkerPool<I>>,
  options: SerializedWorkerPool.Options<I>
) => Layer.Layer<Tag, WorkerError, WorkerManager | Spawner>

Added in v1.0.0

models

BackingWorker (interface)

Signature

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

Added in v1.0.0

BackingWorker (namespace)

Added in v1.0.0

Message (type alias)

Signature

export type Message<O> = readonly [ready: 0] | readonly [data: 1, O]

Added in v1.0.0

PlatformWorker (interface)

Signature

export interface PlatformWorker {
  readonly [PlatformWorkerTypeId]: PlatformWorkerTypeId
  readonly spawn: <I, O>(id: number) => Effect.Effect<BackingWorker<I, O>, WorkerError, Spawner>
}

Added in v1.0.0

SerializedWorker (interface)

Signature

export interface SerializedWorker<I extends Schema.TaggedRequest.All> {
  readonly id: number
  readonly execute: <Req extends I>(
    message: Req
  ) => Req extends Schema.WithResult<infer A, infer _I, infer E, infer _EI, infer R>
    ? Stream.Stream<A, E | WorkerError | ParseResult.ParseError, R>
    : never
  readonly executeEffect: <Req extends I>(
    message: Req
  ) => Req extends Schema.WithResult<infer A, infer _I, infer E, infer _EI, infer R>
    ? Effect.Effect<A, E | WorkerError | ParseResult.ParseError, R>
    : never
}

Added in v1.0.0

SerializedWorker (namespace)

Added in v1.0.0

Options (type alias)

Signature

export type Options<I> =
  Extract<I, { readonly _tag: "InitialMessage" }> extends never
    ? {
        readonly initialMessage?: LazyArg<I>
      }
    : {
        readonly initialMessage: LazyArg<Extract<I, { readonly _tag: "InitialMessage" }>>
      }

Added in v1.0.0

SerializedWorkerPool (interface)

Signature

export interface SerializedWorkerPool<I extends Schema.TaggedRequest.All> {
  readonly backing: Pool.Pool<SerializedWorker<I>, WorkerError>
  readonly broadcast: <Req extends I>(
    message: Req
  ) => Req extends Schema.WithResult<infer _A, infer _I, infer E, infer _EI, infer R>
    ? Effect.Effect<void, E | WorkerError | ParseResult.ParseError, R>
    : never
  readonly execute: <Req extends I>(
    message: Req
  ) => Req extends Schema.WithResult<infer A, infer _I, infer E, infer _EI, infer R>
    ? Stream.Stream<A, E | WorkerError | ParseResult.ParseError, R>
    : never
  readonly executeEffect: <Req extends I>(
    message: Req
  ) => Req extends Schema.WithResult<infer A, infer _I, infer E, infer _EI, infer R>
    ? Effect.Effect<A, E | WorkerError | ParseResult.ParseError, R>
    : never
}

Added in v1.0.0

SerializedWorkerPool (namespace)

Added in v1.0.0

Options (type alias)

Signature

export type Options<I> = SerializedWorker.Options<I> &
  (
    | {
        readonly onCreate?: (worker: Worker<I, unknown, unknown>) => Effect.Effect<void, WorkerError>
        readonly size: number
        readonly concurrency?: number | undefined
        readonly targetUtilization?: number | undefined
      }
    | {
        readonly onCreate?: (worker: Worker<I, unknown, unknown>) => Effect.Effect<void, WorkerError>
        readonly minSize: number
        readonly maxSize: number
        readonly concurrency?: number | undefined
        readonly targetUtilization?: number | undefined
        readonly timeToLive: Duration.DurationInput
      }
  )

Added in v1.0.0

Spawner (interface)

Signature

export interface Spawner {
  readonly _: unique symbol
}

Added in v1.0.0

SpawnerFn (interface)

Signature

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

Added in v1.0.0

Worker (interface)

Signature

export interface Worker<I, O, E = never> {
  readonly id: number
  readonly execute: (message: I) => Stream.Stream<O, E | WorkerError>
  readonly executeEffect: (message: I) => Effect.Effect<O, E | WorkerError>
}

Added in v1.0.0

Worker (namespace)

Added in v1.0.0

Options (interface)

Signature

export interface Options<I> {
  readonly encode?: ((message: I) => Effect.Effect<unknown, WorkerError>) | undefined
  readonly initialMessage?: LazyArg<I> | undefined
}

Added in v1.0.0

Request (type alias)

Signature

export type Request<I = unknown> =
  | readonly [id: number, data: 0, I, trace: Span | undefined]
  | readonly [id: number, interrupt: 1]

Added in v1.0.0

Response (type alias)

Signature

export type Response<E, O = unknown> =
  | readonly [id: number, data: 0, ReadonlyArray<O>]
  | readonly [id: number, end: 1]
  | readonly [id: number, end: 1, ReadonlyArray<O>]
  | readonly [id: number, error: 2, E]
  | readonly [id: number, defect: 3, Schema.CauseEncoded<WorkerErrorFrom, unknown>]

Added in v1.0.0

Span (type alias)

Signature

export type Span = readonly [traceId: string, spanId: string, sampled: boolean]

Added in v1.0.0

WorkerManager (interface)

Signature

export interface WorkerManager {
  readonly [WorkerManagerTypeId]: WorkerManagerTypeId
  readonly spawn: <I, O, E>(
    options: Worker.Options<I>
  ) => Effect.Effect<Worker<I, O, E>, WorkerError, Scope.Scope | Spawner>
}

Added in v1.0.0

WorkerPool (interface)

Signature

export interface WorkerPool<I, O, E = never> {
  readonly backing: Pool.Pool<Worker<I, O, E>, WorkerError>
  readonly broadcast: (message: I) => Effect.Effect<void, E | WorkerError>
  readonly execute: (message: I) => Stream.Stream<O, E | WorkerError>
  readonly executeEffect: (message: I) => Effect.Effect<O, E | WorkerError>
}

Added in v1.0.0

WorkerPool (namespace)

Added in v1.0.0

Options (type alias)

Signature

export type Options<I> = Worker.Options<I> &
  (
    | {
        readonly onCreate?: (worker: Worker<I, unknown, unknown>) => Effect.Effect<void, WorkerError>
        readonly size: number
        readonly concurrency?: number | undefined
        readonly targetUtilization?: number | undefined
      }
    | {
        readonly onCreate?: (worker: Worker<I, unknown, unknown>) => Effect.Effect<void, WorkerError>
        readonly minSize: number
        readonly maxSize: number
        readonly concurrency?: number | undefined
        readonly targetUtilization?: number | undefined
        readonly timeToLive: Duration.DurationInput
      }
  )

Added in v1.0.0

tags

PlatformWorker

Signature

export declare const PlatformWorker: Context.Tag<PlatformWorker, PlatformWorker>

Added in v1.0.0

Spawner

Signature

export declare const Spawner: Context.Tag<Spawner, SpawnerFn<unknown>>

Added in v1.0.0

WorkerManager

Signature

export declare const WorkerManager: Context.Tag<WorkerManager, WorkerManager>

Added in v1.0.0

type ids

PlatformWorkerTypeId

Signature

export declare const PlatformWorkerTypeId: typeof PlatformWorkerTypeId

Added in v1.0.0

PlatformWorkerTypeId (type alias)

Signature

export type PlatformWorkerTypeId = typeof PlatformWorkerTypeId

Added in v1.0.0

WorkerManagerTypeId

Signature

export declare const WorkerManagerTypeId: typeof WorkerManagerTypeId

Added in v1.0.0

WorkerManagerTypeId (type alias)

Signature

export type WorkerManagerTypeId = typeof WorkerManagerTypeId

Added in v1.0.0

utils

makePlatform

Signature

export 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>
}) => PlatformWorker

Added in v1.0.0