Created
February 13, 2022 00:22
-
-
Save djspiewak/7b7cf1d16c9e7c2d8f77ffef6f0294a5 to your computer and use it in GitHub Desktop.
This file contains 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
class Channel[F[_], A] private (q: Queue[F, A], closed: Ref[F, Boolean])(implicit F: Monad[F]) { | |
// doesn't interrupt taking in progress | |
def close: F[Unit] = closed.set(true) | |
def isClosed: F[Boolean] = closed.get | |
def send(a: A): F[Option[Unit]] = | |
closed.get.ifM(q.offer(a).map(Some(_)), F.pure(None)) | |
def take: Stream[F, A] = { | |
val takeN: F[Chunk[A]] = | |
q.tryTakeN(None) flatMap { | |
case None => | |
closed.get.ifM(q.take.map(Chunk.singleton(_)), F.pure(Chunk.empty[A])) | |
case Some(as) => | |
F.pure(Chunk.seq(as)) | |
} | |
// you can do this more efficiently, just proves a point | |
Stream.eval(takeN).repeat.takeWhile(!_.isEmpty).flatMap(Stream.chunk(_)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment