[Gradle] ObservableSet: Only add listeners when a new object is indeed added
^KT-60937 Verification Pending
This commit is contained in:
committed by
Space Team
parent
d2bd749861
commit
41e5a31e1b
+3
-1
@@ -40,10 +40,12 @@ internal class MutableObservableSetImpl<T>(vararg elements: T) : MutableObservab
|
||||
}
|
||||
|
||||
override fun add(element: T): Boolean {
|
||||
return underlying.add(element).also {
|
||||
val added = underlying.add(element)
|
||||
if (added) {
|
||||
whenObjectAddedActions.toTypedArray().forEach { action -> action(element) }
|
||||
forAllActions.toTypedArray().forEach { action -> action(element) }
|
||||
}
|
||||
return added
|
||||
}
|
||||
|
||||
override fun isEmpty(): Boolean {
|
||||
|
||||
+20
@@ -39,6 +39,26 @@ class ObservableSetTest {
|
||||
assertEquals(listOf(1, 2, 3, 4), testListener2.invocations)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test - adding already existing elements`() {
|
||||
val set = MutableObservableSetImpl(1, 2, 3)
|
||||
val forAllListener = TestListener()
|
||||
val whenObjectAddedListener = TestListener()
|
||||
set.forAll(forAllListener)
|
||||
set.whenObjectAdded(whenObjectAddedListener)
|
||||
|
||||
assertEquals(listOf(1, 2, 3), forAllListener.invocations)
|
||||
assertEquals(listOf(), whenObjectAddedListener.invocations)
|
||||
|
||||
/* Adding already existing elements */
|
||||
set.add(1)
|
||||
set.addAll(listOf(1, 2))
|
||||
|
||||
/* No further invocations */
|
||||
assertEquals(listOf(1, 2, 3), forAllListener.invocations)
|
||||
assertEquals(listOf(), whenObjectAddedListener.invocations)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test - whenObjectAdded`() {
|
||||
val set = MutableObservableSetImpl(1)
|
||||
|
||||
Reference in New Issue
Block a user