-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(observers): Entity scoped observers not restricting by event type…
…, not inheriting observers to instances when created before tracking observer tests(observers): Add entity scoped observer tests
- Loading branch information
Showing
4 changed files
with
95 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
geary-core/src/commonMain/kotlin/com/mineinabyss/geary/observers/EventToObserversMap.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.mineinabyss.geary.observers | ||
|
||
import androidx.collection.LongSparseArray | ||
import com.mineinabyss.geary.datatypes.ComponentId | ||
import com.mineinabyss.geary.datatypes.getOrPut | ||
|
||
class EventToObserversMap { | ||
private val eventToObserverMap = LongSparseArray<ObserverList>() | ||
|
||
fun addObserver(observer: Observer) { | ||
observer.listenToEvents.forEach { event -> | ||
eventToObserverMap.getOrPut(event.toLong()) { ObserverList() }.add(observer) | ||
} | ||
} | ||
|
||
operator fun get(event: ComponentId): ObserverList? = eventToObserverMap[event.toLong()] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
.../jvmTest/kotlin/com/mineinabyss/geary/observers/entity_scoped/EntityScopedObserverTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.mineinabyss.geary.observers.entity_scoped | ||
|
||
import com.mineinabyss.geary.helpers.entity | ||
import com.mineinabyss.geary.helpers.tests.GearyTest | ||
import com.mineinabyss.geary.modules.geary | ||
import com.mineinabyss.geary.observers.entity.observe | ||
import com.mineinabyss.geary.systems.builders.observe | ||
import io.kotest.matchers.shouldBe | ||
import org.junit.jupiter.api.Test | ||
|
||
class EntityScopedObserverTest : GearyTest() { | ||
private sealed class Clicked | ||
|
||
@Test | ||
fun `should only observe scoped events on correct entity`() { | ||
// arrange | ||
val scopedObserve = entity() | ||
val globallyObserve = entity() | ||
val lines = mutableListOf<String>() | ||
scopedObserve.observe<Clicked>().exec { lines += "Scoped clicked $entity" } | ||
geary.observe<Clicked>().exec { lines += "Global clicked $entity" } | ||
|
||
// act | ||
scopedObserve.emit<Clicked>() | ||
globallyObserve.emit<Clicked>() | ||
|
||
// assert | ||
lines shouldBe """ | ||
Scoped clicked $scopedObserve | ||
Global clicked $scopedObserve | ||
Global clicked $globallyObserve | ||
""".trimIndent().lines() | ||
} | ||
|
||
@Test | ||
fun `should propagate scoped observers to instances when instance created after tracking observer`() { | ||
// arrange | ||
val prefab = entity() | ||
val lines = mutableListOf<String>() | ||
prefab.observe<Clicked>().exec { lines += "Scoped clicked $entity" } | ||
val instance = entity { extend(prefab) } | ||
|
||
// act | ||
instance.emit<Clicked>() | ||
|
||
// assert | ||
lines shouldBe """ | ||
Scoped clicked $instance | ||
""".trimIndent().lines() | ||
|
||
} | ||
|
||
@Test | ||
fun `should propagate scoped observers to instances when instance created before tracking observer`() { | ||
// arrange | ||
val prefab = entity() | ||
val instance = entity { extend(prefab) } | ||
val lines = mutableListOf<String>() | ||
prefab.observe<Clicked>().exec { lines += "Scoped clicked $entity" } | ||
|
||
// act | ||
instance.emit<Clicked>() | ||
|
||
// assert | ||
lines shouldBe """ | ||
Scoped clicked $instance | ||
""".trimIndent().lines() | ||
} | ||
} |