Request variant of StreamConverters.javaCollector that emits instead of materializing
#27,298 建立於 2019年7月8日
倉庫指標
- 星標
- (13,277 顆星)
- PR 合併指標
- (平均合併 8天 19小時) (30 天內合併 10 個 PR)
描述
The current StreamConverters.javaCollector() returns a Sink[T, CompletionStage[R]], i.e. it uses the materialized value to represent the result of the Collector's reduction. This is analogous to using (object) Sink.fold(), which also uses the materialized value to represent the result of the reduction. The alternative would be (class) Flow.fold(), which "emits its result when the upstream completes", returning Flow[In, T, Mat].
The Flow.fold() approach of emitting one element downstream, instead of materializing, seems more versatile. Obtaining the result doesn't break out of the Graph. To convert from Flow[In, T, Mat] to Sink[In, CompletionStage[T]], we only need to do:
val sink = flow.fold(...)
.toMat(Sink.head, Keep.right)
vs, to convert from Sink[In, CompletionStage[T]] to Flow[In, T, NotUsed], we need something like:
val (result, newSink) = Sink.fold(...).preMaterialize(mat)
val flow = Flow.fromSinkAndSource(newSink, Source.fromFuture(result))
which is conceptually more difficult, and since it requires pre-materialization, breaks the copy-ability of the resulting Flow.
If my above assessment is correct, it makes more sense to model StreamConverters.javaCollector off of the 'emit-style' Flow.fold() rather than the 'materialize-style' Sink.fold().