Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions core-tests/shared/src/test/scala/zio/ZIOSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2671,6 +2671,28 @@ object ZIOSpec extends ZIOBaseSpec {
b <- effect.await
} yield assert(b)(equalTo(42))
} @@ zioTag(interruption),
testM("raceAllFirst interrupts losers on success") {
for {
s <- Promise.make[Nothing, Unit]
effect <- Promise.make[Nothing, Int]
winner = s.await *> IO.fromEither(Right(()))
losers = List(ZIO.bracket(s.succeed(()))(_ => effect.succeed(42))(_ => ZIO.infinity))
race = ZIO.raceFirst(winner, losers)
_ <- race
b <- effect.await
} yield assert(b)(equalTo(42))
} @@ zioTag(interruption),
testM("raceAllFirst interrupts losers on failure") {
for {
s <- Promise.make[Nothing, Unit]
effect <- Promise.make[Nothing, Int]
winner = s.await *> IO.fromEither(Left(new Exception))
losers = List(ZIO.bracket(s.succeed(()))(_ => effect.succeed(42))(_ => ZIO.infinity))
race = ZIO.raceFirst(winner, losers)
_ <- race.either
b <- effect.await
} yield assert(b)(equalTo(42))
} @@ zioTag(interruption),
testM("mergeAll") {
val io = IO.mergeAll(List("a", "aa", "aaa", "aaaa").map(IO.succeed[String](_)))(0)((b, a) => b + a.length)

Expand Down
15 changes: 15 additions & 0 deletions core/shared/src/main/scala/zio/ZIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3884,6 +3884,21 @@ object ZIO extends ZIOCompanionPlatformSpecific {
): ZIO[R1, E, A] =
zio.raceAll(ios)

/**
* Returns an effect that races this effect with all the specified effects,
* 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 "losers", but will not
* resume until the losers have been cleanly terminated. If early return is
* desired, then instead of performing `ZIO.raceFirst(l, rs)`, perform
* `ZIO.raceFirst(l.disconnect, rs.map(_.disconnect))`, which disconnects left
* and rights interrupt signal, allowing a fast return, with interruption
* performed in the background.
*/
def raceFirst[R, R1 <: R, E, A](zio: ZIO[R, E, A], ios: Iterable[ZIO[R1, E, A]]): ZIO[R1, E, A] =
(zio.run raceAll ios.map(_.run)).flatMap(ZIO.done(_)).refailWithTrace

/**
* Reduces an `Iterable[IO]` to a single `IO`, working sequentially.
*/
Expand Down