// ets_tracing: off import type { Cause } from "../Cause/cause.js" import * as Tp from "../Collections/Immutable/Tuple/index.js" import type { ExecutionStrategy } from "../Effect/ExecutionStrategy.js" import { parallel, sequential } from "../Effect/ExecutionStrategy.js" import { pipe } from "../Function/index.js" import { makeRef } from "../Ref/index.js" import * as T from "./deps-core.js" import { fromEffect } from "./fromEffect.js" import { makeExit_ } from "./makeExit.js" import type { Managed } from "./managed.js" import { managedApply } from "./managed.js" import * as add from "./ReleaseMap/add.js" import * as addIfOpen from "./ReleaseMap/addIfOpen.js" import type { Finalizer } from "./ReleaseMap/finalizer.js" import type { ReleaseMap } from "./ReleaseMap/index.js" import * as makeReleaseMap from "./ReleaseMap/makeReleaseMap.js" import * as release from "./ReleaseMap/release.js" import * as releaseAll from "./ReleaseMap/releaseAll.js" import { use_ } from "./use.js" /** * 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. * * @ets_data_first chain_ */ export function chain( f: (a: A) => Managed, __trace?: string ) { return (self: Managed) => chain_(self, f, __trace) } /** * 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 function chain_( self: Managed, f: (a: A) => Managed, __trace?: string ) { return managedApply( T.chain_(self.effect, ({ tuple: [releaseSelf, a] }) => T.map_( f(a).effect, ({ tuple: [releaseThat, b] }) => Tp.tuple( (e) => T.chain_(T.result(releaseThat(e)), (e1) => T.chain_(T.result(releaseSelf(e)), (e2) => T.done(T.exitZipRight_(e1, e2), __trace) ) ), b ), __trace ) ) ) } /** * Imports a synchronous side-effect into a pure value */ export function succeedWith(effect: () => A, __trace?: string) { return fromEffect(T.succeedWith(effect, __trace)) } /** * Ensures that `f` is executed when this Managed is finalized, after * the existing finalizer. * * For usecases that need access to the Managed's result, see `onExit`. */ export function ensuring_( self: Managed, f: T.Effect, __trace?: string ) { return onExit_(self, () => f, __trace) } /** * Ensures that `f` is executed when this Managed is finalized, after * the existing finalizer. * * For usecases that need access to the Managed's result, see `onExit`. * * @ets_data_first ensuring_ */ export function ensuring(f: T.Effect, __trace?: string) { return (self: Managed) => ensuring_(self, f, __trace) } /** * Returns an effect that models failure with the specified error. The moral equivalent of throw for pure code. */ export function fail(e: E, __trace?: string) { return fromEffect(T.fail(e, __trace)) } /** * Returns an effect that models failure with the specified error. The moral equivalent of throw for pure code. */ export function failWith(e: () => E, __trace?: string) { return fromEffect(T.failWith(e, __trace)) } /** * Creates an effect that executes a finalizer stored in a `Ref`. * The `Ref` is yielded as the result of the effect, allowing for * control flows that require mutating finalizers. */ export function finalizerRef(initial: Finalizer, __trace?: string) { return makeExit_( makeRef(initial), (ref, exit) => T.chain_(ref.get, (f) => f(exit)), __trace ) } /** * A more powerful version of `foldM` that allows recovering from any kind of failure except interruptions. * * @ets_data_first foldCauseM_ */ export function foldCauseM( f: (cause: Cause) => Managed, g: (a: A) => Managed, __trace?: string ) { return (self: Managed) => foldCauseM_(self, f, g, __trace) } /** * A more powerful version of `foldM` that allows recovering from any kind of failure except interruptions. */ export function foldCauseM_( self: Managed, f: (cause: Cause) => Managed, g: (a: A) => Managed, __trace?: string ) { return managedApply( pipe( self.effect, T.foldCauseM( (c) => f(c).effect, ({ tuple: [_, a] }) => g(a).effect, __trace ) ) ) } /** * Lifts a `Effect< R, E, A>` into `Managed< R, E, A>` with a release action. * The acquire and release actions will be performed uninterruptibly. * * @ets_data_first make_ */ export function make( release: (a: A) => T.Effect, __trace?: string ): (acquire: T.Effect) => Managed { return (acquire) => make_(acquire, release, __trace) } /** * Lifts a `Effect< R, E, A>` into `Managed< R, E, A>` with a release action. * The acquire and release actions will be performed uninterruptibly. */ export function make_( acquire: T.Effect, release: (a: A) => T.Effect, __trace?: string ): Managed { return makeExit_(acquire, release, __trace) } /** * Lifts a `Effect< R, E, A>` into `Managed< R, E, A>` with a release action. * The acquire action will be performed interruptibly, while release * will be performed uninterruptibly. * * @ets_data_first makeInterruptible_ */ export function makeInterruptible( release: (a: A) => T.Effect, __trace?: string ) { return (acquire: T.Effect) => makeInterruptible_(acquire, release, __trace) } /** * Lifts a `Effect< R, E, A>` into `Managed< R, E, A>` with a release action. * The acquire action will be performed interruptibly, while release * will be performed uninterruptibly. */ export function makeInterruptible_( acquire: T.Effect, release: (a: A) => T.Effect, __trace?: string ) { return onExitFirst_(fromEffect(acquire, __trace), T.exitForeach(release), __trace) } /** * Construct a `ReleaseMap` wrapped in a `Managed`. The `ReleaseMap` will * be released with the specified `ExecutionStrategy` as the release action * for the resulting `Managed`. */ export function makeManagedReleaseMap( es: ExecutionStrategy, __trace?: string ): Managed { return makeExit_( makeReleaseMap.makeReleaseMap, (rm, e) => releaseAll.releaseAll(e, es)(rm), __trace ) } /** * Creates a `Managed` from a `Reservation` produced by an effect. Evaluating * the effect that produces the reservation will be performed *uninterruptibly*, * while the acquisition step of the reservation will be performed *interruptibly*. * The release step will be performed uninterruptibly as usual. * * This two-phase acquisition allows for resource acquisition flows that can be * safely interrupted and released. */ export function makeReserve( reservation: T.Effect>, __trace?: string ) { return managedApply( T.uninterruptibleMask(({ restore }) => pipe( T.do, T.bind("tp", () => T.environment>()), T.let("r", (s) => s.tp.get(0)), T.let("releaseMap", (s) => s.tp.get(1)), T.bind("reserved", (s) => T.provideAll_(reservation, s.r)), T.bind("releaseKey", (s) => addIfOpen.addIfOpen((x) => T.provideAll_(s.reserved.release(x), s.r, __trace) )(s.releaseMap) ), T.bind("finalizerAndA", (s) => { const k = s.releaseKey switch (k._tag) { case "None": { return T.interrupt } case "Some": { return T.map_( restore( T.provideSome_( s.reserved.acquire, ({ tuple: [r] }: Tp.Tuple<[R & R2, ReleaseMap]>) => r, __trace ) ), (a): Tp.Tuple<[Finalizer, A]> => Tp.tuple((e) => release.release(k.value, e)(s.releaseMap), a) ) } } }), T.map((s) => s.finalizerAndA) ) ) ) } /** * Returns a managed whose success is mapped by the specified `f` function. * * @ets_data_first map_ */ export function map(f: (a: A) => B, __trace?: string) { return (self: Managed) => map_(self, f, __trace) } /** * Returns a managed whose success is mapped by the specified `f` function. */ export function map_( self: Managed, f: (a: A) => B, __trace?: string ) { return managedApply( T.map_(self.effect, ({ tuple: [fin, a] }) => Tp.tuple(fin, f(a)), __trace) ) } /** * Returns a managed whose success is mapped by the specified `f` function. */ export function mapM_( self: Managed, f: (a: A) => T.Effect, __trace?: string ) { return managedApply( T.chain_(self.effect, ({ tuple: [fin, a] }) => T.provideSome_( T.map_(f(a), (b) => Tp.tuple(fin, b), __trace), ({ tuple: [r] }: Tp.Tuple<[R & R2, ReleaseMap]>) => r ) ) ) } /** * Returns a managed whose success is mapped by the specified `f` function. */ export function mapM(f: (a: A) => T.Effect, __trace?: string) { return (self: Managed) => mapM_(self, f, __trace) } /** * Ensures that a cleanup function runs when this Managed is finalized, after * the existing finalizers. */ export function onExit_( self: Managed, cleanup: (exit: T.Exit) => T.Effect, __trace?: string ) { return managedApply( T.uninterruptibleMask(({ restore }) => pipe( T.do, T.bind("tp", () => T.environment>()), T.let("r", (s) => s.tp.get(0)), T.let("outerReleaseMap", (s) => s.tp.get(1)), T.bind("innerReleaseMap", () => makeReleaseMap.makeReleaseMap), T.bind("exitEA", (s) => T.provideAll_( T.result(restore(T.map_(self.effect, ({ tuple: [_, a] }) => a))), Tp.tuple(s.r, s.innerReleaseMap) ) ), T.bind("releaseMapEntry", (s) => add.add((e) => pipe( releaseAll.releaseAll(e, sequential)(s.innerReleaseMap), T.result, T.zipWith( pipe(cleanup(s.exitEA), T.provideAll(s.r), T.result), (l, r) => T.done(T.exitZipRight_(l, r)), __trace ), T.flatten ) )(s.outerReleaseMap) ), T.bind("a", (s) => T.done(s.exitEA)), T.map((s) => Tp.tuple(s.releaseMapEntry, s.a)) ) ) ) } /** * Ensures that a cleanup function runs when this Managed is finalized, after * the existing finalizers. * * @ets_data_first onExit_ */ export function onExit( cleanup: (exit: T.Exit) => T.Effect, __trace?: string ) { return (self: Managed) => onExit_(self, cleanup, __trace) } /** * Ensures that a cleanup function runs when this Managed is finalized, before * the existing finalizers. * * @ets_data_first onExitFirst_ */ export function onExitFirst( cleanup: (exit: T.Exit) => T.Effect, __trace?: string ) { return (self: Managed) => onExitFirst_(self, cleanup, __trace) } /** * Ensures that a cleanup function runs when this Managed is finalized, before * the existing finalizers. */ export function onExitFirst_( self: Managed, cleanup: (exit: T.Exit) => T.Effect, __trace?: string ) { return managedApply( T.uninterruptibleMask(({ restore }) => pipe( T.do, T.bind("tp", () => T.environment>()), T.let("r", (s) => s.tp.get(0)), T.let("outerReleaseMap", (s) => s.tp.get(1)), T.bind("innerReleaseMap", () => makeReleaseMap.makeReleaseMap), T.bind("exitEA", (s) => T.provideAll_( T.result(restore(T.map_(self.effect, ({ tuple: [_, a] }) => a))), Tp.tuple(s.r, s.innerReleaseMap) ) ), T.bind("releaseMapEntry", (s) => add.add((e) => T.flatten( T.zipWith_( T.result(T.provideAll_(cleanup(s.exitEA), s.r, __trace)), T.result(releaseAll.releaseAll(e, sequential)(s.innerReleaseMap)), (l, r) => T.done(T.exitZipRight_(l, r)) ) ) )(s.outerReleaseMap) ), T.bind("a", (s) => T.done(s.exitEA)), T.map((s) => Tp.tuple(s.releaseMapEntry, s.a)) ) ) ) } /** * Like provideSome_ for effect but for Managed */ export function provideSome_( self: Managed, f: (r0: R0) => R, __trace?: string ): Managed { return managedApply( T.accessM(({ tuple: [r0, rm] }: Tp.Tuple<[R0, ReleaseMap]>) => T.provideAll_(self.effect, Tp.tuple(f(r0), rm), __trace) ) ) } /** * Like provideSome for effect but for Managed * * @ets_data_first provideSome_ */ export function provideSome(f: (r0: R0) => R, __trace?: string) { return (self: Managed) => provideSome_(self, f, __trace) } /** * Provides the `Managed` effect with its required environment, which eliminates * its dependency on `R`. * * @ets_data_first provideAll_ */ export function provideAll(r: R, __trace?: string) { return (self: Managed) => provideAll_(self, r) } /** * Provides the `Managed` effect with its required environment, which eliminates * its dependency on `R`. */ export function provideAll_(self: Managed, r: R, __trace?: string) { return provideSome_(self, () => r, __trace) } /** * A `Reservation` encapsulates resource acquisition and disposal * without specifying when or how that resource might be used. * * See `Managed#reserve` and `Effect#reserve` for details of usage. */ export class Reservation { static of = ( acquire: T.Effect, release: (exit: T.Exit) => T.Effect ) => new Reservation(acquire, release) private constructor( readonly acquire: T.Effect, readonly release: (exit: T.Exit) => T.Effect ) {} } /** * Make a new reservation */ export function makeReservation_( acquire: T.Effect, release: (exit: T.Exit) => T.Effect ) { return Reservation.of(acquire, release) } /** * Make a new reservation * * @ets_data_first makeReservation_ */ export function makeReservation( release: (exit: T.Exit) => T.Effect ) { return (acquire: T.Effect) => Reservation.of(acquire, release) } /** * Lifts a pure `Reservation< R, E, A>` into `Managed< R, E, A>`. The acquisition step * is performed interruptibly. */ export function reserve(reservation: Reservation, __trace?: string) { return makeReserve(T.succeed(reservation), __trace) } /** * Returns a managed that effectfully peeks at the acquired resource. */ export function tap_( self: Managed, f: (a: A) => Managed, __trace?: string ) { return chain_(self, (a) => map_(f(a), () => a), __trace) } /** * Returns a managed that effectfully peeks at the acquired resource. * * @ets_data_first tap_ */ export function tap(f: (a: A) => Managed, __trace?: string) { return (self: Managed) => tap_(self, f, __trace) } /** * Runs the acquire and release actions and returns the result of this * managed effect. Note that this is only safe if the result of this managed * effect is valid outside its scope. */ export function useNow(self: Managed, __trace?: string) { return use_(self, T.succeed, __trace) } /** * Use the resource until interruption. Useful for resources that you want * to acquire and use as long as the application is running, like a * HTTP server. */ export function useForever(self: Managed, __trace?: string) { return use_(self, () => T.never, __trace) } /** * Returns a managed that executes both this managed and the specified managed, * in sequence, combining their results with the specified `f` function. */ export function zip_( self: Managed, that: Managed, __trace?: string ) { return zipWith_(self, that, (a, a2) => [a, a2] as [A, A2], __trace) } /** * Returns a managed that executes both this managed and the specified managed, * in sequence, combining their results with the specified `f` function. * * @ets_data_first zip_ */ export function zip(that: Managed, __trace?: string) { return (self: Managed) => zip_(self, that, __trace) } /** * Returns a managed that executes both this managed and the specified managed, * in sequence, combining their results with the specified `f` function. * * @ets_data_first zipWith_ */ export function zipWith( that: Managed, f: (a: A, a2: A2) => B, __trace?: string ) { return (self: Managed) => zipWith_(self, that, f, __trace) } /** * Returns a managed that executes both this managed and the specified managed, * in sequence, combining their results with the specified `f` function. */ export function zipWith_( self: Managed, that: Managed, f: (a: A, a2: A2) => B, __trace?: string ) { return chain_(self, (a) => map_(that, (a2) => f(a, a2)), __trace) } /** * Returns a managed that executes both this managed and the specified managed, * in parallel, combining their results with the specified `f` function. * * @ets_data_first zipWithPar_ */ export function zipWithPar( that: Managed, f: (a: A, a2: A2) => B, __trace?: string ) { return (self: Managed): Managed => zipWithPar_(self, that, f, __trace) } /** * Returns a managed that executes both this managed and the specified managed, * in parallel, combining their results with the specified `f` function. */ export function zipWithPar_( self: Managed, that: Managed, f: (a: A, a2: A2) => B, __trace?: string ): Managed { return mapM_(makeManagedReleaseMap(parallel), (parallelReleaseMap) => { const innerMap = T.provideSome_( makeManagedReleaseMap(sequential).effect, (r: R & R2) => Tp.tuple(r, parallelReleaseMap) ) return T.chain_( T.zip_(innerMap, innerMap, __trace), ({ tuple: [ { tuple: [_, l] }, { tuple: [__, r] } ] }) => T.zipWithPar_( T.provideSome_(self.effect, (_: R & R2) => Tp.tuple(_, l)), T.provideSome_(that.effect, (_: R & R2) => Tp.tuple(_, r)), ({ tuple: [_, a] }, { tuple: [__, a2] }) => f(a, a2), __trace ) ) }) } /** * Returns a `Reservation` that allows separately accessing effects * describing resource acquisition and release. */ export function managedReserve( self: Managed ): T.UIO> { return T.map_(makeReleaseMap.makeReleaseMap, (releaseMap) => Reservation.of( T.map_( T.provideSome_(self.effect, (_: R) => Tp.tuple(_, releaseMap)), Tp.get(1) ), (_) => releaseAll.releaseAll(_, T.sequential)(releaseMap) ) ) }