Skip to content

Instantly share code, notes, and snippets.

@amitayh
Created February 14, 2020 06:34
Show Gist options
  • Save amitayh/9649432804c918934285d4b7ea099e3a to your computer and use it in GitHub Desktop.
Save amitayh/9649432804c918934285d4b7ea099e3a to your computer and use it in GitHub Desktop.
import zio.{Promise, Ref, UIO, ZIO}
trait CountDownLatch {
def countDown: UIO[Unit]
def await: UIO[Unit]
}
object CountDownLatch {
def make(count: Int): UIO[CountDownLatch] = for {
ready <- Promise.make[Nothing, Unit]
ref <- Ref.make(count)
} yield new CountDownLatch {
override def countDown: UIO[Unit] =
ref.update(_ - 1).flatMap {
case 0 => ready.succeed(()).unit
case _ => ZIO.unit
}
override def await: UIO[Unit] = ready.await
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment