2014-09-03 21:54:18 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2014 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
2014-10-27 14:35:41 +01:00
|
|
|
package akka.stream.scaladsl
|
2014-09-03 21:54:18 +02:00
|
|
|
|
2014-10-27 09:48:54 +02:00
|
|
|
import akka.actor.Props
|
2014-10-02 17:32:08 +02:00
|
|
|
import org.reactivestreams.Subscriber
|
2014-10-17 14:05:50 +02:00
|
|
|
import scala.util.Try
|
2014-10-27 14:35:41 +01:00
|
|
|
import akka.stream.FlowMaterializer
|
2014-09-03 21:54:18 +02:00
|
|
|
|
|
|
|
|
/**
|
2014-10-02 17:32:08 +02:00
|
|
|
* A `Sink` is a set of stream processing steps that has one open input and an attached output.
|
|
|
|
|
* Can be used as a `Subscriber`
|
2014-09-03 21:54:18 +02:00
|
|
|
*/
|
2014-12-02 16:38:14 +01:00
|
|
|
trait Sink[-In] extends Materializable {
|
2014-10-30 14:58:44 +01:00
|
|
|
|
2014-10-02 13:34:27 +02:00
|
|
|
/**
|
2014-10-17 14:05:50 +02:00
|
|
|
* Connect this `Sink` to a `Source` and run it. The returned value is the materialized value
|
|
|
|
|
* of the `Source`, e.g. the `Subscriber` of a [[SubscriberSource]].
|
2014-10-02 13:34:27 +02:00
|
|
|
*/
|
2014-10-30 14:58:44 +01:00
|
|
|
def runWith(source: Source[In])(implicit materializer: FlowMaterializer): source.MaterializedType =
|
2014-10-31 10:43:42 +02:00
|
|
|
source.to(this).run().get(source)
|
2014-10-10 10:39:29 +02:00
|
|
|
|
2014-09-03 21:54:18 +02:00
|
|
|
}
|
2014-10-08 15:15:46 +02:00
|
|
|
|
|
|
|
|
object Sink {
|
|
|
|
|
/**
|
|
|
|
|
* Helper to create [[Sink]] from `Subscriber`.
|
|
|
|
|
*/
|
2014-10-17 14:05:50 +02:00
|
|
|
def apply[T](subscriber: Subscriber[T]): Sink[T] = SubscriberSink(subscriber)
|
2014-10-10 10:39:29 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a `Sink` by using an empty [[FlowGraphBuilder]] on a block that expects a [[FlowGraphBuilder]] and
|
|
|
|
|
* returns the `UndefinedSource`.
|
|
|
|
|
*/
|
|
|
|
|
def apply[T]()(block: FlowGraphBuilder ⇒ UndefinedSource[T]): Sink[T] =
|
|
|
|
|
createSinkFromBuilder(new FlowGraphBuilder(), block)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a `Sink` by using a FlowGraphBuilder from this [[PartialFlowGraph]] on a block that expects
|
|
|
|
|
* a [[FlowGraphBuilder]] and returns the `UndefinedSource`.
|
|
|
|
|
*/
|
|
|
|
|
def apply[T](graph: PartialFlowGraph)(block: FlowGraphBuilder ⇒ UndefinedSource[T]): Sink[T] =
|
2014-11-28 10:41:57 +01:00
|
|
|
createSinkFromBuilder(new FlowGraphBuilder(graph), block)
|
2014-10-10 10:39:29 +02:00
|
|
|
|
|
|
|
|
private def createSinkFromBuilder[T](builder: FlowGraphBuilder, block: FlowGraphBuilder ⇒ UndefinedSource[T]): Sink[T] = {
|
|
|
|
|
val in = block(builder)
|
|
|
|
|
builder.partialBuild().toSink(in)
|
|
|
|
|
}
|
2014-10-17 14:05:50 +02:00
|
|
|
|
2014-10-27 09:48:54 +02:00
|
|
|
/**
|
|
|
|
|
* Creates a `Sink` that is materialized to an [[akka.actor.ActorRef]] which points to an Actor
|
|
|
|
|
* created according to the passed in [[akka.actor.Props]]. Actor created by the `props` should
|
|
|
|
|
* be [[akka.stream.actor.ActorSubscriber]].
|
|
|
|
|
*/
|
|
|
|
|
def apply[T](props: Props): PropsSink[T] = PropsSink[T](props)
|
|
|
|
|
|
2014-10-17 14:05:50 +02:00
|
|
|
/**
|
|
|
|
|
* A `Sink` that immediately cancels its upstream after materialization.
|
|
|
|
|
*/
|
|
|
|
|
def cancelled[T]: Sink[T] = CancelSink
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A `Sink` that materializes into a `Future` of the first value received.
|
|
|
|
|
*/
|
2014-11-06 18:13:06 +01:00
|
|
|
def head[T]: HeadSink[T] = HeadSink[T]
|
2014-10-17 14:05:50 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A `Sink` that materializes into a [[org.reactivestreams.Publisher]].
|
|
|
|
|
* that can handle one [[org.reactivestreams.Subscriber]].
|
|
|
|
|
*/
|
|
|
|
|
def publisher[T]: PublisherSink[T] = PublisherSink[T]
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A `Sink` that materializes into a [[org.reactivestreams.Publisher]]
|
|
|
|
|
* that can handle more than one [[org.reactivestreams.Subscriber]].
|
|
|
|
|
*/
|
|
|
|
|
def fanoutPublisher[T](initialBufferSize: Int, maximumBufferSize: Int): FanoutPublisherSink[T] =
|
|
|
|
|
FanoutPublisherSink[T](initialBufferSize, maximumBufferSize)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A `Sink` that will consume the stream and discard the elements.
|
|
|
|
|
*/
|
|
|
|
|
def ignore: Sink[Any] = BlackholeSink
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A `Sink` that will invoke the given procedure for each received element. The sink is materialized
|
|
|
|
|
* into a [[scala.concurrent.Future]] will be completed with `Success` when reaching the
|
2015-01-30 10:30:56 +01:00
|
|
|
* normal end of the stream, or completed with `Failure` if there is a failure signaled in
|
2014-10-17 14:05:50 +02:00
|
|
|
* the stream..
|
|
|
|
|
*/
|
|
|
|
|
def foreach[T](f: T ⇒ Unit): ForeachSink[T] = ForeachSink(f)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A `Sink` that will invoke the given function for every received element, giving it its previous
|
|
|
|
|
* output (or the given `zero` value) and the element as input.
|
|
|
|
|
* The returned [[scala.concurrent.Future]] will be completed with value of the final
|
|
|
|
|
* function evaluation when the input stream ends, or completed with `Failure`
|
2015-01-30 10:30:56 +01:00
|
|
|
* if there is a failure signaled in the stream.
|
2014-10-17 14:05:50 +02:00
|
|
|
*/
|
|
|
|
|
def fold[U, T](zero: U)(f: (U, T) ⇒ U): FoldSink[U, T] = FoldSink(zero)(f)
|
|
|
|
|
|
|
|
|
|
/**
|
2015-01-30 10:30:56 +01:00
|
|
|
* A `Sink` that when the flow is completed, either through a failure or normal
|
2014-10-17 14:05:50 +02:00
|
|
|
* completion, apply the provided function with [[scala.util.Success]]
|
|
|
|
|
* or [[scala.util.Failure]].
|
|
|
|
|
*/
|
|
|
|
|
def onComplete[T](callback: Try[Unit] ⇒ Unit): Sink[T] = OnCompleteSink[T](callback)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A `Sink` that will create an object during materialization that the user will need
|
|
|
|
|
* to retrieve in order to access aspects of this sink (could be a completion Future
|
|
|
|
|
* or a cancellation handle, etc.)
|
|
|
|
|
*/
|
2014-12-02 16:38:14 +01:00
|
|
|
trait KeyedSink[-In, M] extends Sink[In] with KeyedMaterializable[M]
|