This repository has been archived by the owner on May 3, 2023. It is now read-only.
generated from korlibs/korge-kproject-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
246 additions
and
32 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
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
8 changes: 4 additions & 4 deletions
8
...c/commonMain/kotlin/korge/i18n/I18nExt.kt → ...Main/kotlin/korlibs/korge/i18n/I18nExt.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
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,81 @@ | ||
package korlibs.korge.bus | ||
|
||
import korlibs.datastructure.iterators.fastForEach | ||
import korlibs.inject.AsyncDestructor | ||
import korlibs.inject.AsyncInjector | ||
import korlibs.io.async.launchUnscoped | ||
import korlibs.io.lang.Closeable | ||
import kotlin.coroutines.CoroutineContext | ||
import kotlin.reflect.KClass | ||
|
||
class Bus( | ||
private val globalBus: GlobalBus, | ||
val coroutineContext: CoroutineContext = globalBus.coroutineContext, | ||
) : Closeable, AsyncDestructor { | ||
private val closeables = arrayListOf<Closeable>() | ||
|
||
suspend fun send(message: Any) { | ||
globalBus.send(message) | ||
} | ||
|
||
fun sendAsync(message: Any, coroutineContext: CoroutineContext = this.coroutineContext) { | ||
globalBus.sendAsync(message, coroutineContext) | ||
} | ||
|
||
fun <T : Any> register(clazz: KClass<out T>, handler: suspend (T) -> Unit): Closeable { | ||
val closeable = globalBus.register(clazz, handler) | ||
closeables += closeable | ||
return closeable | ||
} | ||
|
||
inline fun <reified T : Any> register(noinline handler: suspend (T) -> Unit): Closeable { | ||
return register(T::class, handler) | ||
} | ||
|
||
override fun close() { | ||
closeables.fastForEach { c -> | ||
c.close() | ||
} | ||
} | ||
|
||
override suspend fun deinit() { | ||
close() | ||
} | ||
} | ||
|
||
class GlobalBus( | ||
val coroutineContext: CoroutineContext | ||
) { | ||
val perClassHandlers = HashMap<KClass<*>, ArrayList<suspend (Any) -> Unit>>() | ||
|
||
suspend fun send(message: Any) { | ||
val clazz = message::class | ||
perClassHandlers[clazz]?.fastForEach { handler -> | ||
handler(message) | ||
} | ||
} | ||
|
||
fun sendAsync(message: Any, coroutineContext: CoroutineContext = this.coroutineContext) { | ||
coroutineContext.launchUnscoped { send(message) } | ||
} | ||
|
||
private fun forClass(clazz: KClass<*>) = perClassHandlers.getOrPut(clazz) { arrayListOf() } | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
fun <T : Any> register(clazz: KClass<out T>, handler: suspend (T) -> Unit): Closeable { | ||
val chandler = handler as (suspend (Any) -> Unit) | ||
forClass(clazz).add(chandler) | ||
return Closeable { | ||
forClass(clazz).remove(chandler) | ||
} | ||
} | ||
|
||
inline fun <reified T : Any> register(noinline handler: suspend (T) -> Unit): Closeable { | ||
return register(T::class, handler) | ||
} | ||
} | ||
|
||
fun AsyncInjector.mapBus() { | ||
mapSingleton { GlobalBus(get()) } | ||
mapPrototype { Bus(get(), get()) } | ||
} |
68 changes: 68 additions & 0 deletions
68
korio-bus/src/commonMain/kotlin/korlibs/korge/bus/SyncBus.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,68 @@ | ||
package korlibs.korge.bus | ||
|
||
import korlibs.datastructure.iterators.fastForEach | ||
import korlibs.inject.AsyncDestructor | ||
import korlibs.inject.AsyncInjector | ||
import korlibs.io.lang.Closeable | ||
import kotlin.reflect.KClass | ||
|
||
class SyncBus( | ||
private val globalBus: SyncGlobalBus | ||
) : Closeable, AsyncDestructor { | ||
private val closeables = arrayListOf<Closeable>() | ||
|
||
fun send(message: Any) { | ||
globalBus.send(message) | ||
} | ||
|
||
fun <T : Any> register(clazz: KClass<out T>, handler: (T) -> Unit): Closeable { | ||
val closeable = globalBus.register(clazz, handler) | ||
closeables += closeable | ||
return closeable | ||
} | ||
|
||
inline fun <reified T : Any> register(noinline handler: (T) -> Unit): Closeable { | ||
return register(T::class, handler) | ||
} | ||
|
||
override fun close() { | ||
closeables.fastForEach { c -> | ||
c.close() | ||
} | ||
} | ||
|
||
override suspend fun deinit() { | ||
close() | ||
} | ||
} | ||
|
||
class SyncGlobalBus { | ||
val perClassHandlers = HashMap<KClass<*>, ArrayList<(Any) -> Unit>>() | ||
|
||
fun send(message: Any) { | ||
val clazz = message::class | ||
perClassHandlers[clazz]?.fastForEach { handler -> | ||
handler(message) | ||
} | ||
} | ||
|
||
private fun forClass(clazz: KClass<*>) = perClassHandlers.getOrPut(clazz) { arrayListOf() } | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
fun <T : Any> register(clazz: KClass<out T>, handler: (T) -> Unit): Closeable { | ||
val chandler = handler as ((Any) -> Unit) | ||
forClass(clazz).add(chandler) | ||
return Closeable { | ||
forClass(clazz).remove(chandler) | ||
} | ||
} | ||
|
||
inline fun <reified T : Any> register(noinline handler: (T) -> Unit): Closeable { | ||
return register(T::class, handler) | ||
} | ||
} | ||
|
||
fun AsyncInjector.mapSyncBus() { | ||
mapSingleton { SyncGlobalBus() } | ||
mapPrototype { SyncBus(get()) } | ||
} |
33 changes: 33 additions & 0 deletions
33
korio-bus/src/commonTest/kotlin/korlibs/korge/bus/BusTest.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,33 @@ | ||
package korlibs.korge.bus | ||
|
||
import korlibs.inject.AsyncInjector | ||
import korlibs.io.async.suspendTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class BusTest { | ||
val out = arrayListOf<String>() | ||
|
||
inner class Scene1(val bus: Bus) { | ||
init { | ||
bus.register<Int> { out += "HELLO$it" } | ||
} | ||
} | ||
|
||
@Test | ||
fun test() = suspendTest { | ||
val injector = AsyncInjector() | ||
injector.mapInstance(coroutineContext) // This should be mapped already in the Korge { } block | ||
injector.mapBus() | ||
injector.mapPrototype { Scene1(get()) } | ||
val injector2 = injector.child() | ||
val scene1 = injector2.get<Scene1>() | ||
val bus = injector.get<Bus>() | ||
bus.send(1) | ||
bus.send(2) | ||
//scene1.sceneDestroyInternal() | ||
injector2.deinit() | ||
bus.send(3) | ||
assertEquals("HELLO1,HELLO2", out.joinToString(",")) | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
korio-bus/src/commonTest/kotlin/korlibs/korge/bus/SyncBusTest.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,32 @@ | ||
package korlibs.korge.bus | ||
|
||
import korlibs.inject.AsyncInjector | ||
import korlibs.io.async.suspendTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class SyncBusTest { | ||
val out = arrayListOf<String>() | ||
|
||
inner class Scene1(val bus: SyncBus) { | ||
init { | ||
bus.register<Int> { out += "HELLO$it" } | ||
} | ||
} | ||
|
||
@Test | ||
fun test() = suspendTest { | ||
val injector = AsyncInjector() | ||
injector.mapSyncBus() | ||
injector.mapPrototype { Scene1(get()) } | ||
val injector2 = injector.child() | ||
val scene1 = injector2.get<Scene1>() | ||
val bus = injector.get<SyncBus>() | ||
bus.send(1) | ||
bus.send(2) | ||
//scene1.sceneDestroyInternal() | ||
injector2.deinit() | ||
bus.send(3) | ||
assertEquals("HELLO1,HELLO2", out.joinToString(",")) | ||
} | ||
} |
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
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