2014-09-03 21:54:18 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2014 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
package akka.stream.scaladsl2
|
|
|
|
|
|
2014-10-02 17:32:08 +02:00
|
|
|
import org.reactivestreams.{ Subscriber, Publisher }
|
|
|
|
|
|
2014-09-03 21:54:18 +02:00
|
|
|
import scala.annotation.unchecked.uncheckedVariance
|
|
|
|
|
import scala.collection.immutable
|
|
|
|
|
import scala.concurrent.Future
|
|
|
|
|
import scala.concurrent.duration.FiniteDuration
|
|
|
|
|
|
2014-10-02 17:32:08 +02:00
|
|
|
import scala.language.higherKinds
|
|
|
|
|
import scala.language.implicitConversions
|
2014-09-03 21:54:18 +02:00
|
|
|
|
2014-10-02 17:32:08 +02:00
|
|
|
/**
|
|
|
|
|
* A `Source` is a set of stream processing steps that has one open output and an attached input.
|
|
|
|
|
* Can be used as a `Publisher`
|
|
|
|
|
*/
|
|
|
|
|
trait Source[+Out] extends FlowOps[Out] {
|
|
|
|
|
override type Repr[+O] <: Source[O]
|
2014-09-03 21:54:18 +02:00
|
|
|
|
|
|
|
|
/**
|
2014-10-02 17:32:08 +02:00
|
|
|
* Transform this source by appending the given processing stages.
|
2014-09-03 21:54:18 +02:00
|
|
|
*/
|
2014-10-02 17:32:08 +02:00
|
|
|
def connect[T](flow: Flow[Out, T]): Source[T]
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Connect this source to a sink, concatenating the processing steps of both.
|
|
|
|
|
*/
|
|
|
|
|
def connect(sink: Sink[Out]): RunnableFlow
|
|
|
|
|
|
|
|
|
|
def toPublisher()(implicit materializer: FlowMaterializer): Publisher[Out @uncheckedVariance]
|
|
|
|
|
|
|
|
|
|
def toFanoutPublisher(initialBufferSize: Int, maximumBufferSize: Int)(implicit materializer: FlowMaterializer): Publisher[Out @uncheckedVariance]
|
|
|
|
|
|
|
|
|
|
def publishTo(subscriber: Subscriber[Out @uncheckedVariance])(implicit materializer: FlowMaterializer)
|
2014-09-03 21:54:18 +02:00
|
|
|
|
2014-10-02 17:32:08 +02:00
|
|
|
def consume()(implicit materializer: FlowMaterializer): Unit
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
object Source {
|
2014-09-03 21:54:18 +02:00
|
|
|
/**
|
2014-10-02 17:32:08 +02:00
|
|
|
* Helper to create [[Source]] from `Publisher`.
|
2014-09-03 21:54:18 +02:00
|
|
|
*
|
|
|
|
|
* Construct a transformation starting with given publisher. The transformation steps
|
|
|
|
|
* are executed by a series of [[org.reactivestreams.Processor]] instances
|
|
|
|
|
* that mediate the flow of elements downstream and the propagation of
|
|
|
|
|
* back-pressure upstream.
|
|
|
|
|
*/
|
2014-10-02 17:32:08 +02:00
|
|
|
def apply[T](publisher: Publisher[T]): Tap[T] = PublisherTap(publisher)
|
2014-09-03 21:54:18 +02:00
|
|
|
|
|
|
|
|
/**
|
2014-10-02 17:32:08 +02:00
|
|
|
* Helper to create [[Source]] from `Iterator`.
|
|
|
|
|
* Example usage: `Source(Seq(1,2,3).iterator)`
|
2014-09-03 21:54:18 +02:00
|
|
|
*
|
2014-10-02 17:32:08 +02:00
|
|
|
* Start a new `Source` from the given Iterator. The produced stream of elements
|
2014-09-03 21:54:18 +02:00
|
|
|
* will continue until the iterator runs empty or fails during evaluation of
|
|
|
|
|
* the `next()` method. Elements are pulled out of the iterator
|
|
|
|
|
* in accordance with the demand coming from the downstream transformation
|
|
|
|
|
* steps.
|
|
|
|
|
*/
|
2014-10-02 17:32:08 +02:00
|
|
|
def apply[T](iterator: Iterator[T]): Tap[T] = IteratorTap(iterator)
|
2014-09-03 21:54:18 +02:00
|
|
|
|
|
|
|
|
/**
|
2014-10-02 17:32:08 +02:00
|
|
|
* Helper to create [[Source]] from `Iterable`.
|
|
|
|
|
* Example usage: `Source(Seq(1,2,3))`
|
2014-09-03 21:54:18 +02:00
|
|
|
*
|
2014-10-02 17:32:08 +02:00
|
|
|
* Starts a new `Source` from the given `Iterable`. This is like starting from an
|
2014-09-03 21:54:18 +02:00
|
|
|
* Iterator, but every Subscriber directly attached to the Publisher of this
|
|
|
|
|
* stream will see an individual flow of elements (always starting from the
|
|
|
|
|
* beginning) regardless of when they subscribed.
|
|
|
|
|
*/
|
2014-10-02 17:32:08 +02:00
|
|
|
def apply[T](iterable: immutable.Iterable[T]): Tap[T] = IterableTap(iterable)
|
2014-09-03 21:54:18 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Define the sequence of elements to be produced by the given closure.
|
|
|
|
|
* The stream ends normally when evaluation of the closure returns a `None`.
|
|
|
|
|
* The stream ends exceptionally when an exception is thrown from the closure.
|
|
|
|
|
*/
|
2014-10-02 17:32:08 +02:00
|
|
|
def apply[T](f: () ⇒ Option[T]): Tap[T] = ThunkTap(f)
|
2014-09-03 21:54:18 +02:00
|
|
|
|
|
|
|
|
/**
|
2014-10-02 17:32:08 +02:00
|
|
|
* Start a new `Source` from the given `Future`. The stream will consist of
|
2014-09-03 21:54:18 +02:00
|
|
|
* one element when the `Future` is completed with a successful value, which
|
|
|
|
|
* may happen before or after materializing the `Flow`.
|
|
|
|
|
* The stream terminates with an error if the `Future` is completed with a failure.
|
|
|
|
|
*/
|
2014-10-02 17:32:08 +02:00
|
|
|
def apply[T](future: Future[T]): Tap[T] = FutureTap(future)
|
2014-09-03 21:54:18 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Elements are produced from the tick closure periodically with the specified interval.
|
|
|
|
|
* The tick element will be delivered to downstream consumers that has requested any elements.
|
|
|
|
|
* If a consumer has not requested any elements at the point in time when the tick
|
|
|
|
|
* element is produced it will not receive that tick element later. It will
|
|
|
|
|
* receive new tick elements as soon as it has requested more elements.
|
|
|
|
|
*/
|
2014-10-02 17:32:08 +02:00
|
|
|
def apply[T](initialDelay: FiniteDuration, interval: FiniteDuration, tick: () ⇒ T): Tap[T] =
|
|
|
|
|
TickTap(initialDelay, interval, tick)
|
2014-09-03 21:54:18 +02:00
|
|
|
}
|