Akka: Source from sink an vice versa

Created on 6 Apr 2018  路  2Comments  路  Source: akka/akka

Spawned by this question: https://discuss.lightbend.com/t/create-source-from-sink-and-vice-versa/605

Maybe we should have a prepackaged thing for this?

1 - triaged stream

Most helpful comment

Yeah seems like it may be useful and mind bending for beginners to make it yourself so good to provide it I think

All 2 comments

Yeah seems like it may be useful and mind bending for beginners to make it yourself so good to provide it I think

/**
  * Materializes into a sink connected to a source, i.e. the sink pushes into the source:
  *
  * +----------+       +----------+
  * >   Sink   |------>|  Source  >
  * +----------+       +----------+
  *
  * Should be provided by Akka Streams, see https://github.com/akka/akka/issues/24853.
  */
val connection: RunnableGraph[(Sink[String, NotUsed], Source[String, NotUsed])] =
  Source
    .asSubscriber[String]
    .toMat(Sink.asPublisher[String](fanout = false))(Keep.both)
    .mapMaterializedValue {
      case (sub, pub) => (Sink.fromSubscriber(sub), Source.fromPublisher(pub))
    }
Was this page helpful?
0 / 5 - 0 ratings