Hello,
I've been working on a way to turn a *Seq[Source[T]]* into *Source[Seq[T]]*
using Akka Streams 2.0. I believe I've succeeded, but I'd like people to
critique my code. Thanks in advance.
object StreamUtils {
def sequencedSource[T](sources: immutable.Seq[Source[T, Unit]]):
Source[immutable.Seq[T], Unit] =
Source.fromGraph(FlowGraph.create() { implicit builder =>
import FlowGraph.Implicits._
val sequenceGraphStage = builder.add(new
SequenceGraphStage[T](sources.size))
for ((source, index) <- sources.zipWithIndex) source ~>
sequenceGraphStage.in(index)
SourceShape(sequenceGraphStage.out)
})
}
class SequenceGraphStage[T](numInputs: Int) extends
GraphStage[UniformFanInShape[T, immutable.Seq[T]]] {
require(numInputs > 0, "1 or more inputs required")
val inputs = immutable.IndexedSeq.tabulate(numInputs)(num =>
Inlet[T]("Sequence.in" + num))
val output = Outlet[immutable.Seq[T]]("Sequence.out")
override val shape = UniformFanInShape(output, inputs: _*)
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
new GraphStageLogic(shape) {
val elements = mutable.Buffer.empty[T]
setHandler(output, new OutHandler {
override def onPull(): Unit =
inputs.foreach { input =>
read(input) { element =>
elements += element
if (elements.size == inputs.size) {
push(output, elements.to[immutable.Seq])
elements.clear()
}
}
}
})
inputs.foreach(setHandler(_, eagerTerminateInput))
}
override val toString = "Sequence"
}
BTW, I did something similar in Streams 1.0 using FlexiMerge; the API and
boilerplate necessary was somewhat off-putting. I greatly appreciate the
cleanup in 2.0 so far.
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ:
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.