Minor improvements to FutureObservable
This commit is contained in:
parent
cdc3eaad8e
commit
8df96ed076
@ -33,37 +33,54 @@ object FutureObservable {
|
|||||||
ObjectProperty(a)
|
ObjectProperty(a)
|
||||||
|
|
||||||
case Some(Failure(f)) =>
|
case Some(Failure(f)) =>
|
||||||
logger.debug(s"Got failure from FutureObservable's result: $f")
|
logFailure(f)
|
||||||
ObjectProperty(defaultValue)
|
ObjectProperty(defaultValue)
|
||||||
|
|
||||||
case None =>
|
case None =>
|
||||||
val prop = ObjectProperty[A](defaultValue)
|
val prop = ObjectProperty[A](defaultValue)
|
||||||
future onSuccess { case a =>
|
future onComplete {
|
||||||
|
case Success(a) =>
|
||||||
runLater {
|
runLater {
|
||||||
prop.value = a
|
prop.value = a
|
||||||
}
|
}
|
||||||
|
case Failure(f) =>
|
||||||
|
logFailure(f)
|
||||||
}
|
}
|
||||||
prop
|
prop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Construct an observable that gives `None` until the `Future` completes successfully, after
|
||||||
|
* which the `Option` contains the successful result.
|
||||||
|
*
|
||||||
|
* This method does not allow you to differentiate between a failure and a calculation
|
||||||
|
* that is still running. If you need to differentiate these, you can either use
|
||||||
|
* [[scala.concurrent.Future.recover]] or use [[ofTryOption]] instead.
|
||||||
|
*/
|
||||||
def ofSuccessOption[A](future: Future[A])(implicit ec: ExecutionContext): ReadOnlyObjectProperty[Option[A]] = {
|
def ofSuccessOption[A](future: Future[A])(implicit ec: ExecutionContext): ReadOnlyObjectProperty[Option[A]] = {
|
||||||
future.value match {
|
future.value match {
|
||||||
case Some(Success(a)) =>
|
case Some(Success(a)) =>
|
||||||
ObjectProperty(Some(a))
|
ObjectProperty(Some(a))
|
||||||
|
|
||||||
case Some(Failure(f)) =>
|
case Some(Failure(f)) =>
|
||||||
logger.debug(s"Got failure from FutureObservable's result: $f")
|
logFailure(f)
|
||||||
ObjectProperty(None)
|
ObjectProperty(None)
|
||||||
|
|
||||||
case None =>
|
case None =>
|
||||||
val prop = ObjectProperty[Option[A]](None)
|
val prop = ObjectProperty[Option[A]](None)
|
||||||
future onSuccess { case a =>
|
future onComplete {
|
||||||
|
case Success(a) =>
|
||||||
runLater {
|
runLater {
|
||||||
prop.value = Some(a)
|
prop.value = Some(a)
|
||||||
}
|
}
|
||||||
|
case Failure(f) =>
|
||||||
|
logFailure(f)
|
||||||
}
|
}
|
||||||
prop
|
prop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private[this] def logFailure(f: Throwable): Unit = {
|
||||||
|
logger.debug(s"Got failure from FutureObservable's result: $f")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user