New method to observe an Option[A] from a future

This makes it much easier to mix in an observable from a future. I'd
also like to do a `Try[Option[A]]` method.
This commit is contained in:
Sarah Gerweck 2016-06-04 00:30:04 -07:00
parent 64945853ea
commit ccc644f9d4

View File

@ -46,4 +46,24 @@ object FutureObservable {
prop prop
} }
} }
def ofSuccessOption[A](future: Future[A])(implicit ec: ExecutionContext): ReadOnlyObjectProperty[Option[A]] = {
future.value match {
case Some(Success(a)) =>
ObjectProperty(Some(a))
case Some(Failure(f)) =>
logger.debug(s"Got failure from FutureObservable's result: $f")
ObjectProperty(None)
case None =>
val prop = ObjectProperty[Option[A]](None)
future onSuccess { case a =>
runLater {
prop.value = Some(a)
}
}
prop
}
}
} }