/** * 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. * * Note that both effects are disconnected before being raced. This means that * interruption of the loser will always be performed in the background. This * is a change in behavior compared to ZIO 2.0. If this behavior is not * desired, you can use [[Effect#raceWith]], which will not disconnect or * interrupt losers. * * @tsplus static effect/core/io/Effect.Aspects race * @tsplus pipeable effect/core/io/Effect race */ export function race(that: Effect) { return (self: Effect): Effect => self.disconnect.raceAwait(that.disconnect) } /** * 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. * * @tsplus static effect/core/io/Effect.Aspects raceAwait * @tsplus pipeable effect/core/io/Effect raceAwait */ export function raceAwait(that: Effect) { return (self: Effect): Effect => Effect.withFiberRuntime((state) => self.raceWith( that, (exit, right) => exit.foldEffect( (cause) => right.join.mapErrorCause((_) => cause & _), (a) => right.interruptAs(state.id).as(a) ), (exit, left) => exit.foldEffect( (cause) => left.join.mapErrorCause((_) => _ & cause), (a) => left.interruptAs(state.id).as(a) ) ) ) } /** * 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 effect/core/io/Effect.Aspects raceEither * @tsplus pipeable effect/core/io/Effect raceEither */ export function raceEither(that: Effect) { return (self: Effect): Effect> => self.map(Either.left).race(that.map(Either.right)) } /** * 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 effect/core/io/Effect.Aspects raceFirst * @tsplus pipeable effect/core/io/Effect raceFirst */ export function raceFirst(that: Effect) { return (self: Effect): Effect => self.exit.race(that.exit).flatten }