From 3f30eb32c2ec37d811aaff810168ec8438761ef1 Mon Sep 17 00:00:00 2001 From: Sarah Gerweck Date: Mon, 10 Aug 2015 19:05:54 -0700 Subject: [PATCH] Fix `Observable.map` to suppress nil changes If the new output is equal to the old output, we'll suppress the change message altogether, so as not to do unnecessary recalculations. E.g., imagine you have a property `isOdd`. If you go from 27 to 319, there's no need to trigger all the downstream objects to recalculate. (Most of the other functional primitives I've created already have this behavior.) --- src/main/scala/org/gerweck/scalafx/util/observable.scala | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/scala/org/gerweck/scalafx/util/observable.scala b/src/main/scala/org/gerweck/scalafx/util/observable.scala index 7078ddb..8d17311 100644 --- a/src/main/scala/org/gerweck/scalafx/util/observable.scala +++ b/src/main/scala/org/gerweck/scalafx/util/observable.scala @@ -17,8 +17,13 @@ trait ObservableImplicits { val prop = ObjectProperty[B](originalValue) + var prevValue = originalValue def changeHandler = { - prop.value = recalculate() + val newVal = recalculate() + if (prevValue != newVal) { + prop.value = recalculate() + prevValue = newVal + } } a onChange changeHandler