// ets_tracing: off import * as C from "../Cause/core.js" import { FiberFailure } from "../Cause/errors.js" import * as A from "../Collections/Immutable/Array/index.js" import * as Tp from "../Collections/Immutable/Tuple/index.js" import * as E from "../Either/index.js" import type { FiberID } from "../Fiber/id.js" import { identity, pipe } from "../Function/index.js" import * as O from "../Option/index.js" import type { Exit } from "./exit.js" import { Failure, Success } from "./exit.js" export { Exit, Failure, Success } from "./exit.js" /** * Applicative's ap */ export function ap_(fa: Exit, fab: Exit B>): Exit { return chain_(fab, (f) => map_(fa, (a) => f(a))) } /** * Applicative's ap * * @ets_data_first ap_ */ export function ap(fa: Exit) { return (fab: Exit B>): Exit => ap_(fa, fab) } /** * Replaces the success value with the one provided. */ export function as_(exit: Exit, b: B) { return map_(exit, () => b) } /** * Replaces the success value with the one provided. * * @ets_data_first as_ */ export function as(b: B) { return (exit: Exit) => as_(exit, b) } /** * Maps over both the error and value type. */ export function bimap_( exit: Exit, f: (e: E) => E1, g: (a: A) => A1 ) { return pipe(exit, map(g), mapError(f)) } /** * Maps over both the error and value type. * * @ets_data_first bimap_ */ export function bimap(f: (e: E) => E1, g: (a: A) => A1) { return (exit: Exit) => bimap_(exit, f, g) } /** * Flat maps over the value type. */ export function chain_( exit: Exit, f: (a: A) => Exit ): Exit { switch (exit._tag) { case "Failure": { return exit } case "Success": { return f(exit.value) } } } /** * Flat maps over the value type. * * @ets_data_first chain_ */ export function chain(f: (a: A) => Exit) { return (exit: Exit): Exit => chain_(exit, f) } /** * Collects all the success states and merges sequentially the causes */ export function collectAll( ...exits: readonly Exit[] ): O.Option> { return pipe( A.head(exits), O.map((head) => pipe( A.drop_(exits, 1), A.reduce( pipe( head, map((x): readonly A[] => [x]) ), (acc, el) => pipe( acc, zipWith(el, (acc, el) => [el, ...acc], C.combineSeq) ) ), map(A.reverse) ) ) ) } /** * Zips this together with the specified result using the combination functions. */ export function zipWith_( exit: Exit, that: Exit, f: (a: A, b: B) => C, g: (e: C.Cause, e1: C.Cause) => C.Cause ): Exit { switch (exit._tag) { case "Failure": { switch (that._tag) { case "Success": { return exit } case "Failure": { return halt(g(exit.cause, that.cause)) } } } // eslint-disable-next-line no-fallthrough case "Success": { switch (that._tag) { case "Success": { return succeed(f(exit.value, that.value)) } case "Failure": { return that } } } } } /** * Zips this together with the specified result using the combination functions. * * @ets_data_first zipWith_ */ export function zipWith( that: Exit, f: (a: A, b: B) => C, g: (e: C.Cause, e1: C.Cause) => C.Cause ) { return (exit: Exit): Exit => zipWith_(exit, that, f, g) } /** * Collects all the success states and merges the causes in parallel */ export function collectAllPar( ...exits: readonly Exit[] ): O.Option> { return pipe( A.head(exits), O.map((head) => pipe( A.drop_(exits, 1), A.reduce( pipe( head, map((x): readonly A[] => [x]) ), (acc, el) => pipe( acc, zipWith(el, (acc, el) => [el, ...acc], C.combinePar) ) ), map(A.reverse) ) ) ) } /** * Construct an Exit with an unchecked cause containing the specified error */ export function die(error: unknown) { return halt(C.die(error)) } /** * Returns f(a) if the exit is successful */ export function exists_(exit: Exit, f: (a: A) => boolean): boolean { return pipe( exit, fold(() => false, f) ) } /** * Returns f(a) if the exit is successful * * @ets_data_first exists_ */ export function exists(f: (a: A) => boolean) { return (exit: Exit): boolean => exists_(exit, f) } /** * Constructs a failed exit with the specified checked error */ export function fail(e: E) { return halt(C.fail(e)) } /** * Constructs a failed exit with the specified cause */ export function failCause(cause: C.Cause): Exit { return new Failure(cause) } /** * Flatten nested Exits */ export function flatten(exit: Exit>) { return pipe(exit, chain(identity)) } /** * Folds over the value or cause. */ export function fold_( exit: Exit, failed: (e: C.Cause) => Z1, succeed: (a: A) => Z2 ): Z1 | Z2 { switch (exit._tag) { case "Success": { return succeed(exit.value) } case "Failure": { return failed(exit.cause) } } } /** * Folds over the value or cause. * * @ets_data_first fold_ */ export function fold( failed: (e: C.Cause) => Z1, succeed: (a: A) => Z2 ) { return (exit: Exit): Z1 | Z2 => fold_(exit, failed, succeed) } /** * Embeds Either's Error & Success in an Exit */ export function fromEither(e: E.Either): Exit { return e._tag === "Left" ? fail(e.left) : succeed(e.right) } /** * Embeds an option result into an Exit with the specified error using onNone */ export function fromOption(onNone: () => E) { return (a: O.Option): Exit => a._tag === "None" ? fail(onNone()) : succeed(a.value) } /** * Get successful result falling back to orElse result in case of failure */ export function getOrElse_( exit: Exit, orElse: (_: C.Cause) => A1 ): A | A1 { switch (exit._tag) { case "Success": { return exit.value } case "Failure": { return orElse(exit.cause) } } } /** * Get successful result falling back to orElse result in case of failure * * @ets_data_first getOrElse_ */ export function getOrElse(orElse: (_: C.Cause) => A1) { return (exit: Exit): A | A1 => getOrElse_(exit, orElse) } /** * Constructs a failed exit with the specified cause */ export function halt(cause: C.Cause): Exit { return new Failure(cause) } /** * Constructs an exit with the specified interruption state */ export function interrupt(id: FiberID) { return halt(C.interrupt(id)) } /** * Returns if Exit contains an interruption state */ export function interrupted(exit: Exit): exit is Failure { switch (exit._tag) { case "Success": { return false } case "Failure": { return C.interrupted(exit.cause) } } } /** * Maps over the value type. */ export function map_(exit: Exit, f: (a: A) => A1): Exit { return pipe( exit, chain((a) => succeed(f(a))) ) } /** * Maps over the value type. * * @ets_data_first map_ */ export function map(f: (a: A) => A1) { return (exit: Exit): Exit => map_(exit, f) } /** * Maps over the error type. */ export function mapError_(exit: Exit, f: (e: E) => E1): Exit { switch (exit._tag) { case "Failure": { return halt(C.map(f)(exit.cause)) } case "Success": { return exit } } } /** * Maps over the error type. * * @ets_data_first mapError_ */ export function mapError(f: (e: E) => E1) { return (exit: Exit): Exit => mapError_(exit, f) } /** * Maps over the cause type. */ export function mapErrorCause_( exit: Exit, f: (e: C.Cause) => C.Cause ): Exit { switch (exit._tag) { case "Failure": { return halt(f(exit.cause)) } case "Success": { return exit } } } /** * Maps over the cause type. * * @ets_data_first mapErrorCause_ */ export function mapErrorCause(f: (e: C.Cause) => C.Cause) { return (exit: Exit): Exit => mapErrorCause_(exit, f) } /** * Replaces the error value with the one provided. */ export function orElseFail_(exit: Exit, e: E1): Exit { return pipe( exit, mapError(() => e) ) } /** * Replaces the error value with the one provided. * * @ets_data_first orElseFail_ */ export function orElseFail(e: E1) { return (exit: Exit) => orElseFail_(exit, e) } /** * Construct a succeeded exit with the specified value */ export function succeed(a: A): Exit { return new Success(a) } /** * Returns if an exit is succeeded */ export function succeeded(exit: Exit): exit is Success { switch (exit._tag) { case "Failure": { return false } case "Success": { return true } } } /** * Converts the `Exit` to an `Either`, by wrapping the * cause in `FiberFailure` (if the result is failed). */ export function toEither(exit: Exit): E.Either, A> { switch (exit._tag) { case "Success": { return E.right(exit.value) } case "Failure": { return E.left(new FiberFailure(exit.cause)) } } } /** * Discards the value. */ export const unit: Exit = succeed(undefined) /** * Sequentially zips the this result with the specified result or else returns the failed `Cause[E1]` */ export function zip_( exit: Exit, that: Exit ): Exit> { return pipe( exit, zipWith(that, (a, b) => Tp.tuple(a, b), C.combineSeq) ) } /** * Sequentially zips the this result with the specified result or else returns the failed `Cause[E1]` * * @ets_data_first zip_ */ export function zip(that: Exit) { return (exit: Exit): Exit> => zip_(exit, that) } /** * Sequentially zips the this result with the specified result discarding the second element of the tuple or else returns the failed `Cause[E1]` */ export function zipLeft_( exit: Exit, that: Exit ): Exit { return pipe( exit, zipWith(that, (a, _) => a, C.combineSeq) ) } /** * Sequentially zips the this result with the specified result discarding the second element of the tuple or else returns the failed `Cause[E1]` * * @ets_data_first zipLeft_ */ export function zipLeft(that: Exit) { return (exit: Exit): Exit => zipLeft_(exit, that) } /** * Parallelly zips the this result with the specified result or else returns the failed `Cause[E1]` */ export function zipPar_( exit: Exit, that: Exit ): Exit> { return pipe( exit, zipWith(that, (a, b) => Tp.tuple(a, b), C.combinePar) ) } /** * Parallelly zips the this result with the specified result or else returns the failed `Cause[E1]` * * @ets_data_first zipPar_ */ export function zipPar(that: Exit) { return (exit: Exit): Exit> => zipPar_(exit, that) } /** * Parallelly zips the this result with the specified result discarding the second element of the tuple or else returns the failed `Cause[E1]` */ export function zipParLeft_( exit: Exit, that: Exit ): Exit { return pipe( exit, zipWith(that, (a, _) => a, C.combinePar) ) } /** * Parallelly zips the this result with the specified result discarding the second element of the tuple or else returns the failed `Cause[E1]` * * @ets_data_first zipParLeft_ */ export function zipParLeft(that: Exit) { return (exit: Exit): Exit => zipParLeft_(exit, that) } /** * Parallelly zips the this result with the specified result discarding the first element of the tuple or else returns the failed `Cause[E1]` */ export function zipParRight_( exit: Exit, that: Exit ): Exit { return pipe( exit, zipWith(that, (_, b) => b, C.combinePar) ) } /** * Parallelly zips the this result with the specified result discarding the first element of the tuple or else returns the failed `Cause[E1]` * * @ets_data_first zipParRight_ */ export function zipParRight(that: Exit) { return (exit: Exit): Exit => zipParRight_(exit, that) } /** * Sequentially zips the this result with the specified result discarding the first element of the tuple or else returns the failed `Cause[E1]` */ export function zipRight_( exit: Exit, that: Exit ): Exit { return pipe( exit, zipWith(that, (_, b) => b, C.combineSeq) ) } /** * Sequentially zips the this result with the specified result discarding the first element of the tuple or else returns the failed `Cause[E1]` * * @ets_data_first zipRight_ */ export function zipRight(that: Exit) { return (exit: Exit): Exit => zipRight_(exit, that) } /** * Returns an untraced exit value. */ export function untraced(self: Exit): Exit { return self._tag === "Success" ? self : halt(C.untraced(self.cause)) } /** * Asserts an exit is a failure */ export function assertsFailure(exit: Exit): asserts exit is Failure { if (exit._tag === "Success") { throw new Error("expected a failed exit and got success") } } /** * Maps over both the error and value type. */ export function mapBoth_( self: Exit, f: (e: E) => E1, g: (a: A) => A1 ): Exit { return map_(mapError_(self, f), g) } /** * Maps over both the error and value type. * * @ets_data_first mapBoth_ */ export function mapBoth(f: (e: E) => E1, g: (a: A) => A1) { return (self: Exit) => mapBoth_(self, f, g) }