anyThe State namespace contains the concrete states of a scope: Empty
before any finalizers are registered, Open with registered finalizers, and
Closed with the exit value used to close the scope.
Example (Checking scope states)
import { Effect, Exit, Scope } from "effect"
// Example of checking scope states
const program = Effect.gen(function*() {
const scope = yield* Scope.make()
// When open, the scope accepts finalizers
if (scope.state._tag === "Open") {
console.log("Scope is open")
}
yield* Scope.close(scope, Exit.void)
// When closed, the scope no longer accepts finalizers
if (scope.state._tag === "Closed") {
console.log("Scope is closed")
}
})export declare namespace State {
/**
* Represents an open scope with no registered finalizers yet.
*
* **Details**
*
* Adding the first finalizer transitions the scope to `Open`; closing an
* empty scope transitions directly to `Closed` without producing a finalizer
* effect.
*
* **Example** (Inspecting an empty scope state)
*
* ```ts
* import { Scope } from "effect"
*
* const scope = Scope.makeUnsafe()
*
* // When scope is open, you can check its state
* if (scope.state._tag === "Open") {
* console.log("Scope is open and accepting finalizers")
* console.log(scope.state.finalizers.size) // Number of registered finalizers
* }
* ```
*
* @category models
* @since 4.0.0
*/
export type type State.Empty = {
readonly _tag: "Empty";
}
Represents an open scope with no registered finalizers yet.
Details
Adding the first finalizer transitions the scope to Open; closing an
empty scope transitions directly to Closed without producing a finalizer
effect.
Example (Inspecting an empty scope state)
import { Scope } from "effect"
const scope = Scope.makeUnsafe()
// When scope is open, you can check its state
if (scope.state._tag === "Open") {
console.log("Scope is open and accepting finalizers")
console.log(scope.state.finalizers.size) // Number of registered finalizers
}
Empty = {
readonly _tag: "Empty"_tag: "Empty"
}
/**
* Represents an open scope state where finalizers can be added and
* the scope is still accepting new resources.
*
* **Example** (Inspecting an open scope state)
*
* ```ts
* import { Scope } from "effect"
*
* const scope = Scope.makeUnsafe()
*
* // When scope is open, you can check its state
* if (scope.state._tag === "Open") {
* console.log("Scope is open and accepting finalizers")
* console.log(scope.state.finalizers.size) // Number of registered finalizers
* }
* ```
*
* @category models
* @since 4.0.0
*/
export type type State.Open = {
readonly _tag: "Open";
readonly finalizers: Map<{}, (exit: Exit<any, any>) => Effect<void>>;
}
Represents an open scope state where finalizers can be added and
the scope is still accepting new resources.
Example (Inspecting an open scope state)
import { Scope } from "effect"
const scope = Scope.makeUnsafe()
// When scope is open, you can check its state
if (scope.state._tag === "Open") {
console.log("Scope is open and accepting finalizers")
console.log(scope.state.finalizers.size) // Number of registered finalizers
}
Open = {
readonly _tag: "Open"_tag: "Open"
readonly finalizers: Map<
{},
(exit: Exit<any, any>) => Effect<void>
>
finalizers: interface Map<K, V>Map<{}, (exit: Exit<any, any>exit: type Exit<A, E = never> = Success<A, E> | 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, any>) => 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>>
}
/**
* Represents a closed scope state where finalizers have been executed
* and the scope is no longer accepting new resources.
*
* **Example** (Inspecting a closed scope state)
*
* ```ts
* import { Effect, Exit, Scope } from "effect"
*
* const program = Effect.gen(function*() {
* const scope = yield* Scope.make()
*
* // Close the scope
* yield* Scope.close(scope, Exit.succeed("Done"))
*
* // Check if scope is closed
* if (scope.state._tag === "Closed") {
* console.log("Scope is closed")
* console.log(scope.state.exit) // The exit value used to close the scope
* }
* })
* ```
*
* @category models
* @since 4.0.0
*/
export type type State.Closed = {
readonly _tag: "Closed";
readonly exit: Exit<any, any>;
}
Represents a closed scope state where finalizers have been executed
and the scope is no longer accepting new resources.
Example (Inspecting a closed scope state)
import { Effect, Exit, Scope } from "effect"
const program = Effect.gen(function*() {
const scope = yield* Scope.make()
// Close the scope
yield* Scope.close(scope, Exit.succeed("Done"))
// Check if scope is closed
if (scope.state._tag === "Closed") {
console.log("Scope is closed")
console.log(scope.state.exit) // The exit value used to close the scope
}
})
Closed = {
readonly _tag: "Closed"_tag: "Closed"
readonly exit: Exit<any, any>exit: type Exit<A, E = never> = Success<A, E> | 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, any>
}
}