diff --git a/convention-plugins/src/main/kotlin/root.publication.gradle.kts b/convention-plugins/src/main/kotlin/root.publication.gradle.kts index afad449..8391a14 100644 --- a/convention-plugins/src/main/kotlin/root.publication.gradle.kts +++ b/convention-plugins/src/main/kotlin/root.publication.gradle.kts @@ -3,7 +3,7 @@ plugins { } object Conf { const val GROUP = "net.kigawa" - const val VERSION = "3.1.0" + const val VERSION = "3.2.0" } group = Conf.GROUP diff --git a/hakate/src/commonMain/kotlin/net/kigawa/hakate/api/state/StateContext.kt b/hakate/src/commonMain/kotlin/net/kigawa/hakate/api/state/StateContext.kt index fce27e4..7d18e8b 100644 --- a/hakate/src/commonMain/kotlin/net/kigawa/hakate/api/state/StateContext.kt +++ b/hakate/src/commonMain/kotlin/net/kigawa/hakate/api/state/StateContext.kt @@ -5,6 +5,7 @@ import kotlinx.coroutines.Job import net.kigawa.hakate.impl.state.MergedStateContextImpl interface StateContext { + val coroutineScope: CoroutineScope fun launch(block: suspend CoroutineScope.() -> Unit): Job fun dispatcher(): StateDispatcher fun dispatch(block: suspend StateContext.() -> Unit): Job @@ -18,4 +19,5 @@ interface StateContext { fun merge(other: StateContext): StateContext { return MergedStateContextImpl(this, other) } + fun newStateContext(): StateContext } \ No newline at end of file diff --git a/hakate/src/commonMain/kotlin/net/kigawa/hakate/impl/state/MergedStateContextImpl.kt b/hakate/src/commonMain/kotlin/net/kigawa/hakate/impl/state/MergedStateContextImpl.kt index 88ba43c..baa64aa 100644 --- a/hakate/src/commonMain/kotlin/net/kigawa/hakate/impl/state/MergedStateContextImpl.kt +++ b/hakate/src/commonMain/kotlin/net/kigawa/hakate/impl/state/MergedStateContextImpl.kt @@ -1,17 +1,19 @@ package net.kigawa.hakate.impl.state -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Job -import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext +import kotlinx.coroutines.* import net.kigawa.hakate.api.state.StateContext import net.kigawa.hakate.api.state.StateDispatcher import net.kigawa.hakate.impl.Utl.suspendApply +import kotlin.coroutines.EmptyCoroutineContext class MergedStateContextImpl( private val first: StateContext, private val second: StateContext, ) : StateContext { + override val coroutineScope: CoroutineScope = CoroutineScope( + first.coroutineScope.newCoroutineContext(second.coroutineScope.coroutineContext) + ) + override fun launch(block: suspend CoroutineScope.() -> Unit): Job { return first.launch { val f = this @@ -37,4 +39,11 @@ class MergedStateContextImpl( first.cancel() second.cancel() } + + override fun newStateContext(): StateContext { + return StateContextImpl( + dispatcher(), + CoroutineScope(coroutineScope.newCoroutineContext(EmptyCoroutineContext)) + ) + } } \ No newline at end of file diff --git a/hakate/src/commonMain/kotlin/net/kigawa/hakate/impl/state/StateContextImpl.kt b/hakate/src/commonMain/kotlin/net/kigawa/hakate/impl/state/StateContextImpl.kt index 99d7ee3..a3168bc 100644 --- a/hakate/src/commonMain/kotlin/net/kigawa/hakate/impl/state/StateContextImpl.kt +++ b/hakate/src/commonMain/kotlin/net/kigawa/hakate/impl/state/StateContextImpl.kt @@ -1,16 +1,14 @@ package net.kigawa.hakate.impl.state -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Job -import kotlinx.coroutines.cancel -import kotlinx.coroutines.launch +import kotlinx.coroutines.* import net.kigawa.hakate.api.state.StateContext import net.kigawa.hakate.api.state.StateDispatcher import net.kigawa.hakate.impl.Utl.suspendApply +import kotlin.coroutines.EmptyCoroutineContext class StateContextImpl( private val dispatcher: StateDispatcher, - val coroutineScope: CoroutineScope, + override val coroutineScope: CoroutineScope, ) : StateContext { override fun launch(block: suspend CoroutineScope.() -> Unit): Job { return coroutineScope.launch(block = block) @@ -30,4 +28,8 @@ class StateContextImpl( override fun cancel() { coroutineScope.cancel("cancel by StateContext") } + + override fun newStateContext(): StateContext { + return StateContextImpl(dispatcher, CoroutineScope(coroutineScope.newCoroutineContext(EmptyCoroutineContext))) + } } \ No newline at end of file