diff options
author | Jakob Odersky <jakob@odersky.com> | 2016-02-24 20:33:51 -0800 |
---|---|---|
committer | Jakob Odersky <jakob@odersky.com> | 2016-02-24 20:33:51 -0800 |
commit | 8186b3622ce1c9d2b50df3d264ab526dc1e61d77 (patch) | |
tree | b4446408291c9f179e1c270a561523023ac6a105 /mavigator-cockpit/src/main/scala/mavigator/dashboard/RxUtil.scala | |
parent | 6c4e8849d753734b3e50523dcdb372fbdbccc2c1 (diff) | |
parent | a41de68066007852d7d3dbf019d75b4caf7463ad (diff) | |
download | mavigator-8186b3622ce1c9d2b50df3d264ab526dc1e61d77.tar.gz mavigator-8186b3622ce1c9d2b50df3d264ab526dc1e61d77.tar.bz2 mavigator-8186b3622ce1c9d2b50df3d264ab526dc1e61d77.zip |
Merge branch 'akka-streams'
Diffstat (limited to 'mavigator-cockpit/src/main/scala/mavigator/dashboard/RxUtil.scala')
-rw-r--r-- | mavigator-cockpit/src/main/scala/mavigator/dashboard/RxUtil.scala | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/mavigator-cockpit/src/main/scala/mavigator/dashboard/RxUtil.scala b/mavigator-cockpit/src/main/scala/mavigator/dashboard/RxUtil.scala new file mode 100644 index 0000000..2637ddf --- /dev/null +++ b/mavigator-cockpit/src/main/scala/mavigator/dashboard/RxUtil.scala @@ -0,0 +1,70 @@ +package mavigator.dashboard + +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 { + + /** Rx, implicitly enhanced with additional methods. */ + implicit class RichRx(val rx: Rx[_]) extends AnyVal { + + /** + * Builds a new Rx by applying a partial function to all values of + * this Rx on which the function is defined. + * @param initial initial value of the returned Rx + * @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 + */ + def collect[B](initial: B)(pf: PartialFunction[Any, B]): Rx[B] = { + val result: Var[B] = Var(initial) + Obs(rx, skipInitial = true) { + if (pf.isDefinedAt(rx())) { + result() = pf(rx()) + } + } + result + } + + } + + /** + * 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) + } + +} |