aboutsummaryrefslogtreecommitdiff
path: root/vfd-dashboard/src/main/scala/vfd/dashboard/RxUtil.scala
blob: 51e2fcde49b9681d328fb8f734c3440f5b41f071 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package vfd.dashboard

import rx._

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
    }

  }

}