import type * as HKT from "@principia/prelude/HKT"; import type { Option } from "../../Option"; import type { Exit } from "../Exit/model"; import type { FiberRef } from "../FiberRef/model"; import type { Scope } from "../Scope"; import type { IO } from "../Task/model"; import type { FiberId } from "./FiberId"; import type { FiberStatus } from "./status"; export const URI = "Fiber"; export type URI = typeof URI; export type V = HKT.V<"E", "+">; /** * InterruptStatus tracks interruptability of the current stack region */ export class InterruptStatus { constructor(readonly isInterruptible: boolean) {} get isUninteruptible(): boolean { return !this.isInterruptible; } get toBoolean(): boolean { return this.isInterruptible; } } /** * A record containing information about a `Fiber`. */ export class FiberDescriptor { constructor( readonly id: FiberId, readonly status: FiberStatus, readonly interruptors: ReadonlySet, readonly interruptStatus: InterruptStatus, readonly scope: Scope> ) {} } /** * A fiber is a lightweight thread of execution that never consumes more than a * whole thread (but may consume much less, depending on contention and * asynchronicity). Fibers are spawned by forking ZIO effects, which run * concurrently with the parent effect. * * Fibers can be joined, yielding their result to other fibers, or interrupted, * which terminates the fiber, safely releasing all resources. */ export type Fiber = RuntimeFiber | SyntheticFiber; export interface CommonFiber { /** * Awaits the fiber, which suspends the awaiting fiber until the result of the * fiber has been determined. */ readonly await: IO>; /** * Gets the value of the fiber ref for this fiber, or the initial value of * the fiber ref, if the fiber is not storing the ref. */ readonly getRef: (fiberRef: FiberRef) => IO; /** * Inherits values from all {@link FiberRef} instances into current fiber. * This will resume immediately. */ readonly inheritRefs: IO; /** * Interrupts the fiber as if interrupted from the specified fiber. If the * fiber has already exited, the returned effect will resume immediately. * Otherwise, the effect will resume when the fiber exits. */ readonly interruptAs: (fiberId: FiberId) => IO>; /** * Tentatively observes the fiber, but returns immediately if it is not already done. */ readonly poll: IO>>; } export interface RuntimeFiber extends CommonFiber { _tag: "RuntimeFiber"; /** * The identity of the fiber. */ readonly id: FiberId; readonly scope: Scope>; /** * The status of the fiber. */ readonly status: IO; } export interface SyntheticFiber extends CommonFiber { _tag: "SyntheticFiber"; } /** * ```haskell * makeSynthetic :: Synthetic e a -> Fiber e a * ``` * * A type helper for building a Synthetic Fiber */ export const makeSynthetic = (_: SyntheticFiber): Fiber => _;