Created
April 18, 2017 13:11
-
-
Save anonymous/185be687c90b3f2c2ea62fc93b279e6f to your computer and use it in GitHub Desktop.
the description for this gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final case class Filter[T](p: T ⇒ Boolean) extends SimpleLinearGraphStage[T] { | |
override def initialAttributes: Attributes = DefaultAttributes.filter | |
override def toString: String = "Filter" | |
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = | |
new GraphStageLogic(shape) with OutHandler with InHandler { | |
def decider = inheritedAttributes.get[SupervisionStrategy].map(_.decider).getOrElse(Supervision.stoppingDecider) | |
override def onPush(): Unit = { | |
try { | |
val elem = grab(in) | |
if (p(elem)) { | |
push(out, elem) | |
} else { | |
pull(in) | |
} | |
} catch { | |
case NonFatal(ex) ⇒ decider(ex) match { | |
case Supervision.Stop ⇒ failStage(ex) | |
case _ ⇒ pull(in) | |
} | |
} | |
} | |
override def onPull(): Unit = pull(in) | |
setHandlers(in, out, this) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment