From 941d0c7cb7ffc07915051f7c863317fcd5f174b6 Mon Sep 17 00:00:00 2001 From: Sarah Gerweck Date: Wed, 4 May 2016 22:25:36 -0700 Subject: [PATCH] Update `FutureObservable` to perform much better --- .../scalafx/util/FutureObservable.scala | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/main/scala/org/gerweck/scalafx/util/FutureObservable.scala b/src/main/scala/org/gerweck/scalafx/util/FutureObservable.scala index 9fd7cbb..f842b5a 100644 --- a/src/main/scala/org/gerweck/scalafx/util/FutureObservable.scala +++ b/src/main/scala/org/gerweck/scalafx/util/FutureObservable.scala @@ -1,6 +1,7 @@ package org.gerweck.scalafx.util import scala.concurrent._ +import scala.util._ import org.log4s._ @@ -27,13 +28,23 @@ object FutureObservable { * is already a built-in feature. */ def apply[A](defaultValue: A)(future: Future[A])(implicit ec: ExecutionContext): ReadOnlyObjectProperty[A] = { - val prop = ObjectProperty[A](defaultValue) - future onSuccess { case a => - runLater { - logger.trace("Updating property value after Future's success") - prop.value = a - } + logger.debug(s"Got request to create new FutureObservable") + future.value match { + case Some(Success(a)) => + ObjectProperty(a) + + case Some(Failure(f)) => + logger.info(s"Got failure from FutureObservable's result: $f") + ObjectProperty(defaultValue) + + case None => + val prop = ObjectProperty[A](defaultValue) + future onSuccess { case a => + runLater { + prop.value = a + } + } + prop } - prop } }