I came up with a solution (below) however it is sub optimal - it has to 
materialise the graph every time to get Sink.lastOption - ideally what I 
want is to avoid materialisation on each iteration:

    "Loop on Source on Sink.optionLast" in {
      def dynamicSource = Source(1 to Random.nextInt(4)) // represents a 
cursor from a database that we want to re-run if not empty

      def maybeLastFromMaterialisedGraph: Future[Option[Int]] = {
        val source = dynamicSource
        val sink = Sink.foreach(println)
        val sink2 = Sink.lastOption[Int]
        val dataFlow =
        val (fIgnore, fInt) = dataFlow.run()

      // TODO: make this tail recursive
      def loop(eventuallyMaybeInt: Future[Option[Int]]): Future[Option[Int]] 
= {
        eventuallyMaybeInt.flatMap { o =>
          o match {
            case None => eventuallyMaybeInt
            case _ => loop(maybeLastFromMaterialisedGraph)

      val i = await(loop(maybeLastFromMaterialisedGraph))
      println(s"Sink.last i=$i")

On Tuesday, 21 August 2018 10:09:29 UTC+1, Mr B wrote:
> Hi,
> I am an akka streams newbie and was wondering how to create a Source that 
> restarts if it was not empty (Source would be dynamic eg the result of a 
> database fetch)?
> My initial thoughts were to broadcast the source to say Sink.optionFirst  
> and if the materialised value of Sink.optionFirst is Some(element), then 
> restart the source, if it is None then complete as normal. However I am 
> struggling with how I would do this.
> Thanks

** New discussion forum: https://discuss.akka.io/ replacing akka-user 
google-group soon.
** This group will soon be put into read-only mode, and replaced by 
** More details: https://akka.io/blog/news/2018/03/13/discuss.akka.io-announced
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to