Layer.Layer<WorkerRunner.WorkerRunnerPlatform, never, never>Provides the WorkerRunnerPlatform for code running inside a Node worker
thread or child process, routing parent messages to the registered handler
and sending responses back through the parent channel.
export const const layer: Layer.Layer<WorkerRunner.WorkerRunnerPlatform>const layer: {
build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<WorkerRunnerPlatform>, 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 WorkerRunnerPlatform for code running inside a Node worker
thread or child process, routing parent messages to the registered handler
and sending responses back through the parent channel.
layer: 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 WorkerRunnerWorkerRunner.class WorkerRunnerPlatformclass WorkerRunnerPlatform {
key: Identifier;
Service: {
start: <O = unknown, I = unknown>() => Effect.Effect<WorkerRunner<O, I>, WorkerError>;
};
}
Context service that starts a platform-specific WorkerRunner.
WorkerRunnerPlatform> = import LayerLayer.const succeed: {
<I, S>(service: Context.Key<I, S>): (
resource: S
) => Layer<I>
<I, S>(
service: Context.Key<I, S>,
resource: Types.NoInfer<S>
): Layer<I>
}
Constructs a layer that provides a single service from an already available
value.
When to use
Use when you need a Layer that provides a service from an already
constructed implementation without effectful acquisition.
Example (Creating a layer from a service implementation)
import { Context, Effect, Layer } from "effect"
class Database extends Context.Service<Database, {
readonly query: (sql: string) => Effect.Effect<string>
}>()("Database") {}
const DatabaseLive = Layer.succeed(Database, {
query: Effect.fn("Database.query")((sql: string) => Effect.succeed(`Query result: ${sql}`))
})
succeed(import WorkerRunnerWorkerRunner.class WorkerRunnerPlatformclass WorkerRunnerPlatform {
key: Identifier;
Service: {
start: <O = unknown, I = unknown>() => Effect.Effect<WorkerRunner<O, I>, WorkerError>;
};
of: (this: void, self: { readonly start: <O = unknown, I = unknown>() => Effect.Effect<WorkerRunner.WorkerRunner<O, I>, WorkerError> }) => { readonly start: <O = unknown, I = unknown>() => Effect.Effect<WorkerRunner.WorkerRunner<O, I>, WorkerE…;
context: (self: { readonly start: <O = unknown, I = unknown>() => Effect.Effect<WorkerRunner.WorkerRunner<O, I>, WorkerError> }) => Context<WorkerRunner.WorkerRunnerPlatform>;
use: (f: (service: { readonly start: <O = unknown, I = unknown>() => Effect.Effect<WorkerRunner.WorkerRunner<O, I>, WorkerError> }) => Effect.Effect<A, E, R>) => Effect.Effect<A, E, WorkerRunner.WorkerRunnerPlatform | R>;
useSync: (f: (service: { readonly start: <O = unknown, I = unknown>() => Effect.Effect<WorkerRunner.WorkerRunner<O, I>, WorkerError> }) => A) => Effect.Effect<A, never, WorkerRunner.WorkerRunnerPlatform>;
Identifier: Identifier;
stack: string | undefined;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
Context service that starts a platform-specific WorkerRunner.
WorkerRunnerPlatform)({
start: <
O = unknown,
I = unknown
>() => Effect.Effect<
{
run: <A, E, R>(
handler: (
portId: number,
message: I
) => Effect.Effect<A, E, R> | void
) => Effect.Effect<void, WorkerError, R>
send: (
_portId: number,
message: O,
transfers?: ReadonlyArray<unknown>
) => Effect.Effect<any, never, never>
sendUnsafe: (
_portId: number,
message: any,
transfers?: any
) => any
},
WorkerError,
never
>
start<function (type parameter) O in start<O = unknown, I = unknown>(): Effect.Effect<{
run: <A, E, R>(handler: (portId: number, message: I) => Effect.Effect<A, E, R> | void) => Effect.Effect<void, WorkerError, R>;
send: (_portId: number, message: O, transfers?: ReadonlyArray<unknown>) => Effect.Effect<...>;
sendUnsafe: (_portId: number, message: any, transfers?: any) => any;
}, WorkerError, never>
O = unknown, function (type parameter) I in start<O = unknown, I = unknown>(): Effect.Effect<{
run: <A, E, R>(handler: (portId: number, message: I) => Effect.Effect<A, E, R> | void) => Effect.Effect<void, WorkerError, R>;
send: (_portId: number, message: O, transfers?: ReadonlyArray<unknown>) => Effect.Effect<...>;
sendUnsafe: (_portId: number, message: any, transfers?: any) => any;
}, WorkerError, never>
I = unknown>() {
return import EffectEffect.const gen: {
<Eff extends Effect<any, any, any>, AEff>(
f: () => Generator<Eff, AEff, never>
): Effect<
AEff,
[Eff] extends [never]
? never
: [Eff] extends [
Effect<infer _A, infer E, infer _R>
]
? E
: never,
[Eff] extends [never]
? never
: [Eff] extends [
Effect<infer _A, infer _E, infer R>
]
? R
: never
>
<Self, Eff extends Effect<any, any, any>, AEff>(
options: { readonly self: Self },
f: (this: Self) => Generator<Eff, AEff, never>
): Effect<
AEff,
[Eff] extends [never]
? never
: [Eff] extends [
Effect<infer _A, infer E, infer _R>
]
? E
: never,
[Eff] extends [never]
? never
: [Eff] extends [
Effect<infer _A, infer _E, infer R>
]
? R
: never
>
}
Provides a way to write effectful code using generator functions, simplifying
control flow and error handling.
When to use
Use when you want to write effectful code that looks and behaves like
synchronous code, while still handling asynchronous tasks, errors, and complex
control flow such as loops and conditions.
Generator functions work similarly to async/await but keep errors,
requirements, and interruption in the Effect type. You can yield* values
from effects and return the final result at the end.
Example (Sequencing effects with generators)
import { Data, Effect } from "effect"
class DiscountRateError extends Data.TaggedError("DiscountRateError")<{}> {}
const addServiceCharge = (amount: number) => amount + 1
const applyDiscount = (
total: number,
discountRate: number
): Effect.Effect<number, DiscountRateError> =>
discountRate === 0
? Effect.fail(new DiscountRateError())
: Effect.succeed(total - (total * discountRate) / 100)
const fetchTransactionAmount = Effect.promise(() => Promise.resolve(100))
const fetchDiscountRate = Effect.promise(() => Promise.resolve(5))
export const program = Effect.gen(function*() {
const transactionAmount = yield* fetchTransactionAmount
const discountRate = yield* fetchDiscountRate
const discountedAmount = yield* applyDiscount(
transactionAmount,
discountRate
)
const finalAmount = addServiceCharge(discountedAmount)
return `Final amount to charge: ${finalAmount}`
})
gen(function*() {
if (!import WorkerThreadsWorkerThreads.parentPort && !process.send) {
return yield* new new WorkerError(props: {
readonly reason: WorkerErrorReason;
}): WorkerError
(alias) new WorkerError(props: {
readonly reason: WorkerErrorReason;
}): {
Type: Self;
Encoded: S["Encoded"];
DecodingServices: S["DecodingServices"];
EncodingServices: S["EncodingServices"];
Iso: S["Iso"];
identifier: string;
fields: S["fields"];
mapFields: (f: (fields: { readonly _tag: tag<'WorkerError'>; readonly reason: Union<[typeof WorkerSpawnError, typeof WorkerSendError, typeof WorkerReceiveError, typeof WorkerUnknownError]> }) => To, options?: { readonly unsafePreserveChecks?: boolean…;
extend: (identifier: string) => { (fields: NewFields, annotations?: Annotations.Declaration<Extended, readonly [Struct<{ [K in keyof { [K in keyof (('_tag' | 'reason') & keyof NewFields extends never ? { readonly _tag: tag<'WorkerError'>; readonly…;
Rebuild: Rebuild;
ast: Ast;
annotate: (annotations: Annotations.Bottom<WorkerError, readonly [Struct<{ readonly _tag: tag<'WorkerError'>; readonly reason: Union<[typeof WorkerSpawnError, typeof WorkerSendError, typeof WorkerReceiveError, typeof WorkerUnknownError]> }>]>) => de…;
annotateKey: (annotations: Annotations.Key<WorkerError>) => decodeTo<declareConstructor<WorkerError, Struct.ReadonlySide<{ readonly _tag: tag<'WorkerError'>; readonly reason: Union<[typeof WorkerSpawnError, typeof WorkerSendError, typeof WorkerReceiveE…;
check: (checks_0: Check<WorkerError>, ...checks: Array<Check<WorkerError>>) => decodeTo<declareConstructor<WorkerError, Struct.ReadonlySide<{ readonly _tag: tag<'WorkerError'>; readonly reason: Union<[typeof WorkerSpawnError, typeof WorkerSendErr…;
rebuild: (ast: Declaration) => decodeTo<declareConstructor<WorkerError, Struct.ReadonlySide<{ readonly _tag: tag<'WorkerError'>; readonly reason: Union<[typeof WorkerSpawnError, typeof WorkerSendError, typeof WorkerReceiveError, typeof WorkerUnknow…;
make: (input: { readonly reason: WorkerSpawnError | WorkerSendError | WorkerReceiveError | WorkerUnknownError; readonly _tag?: 'WorkerError' | undefined }, options?: MakeOptions) => WorkerError;
makeOption: (input: { readonly reason: WorkerSpawnError | WorkerSendError | WorkerReceiveError | WorkerUnknownError; readonly _tag?: 'WorkerError' | undefined }, options?: MakeOptions) => Option_.Option<WorkerError>;
makeEffect: (input: { readonly reason: WorkerSpawnError | WorkerSendError | WorkerReceiveError | WorkerUnknownError; readonly _tag?: 'WorkerError' | undefined }, options?: MakeOptions) => Effect.Effect<WorkerError, SchemaError, 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; <…;
}
Error raised by worker APIs, wrapping a specific WorkerErrorReason and
exposing its message and cause.
WorkerError({
reason: WorkerErrorReason(property) reason: {
_tag: 'WorkerSpawnError';
message: string;
cause: unknown;
name: string;
stack: string;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
reason: new new WorkerSpawnError(props: {
readonly message: string;
readonly _tag?: "WorkerSpawnError" | undefined;
readonly cause?: unknown;
}, options?: MakeOptions | undefined): WorkerSpawnError
(alias) new WorkerSpawnError(props: {
readonly message: string;
readonly _tag?: "WorkerSpawnError" | undefined;
readonly cause?: unknown;
}, options?: MakeOptions | undefined): {
Type: Self;
Encoded: S["Encoded"];
DecodingServices: S["DecodingServices"];
EncodingServices: S["EncodingServices"];
Iso: S["Iso"];
identifier: string;
fields: S["fields"];
mapFields: (f: (fields: { readonly _tag: tag<'WorkerSpawnError'>; readonly message: String; readonly cause: optional<Defect> }) => To, options?: { readonly unsafePreserveChecks?: boolean | undefined } | undefined) => Struct<{ [K in keyof Readonly<To>…;
extend: (identifier: string) => { (fields: NewFields, annotations?: Annotations.Declaration<Extended, readonly [Struct<{ [K in keyof { [K in keyof (('_tag' | 'cause' | 'message') & keyof NewFields extends never ? { readonly _tag: tag<'WorkerSpawnE…;
Rebuild: Rebuild;
ast: Ast;
annotate: (annotations: Annotations.Bottom<WorkerSpawnError, readonly [Struct<{ readonly _tag: tag<'WorkerSpawnError'>; readonly message: String; readonly cause: optional<Defect> }>]>) => decodeTo<declareConstructor<WorkerSpawnError, { readonly _tag…;
annotateKey: (annotations: Annotations.Key<WorkerSpawnError>) => decodeTo<declareConstructor<WorkerSpawnError, { readonly _tag: 'WorkerSpawnError'; readonly message: string; readonly cause?: Json | undefined }, readonly [Struct<{ readonly _tag: tag<'Wo…;
check: (checks_0: Check<WorkerSpawnError>, ...checks: Array<Check<WorkerSpawnError>>) => decodeTo<declareConstructor<WorkerSpawnError, { readonly _tag: 'WorkerSpawnError'; readonly message: string; readonly cause?: Json | undefined }, readonly [S…;
rebuild: (ast: Declaration) => decodeTo<declareConstructor<WorkerSpawnError, { readonly _tag: 'WorkerSpawnError'; readonly message: string; readonly cause?: Json | undefined }, readonly [Struct<{ readonly _tag: tag<'WorkerSpawnError'>; readonly mes…;
make: (input: { readonly message: string; readonly _tag?: 'WorkerSpawnError' | undefined; readonly cause?: unknown }, options?: MakeOptions) => WorkerSpawnError;
makeOption: (input: { readonly message: string; readonly _tag?: 'WorkerSpawnError' | undefined; readonly cause?: unknown }, options?: MakeOptions) => Option_.Option<WorkerSpawnError>;
makeEffect: (input: { readonly message: string; readonly _tag?: 'WorkerSpawnError' | undefined; readonly cause?: unknown }, options?: MakeOptions) => Effect.Effect<WorkerSpawnError, SchemaError, 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; <…;
}
Worker error reason for failures while spawning or setting up a worker.
WorkerSpawnError({ message: stringmessage: "not in a worker" })
})
}
const const sendUnsafe: (
_portId: number,
message: any,
transfers?: any
) => any
sendUnsafe = import WorkerThreadsWorkerThreads.parentPort
? (_portId: number_portId: number, message: anymessage: any, transfers: anytransfers?: any) => import WorkerThreadsWorkerThreads.parentPort!.postMessage(message: anymessage, transfers: anytransfers)
: (_portId: number_portId: number, message: anymessage: any, _transfers: any_transfers?: any) => process.send!(message: anymessage)
const const send: (
_portId: number,
message: O,
transfers?: ReadonlyArray<unknown>
) => Effect.Effect<any, never, never>
send = (_portId: number_portId: number, message: O = unknownmessage: function (type parameter) O in start<O = unknown, I = unknown>(): Effect.Effect<{
run: <A, E, R>(handler: (portId: number, message: I) => Effect.Effect<A, E, R> | void) => Effect.Effect<void, WorkerError, R>;
send: (_portId: number, message: O, transfers?: ReadonlyArray<unknown>) => Effect.Effect<...>;
sendUnsafe: (_portId: number, message: any, transfers?: any) => any;
}, WorkerError, never>
O, transfers: readonly unknown[] | undefinedtransfers?: interface ReadonlyArray<T>ReadonlyArray<unknown>) =>
import EffectEffect.const sync: <A>(
thunk: LazyArg<A>
) => Effect<A>
Creates an Effect that represents a synchronous side-effectful computation.
When to use
Use when you need to wrap a synchronous side-effectful operation that is not
expected to throw.
Details
The provided function is evaluated lazily when the effect runs.
Gotchas
The function must not throw. If it throws, the thrown value is treated as a
defect, not as a typed failure. Use try when throwing is expected.
Example (Capturing synchronous logging in an Effect)
import { Effect } from "effect"
const log = (message: string) =>
Effect.sync(() => {
console.log(message) // side effect
})
// ┌─── Effect<void, never, never>
// ▼
const program = log("Hello, World!")
sync(() => const sendUnsafe: (
_portId: number,
message: any,
transfers?: any
) => any
sendUnsafe(_portId: number_portId, [1, message: O = unknownmessage], transfers: readonly unknown[] | undefinedtransfers as any))
const const run: <A, E, R>(
handler: (
portId: number,
message: I
) => Effect.Effect<A, E, R> | void
) => Effect.Effect<void, WorkerError, R>
run = <function (type parameter) A in <A, E, R>(handler: (portId: number, message: I) => Effect.Effect<A, E, R> | void): Effect.Effect<void, WorkerError, R>A, function (type parameter) E in <A, E, R>(handler: (portId: number, message: I) => Effect.Effect<A, E, R> | void): Effect.Effect<void, WorkerError, R>E, function (type parameter) R in <A, E, R>(handler: (portId: number, message: I) => Effect.Effect<A, E, R> | void): Effect.Effect<void, WorkerError, R>R>(
handler: (
portId: number,
message: I
) => Effect.Effect<A, E, R> | void
handler: (portId: numberportId: number, message: I = unknownmessage: function (type parameter) I in start<O = unknown, I = unknown>(): Effect.Effect<{
run: <A, E, R>(handler: (portId: number, message: I) => Effect.Effect<A, E, R> | void) => Effect.Effect<void, WorkerError, R>;
send: (_portId: number, message: O, transfers?: ReadonlyArray<unknown>) => Effect.Effect<...>;
sendUnsafe: (_portId: number, message: any, transfers?: any) => any;
}, WorkerError, never>
I) => import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<function (type parameter) A in <A, E, R>(handler: (portId: number, message: I) => Effect.Effect<A, E, R> | void): Effect.Effect<void, WorkerError, R>A, function (type parameter) E in <A, E, R>(handler: (portId: number, message: I) => Effect.Effect<A, E, R> | void): Effect.Effect<void, WorkerError, R>E, function (type parameter) R in <A, E, R>(handler: (portId: number, message: I) => Effect.Effect<A, E, R> | void): Effect.Effect<void, WorkerError, R>R> | void
): import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<void, class WorkerErrorclass WorkerError {
message: string;
_tag: 'WorkerError';
reason: WorkerSpawnError | WorkerSendError | WorkerReceiveError | WorkerUnknownError;
name: string;
stack: string;
cause: unknown;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
Error raised by worker APIs, wrapping a specific WorkerErrorReason and
exposing its message and cause.
WorkerError, function (type parameter) R in <A, E, R>(handler: (portId: number, message: I) => Effect.Effect<A, E, R> | void): Effect.Effect<void, WorkerError, R>R> =>
import EffectEffect.const scopedWith: <A, E, R>(
f: (scope: Scope) => Effect<A, E, R>
) => Effect<A, E, R>
Creates a scoped effect by providing access to the scope.
When to use
Use when resource acquisition needs direct access to the scope being created,
for example to register finalizers manually.
Example (Working with an explicit scope)
import { Console, Effect, Scope } from "effect"
const program = Effect.scopedWith((scope) =>
Effect.gen(function*() {
yield* Console.log("Inside scoped context")
// Manually add a finalizer to the scope
yield* Scope.addFinalizer(scope, Console.log("Manual finalizer"))
// Create a scoped resource
const resource = yield* Effect.scoped(
Effect.acquireRelease(
Console.log("Acquiring resource").pipe(Effect.as("resource")),
() => Console.log("Releasing resource")
)
)
return resource
})
)
Effect.runPromise(program).then(console.log)
// Output:
// Inside scoped context
// Acquiring resource
// resource
// Releasing resource
// Manual finalizer
scopedWith(import EffectEffect.const fnUntraced: <Effect.Effect<void, WorkerError, never> | Effect.Effect<Context<R>, never, R>, void, [scope: Scope]>(body: (this: unassigned, scope: Scope) => Generator<Effect.Effect<void, WorkerError, never> | Effect.Effect<Context<R>, never, R>, void, never>) => (scope: Scope) => Effect.Effect<void, WorkerError, R> (+41 overloads)fnUntraced(function*(scope: Scope(parameter) scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope) {
const const closeLatch: Deferred.Deferred<
void,
WorkerError
>
const closeLatch: {
effect: Effect<A, E>;
resumes: Array<(effect: Effect<A, E>) => void> | undefined;
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; <…;
}
closeLatch = import DeferredDeferred.const makeUnsafe: <
A,
E = never
>() => Deferred<A, E>
Creates an empty Deferred synchronously outside the Effect runtime.
When to use
Use to allocate a Deferred synchronously when direct allocation outside
Effect is required.
Example (Creating a Deferred unsafely)
import { Deferred } from "effect"
const deferred = Deferred.makeUnsafe<number>()
console.log(deferred)
makeUnsafe<void, class WorkerErrorclass WorkerError {
message: string;
_tag: 'WorkerError';
reason: WorkerSpawnError | WorkerSendError | WorkerReceiveError | WorkerUnknownError;
name: string;
stack: string;
cause: unknown;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
Error raised by worker APIs, wrapping a specific WorkerErrorReason and
exposing its message and cause.
WorkerError>()
const const trackFiber: <A, E>(
self: Fiber<A, E>
) => Fiber<A, E>
trackFiber = import FiberFiber.const runIn: {
(scope: Scope): <A, E>(
self: Fiber<A, E>
) => Fiber<A, E>
<A, E>(self: Fiber<A, E>, scope: Scope): Fiber<
A,
E
>
}
Adds a fiber to a Scope and returns the same fiber.
When to use
Use when a manually managed fiber should be interrupted when a Scope closes.
Details
When the scope is closed, the fiber is interrupted. If the scope is already
closed, the fiber is interrupted immediately.
Gotchas
This does not wait for the fiber to complete. It only registers the
interruption finalizer and returns the same fiber.
runIn(scope: Scope(parameter) scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope)
const const services: Context<R>const services: {
mapUnsafe: ReadonlyMap<string, any>;
mutable: boolean;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
services = yield* import EffectEffect.const context: <R = never>() => Effect<
Context.Context<R>,
never,
R
>
Returns the complete context.
When to use
Use to read the complete Context available to the current effect.
Details
This function allows you to access all services that are currently available
in the effect's environment. This can be useful for debugging, introspection,
or when you need to pass the entire context to another function.
Example (Reading the full context)
import { Console, Context, Effect, Option } from "effect"
const Logger = Context.Service<{
log: (msg: string) => void
}>("Logger")
const Database = Context.Service<{
query: (sql: string) => string
}>("Database")
const program = Effect.gen(function*() {
const allServices = yield* Effect.context()
// Check if specific services are available
const loggerOption = Context.getOption(allServices, Logger)
const databaseOption = Context.getOption(allServices, Database)
yield* Console.log(`Logger available: ${Option.isSome(loggerOption)}`)
yield* Console.log(`Database available: ${Option.isSome(databaseOption)}`)
})
const context = Context.make(Logger, { log: console.log })
.pipe(Context.add(Database, { query: () => "result" }))
const provided = Effect.provideContext(program, context)
context<function (type parameter) R in <A, E, R>(handler: (portId: number, message: I) => Effect.Effect<A, E, R> | void): Effect.Effect<void, WorkerError, R>R>()
const const runFork: (
effect: Effect<A, E, R>,
options?: RunOptions | undefined
) => Fiber<A, E>
runFork = import EffectEffect.const runForkWith: <R>(
context: Context.Context<R>
) => <A, E>(
effect: Effect<A, E, R>,
options?: RunOptions | undefined
) => Fiber<A, E>
Runs an effect in the background with the provided services.
When to use
Use when an effect still requires services, you already have a Context, and
you want a background fiber.
Example (Running with services in the background)
import { Context, Effect } from "effect"
interface Logger {
log: (message: string) => void
}
const Logger = Context.Service<Logger>("Logger")
const services = Context.make(Logger, {
log: (message) => console.log(message)
})
const program = Effect.gen(function*() {
const logger = yield* Logger
logger.log("Hello from service!")
return "done"
})
const fiber = Effect.runForkWith(services)(program)
runForkWith(const services: Context<R>const services: {
mapUnsafe: ReadonlyMap<string, any>;
mutable: boolean;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
services)
const const onExit: (
exit: Exit.Exit<any, E>
) => void
onExit = (exit: Exit.Exit<any, E>exit: import ExitExit.type Exit<A, E = never> = Exit.Success<A, E> | Exit.Failure<A, E>Represents the result of an Effect computation.
When to use
Use when you need to synchronously inspect whether an Effect computation
succeeded or failed.
Details
An Exit<A, E> is either Success<A, E> containing a value of type A, or
Failure<A, E> containing a Cause<E> describing why the computation
failed.
Since Exit is also an Effect, you can yield it inside Effect.gen.
Example (Pattern matching on an Exit)
import { Exit } from "effect"
const success: Exit.Exit<number> = Exit.succeed(42)
const failure: Exit.Exit<number, string> = Exit.fail("error")
const result = Exit.match(success, {
onSuccess: (value) => `Got value: ${value}`,
onFailure: (cause) => `Got error: ${cause}`
})
Namespace containing helper types shared by Exit values.
When to use
Use to reference helper types that describe the shared structure of Exit
values.
Exit<any, function (type parameter) E in <A, E, R>(handler: (portId: number, message: I) => Effect.Effect<A, E, R> | void): Effect.Effect<void, WorkerError, R>E>) => {
if (exit: Exit.Exit<any, E>exit._tag: "Success" | "Failure"_tag === "Failure" && !import CauseCause.const hasInterruptsOnly: <E>(
self: Cause<E>
) => boolean
Returns true if every reason in the cause is an Interrupt (and
there is at least one reason).
When to use
Use when you need to detect failures caused only by interruption.
Example (Checking interrupt-only causes)
import { Cause } from "effect"
console.log(Cause.hasInterruptsOnly(Cause.interrupt(123))) // true
console.log(Cause.hasInterruptsOnly(Cause.fail("error"))) // false
console.log(Cause.hasInterruptsOnly(Cause.empty)) // false
hasInterruptsOnly(exit: Exit.Exit<any, E>(parameter) exit: {
_tag: "Failure";
cause: Cause.Cause<E>;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
exit.Failure<any, E>.cause: Cause.Cause<E>(property) Failure<any, E>.cause: {
reasons: ReadonlyArray<Reason<E>>;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
cause)) {
const runFork: (
effect: Effect<A, E, R>,
options?: RunOptions | undefined
) => Fiber<A, E>
runFork(import EffectEffect.const logError: (
...message: ReadonlyArray<any>
) => Effect<void>
Logs one or more messages at the ERROR level.
Example (Logging errors)
import { Effect } from "effect"
const program = Effect.gen(function*() {
yield* Effect.logError("Database connection failed")
yield* Effect.logError(
"Error code:",
500,
"Message:",
"Internal server error"
)
// Can be used with error objects
const error = new Error("Something went wrong")
yield* Effect.logError("Caught error:", error.message)
})
Effect.runPromise(program)
// Output:
// timestamp=2023-... level=ERROR message="Database connection failed"
// timestamp=2023-... level=ERROR message="Error code: 500 Message: Internal server error"
// timestamp=2023-... level=ERROR message="Caught error: Something went wrong"
logError("unhandled error in worker", exit: Exit.Exit<any, E>(parameter) exit: {
_tag: "Failure";
cause: Cause.Cause<E>;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
exit.Failure<any, E>.cause: Cause.Cause<E>(property) Failure<any, E>.cause: {
reasons: ReadonlyArray<Reason<E>>;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
cause))
}
}
;(import WorkerThreadsWorkerThreads.parentPort ?? process).on("message", (message: WorkerRunner.PlatformMessage<I>message: import WorkerRunnerWorkerRunner.type PlatformMessage<I> =
| readonly [request: 0, I]
| readonly [close: 1]
Wire protocol message used by worker platforms: a request carrying input or a
close signal.
PlatformMessage<function (type parameter) I in start<O = unknown, I = unknown>(): Effect.Effect<{
run: <A, E, R>(handler: (portId: number, message: I) => Effect.Effect<A, E, R> | void) => Effect.Effect<void, WorkerError, R>;
send: (_portId: number, message: O, transfers?: ReadonlyArray<unknown>) => Effect.Effect<...>;
sendUnsafe: (_portId: number, message: any, transfers?: any) => any;
}, WorkerError, never>
I>) => {
if (message: WorkerRunner.PlatformMessage<I>message[0] === 0) {
const const result: void | Effect.Effect<
A,
E,
R
>
result = handler: (
portId: number,
message: I
) => Effect.Effect<A, E, R> | void
handler(0, message: readonly [request: 0, I](parameter) message: {
0: 0;
1: I;
length: 2;
toString: () => string;
toLocaleString: { (): string; (locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string };
concat: { (...items: Array<ConcatArray<0 | I>>): Array<0 | I>; (...items: Array<0 | I | ConcatArray<0 | I>>): Array<0 | I> };
join: (separator?: string) => string;
slice: (start?: number, end?: number) => Array<0 | I>;
indexOf: (searchElement: 0 | I, fromIndex?: number) => number;
lastIndexOf: (searchElement: 0 | I, fromIndex?: number) => number;
every: { (predicate: (value: 0 | I, index: number, array: ReadonlyArray<0 | I>) => value is S, thisArg?: any): this is readonly S[]; (predicate: (value: 0 | I, index: number, array: ReadonlyArray<0 | I>) => unknown, thisArg?: any): boolean };
some: (predicate: (value: 0 | I, index: number, array: ReadonlyArray<0 | I>) => unknown, thisArg?: any) => boolean;
forEach: (callbackfn: (value: 0 | I, index: number, array: ReadonlyArray<0 | I>) => void, thisArg?: any) => void;
map: (callbackfn: (value: 0 | I, index: number, array: ReadonlyArray<0 | I>) => U, thisArg?: any) => Array<U>;
filter: { (predicate: (value: 0 | I, index: number, array: ReadonlyArray<0 | I>) => value is S, thisArg?: any): Array<S>; (predicate: (value: 0 | I, index: number, array: ReadonlyArray<0 | I>) => unknown, thisArg?: any): Array<0 | I> };
reduce: { (callbackfn: (previousValue: 0 | I, currentValue: 0 | I, currentIndex: number, array: ReadonlyArray<0 | I>) => 0 | I): 0 | I; (callbackfn: (previousValue: 0 | I, currentValue: 0 | I, currentIndex: number, array: ReadonlyArray<0 | I>) => …;
reduceRight: { (callbackfn: (previousValue: 0 | I, currentValue: 0 | I, currentIndex: number, array: ReadonlyArray<0 | I>) => 0 | I): 0 | I; (callbackfn: (previousValue: 0 | I, currentValue: 0 | I, currentIndex: number, array: ReadonlyArray<0 | I>) => …;
find: { (predicate: (value: 0 | I, index: number, obj: ReadonlyArray<0 | I>) => value is S, thisArg?: any): S | undefined; (predicate: (value: 0 | I, index: number, obj: ReadonlyArray<0 | I>) => unknown, thisArg?: any): 0 | I | undefined };
findIndex: (predicate: (value: 0 | I, index: number, obj: ReadonlyArray<0 | I>) => unknown, thisArg?: any) => number;
entries: () => ArrayIterator<[number, 0 | I]>;
keys: () => ArrayIterator<number>;
values: () => ArrayIterator<0 | I>;
includes: (searchElement: 0 | I, fromIndex?: number) => boolean;
flatMap: (callback: (this: This, value: 0 | I, index: number, array: Array<0 | I>) => U | ReadonlyArray<U>, thisArg?: This | undefined) => Array<U>;
flat: (this: A, depth?: D | undefined) => Array<FlatArray<A, D>>;
at: (index: number) => 0 | I | undefined;
findLast: { (predicate: (value: 0 | I, index: number, array: ReadonlyArray<0 | I>) => value is S, thisArg?: any): S | undefined; (predicate: (value: 0 | I, index: number, array: ReadonlyArray<0 | I>) => unknown, thisArg?: any): 0 | I | undefined };
findLastIndex: (predicate: (value: 0 | I, index: number, array: ReadonlyArray<0 | I>) => unknown, thisArg?: any) => number;
toReversed: () => Array<0 | I>;
toSorted: (compareFn?: ((a: 0 | I, b: 0 | I) => number) | undefined) => Array<0 | I>;
toSpliced: { (start: number, deleteCount: number, ...items: Array<0 | I>): Array<0 | I>; (start: number, deleteCount?: number): Array<0 | I> };
with: (index: number, value: 0 | I) => Array<0 | I>;
}
message[1])
if (import EffectEffect.const isEffect: (
u: unknown
) => u is Effect<any, any, any>
Checks whether a value is an Effect.
Example (Checking whether a value is an Effect)
import { Effect } from "effect"
console.log(Effect.isEffect(Effect.succeed(1))) // true
console.log(Effect.isEffect("hello")) // false
isEffect(const result: void | Effect.Effect<
A,
E,
R
>
result)) {
const const fiber: Fiber.Fiber<A, E>const fiber: {
id: number;
currentOpCount: number;
getRef: <A>(ref: Context.Reference<A>) => A;
context: Context.Context<never>;
setContext: (context: Context.Context<never>) => void;
currentScheduler: Scheduler;
currentDispatcher: SchedulerDispatcher;
currentSpan: AnySpan | undefined;
currentLogLevel: LogLevel;
minimumLogLevel: LogLevel;
currentStackFrame: StackFrame | undefined;
maxOpsBeforeYield: number;
currentPreventYield: boolean;
addObserver: (cb: (exit: Exit<A, E>) => void) => () => void;
interruptUnsafe: (fiberId?: number | undefined, annotations?: Context.Context<never> | undefined) => void;
pollUnsafe: () => Exit<A, E> | undefined;
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; <…;
}
fiber = const runFork: (
effect: Effect<A, E, R>,
options?: RunOptions | undefined
) => Fiber<A, E>
runFork(const result: Effect.Effect<A, E, R>const result: {
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; <…;
toString: () => string;
toJSON: () => unknown;
}
result)
const fiber: Fiber.Fiber<A, E>const fiber: {
id: number;
currentOpCount: number;
getRef: <A>(ref: Context.Reference<A>) => A;
context: Context.Context<never>;
setContext: (context: Context.Context<never>) => void;
currentScheduler: Scheduler;
currentDispatcher: SchedulerDispatcher;
currentSpan: AnySpan | undefined;
currentLogLevel: LogLevel;
minimumLogLevel: LogLevel;
currentStackFrame: StackFrame | undefined;
maxOpsBeforeYield: number;
currentPreventYield: boolean;
addObserver: (cb: (exit: Exit<A, E>) => void) => () => void;
interruptUnsafe: (fiberId?: number | undefined, annotations?: Context.Context<never> | undefined) => void;
pollUnsafe: () => Exit<A, E> | undefined;
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; <…;
}
fiber.Fiber<A, E>.addObserver: (cb: (exit: Exit<A, E>) => void) => () => voidaddObserver(const onExit: (
exit: Exit.Exit<any, E>
) => void
onExit)
const trackFiber: <A, E>(
self: Fiber<A, E>
) => Fiber<A, E>
trackFiber(const fiber: Fiber.Fiber<A, E>const fiber: {
id: number;
currentOpCount: number;
getRef: <A>(ref: Context.Reference<A>) => A;
context: Context.Context<never>;
setContext: (context: Context.Context<never>) => void;
currentScheduler: Scheduler;
currentDispatcher: SchedulerDispatcher;
currentSpan: AnySpan | undefined;
currentLogLevel: LogLevel;
minimumLogLevel: LogLevel;
currentStackFrame: StackFrame | undefined;
maxOpsBeforeYield: number;
currentPreventYield: boolean;
addObserver: (cb: (exit: Exit<A, E>) => void) => () => void;
interruptUnsafe: (fiberId?: number | undefined, annotations?: Context.Context<never> | undefined) => void;
pollUnsafe: () => Exit<A, E> | undefined;
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; <…;
}
fiber)
}
} else {
if (import WorkerThreadsWorkerThreads.parentPort) {
import WorkerThreadsWorkerThreads.parentPort.close()
} else {
process.channel?.unref()
}
import DeferredDeferred.const doneUnsafe: <A, E>(
self: Deferred<A, E>,
effect: Effect<A, E>
) => boolean
Attempts to complete the Deferred synchronously with the specified
completion effect.
When to use
Use to complete a Deferred synchronously in low-level code that already has
the completion effect.
Details
This mutates the Deferred directly and should be reserved for low-level
code; prefer the effectful completion APIs when possible. Returns true if
this call completed the Deferred, or false if it was already completed.
Example (Completing a Deferred unsafely)
import { Deferred, Effect } from "effect"
const deferred = Deferred.makeUnsafe<number>()
const success = Deferred.doneUnsafe(deferred, Effect.succeed(42))
console.log(success) // true
doneUnsafe(const closeLatch: Deferred.Deferred<
void,
WorkerError
>
const closeLatch: {
effect: Effect<A, E>;
resumes: Array<(effect: Effect<A, E>) => void> | undefined;
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; <…;
}
closeLatch, import ExitExit.const void: Exit.Exit<void, never>Provides a pre-allocated successful Exit with a void value.
When to use
Use when you need a shared successful Exit with no meaningful value.
Details
Equivalent to Exit.succeed(undefined) but shared as a single instance,
avoiding allocation for a common case.
Example (Referencing the void Exit)
import { Exit } from "effect"
const exit = Exit.void
console.log(Exit.isSuccess(exit)) // true
void)
}
})
if (import WorkerThreadsWorkerThreads.parentPort) {
import WorkerThreadsWorkerThreads.parentPort.on("messageerror", (cause: anycause) => {
import DeferredDeferred.const doneUnsafe: <A, E>(
self: Deferred<A, E>,
effect: Effect<A, E>
) => boolean
Attempts to complete the Deferred synchronously with the specified
completion effect.
When to use
Use to complete a Deferred synchronously in low-level code that already has
the completion effect.
Details
This mutates the Deferred directly and should be reserved for low-level
code; prefer the effectful completion APIs when possible. Returns true if
this call completed the Deferred, or false if it was already completed.
Example (Completing a Deferred unsafely)
import { Deferred, Effect } from "effect"
const deferred = Deferred.makeUnsafe<number>()
const success = Deferred.doneUnsafe(deferred, Effect.succeed(42))
console.log(success) // true
doneUnsafe(
const closeLatch: Deferred.Deferred<
void,
WorkerError
>
const closeLatch: {
effect: Effect<A, E>;
resumes: Array<(effect: Effect<A, E>) => void> | undefined;
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; <…;
}
closeLatch,
new new WorkerError(props: {
readonly reason: WorkerErrorReason;
}): WorkerError
(alias) new WorkerError(props: {
readonly reason: WorkerErrorReason;
}): {
Type: Self;
Encoded: S["Encoded"];
DecodingServices: S["DecodingServices"];
EncodingServices: S["EncodingServices"];
Iso: S["Iso"];
identifier: string;
fields: S["fields"];
mapFields: (f: (fields: { readonly _tag: tag<'WorkerError'>; readonly reason: Union<[typeof WorkerSpawnError, typeof WorkerSendError, typeof WorkerReceiveError, typeof WorkerUnknownError]> }) => To, options?: { readonly unsafePreserveChecks?: boolean…;
extend: (identifier: string) => { (fields: NewFields, annotations?: Annotations.Declaration<Extended, readonly [Struct<{ [K in keyof { [K in keyof (('_tag' | 'reason') & keyof NewFields extends never ? { readonly _tag: tag<'WorkerError'>; readonly…;
Rebuild: Rebuild;
ast: Ast;
annotate: (annotations: Annotations.Bottom<WorkerError, readonly [Struct<{ readonly _tag: tag<'WorkerError'>; readonly reason: Union<[typeof WorkerSpawnError, typeof WorkerSendError, typeof WorkerReceiveError, typeof WorkerUnknownError]> }>]>) => de…;
annotateKey: (annotations: Annotations.Key<WorkerError>) => decodeTo<declareConstructor<WorkerError, Struct.ReadonlySide<{ readonly _tag: tag<'WorkerError'>; readonly reason: Union<[typeof WorkerSpawnError, typeof WorkerSendError, typeof WorkerReceiveE…;
check: (checks_0: Check<WorkerError>, ...checks: Array<Check<WorkerError>>) => decodeTo<declareConstructor<WorkerError, Struct.ReadonlySide<{ readonly _tag: tag<'WorkerError'>; readonly reason: Union<[typeof WorkerSpawnError, typeof WorkerSendErr…;
rebuild: (ast: Declaration) => decodeTo<declareConstructor<WorkerError, Struct.ReadonlySide<{ readonly _tag: tag<'WorkerError'>; readonly reason: Union<[typeof WorkerSpawnError, typeof WorkerSendError, typeof WorkerReceiveError, typeof WorkerUnknow…;
make: (input: { readonly reason: WorkerSpawnError | WorkerSendError | WorkerReceiveError | WorkerUnknownError; readonly _tag?: 'WorkerError' | undefined }, options?: MakeOptions) => WorkerError;
makeOption: (input: { readonly reason: WorkerSpawnError | WorkerSendError | WorkerReceiveError | WorkerUnknownError; readonly _tag?: 'WorkerError' | undefined }, options?: MakeOptions) => Option_.Option<WorkerError>;
makeEffect: (input: { readonly reason: WorkerSpawnError | WorkerSendError | WorkerReceiveError | WorkerUnknownError; readonly _tag?: 'WorkerError' | undefined }, options?: MakeOptions) => Effect.Effect<WorkerError, SchemaError, 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; <…;
}
Error raised by worker APIs, wrapping a specific WorkerErrorReason and
exposing its message and cause.
WorkerError({
reason: WorkerErrorReason(property) reason: {
_tag: 'WorkerReceiveError';
message: string;
cause: unknown;
name: string;
stack: string;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
reason: new new WorkerReceiveError(props: {
readonly message: string;
readonly _tag?: "WorkerReceiveError" | undefined;
readonly cause?: unknown;
}, options?: MakeOptions | undefined): WorkerReceiveError
(alias) new WorkerReceiveError(props: {
readonly message: string;
readonly _tag?: "WorkerReceiveError" | undefined;
readonly cause?: unknown;
}, options?: MakeOptions | undefined): {
Type: Self;
Encoded: S["Encoded"];
DecodingServices: S["DecodingServices"];
EncodingServices: S["EncodingServices"];
Iso: S["Iso"];
identifier: string;
fields: S["fields"];
mapFields: (f: (fields: { readonly _tag: tag<'WorkerReceiveError'>; readonly message: String; readonly cause: optional<Defect> }) => To, options?: { readonly unsafePreserveChecks?: boolean | undefined } | undefined) => Struct<{ [K in keyof Readonly<T…;
extend: (identifier: string) => { (fields: NewFields, annotations?: Annotations.Declaration<Extended, readonly [Struct<{ [K in keyof { [K in keyof (('_tag' | 'cause' | 'message') & keyof NewFields extends never ? { readonly _tag: tag<'WorkerReceiv…;
Rebuild: Rebuild;
ast: Ast;
annotate: (annotations: Annotations.Bottom<WorkerReceiveError, readonly [Struct<{ readonly _tag: tag<'WorkerReceiveError'>; readonly message: String; readonly cause: optional<Defect> }>]>) => decodeTo<declareConstructor<WorkerReceiveError, { readonl…;
annotateKey: (annotations: Annotations.Key<WorkerReceiveError>) => decodeTo<declareConstructor<WorkerReceiveError, { readonly _tag: 'WorkerReceiveError'; readonly message: string; readonly cause?: Json | undefined }, readonly [Struct<{ readonly _tag: t…;
check: (checks_0: Check<WorkerReceiveError>, ...checks: Array<Check<WorkerReceiveError>>) => decodeTo<declareConstructor<WorkerReceiveError, { readonly _tag: 'WorkerReceiveError'; readonly message: string; readonly cause?: Json | undefined }, rea…;
rebuild: (ast: Declaration) => decodeTo<declareConstructor<WorkerReceiveError, { readonly _tag: 'WorkerReceiveError'; readonly message: string; readonly cause?: Json | undefined }, readonly [Struct<{ readonly _tag: tag<'WorkerReceiveError'>; readon…;
make: (input: { readonly message: string; readonly _tag?: 'WorkerReceiveError' | undefined; readonly cause?: unknown }, options?: MakeOptions) => WorkerReceiveError;
makeOption: (input: { readonly message: string; readonly _tag?: 'WorkerReceiveError' | undefined; readonly cause?: unknown }, options?: MakeOptions) => Option_.Option<WorkerReceiveError>;
makeEffect: (input: { readonly message: string; readonly _tag?: 'WorkerReceiveError' | undefined; readonly cause?: unknown }, options?: MakeOptions) => Effect.Effect<WorkerReceiveError, SchemaError, 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; <…;
}
Worker error reason for failures while receiving or handling a message from a
worker.
WorkerReceiveError({
message: stringmessage: "received messageerror event",
cause?: unknowncause
})
})
)
})
import WorkerThreadsWorkerThreads.parentPort.on("error", (cause: anycause) => {
import DeferredDeferred.const doneUnsafe: <A, E>(
self: Deferred<A, E>,
effect: Effect<A, E>
) => boolean
Attempts to complete the Deferred synchronously with the specified
completion effect.
When to use
Use to complete a Deferred synchronously in low-level code that already has
the completion effect.
Details
This mutates the Deferred directly and should be reserved for low-level
code; prefer the effectful completion APIs when possible. Returns true if
this call completed the Deferred, or false if it was already completed.
Example (Completing a Deferred unsafely)
import { Deferred, Effect } from "effect"
const deferred = Deferred.makeUnsafe<number>()
const success = Deferred.doneUnsafe(deferred, Effect.succeed(42))
console.log(success) // true
doneUnsafe(
const closeLatch: Deferred.Deferred<
void,
WorkerError
>
const closeLatch: {
effect: Effect<A, E>;
resumes: Array<(effect: Effect<A, E>) => void> | undefined;
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; <…;
}
closeLatch,
new new WorkerError(props: {
readonly reason: WorkerErrorReason;
}): WorkerError
(alias) new WorkerError(props: {
readonly reason: WorkerErrorReason;
}): {
Type: Self;
Encoded: S["Encoded"];
DecodingServices: S["DecodingServices"];
EncodingServices: S["EncodingServices"];
Iso: S["Iso"];
identifier: string;
fields: S["fields"];
mapFields: (f: (fields: { readonly _tag: tag<'WorkerError'>; readonly reason: Union<[typeof WorkerSpawnError, typeof WorkerSendError, typeof WorkerReceiveError, typeof WorkerUnknownError]> }) => To, options?: { readonly unsafePreserveChecks?: boolean…;
extend: (identifier: string) => { (fields: NewFields, annotations?: Annotations.Declaration<Extended, readonly [Struct<{ [K in keyof { [K in keyof (('_tag' | 'reason') & keyof NewFields extends never ? { readonly _tag: tag<'WorkerError'>; readonly…;
Rebuild: Rebuild;
ast: Ast;
annotate: (annotations: Annotations.Bottom<WorkerError, readonly [Struct<{ readonly _tag: tag<'WorkerError'>; readonly reason: Union<[typeof WorkerSpawnError, typeof WorkerSendError, typeof WorkerReceiveError, typeof WorkerUnknownError]> }>]>) => de…;
annotateKey: (annotations: Annotations.Key<WorkerError>) => decodeTo<declareConstructor<WorkerError, Struct.ReadonlySide<{ readonly _tag: tag<'WorkerError'>; readonly reason: Union<[typeof WorkerSpawnError, typeof WorkerSendError, typeof WorkerReceiveE…;
check: (checks_0: Check<WorkerError>, ...checks: Array<Check<WorkerError>>) => decodeTo<declareConstructor<WorkerError, Struct.ReadonlySide<{ readonly _tag: tag<'WorkerError'>; readonly reason: Union<[typeof WorkerSpawnError, typeof WorkerSendErr…;
rebuild: (ast: Declaration) => decodeTo<declareConstructor<WorkerError, Struct.ReadonlySide<{ readonly _tag: tag<'WorkerError'>; readonly reason: Union<[typeof WorkerSpawnError, typeof WorkerSendError, typeof WorkerReceiveError, typeof WorkerUnknow…;
make: (input: { readonly reason: WorkerSpawnError | WorkerSendError | WorkerReceiveError | WorkerUnknownError; readonly _tag?: 'WorkerError' | undefined }, options?: MakeOptions) => WorkerError;
makeOption: (input: { readonly reason: WorkerSpawnError | WorkerSendError | WorkerReceiveError | WorkerUnknownError; readonly _tag?: 'WorkerError' | undefined }, options?: MakeOptions) => Option_.Option<WorkerError>;
makeEffect: (input: { readonly reason: WorkerSpawnError | WorkerSendError | WorkerReceiveError | WorkerUnknownError; readonly _tag?: 'WorkerError' | undefined }, options?: MakeOptions) => Effect.Effect<WorkerError, SchemaError, 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; <…;
}
Error raised by worker APIs, wrapping a specific WorkerErrorReason and
exposing its message and cause.
WorkerError({
reason: WorkerErrorReason(property) reason: {
_tag: 'WorkerReceiveError';
message: string;
cause: unknown;
name: string;
stack: string;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
reason: new new WorkerReceiveError(props: {
readonly message: string;
readonly _tag?: "WorkerReceiveError" | undefined;
readonly cause?: unknown;
}, options?: MakeOptions | undefined): WorkerReceiveError
(alias) new WorkerReceiveError(props: {
readonly message: string;
readonly _tag?: "WorkerReceiveError" | undefined;
readonly cause?: unknown;
}, options?: MakeOptions | undefined): {
Type: Self;
Encoded: S["Encoded"];
DecodingServices: S["DecodingServices"];
EncodingServices: S["EncodingServices"];
Iso: S["Iso"];
identifier: string;
fields: S["fields"];
mapFields: (f: (fields: { readonly _tag: tag<'WorkerReceiveError'>; readonly message: String; readonly cause: optional<Defect> }) => To, options?: { readonly unsafePreserveChecks?: boolean | undefined } | undefined) => Struct<{ [K in keyof Readonly<T…;
extend: (identifier: string) => { (fields: NewFields, annotations?: Annotations.Declaration<Extended, readonly [Struct<{ [K in keyof { [K in keyof (('_tag' | 'cause' | 'message') & keyof NewFields extends never ? { readonly _tag: tag<'WorkerReceiv…;
Rebuild: Rebuild;
ast: Ast;
annotate: (annotations: Annotations.Bottom<WorkerReceiveError, readonly [Struct<{ readonly _tag: tag<'WorkerReceiveError'>; readonly message: String; readonly cause: optional<Defect> }>]>) => decodeTo<declareConstructor<WorkerReceiveError, { readonl…;
annotateKey: (annotations: Annotations.Key<WorkerReceiveError>) => decodeTo<declareConstructor<WorkerReceiveError, { readonly _tag: 'WorkerReceiveError'; readonly message: string; readonly cause?: Json | undefined }, readonly [Struct<{ readonly _tag: t…;
check: (checks_0: Check<WorkerReceiveError>, ...checks: Array<Check<WorkerReceiveError>>) => decodeTo<declareConstructor<WorkerReceiveError, { readonly _tag: 'WorkerReceiveError'; readonly message: string; readonly cause?: Json | undefined }, rea…;
rebuild: (ast: Declaration) => decodeTo<declareConstructor<WorkerReceiveError, { readonly _tag: 'WorkerReceiveError'; readonly message: string; readonly cause?: Json | undefined }, readonly [Struct<{ readonly _tag: tag<'WorkerReceiveError'>; readon…;
make: (input: { readonly message: string; readonly _tag?: 'WorkerReceiveError' | undefined; readonly cause?: unknown }, options?: MakeOptions) => WorkerReceiveError;
makeOption: (input: { readonly message: string; readonly _tag?: 'WorkerReceiveError' | undefined; readonly cause?: unknown }, options?: MakeOptions) => Option_.Option<WorkerReceiveError>;
makeEffect: (input: { readonly message: string; readonly _tag?: 'WorkerReceiveError' | undefined; readonly cause?: unknown }, options?: MakeOptions) => Effect.Effect<WorkerReceiveError, SchemaError, 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; <…;
}
Worker error reason for failures while receiving or handling a message from a
worker.
WorkerReceiveError({
message: stringmessage: "received messageerror event",
cause?: unknowncause
})
})
)
})
}
const sendUnsafe: (
_portId: number,
message: any,
transfers?: any
) => any
sendUnsafe(0, [0])
return yield* import DeferredDeferred.await<A, E>(self: Deferred<A, E>): Effect<A, E>Retrieves the value of the Deferred, suspending the fiber running the
workflow until the result is available.
When to use
Use to wait for a Deferred to be completed and resume with its success,
failure, defect, or interruption.
Details
Awaiters observe the completion effect stored in the Deferred.
Example (Awaiting a Deferred value)
import { Deferred, Effect } from "effect"
const program = Effect.gen(function*() {
const deferred = yield* Deferred.make<number>()
yield* Deferred.succeed(deferred, 42)
const value = yield* Deferred.await(deferred)
console.log(value) // 42
})
await(const closeLatch: Deferred.Deferred<
void,
WorkerError
>
const closeLatch: {
effect: Effect<A, E>;
resumes: Array<(effect: Effect<A, E>) => void> | undefined;
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; <…;
}
closeLatch)
}))
return { run: <A, E, R>(
handler: (
portId: number,
message: I
) => Effect.Effect<A, E, R> | void
) => Effect.Effect<void, WorkerError, R>
run, send: (
_portId: number,
message: O,
transfers?: ReadonlyArray<unknown>
) => Effect.Effect<any, never, never>
send, sendUnsafe: (
_portId: number,
message: any,
transfers?: any
) => any
sendUnsafe }
})
}
})