Skip to content

Supress non-forced updates on transformed properties #474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package io.udash.properties
package single

import com.avsystem.commons.misc.Opt
import com.avsystem.commons._
import io.udash.utils.Registration

import scala.concurrent.Future

/** Represents ReadableProperty[A] transformed to ReadableProperty[B]. */
private[properties] class TransformedReadableProperty[A, B](
override protected val origin: ReadableProperty[A],
Expand All @@ -16,15 +14,21 @@ private[properties] class TransformedReadableProperty[A, B](
protected var originListenerRegistration: Registration = _

protected def originListener(originValue: A) : Unit = {
lastValue = Opt(originValue)
transformedValue = transformer(originValue)
fireValueListeners()
val forced = lastValue.contains(originValue) //if the listener was triggered despite equal value, the update was forced
val newValue = transformer(originValue)
val transformedValueChanged = newValue != transformedValue
lastValue = originValue.opt
transformedValue = newValue
if (forced || transformedValueChanged) fireValueListeners()
}

private def initOriginListener(): Unit = {
if (originListenerRegistration == null || !originListenerRegistration.isActive) {
listeners.clear()
originListenerRegistration = origin.listen(originListener)
val originValue = origin.get
lastValue = originValue.opt
lastValue.foreach(v => transformedValue = transformer(v))
}
}

Expand Down Expand Up @@ -63,7 +67,7 @@ private[properties] class TransformedReadableProperty[A, B](

override def get: B = {
val originValue = origin.get
if (lastValue.isEmpty || lastValue.get != originValue) {
if (originListenerRegistration == null && (lastValue.isEmpty || lastValue.get != originValue)) {
lastValue = Opt(originValue)
transformedValue = transformer(originValue)
}
Expand Down
62 changes: 61 additions & 1 deletion core/src/test/scala/io/udash/properties/PropertyTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,58 @@ class PropertyTest extends UdashCoreTest {
counter2 should be(1)
}

"fire on transformed value changed or when forced" in {
val origin: Property[Option[Int]] = Property(Some(0))
val transformed: ReadableProperty[Boolean] = origin.transform((q: Option[Int]) => q.isDefined)
var counter = 0

transformed.listen(_ => counter += 1)

origin.set(Some(0))
counter shouldBe 0 //suppressed at origin

origin.set(Some(1))
counter shouldBe 0 //suppressed at transformed

origin.set(None)
counter shouldBe 1

origin.set(None)
counter shouldBe 1

origin.set(None, force = true)
counter shouldBe 2

origin.touch()
counter shouldBe 3
}

"fire on streamed value changed or when forced" in {
val origin: Property[Option[Int]] = Property(Some(0))
val target = Property.blank[Boolean]

origin.streamTo(target)((q: Option[Int]) => q.isDefined)
var counter = 0

target.listen(_ => counter += 1)

origin.set(Some(0))
counter shouldBe 0 //suppressed at origin

origin.set(Some(1))
counter shouldBe 0 //suppressed at target

origin.set(None)
counter shouldBe 1

origin.set(None)
counter shouldBe 1

//todo detect forced / touched?
//origin.set(None, force = true)
//counter shouldBe 2
}

"combine with other properties (single properties)" in {
val p1 = Property(1)
val p2 = Property(2)
Expand Down Expand Up @@ -518,7 +570,7 @@ class PropertyTest extends UdashCoreTest {

val p = Property("1,2,3,4,5")
val s: ReadableSeqProperty[Int, ReadableProperty[Int]] =
p.transformToSeq((v: String) => Try(v.split(",").map(_.toInt).toSeq).getOrElse(Seq[Int]()))
p.transformToSeq((v: String) => Try(v.split(",").map(_.trim.toInt).toSeq).getOrElse(Seq[Int]()))

p.listenersCount() should be(0)

Expand Down Expand Up @@ -549,6 +601,14 @@ class PropertyTest extends UdashCoreTest {
lastPatch.removed.size should be(2)
elementsUpdated should be(2)

//suppressed at s
p.set(" 5 ,4 ,3")
s.get should be(Seq(5, 4, 3))
lastValue should be(s.get)
lastPatch.added.size should be(0)
lastPatch.removed.size should be(2)
elementsUpdated should be(2)

lastValue = null
lastPatch = null
elementsUpdated = 0
Expand Down