Created
February 14, 2020 06:34
-
-
Save amitayh/9649432804c918934285d4b7ea099e3a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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