+str #15998 Add Source singleton, empty, failed factories

This commit is contained in:
Patrik Nordwall 2014-10-06 11:28:13 +02:00
parent afd45a09f4
commit 7f9b018fe7
4 changed files with 94 additions and 6 deletions

View file

@ -4,14 +4,15 @@
package akka.stream.scaladsl2
import org.reactivestreams.{ Subscriber, Publisher }
import scala.annotation.unchecked.uncheckedVariance
import scala.collection.immutable
import scala.concurrent.Future
import scala.concurrent.duration.FiniteDuration
import scala.language.higherKinds
import scala.language.implicitConversions
import akka.stream.impl.SynchronousPublisherFromIterable
import akka.stream.impl.EmptyPublisher
import akka.stream.impl.ErrorPublisher
/**
* A `Source` is a set of stream processing steps that has one open output and an attached input.
@ -118,4 +119,21 @@ object Source {
def apply[T](initialDelay: FiniteDuration, interval: FiniteDuration, tick: () T): Source[T] =
TickTap(initialDelay, interval, tick)
/**
* Create a `Source` with one element.
* Every connected `Sink` of this stream will see an individual stream consisting of one element.
*/
def singleton[T](element: T): Source[T] = apply(SynchronousPublisherFromIterable(List(element)))
/**
* Create a `Source` with no elements, i.e. an empty stream that is completed immediately
* for every connected `Sink`.
*/
def empty[T](): Source[T] = apply(EmptyPublisher[T])
/**
* Create a `Source` that immediately ends the stream with the `cause` error to every connected `Sink`.
*/
def failed[T](cause: Throwable): Source[T] = apply(ErrorPublisher(cause))
}