F K
F K

Reputation: 21

How to detect an empty Akka Source

In some edge cases I return an empty Source. Is there any way for the caller to check if the returning Source is empty or not before running it?

Upvotes: 0

Views: 1139

Answers (2)

Jeffrey Chung
Jeffrey Chung

Reputation: 19517

Is there any way for the caller to check if the returning Source is empty or not before running it?

No. The only way to determine whether a Source is empty is to run it.

Upvotes: 2

Shanti Swarup Tunga
Shanti Swarup Tunga

Reputation: 641

Source A processing stage with exactly one output, emitting data elements whenever downstream processing stages are ready to receive them.

As per definition we can't know the element of source unless a downstream is there to receive.

So As per my understanding we can't check a source is empty or not without running it. Please feel free to comment your thoughts

I think one way to achieve the requirement is

val x: Future[Boolean] = Source.empty.runWith(Sink.seq).transform{
   case Success(lst) => Success(lst.isEmpty)
   case Failure(_) => Success(true)
}

Upvotes: 1

Related Questions