Minor additions jfx monix implicits

Added implicit conversion from os.relpath to string for more convenient css
path dsl
Fixed some property sinks not using onchange previously
Added one sample property source binding that uses an implicit
composite cancelable
This commit is contained in:
Rohan Sircar 2020-12-23 13:51:11 +05:30
parent 2f63119c6c
commit b0b914424b

View File

@ -11,6 +11,7 @@ import monix.eval.Coeval
import monix.execution.Ack import monix.execution.Ack
import monix.execution.Cancelable import monix.execution.Cancelable
import monix.execution.Scheduler import monix.execution.Scheduler
import monix.execution.cancelables.CompositeCancelable
import monix.execution.cancelables.SingleAssignCancelable import monix.execution.cancelables.SingleAssignCancelable
import monix.reactive.Observable import monix.reactive.Observable
import monix.reactive.Observer import monix.reactive.Observer
@ -43,11 +44,17 @@ trait JavaFXMonixObservables {
implicit def extendedObservableList[A]( implicit def extendedObservableList[A](
list: ObservableList[A] list: ObservableList[A]
) = new ObservableListExt(list) ) = new ObservableListExt(list)
implicit def extendedStringObservableList(
list: ObservableList[String]
) = new StringObservableListExt(list)
implicit def extendedObjectPropertyObservableList[A]( implicit def extendedObjectPropertyObservableList[A](
prop: ObjectProperty[ObservableList[A]] prop: ObjectProperty[ObservableList[A]]
) = new ObjectPropertyObservableListExt(prop) ) = new ObjectPropertyObservableListExt(prop)
implicit def extendedButton(button: ButtonBase) = new ButtonBaseExt(button) implicit def extendedButton(button: ButtonBase) = new ButtonBaseExt(button)
implicit def extendedMenuItem(item: MenuItem) = new MenuItemExt(item) implicit def extendedMenuItem(item: MenuItem) = new MenuItemExt(item)
// implicit val implShowForOsRelPath = Show.fromToString[os.Path]
implicit def osRelPath2String(path: os.RelPath): String = path.toString()
} }
object JavaFXMonixObservables { object JavaFXMonixObservables {
@ -99,16 +106,18 @@ object JavaFXMonixObservables {
final class PropertyExt[T, J](private val prop: Property[T, J]) final class PropertyExt[T, J](private val prop: Property[T, J])
extends AnyVal { extends AnyVal {
def -->(op: Observer[T]) = { def -->[J1 >: J](sub: Observer[J1]) = {
op.onNext(prop.value) prop.onChange((a, b, c) => if (c != null) sub.onNext(c))
} }
def ==>(op: Property[T, J]) = { def ==>(op: Property[T, J]) = {
op <== prop op <== prop
} }
def <--(obs: Observable[T])(implicit s: Scheduler) = { def <--(
obs.doOnNextF(v => Coeval(prop.value = v)).subscribe() obs: Observable[_ <: T]
)(implicit s: Scheduler, c: CompositeCancelable): Unit = {
c += obs.doOnNextF(v => Coeval(prop.value = v)).subscribe()
} }
def asOption = prop.map(Option(_)) def asOption = prop.map(Option(_))
@ -211,11 +220,20 @@ object JavaFXMonixObservables {
} else Task.unit } else Task.unit
} }
final class StringObservableListExt(
private val buffer: ObservableList[String]
) extends AnyVal {
// def ++=[T](that: Seq[T])(implicit S: Show[T]): Unit =
// buffer ++= that.map(S.show)
// def ++=[T](that: Seq[T])(implicit C: CssPath[T]): Unit =
// buffer ++= that.map(C.path)
}
final class ReadOnlyPropertyExt[T, J]( final class ReadOnlyPropertyExt[T, J](
private val prop: ReadOnlyProperty[T, J] private val prop: ReadOnlyProperty[T, J]
) extends AnyVal { ) extends AnyVal {
def -->(op: Observer[T]) = { def -->[J1 >: J](sub: Observer[J1]) = {
op.onNext(prop.value) prop.onChange((a, b, c) => if (c != null) sub.onNext(c))
} }
def ==>(op: Property[T, J]) = { def ==>(op: Property[T, J]) = {