package eu.xeppaka.scalalearn

import akka.NotUsed
import akka.actor.ActorSystem
import akka.stream.scaladsl.{Sink, Source}
import akka.stream.{ActorMaterializer, Materializer}

import scala.concurrent.Future
import scala.io.StdIn
import scala.util.Random
import scala.concurrent.duration._

object Main8 {
  def main(args: Array[String]): Unit = {
    implicit val actorSystem: ActorSystem = ActorSystem()
    implicit val materializer: Materializer = ActorMaterializer()

    Source
      .repeat(NotUsed)
      .throttle(10, 1.second)
      .mapAsync(1) { _ =>
        Future.successful(Random.nextInt())
      }
      .mapAsync(1) { _ =>
        Future.successful(Random.nextInt())
      }
      .flatMapConcat { i =>
        if (i % 2 == 0) {
          Source.empty
        } else {
          Source.single(i)
        }
      }
      .to(Sink.foreach(println))
      .run()

    StdIn.readLine()
    actorSystem.terminate()
  }
}
