stream: provide mapMaterializedValue for Graph (#28610)

This commit is contained in:
eyal farago 2020-02-20 13:30:32 +01:00 committed by GitHub
parent 8a354ec3f0
commit 5bb9a7145a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 0 deletions

View file

@ -6,6 +6,7 @@ package akka.stream
import akka.annotation.InternalApi
import akka.stream.impl.TraversalBuilder
import akka.stream.scaladsl.GenericGraph
import scala.annotation.unchecked.uncheckedVariance
@ -69,6 +70,34 @@ trait Graph[+S <: Shape, +M] {
def addAttributes(attr: Attributes): Graph[S, M] = withAttributes(traversalBuilder.attributes and attr)
}
object Graph {
/**
* Java API
* Transform the materialized value of this Flow, leaving all other properties as they were.
*
* @param g the graph being transformed
* @param f function to map the graph's materialized value
* @return a graph with same semantics as the given graph, except from the materialized value which is mapped using f.
*/
def mapMaterializedValue[S <: Shape, M1, M2](g: Graph[S, M1])(f: M1 => M2): Graph[S, M2] =
new GenericGraph(g.shape, g.traversalBuilder).mapMaterializedValue(f)
/**
* Scala API, see https://github.com/akka/akka/issues/28501 for discussion why this can't be an instance method on class Graph.
* @param self the graph whose materialized value will be mapped
*/
final implicit class GraphMapMatVal[S <: Shape, M](self: Graph[S, M]) {
/**
* Transform the materialized value of this Graph, leaving all other properties as they were.
*
* @param f function to map the graph's materialized value
*/
def mapMaterializedValue[M2](f: M => M2): Graph[S, M2] = Graph.mapMaterializedValue(self)(f)
}
}
/**
* INTERNAL API
*