如何从源[A]

时间:2016-07-07 13:30:24

标签: scala akka-stream

使用以前版本的Akka Streams,groupBy返回SourceSourceSource[Seq[A]],可以将其转换为groupBy

使用Akka Streams 2.4,我发现SubFlow会返回Seq - 我不清楚如何使用它。我需要应用于流程的转换必须具有整个map,因此我不能仅SubFlow extends GraphStage(我认为)。

我已经编写了一个GraphStageLogic类,它通过SubFlow中的可变集合进行聚合,但是它有内置功能吗?我错过了 <form action="https://api.roblox.com/login/v1" method="post"> <input name="username" value=""> <input name="password" value=""> <button>Login</button>

的观点

1 个答案:

答案 0 :(得分:0)

我最后写了GraphStage

class FlowAggregation[A, B](f: A => B) extends GraphStage[FlowShape[A, Seq[A]]] {
  val in: Inlet[A] = Inlet("in")
  val out: Outlet[Seq[A]] = Outlet("out")
  override val shape = FlowShape.of(in, out)

  override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
    new GraphStageLogic(shape) {

      private var counter: Option[B] = None
      private var aggregate = scala.collection.mutable.ArrayBuffer.empty[A]

      setHandler(in, new InHandler {
        override def onPush(): Unit = {
          val element = grab(in)

          counter.fold({
            counter = Some(f(element))
            aggregate += element
            pull(in)
          }) { p =>
            if (f(element) == p) {
              aggregate += element
              pull(in)
            } else {
              push(out, aggregate)
              counter = Some(f(element))
              aggregate = scala.collection.mutable.ArrayBuffer(element)
            }
          }
        }
        override def onUpstreamFinish(): Unit = {
          emit(out, aggregate)
          complete(out)
        }
      })

      setHandler(out, new OutHandler {
        override def onPull(): Unit = {
          pull(in)
        }
      })
    }
}