import * as tsplus_module_1 from "@effect/core/io/Effect/operations/interruption";
import * as tsplus_module_2 from "@effect/core/io/Cause/definition";
import * as tsplus_module_3 from "@effect/core/io/Fiber/operations/join";
import * as tsplus_module_4 from "@effect/core/io/Effect/operations/mapErrorCause";
import * as tsplus_module_5 from "@effect/core/io/Fiber/operations/interruptAs";
import * as tsplus_module_6 from "@effect/core/io/Effect/operations/as";
import * as tsplus_module_7 from "@effect/core/io/Exit/operations/foldEffect";
import * as tsplus_module_8 from "@effect/core/io/Effect/operations/raceWith";
import * as tsplus_module_9 from "@effect/core/io/Effect/operations/descriptorWith";
import * as tsplus_module_10 from "@tsplus/stdlib/data/Either/definition";
import * as tsplus_module_11 from "@effect/core/io/Effect/operations/map";
import * as tsplus_module_12 from "@effect/core/io/Effect/operations/done";
import * as tsplus_module_13 from "@effect/core/io/Effect/operations/exit";
import * as tsplus_module_14 from "@effect/core/io/Effect/operations/flatMap";
export const race_ = race_1;
/**
 * Returns an effect that races this effect with the specified effect,
 * returning the first successful `A` from the faster side. If one effect
 * succeeds, the other will be interrupted. If neither succeeds, then the
 * effect will fail with some error.
 *
 * WARNING: The raced effect will safely interrupt the "loser", but will not
 * resume until the loser has been cleanly terminated. If early return is
 * desired, then instead of performing `l race r`, perform `l.disconnect race
 * r.disconnect`, which disconnects left and right interruption signals,
 * allowing a fast return, with interruption performed in the background.
 *
 * Note that if the `race` is embedded into an uninterruptible region, then
 * because the loser cannot be interrupted, it will be allowed to continue
 * executing in the background, without delaying the return of the race.
 *
 * @tsplus fluent ets/Effect race
 */

function race_1(self, that, __tsplusTrace) {
  return tsplus_module_9.descriptorWith(descriptor => {
    const parentFiberId = descriptor.id;

    function maybeDisconnect(io) {
      return tsplus_module_1.uninterruptibleMask(({
        force
      }) => force(io, __tsplusTrace), __tsplusTrace);
    }

    return tsplus_module_8.raceWith_(maybeDisconnect(() => self), () => maybeDisconnect(that), (exit, right) => tsplus_module_7.foldEffect_(exit, cause => tsplus_module_4.mapErrorCause_(tsplus_module_3.join(right, __tsplusTrace), _ => tsplus_module_2.combinePar(cause, _), __tsplusTrace), a => tsplus_module_6.as_(tsplus_module_5.interruptAsNow_(right, parentFiberId, __tsplusTrace), () => a, __tsplusTrace), __tsplusTrace), (exit, left) => tsplus_module_7.foldEffect_(exit, cause => tsplus_module_4.mapErrorCause_(tsplus_module_3.join(left, __tsplusTrace), _ => tsplus_module_2.combinePar(_, cause), __tsplusTrace), a => tsplus_module_6.as_(tsplus_module_5.interruptAsNow_(left, parentFiberId, __tsplusTrace), () => a, __tsplusTrace), __tsplusTrace), __tsplusTrace);
  }, __tsplusTrace);
}
/**
 * Returns an effect that races this effect with the specified effect,
 * returning the first successful `A` from the faster side. If one effect
 * succeeds, the other will be interrupted. If neither succeeds, then the
 * effect will fail with some error.
 *
 * WARNING: The raced effect will safely interrupt the "loser", but will not
 * resume until the loser has been cleanly terminated. If early return is
 * desired, then instead of performing `l race r`, perform `l.disconnect race
 * r.disconnect`, which disconnects left and right interruption signals,
 * allowing a fast return, with interruption performed in the background.
 *
 * Note that if the `race` is embedded into an uninterruptible region, then
 * because the loser cannot be interrupted, it will be allowed to continue
 * executing in the background, without delaying the return of the race.
 *
 * @tsplus static ets/Effect/Aspects race
 */


export const race = (that, __tsplusTrace) => self => race_(self, that, __tsplusTrace);
/**
 * Returns an effect that races this effect with the specified effect,
 * yielding the first result to succeed. If neither effect succeeds, then the
 * composed effect will fail with some error.
 *
 * WARNING: The raced effect will safely interrupt the "loser", but will not
 * resume until the loser has been cleanly terminated.
 *
 * @tsplus fluent ets/Effect raceEither
 */

export function raceEither_(self, that, __tsplusTrace) {
  return race_1(tsplus_module_11.map_(self, tsplus_module_10.left, __tsplusTrace), () => tsplus_module_11.map_(that(), tsplus_module_10.right, __tsplusTrace), __tsplusTrace);
}
/**
 * Returns an effect that races this effect with the specified effect,
 * yielding the first result to succeed. If neither effect succeeds, then the
 * composed effect will fail with some error.
 *
 * WARNING: The raced effect will safely interrupt the "loser", but will not
 * resume until the loser has been cleanly terminated.
 *
 * @tsplus static ets/Effect/Aspects raceEither
 */

export const raceEither = (that, __tsplusTrace) => self => raceEither_(self, that, __tsplusTrace);
/**
 * Returns an effect that races this effect with the specified effect,
 * yielding the first result to complete, whether by success or failure. If
 * neither effect completes, then the composed effect will not complete.
 *
 * WARNING: The raced effect will safely interrupt the "loser", but will not
 * resume until the loser has been cleanly terminated. If early return is
 * desired, then instead of performing `l raceFirst r`, perform
 * `l.disconnect raceFirst r.disconnect`, which disconnects left and right
 * interrupt signal, allowing a fast return, with interruption performed
 * in the background.
 *
 * @tsplus fluent ets/Effect raceFirst
 */

export function raceFirst_(self, that, __tsplusTrace) {
  return tsplus_module_14.flatMap_(race_1(tsplus_module_13.exit(self, __tsplusTrace), () => tsplus_module_13.exit(that(), __tsplusTrace), __tsplusTrace), a => tsplus_module_12.done(() => a, __tsplusTrace), __tsplusTrace);
}
/**
 * Returns an effect that races this effect with the specified effect,
 * yielding the first result to complete, whether by success or failure. If
 * neither effect completes, then the composed effect will not complete.
 *
 * WARNING: The raced effect will safely interrupt the "loser", but will not
 * resume until the loser has been cleanly terminated. If early return is
 * desired, then instead of performing `l raceFirst r`, perform
 * `l.disconnect raceFirst r.disconnect`, which disconnects left and right
 * interrupt signal, allowing a fast return, with interruption performed
 * in the background.
 *
 * @tsplus static ets/Effect/Aspects raceFirst
 */

export const raceFirst = (that, __tsplusTrace) => self => raceFirst_(self, that, __tsplusTrace);
//# sourceMappingURL=race.mjs.map