import type { FiberId } from "../../Fiber/FiberId"; import * as T from "../../Task/_core"; import { checkFiberId } from "../../Task/combinators/checkFiberId"; import { interruptAs as effectInterruptAs } from "../../Task/combinators/interrupt"; import type { Canceler, EIO, IO } from "../../Task/model"; import type { XPromise } from "../model"; import { Pending } from "../state"; import { completeWith } from "./completeWith"; /** * Completes the promise with interruption. This will interrupt all fibers * waiting on the value of the promise as by the fiber calling this method. */ export const interrupt = (promise: XPromise): IO => T.chain_(checkFiberId(), (id) => completeWith(effectInterruptAs(id))(promise)); /** * Completes the promise with interruption. This will interrupt all fibers * waiting on the value of the promise as by the specified fiber. */ export const interruptAs = (id: FiberId) => (promise: XPromise): IO => completeWith(effectInterruptAs(id))(promise); export const interruptJoiner = (joiner: (a: EIO) => void) => (promise: XPromise): Canceler => T.total(() => { const state = promise.state.get; if (state._tag === "Pending") { promise.state.set(new Pending(state.joiners.filter((j) => j !== joiner))); } });