Skip to content

Commit

Permalink
fix: PropertyMap remove() correctly removes internal listeners, closes
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmasB committed Mar 14, 2023
1 parent 4bf54b7 commit e0d7015
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
Expand Up @@ -136,6 +136,16 @@ class PropertyMap {
mapChangeListeners.forEach { it.onRemoved(propertyName, value) }
}

listeners.filter { it.key.propertyName == propertyName }.forEach { (key, listener) ->
// clean up all non-removed JavaFX listeners for given [propertyName]
(get(key.propertyName) as ObservableValue<Any>).removeListener(listener as ChangeListener<Any>)
}

// remove all FXGL listeners for given [propertyName]
listeners.keys
.filter { it.propertyName == propertyName }
.forEach { listeners.remove(it) }

properties.remove(propertyName)
}

Expand Down
Expand Up @@ -11,9 +11,7 @@ import javafx.beans.property.SimpleIntegerProperty
import javafx.beans.property.StringProperty
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.*
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
Expand Down Expand Up @@ -240,6 +238,13 @@ class PropertyMapTest {
assertThat(count, `is`(2))
}

@Test
fun `Removing a property also removes its listeners`() {
map.setValue("test", 0)
map.remove("test")
assertDoesNotThrow { map.clear() }
}

@Test
fun `Copy returns a shallow copy`() {
map.setValue("testInt", 3)
Expand Down

0 comments on commit e0d7015

Please sign in to comment.