!str #16039 Remove old scaladsl, rename scaladsl2
* and impl2, testkit2 * keeping io2 for now
This commit is contained in:
parent
5562ceb94b
commit
7c0c618791
221 changed files with 1540 additions and 7985 deletions
123
akka-stream/src/main/scala/akka/stream/scaladsl/Sink.scala
Normal file
123
akka-stream/src/main/scala/akka/stream/scaladsl/Sink.scala
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
/**
|
||||
* Copyright (C) 2014 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
package akka.stream.scaladsl
|
||||
|
||||
import akka.actor.Props
|
||||
import org.reactivestreams.Subscriber
|
||||
import scala.util.Try
|
||||
import akka.stream.FlowMaterializer
|
||||
|
||||
/**
|
||||
* A `Sink` is a set of stream processing steps that has one open input and an attached output.
|
||||
* Can be used as a `Subscriber`
|
||||
*/
|
||||
trait Sink[-In] {
|
||||
/**
|
||||
* 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]].
|
||||
*/
|
||||
def runWith(source: KeyedSource[In])(implicit materializer: FlowMaterializer): source.MaterializedType =
|
||||
source.connect(this).run().get(source)
|
||||
|
||||
/**
|
||||
* 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]].
|
||||
*/
|
||||
def runWith(source: Source[In])(implicit materializer: FlowMaterializer): Unit =
|
||||
source.connect(this).run()
|
||||
}
|
||||
|
||||
object Sink {
|
||||
/**
|
||||
* Helper to create [[Sink]] from `Subscriber`.
|
||||
*/
|
||||
def apply[T](subscriber: Subscriber[T]): Sink[T] = SubscriberSink(subscriber)
|
||||
|
||||
/**
|
||||
* 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)
|
||||
|
||||
private def createSinkFromBuilder[T](builder: FlowGraphBuilder, block: FlowGraphBuilder ⇒ UndefinedSource[T]): Sink[T] = {
|
||||
val in = block(builder)
|
||||
builder.partialBuild().toSink(in)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
def future[T]: FutureSink[T] = FutureSink[T]
|
||||
|
||||
/**
|
||||
* 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
|
||||
* normal end of the stream, or completed with `Failure` if there is an error is signaled in
|
||||
* 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`
|
||||
* if there is an error is signaled in the stream.
|
||||
*/
|
||||
def fold[U, T](zero: U)(f: (U, T) ⇒ U): FoldSink[U, T] = FoldSink(zero)(f)
|
||||
|
||||
/**
|
||||
* A `Sink` that when the flow is completed, either through an error or normal
|
||||
* 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.)
|
||||
*/
|
||||
trait KeyedSink[-In] extends Sink[In] {
|
||||
type MaterializedType
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue