Skip to content

Commit

Permalink
Fix the race combinators (#205)
Browse files Browse the repository at this point in the history
* Fix the race combinators

* Law abiding citizen

Co-authored-by: [email protected] <[email protected]>
  • Loading branch information
Fristi and Fristi authored Jul 14, 2020
1 parent 74c130b commit 8ebd453
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions interop-cats/shared/src/main/scala/zio/interop/cats.scala
Original file line number Diff line number Diff line change
Expand Up @@ -227,20 +227,28 @@ private class CatsConcurrent[R] extends CatsMonadError[R, Throwable] with Concur
Left(token.orDie)
}

override final def race[A, B](fa: RIO[R, A], fb: RIO[R, B]): RIO[R, Either[A, B]] =
fa.map(Left(_)).interruptible raceFirst fb.map(Right(_)).interruptible
override final def race[A, B](fa: RIO[R, A], fb: RIO[R, B]): RIO[R, Either[A, B]] = {
def run[C](fc: RIO[R, C]): ZIO[R, Throwable, C] =
fc.interruptible.overrideForkScope(ZScope.global)

run(fa.map(Left(_))) raceFirst run(fb.map(Right(_)))
}

override final def start[A](fa: RIO[R, A]): RIO[R, effect.Fiber[RIO[R, *], A]] =
fa.interruptible.forkDaemon.map(toFiber)

override final def racePair[A, B](
fa: RIO[R, A],
fb: RIO[R, B]
): RIO[R, Either[(A, effect.Fiber[RIO[R, *], B]), (effect.Fiber[RIO[R, *], A], B)]] =
(fa.interruptible raceWith fb.interruptible)(
): RIO[R, Either[(A, effect.Fiber[RIO[R, *], B]), (effect.Fiber[RIO[R, *], A], B)]] = {
def run[C](fc: RIO[R, C]): ZIO[R, Throwable, C] =
fc.interruptible.overrideForkScope(ZScope.global)

(run(fa) raceWith run(fb))(
{ case (l, f) => l.fold(f.interrupt *> ZIO.halt(_), ZIO.succeedNow).map(lv => Left((lv, toFiber(f)))) },
{ case (r, f) => r.fold(f.interrupt *> ZIO.halt(_), ZIO.succeedNow).map(rv => Right((toFiber(f), rv))) }
)
}

override final def never[A]: RIO[R, A] =
ZIO.never
Expand Down

0 comments on commit 8ebd453

Please sign in to comment.