I think throttlethis is what you need. A fully-run example with Sourceapplied to iterable that uses a function in next():
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.ThrottleMode.Shaping
import akka.stream.scaladsl.Source
import scala.concurrent.duration._
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
var i = 0
def myFunction(): Int = {
i = i + 1
i
}
import scala.collection.immutable.Iterable
val x: Iterable[Int] = new Iterable[Int] {
override def iterator: Iterator[Int] =
new Iterator[Int] {
override def hasNext: Boolean = true
override def next(): Int = myFunction()
}
}
Source(x).throttle(1, 1.second, 1, Shaping).runForeach(println)
throttle: 1 1 max burst = 1, ( Shaping ).