<K, A, E>(self: FiberMap<K, A, E>): <R = never>() => Effect.Effect<
<XE extends E, XA extends A>(
key: K,
effect: Effect.Effect<XA, XE, R>,
options?:
| (Effect.RunOptions & {
readonly onlyIfMissing?: boolean | undefined
readonly propagateInterruption?: boolean | undefined
})
| undefined
) => Promise<XA>,
never,
R
>Captures the current runtime and returns a function for running effects in
an existing FiberMap as Promises.
Details
Each call stores the forked fiber under the supplied key, interrupting any
previous fiber for that key unless onlyIfMissing is set. The Promise
resolves with the effect's success value or rejects with the squashed failure
cause.
Example (Running effects as promises)
import { Effect, FiberMap } from "effect"
const program = Effect.gen(function*() {
const map = yield* FiberMap.make<string>()
const runPromise = yield* FiberMap.runtimePromise(map)<never>()
// Create promises that will be backed by fibers in the map
const promise1 = runPromise("task1", Effect.succeed("Hello"))
const promise2 = runPromise("task2", Effect.succeed("World"))
// Convert promises back to Effects and await
const result1 = yield* Effect.promise(() => promise1)
const result2 = yield* Effect.promise(() => promise2)
console.log(result1, result2) // "Hello", "World"
})export const const runtimePromise: <K, A, E>(
self: FiberMap<K, A, E>
) => <R = never>() => Effect.Effect<
<XE extends E, XA extends A>(
key: K,
effect: Effect.Effect<XA, XE, R>,
options?:
| (Effect.RunOptions & {
readonly onlyIfMissing?:
| boolean
| undefined
readonly propagateInterruption?:
| boolean
| undefined
})
| undefined
) => Promise<XA>,
never,
R
>
Captures the current runtime and returns a function for running effects in
an existing FiberMap as Promises.
Details
Each call stores the forked fiber under the supplied key, interrupting any
previous fiber for that key unless onlyIfMissing is set. The Promise
resolves with the effect's success value or rejects with the squashed failure
cause.
Example (Running effects as promises)
import { Effect, FiberMap } from "effect"
const program = Effect.gen(function*() {
const map = yield* FiberMap.make<string>()
const runPromise = yield* FiberMap.runtimePromise(map)<never>()
// Create promises that will be backed by fibers in the map
const promise1 = runPromise("task1", Effect.succeed("Hello"))
const promise2 = runPromise("task2", Effect.succeed("World"))
// Convert promises back to Effects and await
const result1 = yield* Effect.promise(() => promise1)
const result2 = yield* Effect.promise(() => promise2)
console.log(result1, result2) // "Hello", "World"
})
runtimePromise = <function (type parameter) K in <K, A, E>(self: FiberMap<K, A, E>): <R = never>() => Effect.Effect<(<XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & {
readonly onlyIfMissing?: boolean | undefined;
readonly propagateInterruption?: boolean | undefined;
}) | undefined) => Promise<XA>), never, R>
K, function (type parameter) A in <K, A, E>(self: FiberMap<K, A, E>): <R = never>() => Effect.Effect<(<XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & {
readonly onlyIfMissing?: boolean | undefined;
readonly propagateInterruption?: boolean | undefined;
}) | undefined) => Promise<XA>), never, R>
A, function (type parameter) E in <K, A, E>(self: FiberMap<K, A, E>): <R = never>() => Effect.Effect<(<XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & {
readonly onlyIfMissing?: boolean | undefined;
readonly propagateInterruption?: boolean | undefined;
}) | undefined) => Promise<XA>), never, R>
E>(self: FiberMap<K, A, E>(parameter) self: {
deferred: Deferred.Deferred<void, unknown>;
state: { readonly _tag: "Open"; readonly backing: MutableHashMap.MutableHashMap<K, Fiber.Fiber<A, E>> } | { readonly _tag: "Closed" };
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;
}
self: interface FiberMap<in out K, out A = unknown, out E = unknown>A FiberMap is a collection of fibers, indexed by a key. When the associated
Scope is closed, all fibers in the map will be interrupted. Fibers are
automatically removed from the map when they complete.
Example (Managing fibers in a map)
import { Effect, FiberMap } from "effect"
// Create a FiberMap with string keys
const program = Effect.gen(function*() {
const map = yield* FiberMap.make<string>()
// Add some fibers to the map
yield* FiberMap.run(map, "task1", Effect.never)
yield* FiberMap.run(map, "task2", Effect.never)
// Get the size of the map
const size = yield* FiberMap.size(map)
console.log(size) // 2
})
FiberMap<function (type parameter) K in <K, A, E>(self: FiberMap<K, A, E>): <R = never>() => Effect.Effect<(<XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & {
readonly onlyIfMissing?: boolean | undefined;
readonly propagateInterruption?: boolean | undefined;
}) | undefined) => Promise<XA>), never, R>
K, function (type parameter) A in <K, A, E>(self: FiberMap<K, A, E>): <R = never>() => Effect.Effect<(<XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & {
readonly onlyIfMissing?: boolean | undefined;
readonly propagateInterruption?: boolean | undefined;
}) | undefined) => Promise<XA>), never, R>
A, function (type parameter) E in <K, A, E>(self: FiberMap<K, A, E>): <R = never>() => Effect.Effect<(<XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & {
readonly onlyIfMissing?: boolean | undefined;
readonly propagateInterruption?: boolean | undefined;
}) | undefined) => Promise<XA>), never, R>
E>): <function (type parameter) R in <R = never>(): Effect.Effect<(<XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & {
readonly onlyIfMissing?: boolean | undefined;
readonly propagateInterruption?: boolean | undefined;
}) | undefined) => Promise<XA>), never, R>
R = never>() => 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) XE in <XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & {
readonly onlyIfMissing?: boolean | undefined;
readonly propagateInterruption?: boolean | undefined;
}) | undefined): Promise<XA>
XE extends function (type parameter) E in <K, A, E>(self: FiberMap<K, A, E>): <R = never>() => Effect.Effect<(<XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & {
readonly onlyIfMissing?: boolean | undefined;
readonly propagateInterruption?: boolean | undefined;
}) | undefined) => Promise<XA>), never, R>
E, function (type parameter) XA in <XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & {
readonly onlyIfMissing?: boolean | undefined;
readonly propagateInterruption?: boolean | undefined;
}) | undefined): Promise<XA>
XA extends function (type parameter) A in <K, A, E>(self: FiberMap<K, A, E>): <R = never>() => Effect.Effect<(<XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & {
readonly onlyIfMissing?: boolean | undefined;
readonly propagateInterruption?: boolean | undefined;
}) | undefined) => Promise<XA>), never, R>
A>(
key: Kkey: function (type parameter) K in <K, A, E>(self: FiberMap<K, A, E>): <R = never>() => Effect.Effect<(<XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & {
readonly onlyIfMissing?: boolean | undefined;
readonly propagateInterruption?: boolean | undefined;
}) | undefined) => Promise<XA>), never, R>
K,
effect: Effect.Effect<XA, XE, R>(parameter) effect: {
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;
}
effect: 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) XA in <XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & {
readonly onlyIfMissing?: boolean | undefined;
readonly propagateInterruption?: boolean | undefined;
}) | undefined): Promise<XA>
XA, function (type parameter) XE in <XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & {
readonly onlyIfMissing?: boolean | undefined;
readonly propagateInterruption?: boolean | undefined;
}) | undefined): Promise<XA>
XE, function (type parameter) R in <R = never>(): Effect.Effect<(<XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & {
readonly onlyIfMissing?: boolean | undefined;
readonly propagateInterruption?: boolean | undefined;
}) | undefined) => Promise<XA>), never, R>
R>,
options: | (Effect.RunOptions & {
readonly onlyIfMissing?: boolean | undefined
readonly propagateInterruption?:
| boolean
| undefined
})
| undefined
options?:
| import EffectEffect.RunOptions & {
readonly onlyIfMissing?: boolean | undefinedonlyIfMissing?: boolean | undefined
readonly propagateInterruption?: boolean | undefinedpropagateInterruption?: boolean | undefined
}
| undefined
) => interface Promise<T>Represents the completion of an asynchronous operation
Promise<function (type parameter) XA in <XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & {
readonly onlyIfMissing?: boolean | undefined;
readonly propagateInterruption?: boolean | undefined;
}) | undefined): Promise<XA>
XA>,
never,
function (type parameter) R in <R = never>(): Effect.Effect<(<XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & {
readonly onlyIfMissing?: boolean | undefined;
readonly propagateInterruption?: boolean | undefined;
}) | undefined) => Promise<XA>), never, R>
R
> =>
<function (type parameter) R in <R>(): Effect.Effect<(<XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & {
readonly propagateInterruption?: boolean | undefined;
}) | undefined) => Promise<XA>), never, R>
R>() =>
import EffectEffect.const map: {
<A, B>(f: (a: A) => B): <E, R>(
self: Effect<A, E, R>
) => Effect<B, E, R>
<A, E, R, B>(
self: Effect<A, E, R>,
f: (a: A) => B
): Effect<B, E, R>
}
map(
const runtime: <K, A, E>(
self: FiberMap<K, A, E>
) => <R = never>() => Effect.Effect<
<XE extends E, XA extends A>(
key: K,
effect: Effect.Effect<XA, XE, R>,
options?:
| (Effect.RunOptions & {
readonly onlyIfMissing?:
| boolean
| undefined
readonly propagateInterruption?:
| boolean
| undefined
})
| undefined
) => Fiber.Fiber<XA, XE>,
never,
R
>
Captures the current runtime and returns a function for forking effects into
an existing FiberMap.
Details
Each call stores the forked fiber under the supplied key. If that key already
has a fiber, the previous fiber is interrupted unless onlyIfMissing is set.
Example (Capturing a runtime)
import { Context, Effect, FiberMap } from "effect"
interface Users {
readonly _: unique symbol
}
const Users = Context.Service<Users, {
getAll: Effect.Effect<Array<unknown>>
}>("Users")
Effect.gen(function*() {
const map = yield* FiberMap.make<string>()
const run = yield* FiberMap.runtime(map)<Users>()
// run some effects and add the fibers to the map
run("effect-a", Effect.andThen(Users, (_) => _.getAll))
run("effect-b", Effect.andThen(Users, (_) => _.getAll))
}).pipe(
Effect.scoped // The fibers will be interrupted when the scope is closed
)
runtime(self: FiberMap<K, A, E>(parameter) self: {
deferred: Deferred.Deferred<void, unknown>;
state: { readonly _tag: "Open"; readonly backing: MutableHashMap.MutableHashMap<K, Fiber.Fiber<A, E>> } | { readonly _tag: "Closed" };
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;
}
self)<function (type parameter) R in <R>(): Effect.Effect<(<XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & {
readonly propagateInterruption?: boolean | undefined;
}) | undefined) => Promise<XA>), never, R>
R>(),
(runFork: (
key: K,
effect: Effect.Effect<XA, XE, R>,
options?:
| (Effect.RunOptions & {
readonly onlyIfMissing?:
| boolean
| undefined
readonly propagateInterruption?:
| boolean
| undefined
})
| undefined
) => Fiber.Fiber<XA, XE>
runFork) =>
<function (type parameter) XE in <XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & {
readonly propagateInterruption?: boolean | undefined;
}) | undefined): Promise<XA>
XE extends function (type parameter) E in <K, A, E>(self: FiberMap<K, A, E>): <R = never>() => Effect.Effect<(<XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & {
readonly onlyIfMissing?: boolean | undefined;
readonly propagateInterruption?: boolean | undefined;
}) | undefined) => Promise<XA>), never, R>
E, function (type parameter) XA in <XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & {
readonly propagateInterruption?: boolean | undefined;
}) | undefined): Promise<XA>
XA extends function (type parameter) A in <K, A, E>(self: FiberMap<K, A, E>): <R = never>() => Effect.Effect<(<XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & {
readonly onlyIfMissing?: boolean | undefined;
readonly propagateInterruption?: boolean | undefined;
}) | undefined) => Promise<XA>), never, R>
A>(
key: Kkey: function (type parameter) K in <K, A, E>(self: FiberMap<K, A, E>): <R = never>() => Effect.Effect<(<XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & {
readonly onlyIfMissing?: boolean | undefined;
readonly propagateInterruption?: boolean | undefined;
}) | undefined) => Promise<XA>), never, R>
K,
effect: Effect.Effect<XA, XE, R>(parameter) effect: {
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;
}
effect: 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) XA in <XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & {
readonly propagateInterruption?: boolean | undefined;
}) | undefined): Promise<XA>
XA, function (type parameter) XE in <XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & {
readonly propagateInterruption?: boolean | undefined;
}) | undefined): Promise<XA>
XE, function (type parameter) R in <R>(): Effect.Effect<(<XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & {
readonly propagateInterruption?: boolean | undefined;
}) | undefined) => Promise<XA>), never, R>
R>,
options: | (Effect.RunOptions & {
readonly propagateInterruption?:
| boolean
| undefined
})
| undefined
options?:
| import EffectEffect.RunOptions & { readonly propagateInterruption?: boolean | undefinedpropagateInterruption?: boolean | undefined }
| undefined
): interface Promise<T>Represents the completion of an asynchronous operation
Promise<function (type parameter) XA in <XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: (Effect.RunOptions & {
readonly propagateInterruption?: boolean | undefined;
}) | undefined): Promise<XA>
XA> =>
new var Promise: PromiseConstructor
new <XA>(executor: (resolve: (value: XA | PromiseLike<XA>) => void, reject: (reason?: any) => void) => void) => Promise<XA>
Creates a new Promise.
Promise((resolve: (value: XA | PromiseLike<XA>) => voidresolve, reject: (reason?: any) => voidreject) =>
runFork: (
key: K,
effect: Effect.Effect<XA, XE, R>,
options?:
| (Effect.RunOptions & {
readonly onlyIfMissing?:
| boolean
| undefined
readonly propagateInterruption?:
| boolean
| undefined
})
| undefined
) => Fiber.Fiber<XA, XE>
runFork(key: Kkey, effect: Effect.Effect<XA, XE, R>(parameter) effect: {
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;
}
effect, options: | (Effect.RunOptions & {
readonly propagateInterruption?:
| boolean
| undefined
})
| undefined
options).Fiber<XA, XE>.addObserver: (cb: (exit: Exit<A, E>) => void) => () => voidaddObserver((exit: Exit.Exit<XA, XE>exit) => {
if (import ExitExit.const isSuccess: <A, E>(
self: Exit<A, E>
) => self is Success<A, E>
Checks whether an Exit is a Success.
When to use
Use as a type guard to narrow Exit<A, E> to Success<A, E> and access the
value property.
Example (Narrowing to success)
import { Exit } from "effect"
const exit = Exit.succeed(42)
if (Exit.isSuccess(exit)) {
console.log(exit.value) // 42
}
isSuccess(exit: Exit.Exit<XA, XE>exit)) {
resolve: (value: XA | PromiseLike<XA>) => voidresolve(exit: Exit.Success<XA, XE>(parameter) exit: {
_tag: "Success";
value: A;
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.Success<XA, XE>.value: XA extends Avalue)
} else {
reject: (reason?: any) => voidreject(import CauseCause.const squash: <E>(
self: Cause<E>
) => unknown
Collapses a Cause into a single unknown value, picking the "most
important" failure in this order:
When to use
Use to collapse a structured cause to the single value that synchronous and
promise runners would throw.
Details
- First
Fail error (the E value)
- First
Die defect
- A generic
Error("All fibers interrupted without error") for interrupt-only causes
- A generic
Error("Empty cause") for empty
This is the function used by Effect.runPromise and Effect.runSync to
decide what to throw.
Gotchas
This function is lossy. Use
prettyErrors
or iterate cause.reasons
when you need all failures.
Example (Squashing a cause)
import { Cause } from "effect"
console.log(Cause.squash(Cause.fail("error"))) // "error"
console.log(Cause.squash(Cause.die("defect"))) // "defect"
squash(exit: Exit.Failure<XA, XE>(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<XA, XE>.cause: Cause.Cause<E>(property) Failure<XA, XE>.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))
}
})
)
)