stream: add flatMapPrefix operator (#28380)
This commit is contained in:
parent
722ea4ff87
commit
ccd8481fec
12 changed files with 945 additions and 2 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue