Hyperlinkv0.8.0-beta.28

Scope

Scope.Statenamespaceeffect/Scope.ts:109
any

The 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")
  }
})
Source effect/Scope.ts:10986 lines
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 Empty = {
    readonly _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 Open = {
    readonly _tag: "Open"
    readonly finalizers: Map<{}, (exit: Exit<any, any>) => 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 Closed = {
    readonly _tag: "Closed"
    readonly exit: Exit<any, any>
  }
}
Referenced by 1 symbols