stream: add flatMapPrefix operator (#28380)

This commit is contained in:
eyal farago 2020-02-05 15:37:27 +01:00 committed by GitHub
parent 722ea4ff87
commit ccd8481fec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 945 additions and 2 deletions

View file

@ -3148,6 +3148,47 @@ final class Source[Out, Mat](delegate: scaladsl.Source[Out, Mat]) extends Graph[
Mat] =
new Source(delegate.prefixAndTail(n).map { case (taken, tail) => Pair(taken.asJava, tail.asJava) })
/**
* Takes up to `n` elements from the stream (less than `n` only if the upstream completes before emitting `n` elements),
* then apply `f` on these elements in order to obtain a flow, this flow is then materialized and the rest of the input is processed by this flow (similar to via).
* This method returns a flow consuming the rest of the stream producing the materialized flow's output.
*
* '''Emits when''' the materialized flow emits.
* Notice the first `n` elements are buffered internally before materializing the flow and connecting it to the rest of the upstream - producing elements at its own discretion (might 'swallow' or multiply elements).
*
* '''Backpressures when''' downstream backpressures
*
* '''Completes when''' the materialized flow completes.
* If upstream completes before producing `n` elements, `f` will be applied with the provided elements,
* the resulting flow will be materialized and signalled for upstream completion, it can then complete or continue to emit elements at its own discretion.
*
* '''Cancels when''' the materialized flow cancels.
* Notice that when downstream cancels prior to prefix completion, the cancellation cause is stashed until prefix completion (or upstream completion) and then handed to the materialized flow.
*
* @param n the number of elements to accumulate before materializing the downstream flow.
* @param f a function that produces the downstream flow based on the upstream's prefix.
**/
def flatMapPrefix[Out2, Mat2](
n: Int,
f: function.Function[java.lang.Iterable[Out], javadsl.Flow[Out, Out2, Mat2]]): javadsl.Source[Out2, Mat] = {
val newDelegate = delegate.flatMapPrefix(n)(seq => f(seq.asJava).asScala)
new javadsl.Source(newDelegate)
}
/**
* mat version of [[#flatMapPrefix]], this method gives access to a future materialized value of the downstream flow (as a completion stage).
* see [[#flatMapPrefix]] for details.
*/
def flatMapPrefixMat[Out2, Mat2, Mat3](
n: Int,
f: function.Function[java.lang.Iterable[Out], javadsl.Flow[Out, Out2, Mat2]],
matF: function.Function2[Mat, CompletionStage[Mat2], Mat3]): javadsl.Source[Out2, Mat3] = {
val newDelegate = delegate.flatMapPrefixMat(n)(seq => f(seq.asJava).asScala) { (m1, fm2) =>
matF(m1, fm2.toJava)
}
new javadsl.Source(newDelegate)
}
/**
* This operation demultiplexes the incoming stream into separate output
* streams, one for each element key. The key is computed for each element