-
Notifications
You must be signed in to change notification settings - Fork 3.6k
=str Add IterableSource. #31372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
=str Add IterableSource. #31372
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,26 +4,33 @@ | |
|
|
||
| package akka.stream.impl.fusing | ||
|
|
||
| import java.util.concurrent.atomic.{ AtomicBoolean, AtomicReference } | ||
|
|
||
| import scala.annotation.unchecked.uncheckedVariance | ||
| import scala.concurrent.{ Future, Promise } | ||
| import scala.concurrent.duration.FiniteDuration | ||
| import scala.util.Try | ||
|
|
||
| import akka.Done | ||
| import akka.actor.Cancellable | ||
| import akka.annotation.InternalApi | ||
| import akka.dispatch.ExecutionContexts | ||
| import akka.event.Logging | ||
| import akka.stream.{ Shape, _ } | ||
| import akka.stream.ActorAttributes.SupervisionStrategy | ||
| import akka.stream.FlowMonitorState._ | ||
| import akka.stream.impl.{ ContextPropagation, LinearTraversalBuilder, ReactiveStreamsCompliance } | ||
| import akka.stream.Shape | ||
| import akka.stream._ | ||
| import akka.stream.impl.ContextPropagation | ||
| import akka.stream.impl.LinearTraversalBuilder | ||
| import akka.stream.impl.ReactiveStreamsCompliance | ||
| import akka.stream.impl.Stages.DefaultAttributes | ||
| import akka.stream.impl.StreamLayout._ | ||
| import akka.stream.scaladsl._ | ||
| import akka.stream.stage._ | ||
|
|
||
| import java.util.concurrent.atomic.AtomicBoolean | ||
| import java.util.concurrent.atomic.AtomicReference | ||
| import scala.annotation.unchecked.uncheckedVariance | ||
| import scala.collection.immutable | ||
| import scala.concurrent.Future | ||
| import scala.concurrent.Promise | ||
| import scala.concurrent.duration.FiniteDuration | ||
| import scala.util.Try | ||
| import scala.util.control.NonFatal | ||
|
|
||
| /** | ||
| * INTERNAL API | ||
| */ | ||
|
|
@@ -284,6 +291,51 @@ import akka.stream.stage._ | |
| override def toString: String = "SingleSource" | ||
| } | ||
|
|
||
| final class IterableSource[T](val elements: immutable.Iterable[T]) extends GraphStage[SourceShape[T]] { | ||
| ReactiveStreamsCompliance.requireNonNullElement(elements) | ||
| override protected def initialAttributes: Attributes = DefaultAttributes.iterableSource | ||
| private val out = Outlet[T]("IterableSource.out") | ||
| override val shape: SourceShape[T] = SourceShape(out) | ||
| override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = | ||
| new GraphStageLogic(shape) with OutHandler { | ||
| private lazy val decider = inheritedAttributes.mandatoryAttribute[SupervisionStrategy].decider | ||
| private var currentIterator: Iterator[T] = _ | ||
|
|
||
| override def onPull(): Unit = | ||
| try { | ||
| if (currentIterator eq null) { | ||
| currentIterator = elements.iterator | ||
| } | ||
| tryPushNextOrComplete() | ||
| } catch { | ||
| case NonFatal(ex) => | ||
| decider(ex) match { | ||
| case Supervision.Stop => failStage(ex) | ||
| case Supervision.Resume => tryPushNextOrComplete() | ||
| case Supervision.Restart => | ||
| currentIterator = elements.iterator | ||
| tryPushNextOrComplete() | ||
| } | ||
| } | ||
|
|
||
| private def tryPushNextOrComplete(): Unit = | ||
| if (currentIterator.hasNext) { | ||
| if (isAvailable(out)) { | ||
| push(out, currentIterator.next()) | ||
| } | ||
| if (!currentIterator.hasNext) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why call hasNext here,is it ok not to to call here?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it just pushed the last element it can immediately complete the stage after that instead of waiting for next pull to make that decision. |
||
| completeStage() | ||
| } | ||
| } else { | ||
| completeStage() | ||
| } | ||
|
|
||
| setHandler(out, this) | ||
| } | ||
|
|
||
| override def toString: String = "IterableSource" | ||
| } | ||
|
|
||
| final class FutureFlattenSource[T, M](futureSource: Future[Graph[SourceShape[T], M]]) | ||
| extends GraphStageWithMaterializedValue[SourceShape[T], Future[M]] { | ||
| ReactiveStreamsCompliance.requireNonNullElement(futureSource) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.