Operator docs for Source.tick (#28225)

This commit is contained in:
Johan Andrén 2019-11-25 13:25:07 +01:00 committed by GitHub
parent 5e2435a424
commit ac15cb3289
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 177 additions and 3 deletions

View file

@ -0,0 +1,59 @@
/*
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com>
*/
package docs.stream.operators.source
import akka.NotUsed
import akka.actor.Cancellable
import akka.actor.typed.ActorRef
import akka.actor.typed.ActorSystem
import akka.stream.scaladsl.Source
import akka.actor.typed.scaladsl.AskPattern._
import akka.stream.scaladsl.Flow
import akka.util.Timeout
import scala.concurrent.Future
import scala.concurrent.duration._
object Tick {
// not really a runnable example, these are just pretend
implicit val system: ActorSystem[Nothing] = null
val myActor: ActorRef[MyActor.Command] = null;
object MyActor {
sealed trait Command {}
case class Query(replyTo: ActorRef[Response]) extends Command
case class Response(text: String)
}
def simple() {
// #simple
Source
.tick(
1.second, // delay of first tick
1.second, // delay of subsequent ticks
"tick" // element emitted each tick
)
.runForeach(println)
// #simple
}
def pollSomething() {
// #poll-actor
val periodicActorResponse: Source[String, Cancellable] = Source
.tick(1.second, 1.second, "tick")
.mapAsync(1) { _ =>
implicit val timeout: Timeout = 3.seconds
val response: Future[MyActor.Response] = myActor.ask(MyActor.Query)
response
}
.map(_.text);
// #poll-actor
// #zip-latest
val zipWithLatestResponse: Flow[Int, (Int, String), NotUsed] =
Flow[Int].zipLatest(periodicActorResponse);
// #zip-latest
}
}