// ets_tracing: off import type { Cause } from "../Cause/index.js" import { failureOrCause } from "../Cause/index.js" import * as E from "../Either/index.js" import { pipe } from "../Function/index.js" import { bracketExit_ } from "./bracketExit.js" import { unit } from "./core.js" import type { Effect, RIO } from "./effect.js" /** * Runs the specified effect if this effect is terminated, either because of * a defect or because of interruption. */ export function onTermination_( self: Effect, cleanup: (_: Cause) => RIO, __trace?: string ): Effect { return bracketExit_( unit, () => self, (_, eb): RIO => { switch (eb._tag) { case "Success": { return unit } case "Failure": { return pipe( failureOrCause(eb.cause), E.fold(() => unit, cleanup) ) } } }, __trace ) } /** * Runs the specified effect if this effect is terminated, either because of * a defect or because of interruption. * * @ets_data_first onTermination_ */ export function onTermination( cleanup: (_: Cause) => RIO, __trace?: string ): (self: Effect) => Effect { return (self) => onTermination_(self, cleanup, __trace) }