(
spawn: (id: number) => WorkerThreads.Worker | ChildProcess.ChildProcess
): Layer.Layer<Worker.WorkerPlatform | Worker.Spawner>Provides the Node WorkerPlatform together with a Worker.Spawner created
from the supplied worker or child-process spawning function.
export const const layer: (
spawn: (
id: number
) =>
| WorkerThreads.Worker
| ChildProcess.ChildProcess
) => Layer.Layer<
Worker.WorkerPlatform | Worker.Spawner
>
Provides the Node WorkerPlatform together with a Worker.Spawner created
from the supplied worker or child-process spawning function.
layer = (
spawn: (
id: number
) =>
| WorkerThreads.Worker
| ChildProcess.ChildProcess
spawn: (id: numberid: number) => import WorkerThreadsWorkerThreads.type WorkerThreads.Worker = /*unresolved*/ anyWorker | import ChildProcessChildProcess.type ChildProcess.ChildProcess = /*unresolved*/ anyChildProcess
): import LayerLayer.interface Layer<in ROut, out E = never, out RIn = never>A Layer describes how to build one or more services for dependency injection.
When to use
Use to model construction of application services for dependency injection,
especially when services have dependencies, can fail during construction, or
need scoped setup and release.
Details
A Layer<ROut, E, RIn> represents ROut as the services this layer
provides, E as the possible errors during layer construction, and RIn as
the services this layer requires as dependencies.
Layer<import WorkerWorker.class WorkerPlatformclass WorkerPlatform {
key: Identifier;
Service: {
spawn: <O = unknown, I = unknown>(id: number) => Effect.Effect<Worker<O, I>, WorkerError, Spawner>;
};
}
Service that spawns effect Worker instances for numeric worker ids using
the configured Spawner.
WorkerPlatform | import WorkerWorker.Spawner> =>
import LayerLayer.const merge: {
<RIn, E, ROut>(that: Layer<ROut, E, RIn>): <
RIn2,
E2,
ROut2
>(
self: Layer<ROut2, E2, RIn2>
) => Layer<ROut | ROut2, E | E2, RIn | RIn2>
<Layers extends [Any, ...Array<Any>]>(
that: Layers
): <A, E, R>(
self: Layer<A, E, R>
) => Layer<
A | Success<Layers[number]>,
E | Error<Layers[number]>,
Services<Layers[number]> | R
>
<RIn2, E2, ROut2, RIn, E, ROut>(
self: Layer<ROut2, E2, RIn2>,
that: Layer<ROut, E, RIn>
): Layer<ROut | ROut2, E | E2, RIn | RIn2>
<A, E, R, Layers extends [Any, ...Array<Any>]>(
self: Layer<A, E, R>,
that: Layers
): Layer<
A | Success<Layers[number]>,
E | Error<Layers[number]>,
Services<Layers[number]> | R
>
}
Merges this layer with another layer concurrently, producing a new layer with
combined input, error, and output types.
When to use
Use to combine an existing Layer with another Layer or an array of
layers while preserving pipeline style.
Details
This is a binary version of mergeAll that merges exactly two layers or one
layer with an array of layers. The layers are built concurrently and their
outputs are combined.
Example (Merging two layers)
import { Context, Effect, Layer } from "effect"
class Database extends Context.Service<Database, {
readonly query: (sql: string) => Effect.Effect<string>
}>()("Database") {}
class Logger extends Context.Service<Logger, {
readonly log: (msg: string) => Effect.Effect<void>
}>()("Logger") {}
const dbLayer = Layer.succeed(Database, {
query: Effect.fn("Database.query")((sql: string) => Effect.succeed("result"))
})
const loggerLayer = Layer.succeed(Logger, {
log: Effect.fn("Logger.log")((msg: string) => Effect.sync(() => console.log(msg)))
})
const mergedLayer = Layer.merge(dbLayer, loggerLayer)
merge(
import WorkerWorker.const layerSpawner: <W = unknown>(
spawner: SpawnerFn<W>
) => Layer.Layer<Spawner>
Creates a layer that provides a worker Spawner service from a SpawnerFn.
layerSpawner(spawn: (
id: number
) =>
| WorkerThreads.Worker
| ChildProcess.ChildProcess
spawn),
const layerPlatform: Layer.Layer<Worker.WorkerPlatform>const layerPlatform: {
build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<WorkerPlatform>, never, never>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
Provides the Node WorkerPlatform for worker_threads workers and child
process workers, wiring messages, errors, and exits into Effect workers and
terminating the worker if graceful shutdown times out.
layerPlatform
)