<R>(context: Context.Context<R>): <A, E>(
effect: Effect<A, E, R>,
options?:
| (RunOptions & { readonly onExit: (exit: Exit.Exit<A, E>) => void })
| undefined
) => (interruptor?: number | undefined) => voidForks an effect with the provided services, registers onExit as a fiber observer, and returns an interruptor.
When to use
Use when embedding an effect into callback-style code with explicit services and a synchronous interruptor.
Details
The returned interruptor calls fiber.interruptUnsafe, optionally with an interruptor id.
Example (Running with services and a callback)
import { Console, Context, Effect, Exit } from "effect"
interface Logger {
log: (message: string) => Effect.Effect<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
yield* logger.log("Started")
return "done"
})
const interrupt = Effect.runCallbackWith(services)(program, {
onExit: (exit) => {
if (Exit.isFailure(exit)) {
// handle failure or interruption
}
}
})
// Use the interruptor if you need to cancel the fiber later.
interrupt()export const const runCallbackWith: <R>(
context: Context.Context<R>
) => <A, E>(
effect: Effect<A, E, R>,
options?:
| (RunOptions & {
readonly onExit: (
exit: Exit.Exit<A, E>
) => void
})
| undefined
) => (interruptor?: number | undefined) => void
Forks an effect with the provided services, registers onExit as a fiber observer, and returns an interruptor.
When to use
Use when embedding an effect into callback-style code with explicit services
and a synchronous interruptor.
Details
The returned interruptor calls fiber.interruptUnsafe, optionally with an interruptor id.
Example (Running with services and a callback)
import { Console, Context, Effect, Exit } from "effect"
interface Logger {
log: (message: string) => Effect.Effect<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
yield* logger.log("Started")
return "done"
})
const interrupt = Effect.runCallbackWith(services)(program, {
onExit: (exit) => {
if (Exit.isFailure(exit)) {
// handle failure or interruption
}
}
})
// Use the interruptor if you need to cancel the fiber later.
interrupt()
runCallbackWith: <function (type parameter) R in <R>(context: Context.Context<R>): <A, E>(effect: Effect<A, E, R>, options?: (RunOptions & {
readonly onExit: (exit: Exit.Exit<A, E>) => void;
}) | undefined) => (interruptor?: number | undefined) => void
R>(
context: Context.Context<R>(parameter) context: {
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;
}
context: import ContextContext.interface Context<in Services>Immutable collection of service implementations used for dependency
injection in Effect programs.
Details
The type parameter tracks the service identifiers available in the context.
At runtime, services are stored by each key's string key.
Example (Creating a context with multiple services)
import { Context } from "effect"
// Create a context with multiple services
const Logger = Context.Service<{ log: (msg: string) => void }>("Logger")
const Database = Context.Service<{ query: (sql: string) => string }>(
"Database"
)
const context = Context.make(Logger, {
log: (msg: string) => console.log(msg)
})
.pipe(Context.add(Database, { query: (sql) => `Result: ${sql}` }))
Context<function (type parameter) R in <R>(context: Context.Context<R>): <A, E>(effect: Effect<A, E, R>, options?: (RunOptions & {
readonly onExit: (exit: Exit.Exit<A, E>) => void;
}) | undefined) => (interruptor?: number | undefined) => void
R>
) => <function (type parameter) A in <A, E>(effect: Effect<A, E, R>, options?: (RunOptions & {
readonly onExit: (exit: Exit.Exit<A, E>) => void;
}) | undefined): (interruptor?: number | undefined) => void
A, function (type parameter) E in <A, E>(effect: Effect<A, E, R>, options?: (RunOptions & {
readonly onExit: (exit: Exit.Exit<A, E>) => void;
}) | undefined): (interruptor?: number | undefined) => void
E>(
effect: Effect<A, E, 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: 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>(effect: Effect<A, E, R>, options?: (RunOptions & {
readonly onExit: (exit: Exit.Exit<A, E>) => void;
}) | undefined): (interruptor?: number | undefined) => void
A, function (type parameter) E in <A, E>(effect: Effect<A, E, R>, options?: (RunOptions & {
readonly onExit: (exit: Exit.Exit<A, E>) => void;
}) | undefined): (interruptor?: number | undefined) => void
E, function (type parameter) R in <R>(context: Context.Context<R>): <A, E>(effect: Effect<A, E, R>, options?: (RunOptions & {
readonly onExit: (exit: Exit.Exit<A, E>) => void;
}) | undefined) => (interruptor?: number | undefined) => void
R>,
options: | (RunOptions & {
readonly onExit: (
exit: Exit.Exit<A, E>
) => void
})
| undefined
options?: (RunOptions & { readonly onExit: (exit: Exit.Exit<A, E>) => voidonExit: (exit: Exit.Exit<A, 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<function (type parameter) A in <A, E>(effect: Effect<A, E, R>, options?: (RunOptions & {
readonly onExit: (exit: Exit.Exit<A, E>) => void;
}) | undefined): (interruptor?: number | undefined) => void
A, function (type parameter) E in <A, E>(effect: Effect<A, E, R>, options?: (RunOptions & {
readonly onExit: (exit: Exit.Exit<A, E>) => void;
}) | undefined): (interruptor?: number | undefined) => void
E>) => void }) | undefined
) => (interruptor: number | undefinedinterruptor?: number | undefined) => void = import internalinternal.const runCallbackWith: <R>(
context: Context.Context<R>
) => <A, E>(
effect: Effect.Effect<A, E, R>,
options?:
| (Effect.RunOptions & {
readonly onExit: (
exit: Exit.Exit<A, E>
) => void
})
| undefined
) => (interruptor?: number | undefined) => void
runCallbackWith