Hi,
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 =
source.alsoToMat(sink)(Keep.right)
.toMat(sink2)(Keep.both)
val (fIgnore, fInt) = dataFlow.run()
fInt
}
// 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
discuss.akka.io
** 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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.