Adding preMaterialize operator for Source #23894 (#24497)

This commit is contained in:
Stefano Bonetti 2018-02-21 06:06:01 +00:00 committed by Konrad `ktoso` Malawski
parent e44fafd4b7
commit 3ea59b1e76
7 changed files with 135 additions and 4 deletions

View file

@ -87,6 +87,15 @@ final class Source[+Out, +Mat](
override def mapMaterializedValue[Mat2](f: Mat Mat2): ReprMat[Out, Mat2] =
new Source[Out, Mat2](traversalBuilder.transformMat(f.asInstanceOf[Any Any]), shape)
/**
* Materializes this Source, immediately returning (1) its materialized value, and (2) a new Source
* that can be used to consume elements from the newly materialized Source.
*/
def preMaterialize()(implicit materializer: Materializer): (Mat, ReprMat[Out, NotUsed]) = {
val (mat, pub) = toMat(Sink.asPublisher(fanout = true))(Keep.both).run()
(mat, Source.fromPublisher(pub))
}
/**
* Connect this `Source` to a `Sink` and run it. The returned value is the materialized value
* of the `Sink`, e.g. the `Publisher` of a [[akka.stream.scaladsl.Sink#publisher]].