Skip to content

Commit

Permalink
add Rx boolean ops toggle and negated
Browse files Browse the repository at this point in the history
  • Loading branch information
cornerman committed Oct 24, 2022
1 parent f26a9dc commit 622035e
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions reactive/src/main/scala/colibri/reactive/Reactive.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package colibri.reactive

import cats.Monoid
import colibri._
import colibri.effect._
import monocle.{Iso, Lens, Prism}
Expand Down Expand Up @@ -69,6 +70,17 @@ object Rx extends RxPlatform {
def filter(f: A => Boolean)(seed: => A)(implicit owner: Owner): Rx[A] = self.transformRx(_.filter(f))(seed)
}

@inline implicit class RxBooleanOps(private val source: Rx[Boolean]) extends AnyVal {
@inline def toggle[A](ifTrue: => A, ifFalse: A): Rx[A] = source.map {
case true => ifTrue
case false => ifFalse
}

@inline def toggle[A: Monoid](ifTrue: => A): Rx[A] = toggle(ifTrue, Monoid[A].empty)

@inline def negated: Rx[Boolean] = source.map(x => !x)
}

implicit object source extends Source[Rx] {
def unsafeSubscribe[A](source: Rx[A])(sink: Observer[A]): Cancelable = source.observable.unsafeSubscribe(sink)
}
Expand Down

0 comments on commit 622035e

Please sign in to comment.