// ets_tracing: off import type { Cause } from "../Cause/cause.js" import type { Exit } from "../Exit/exit.js" import { bracketExit_ } from "./bracketExit.js" import { unit } from "./core.js" import type { Effect } from "./effect.js" /** * Execute a cleanup function when the effect completes */ export function onExit_( self: Effect, cleanup: (exit: Exit) => Effect, __trace?: string ): Effect { return bracketExit_( unit, () => self, (_, e) => cleanup(e), __trace ) } /** * Execute a cleanup function when the effect completes * * @ets_data_first onExit_ */ export function onExit( cleanup: (exit: Exit) => Effect, __trace?: string ) { return (self: Effect): Effect => onExit_(self, cleanup, __trace) } /** * Execute a cleanup function when the effect errors * * @ets_data_first onError_ */ export function onError( cleanup: (exit: Cause) => Effect, __trace?: string ) { return (self: Effect): Effect => onError_(self, cleanup, __trace) } /** * Execute a cleanup function when the effect errors */ export function onError_( self: Effect, cleanup: (exit: Cause) => Effect, __trace?: string ): Effect { return onExit_( self, (e) => { switch (e._tag) { case "Failure": { return cleanup(e.cause) } case "Success": { return unit } } }, __trace ) }