Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buffer through #2205

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 5 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
19 changes: 19 additions & 0 deletions core/shared/src/main/scala/fs2/Stream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,25 @@ final class Stream[+F[_], +O] private[fs2] (private[fs2] val underlying: Pull[F,
go(Nil, false, this).stream
}

// A builder for queues.
trait MakeQueue[F[_]] {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really sure where to put this, but it lets us create a queue-variant chosen by the user but they can't tamper with its contents at all since create is polymorphic.

def create[A]: F[Queue[F, A]]
}

/** Buffers the chunks of this stream through a queue that is created by the supplied builder,
* and returns a new stream that consumes the chunks from that queue. The new stream terminates
* after both this stream terminates and all chunks are emitted. Errors from this stream are
* propagated to the new stream.
*/
def bufferThrough[F2[x] >: F[x]: Concurrent, O2 >: O](
mkQueue: MakeQueue[F2]
): Stream[F2, O2] =
Stream.eval(mkQueue.create[Option[Chunk[O2]]]).flatMap { queue =>
Stream
.fromQueueNoneTerminatedChunk(queue)
.concurrently(this.enqueueNoneTerminatedChunks(queue))
}

/** Emits only elements that are distinct from their immediate predecessors,
* using natural equality for comparison.
*
Expand Down