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 all 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
17 changes: 14 additions & 3 deletions core/shared/src/main/scala/fs2/Stream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,19 @@ final class Stream[+F[_], +O] private[fs2] (private[fs2] val underlying: Pull[F,
go(Nil, false, this).stream
}

/** Buffers the chunks of this stream through a queue, and returns a new stream that consumes
* 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.
*
* If the supplied queue is not empty, any residing chunks will be emitted to the new stream.
*/
def bufferThrough[F2[x] >: F[x]: Concurrent, O2 >: O](
queue: Queue[F2, Option[Chunk[O2]]]
): Stream[F2, O2] =
Stream
.fromQueueNoneTerminatedChunk(queue)
.concurrently(this.enqueueNoneTerminatedChunks(queue))

/** Emits only elements that are distinct from their immediate predecessors,
* using natural equality for comparison.
*
Expand Down Expand Up @@ -2121,9 +2134,7 @@ final class Stream[+F[_], +O] private[fs2] (private[fs2] val underlying: Pull[F,
n: Int
): Stream[F2, O] =
Stream.eval(Queue.bounded[F2, Option[Chunk[O]]](n)).flatMap { queue =>
Stream
.fromQueueNoneTerminatedChunk(queue)
.concurrently(enqueueNoneTerminatedChunks(queue))
this.bufferThrough(queue)
}

/** Rechunks the stream such that output chunks are within `[inputChunk.size * minFactor, inputChunk.size * maxFactor]`.
Expand Down