doc: Source.maybe, #25468 (#27832)

This commit is contained in:
Patrik Nordwall 2019-10-03 14:09:45 +02:00 committed by GitHub
parent a217d5566e
commit 1a8a438144
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 93 additions and 22 deletions

View file

@ -9,6 +9,8 @@ import akka.testkit.TestProbe
object SourceOperators {
implicit val system: ActorSystem = ???
def fromFuture = {
//#sourceFromFuture
@ -18,8 +20,6 @@ object SourceOperators {
import scala.concurrent.Future
implicit val system: ActorSystem = ActorSystem()
val source: Source[Int, NotUsed] = Source.fromFuture(Future.successful(10))
val sink: Sink[Int, Future[Done]] = Sink.foreach((i: Int) => println(i))
@ -36,7 +36,6 @@ object SourceOperators {
import akka.stream.CompletionStrategy
import akka.stream.scaladsl._
implicit val system: ActorSystem = ActorSystem()
val bufferSize = 100
val source: Source[Any, ActorRef] = Source.actorRef[Any](bufferSize, OverflowStrategy.dropHead)
@ -58,7 +57,6 @@ object SourceOperators {
import akka.stream.CompletionStrategy
import akka.stream.scaladsl._
implicit val system: ActorSystem = ActorSystem()
val probe = TestProbe()
val source: Source[Any, ActorRef] = Source.actorRefWithBackpressure[Any]("ack", {
@ -75,4 +73,20 @@ object SourceOperators {
actorRef ! Success(())
//#actorRefWithBackpressure
}
def maybe(): Unit = {
//#maybe
import akka.stream.scaladsl._
import scala.concurrent.Promise
val source = Source.maybe[Int].to(Sink.foreach(elem => println(elem)))
val promise1: Promise[Option[Int]] = source.run()
promise1.success(Some(1)) // prints 1
// a new Promise is returned when the stream is materialized
val promise2 = source.run()
promise2.success(Some(2)) // prints 2
//#maybe
}
}