Search code examples
scalaakka-stream

Akka-streams - How to take the materialized value of source which is flatMapConcatenated in to another source?


I am trying to create a Source[Out,Mat] from a Future[Source[Out,Mat]] and was using the built-in source.fromFuture for that in combination with flatMapConcat. However when using fromFuture the source has a materialized value of NotUsed. When flatMapConcatenating the actual source, the materialized value keeps being notUsed.

Is it possible to take the materialized value of a source which is used inside the flatMapConcat?

The code looks as follows:

def sourceFromFutureSource[Out,Mat](futureSource: Future[Source[Out,Mat]]): Source[Out, Mat] = {
Source.fromFuture(futureSource)
  .flatMapConcat(identity)
}

Solution

  • Since Akka 2.5.1 it is possible to use Source.fromFutureSource. Now you can do Source.fromFutureSource(futureSource).