import * as Ex from "../Exit"; import * as T from "./_internal/task"; import { map_ } from "./functor"; import { Managed } from "./model"; /* * ------------------------------------------- * Monad Managed * ------------------------------------------- */ /** * Returns a managed that models the execution of this managed, followed by * the passing of its value to the specified continuation function `f`, * followed by the managed that it returns. */ export const chain = (f: (a: A) => Managed) => (self: Managed) => chain_(self, f); /** * Returns a managed that models the execution of this managed, followed by * the passing of its value to the specified continuation function `f`, * followed by the managed that it returns. */ export const chain_ = (self: Managed, f: (a: A) => Managed) => new Managed( T.chain_(self.task, ([releaseSelf, a]) => T.map_(f(a).task, ([releaseThat, b]) => [ (e) => T.chain_(T.result(releaseThat(e)), (e1) => T.chain_(T.result(releaseSelf(e1)), (e2) => T.done(Ex.apSecond_(e1, e2))) ), b ]) ) ); /** * Returns a managed that effectfully peeks at the acquired resource. */ export const tap = (f: (a: A) => Managed) => (self: Managed) => chain_(self, (a) => map_(f(a), () => a)); /** * Returns a managed that effectfully peeks at the acquired resource. */ export const tap_ = (ma: Managed, f: (a: A) => Managed): Managed => tap(f)(ma);