"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.raceEither = exports.race = void 0;
exports.raceEither_ = raceEither_;
exports.raceFirst = void 0;
exports.raceFirst_ = raceFirst_;
exports.race_ = void 0;

var tsplus_module_1 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/interruption"));

var tsplus_module_2 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Cause/definition"));

var tsplus_module_3 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Fiber/operations/join"));

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/mapErrorCause"));

var tsplus_module_5 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Fiber/operations/interruptAs"));

var tsplus_module_6 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/as"));

var tsplus_module_7 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Exit/operations/foldEffect"));

var tsplus_module_8 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/raceWith"));

var tsplus_module_9 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/descriptorWith"));

var tsplus_module_10 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Either/definition"));

var tsplus_module_11 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/map"));

var tsplus_module_12 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/done"));

var tsplus_module_13 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/exit"));

var tsplus_module_14 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/flatMap"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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
 */

exports.race_ = 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
 */


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
 */


exports.race = race;

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
 */


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
 */


exports.raceEither = raceEither;

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
 */


const raceFirst = (that, __tsplusTrace) => self => raceFirst_(self, that, __tsplusTrace);

exports.raceFirst = raceFirst;
//# sourceMappingURL=race.js.map