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
|
2014-09-01 13:30:15 +02:00
|
|
|
|
2014-10-08 15:15:46 +02:00
|
|
|
import scala.concurrent.Future
|
2014-10-02 17:32:08 +02:00
|
|
|
import scala.language.implicitConversions
|
2014-09-03 21:54:18 +02:00
|
|
|
import scala.annotation.unchecked.uncheckedVariance
|
|
|
|
|
|
|
|
|
|
/**
|
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-10-02 17:32:08 +02:00
|
|
|
trait Sink[-In] {
|
2014-10-02 13:34:27 +02:00
|
|
|
/**
|
|
|
|
|
* Connect this `Sink` to a `Tap` and run it. The returned value is the materialized value
|
|
|
|
|
* of the `Tap`, e.g. the `Subscriber` of a [[SubscriberTap]].
|
|
|
|
|
*/
|
2014-10-10 10:39:29 +02:00
|
|
|
def runWith(tap: TapWithKey[In])(implicit materializer: FlowMaterializer): tap.MaterializedType =
|
|
|
|
|
tap.connect(this).run().materializedTap(tap)
|
|
|
|
|
|
2014-10-03 17:33:14 +02:00
|
|
|
/**
|
|
|
|
|
* Connect this `Sink` to a `Tap` and run it. The returned value is the materialized value
|
|
|
|
|
* of the `Tap`, e.g. the `Subscriber` of a [[SubscriberTap]].
|
|
|
|
|
*/
|
|
|
|
|
def runWith(tap: SimpleTap[In])(implicit materializer: FlowMaterializer): Unit =
|
|
|
|
|
tap.connect(this).run()
|
2014-09-03 21:54:18 +02:00
|
|
|
}
|
2014-10-08 15:15:46 +02:00
|
|
|
|
|
|
|
|
object Sink {
|
|
|
|
|
/**
|
|
|
|
|
* Helper to create [[Sink]] from `Subscriber`.
|
|
|
|
|
*/
|
|
|
|
|
def apply[T](subscriber: Subscriber[T]): Drain[T] = SubscriberDrain(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] =
|
|
|
|
|
createSinkFromBuilder(new FlowGraphBuilder(graph.graph), block)
|
|
|
|
|
|
2014-10-07 13:55:56 +02:00
|
|
|
/**
|
|
|
|
|
* A `Sink` that immediately cancels its upstream after materialization.
|
|
|
|
|
*/
|
|
|
|
|
def cancelled[T]: Drain[T] = CancelDrain
|
2014-10-03 17:33:14 +02:00
|
|
|
|
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-08 15:15:46 +02:00
|
|
|
}
|