Skip to content

Commit e8a8045

Browse files
committed
feat: Expose EventRunner.removeObserver (closes #114)
1 parent 7acc16b commit e8a8045

4 files changed

Lines changed: 90 additions & 2 deletions

File tree

geary-core/src/com/mineinabyss/geary/observers/ArchetypeEventRunner.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ class ArchetypeEventRunner(
1818
val observerComponent: ComponentId = compProvider.id<Observer>()
1919

2020
private val eventToObserversMap = EventToObserversMap(records)
21+
2122
override fun addObserver(observer: Observer) {
2223
eventToObserversMap.addObserver(observer)
2324
}
2425

26+
override fun removeObserver(observer: Observer) {
27+
eventToObserversMap.removeObserver(observer)
28+
}
29+
2530
private inline fun matchObservers(
2631
eventType: ComponentId,
2732
involvedComponent: ComponentId,

geary-core/src/com/mineinabyss/geary/observers/EventRunner.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.mineinabyss.geary.observers
22

33
import com.mineinabyss.geary.datatypes.ComponentId
4-
import com.mineinabyss.geary.datatypes.Entity
54
import com.mineinabyss.geary.datatypes.EntityId
65

76
interface EventRunner {
87
fun addObserver(observer: Observer)
98

9+
fun removeObserver(observer: Observer)
10+
1011
fun callEvent(
1112
eventType: ComponentId,
1213
eventData: Any?,

geary-core/test@jvm/com/mineinabyss/geary/components/ComponentAsEntityProviderTest.kt

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,63 @@ import com.mineinabyss.geary.helpers.entity
55
import com.mineinabyss.geary.test.GearyTest
66
import io.kotest.matchers.shouldBe
77
import org.junit.jupiter.api.Test
8+
import org.koin.core.scope.Scope
9+
import org.koin.core.scope.ScopeCallback
10+
import org.koin.dsl.koinApplication
11+
import org.koin.dsl.module
12+
import kotlin.random.Random
813

9-
class ComponentAsEntityProviderTest: GearyTest() {
14+
class ComponentAsEntityProviderTest : GearyTest() {
1015
@Test
1116
fun `should correctly register reserved components`() {
1217
entity()
1318
componentId<Any>() shouldBe ReservedComponents.ANY
1419
}
1520
}
21+
22+
class Something {
23+
24+
class A(val test: String) {
25+
}
26+
27+
class B(val test: String)
28+
29+
@Test
30+
fun main() {
31+
val app = koinApplication() {
32+
modules(module {
33+
single { "default" }
34+
scope<A> {
35+
scoped {
36+
registerCallback(object : ScopeCallback {
37+
override fun onScopeClose(scope: Scope) {
38+
println("Closed")
39+
}
40+
})
41+
Random.nextInt()
42+
}
43+
scoped {
44+
get<Double>().toString()
45+
}
46+
}
47+
scope<B> {
48+
scoped { get<Int>().toString() }
49+
scoped<Double> { 0.1 }
50+
// scoped<String> { "B" + Random.nextInt(100) }
51+
}
52+
})
53+
}
54+
app.koin.createScope<A>("A").apply {
55+
get<Int>()
56+
}.close()
57+
val scopeB = app.koin.createScope<A>("A").get<Int>().let { println(it) }
58+
// val scopeB = app.koin.createScope<B>().apply {
59+
// linkTo(app.koin.getScope("A"))
60+
// }
61+
62+
// scopeA.linkTo(scopeB)
63+
// scopeA.get<Int>().let { println(it) }
64+
// scopeB.get<String>().let { println(it) }
65+
// scopeA.close()
66+
}
67+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.mineinabyss.geary.observers
2+
3+
import com.mineinabyss.geary.helpers.entity
4+
import com.mineinabyss.geary.modules.observe
5+
import com.mineinabyss.geary.observers.events.OnSet
6+
import com.mineinabyss.geary.systems.query.query
7+
import com.mineinabyss.geary.test.GearyTest
8+
import io.kotest.matchers.shouldBe
9+
import kotlin.test.Test
10+
11+
class RemoveObserverTest : GearyTest() {
12+
@Test
13+
fun `should stop observing events after removed`() {
14+
val called = mutableListOf<Int>()
15+
val observer = observe<OnSet>().exec(query<Int>()) { (data) ->
16+
called += data
17+
}
18+
19+
val entity = entity {
20+
set(1)
21+
set(2)
22+
}
23+
24+
eventRunner.removeObserver(observer)
25+
26+
entity.set(3)
27+
28+
called shouldBe listOf(1, 2)
29+
}
30+
}

0 commit comments

Comments
 (0)