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

@ -1357,6 +1357,33 @@ class SubSource[Out, Mat](
Mat] =
new SubSource(delegate.prefixAndTail(n).map { case (taken, tail) => akka.japi.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.SubSource[Out2, Mat] = {
val newDelegate = delegate.flatMapPrefix(n)(seq => f(seq.asJava).asScala)
new javadsl.SubSource(newDelegate)
}
/**
* Transform each input element into a `Source` of output elements that is
* then flattened into the output stream by concatenation,