diff options
Diffstat (limited to 'vfd-dashboard/src/main/scala/vfd/dashboard/RxUtil.scala')
-rw-r--r-- | vfd-dashboard/src/main/scala/vfd/dashboard/RxUtil.scala | 43 |
1 files changed, 41 insertions, 2 deletions
diff --git a/vfd-dashboard/src/main/scala/vfd/dashboard/RxUtil.scala b/vfd-dashboard/src/main/scala/vfd/dashboard/RxUtil.scala index 51e2fcd..a73c3ba 100644 --- a/vfd-dashboard/src/main/scala/vfd/dashboard/RxUtil.scala +++ b/vfd-dashboard/src/main/scala/vfd/dashboard/RxUtil.scala @@ -1,6 +1,23 @@ package vfd.dashboard -import rx._ +import scala.language.implicitConversions +import scala.util.Failure +import scala.util.Success + +import org.scalajs.dom.html + +import rx.Obs +import rx.Rx +import rx.Rx +import rx.Var +import rx.Var +import scalatags.JsDom.all.Frag +import scalatags.JsDom.all.HtmlTag +import scalatags.JsDom.all.backgroundColor +import scalatags.JsDom.all.bindNode +import scalatags.JsDom.all.span +import scalatags.JsDom.all.stringFrag +import scalatags.JsDom.all.stringStyle package object rxutil { @@ -14,7 +31,7 @@ package object rxutil { * @param pf the partial function which filters and maps this Rx * @return a new Rx resulting from applying the given partial * function pf to each value on which it is defined and collecting - * the result + * the result */ def collect[B](initial: B)(pf: PartialFunction[Any, B]): Rx[B] = { val result: Var[B] = Var(initial) @@ -28,4 +45,26 @@ package object rxutil { } + /** + * Copied from https://github.com/lihaoyi/workbench-example-app/blob/todomvc/src/main/scala/example/Framework.scala + * + * Sticks some Rx into a Scalatags fragment, which means hooking up an Obs + * to propagate changes into the DOM via the element's ID. Monkey-patches + * the Obs onto the element itself so we have a reference to kill it when + * the element leaves the DOM (e.g. it gets deleted). + */ + implicit def rxMod[T <: html.Element](r: Rx[HtmlTag]): Frag = { + def rSafe = r.toTry match { + case Success(v) => v.render + case Failure(e) => span(e.toString, backgroundColor := "red").render + } + var last = rSafe + Obs(r, skipInitial = true) { + val newLast = rSafe + last.parentElement.replaceChild(newLast, last) + last = newLast + } + bindNode(last) + } + }
\ No newline at end of file |