Skip to content

Instantly share code, notes, and snippets.

@busti
Last active June 9, 2018 08:50
Show Gist options
  • Save busti/6bc4bbb071db25569df2912230c88a80 to your computer and use it in GitHub Desktop.
Save busti/6bc4bbb071db25569df2912230c88a80 to your computer and use it in GitHub Desktop.
Builds an rx tree.
object Builder {
implicit def rx2tree(rx: Rx[_]) = show(rx)
sealed trait RxTree
final case class Branch(rx: Rx[Any], branches: RxTree *) extends RxTree
final case class RxBranch(rx: Rx[Any], input: RxTree, branches: Rx[Option[RxTree]]) extends RxTree
final case class Leaf(rx: Rx[Any]) extends RxTree
val flatMapStreams = mutable.Map[FlatMap[_, _], Rx[Option[RxTree]]]()
def show[A](rx: Rx[A]): RxTree = rx match {
case Map(self, _) => Branch(rx, self)
case fm @ FlatMap(self, _) => {
RxBranch(rx, self, flatMapStreams(fm))
}
case Zip(self, other) => Branch(rx, self, other)
case DropRep(self) => Branch(rx, self)
case Merge(self, other) => Branch(rx, self, other)
case Foldp(self, _, _) => Branch(rx, self)
case Collect(self, _, _) => Branch(rx, self)
case SampleOn(self, other) => Branch(rx, self, other)
case Imitate(self, other) => Branch(rx, self, other)
case Sharing(self) => Branch(rx, self)
case leaf: Var[A] => Leaf(leaf)
}
def spyFlatMaps[A](rx: Rx[A]): Rx[A] = rx match {
case fm @ FlatMap(r, f) => {
val v = Var[Option[RxTree]](None)
val g = FlatMap(r, { x =>
val res = f(x)
v := Some(res)
res
})
flatMapStreams(g) = v
g
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment