How can I use and return Source queue to caller without materializing it?

前提是你 提交于 2020-01-13 10:53:14

问题


I'm trying to use new Akka streams and wonder how I can use and return Source queue to caller without materializing it in my code ?

Imagine we have library that makes number of async calls and returns results via Source. Function looks like this

def findArticlesByTitle(text: String): Source[String, SourceQueue[String]] = {

  val source = Source.queue[String](100, backpressure)

  source.mapMaterializedValue { case queue =>

    val url = s"http://.....&term=$text"
    httpclient.get(url).map(httpResponseToSprayJson[SearchResponse]).map { v =>
      v.idlist.foreach { id =>
        queue.offer(id)
      }

      queue.complete()
    }
  }

  source
}

and caller might use it like this

// There is implicit ActorMaterializer somewhere
val stream = plugin.findArticlesByTitle(title)
val results = stream.runFold(List[String]())((result, article) => article :: result)

When I run this code within mapMaterializedValue is never executed.

I can't understand why I don't have access to instance of SourceQueue if it should be up to caller to decide how to materialize the source.

How should I implement this ?


回答1:


In your code example you're returning source instead of the return value of source.mapMaterializedValue (the method call doesn't mutate the Source object).



来源:https://stackoverflow.com/questions/37113877/how-can-i-use-and-return-source-queue-to-caller-without-materializing-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!