diff --git a/convention-plugins/src/main/kotlin/root.publication.gradle.kts b/convention-plugins/src/main/kotlin/root.publication.gradle.kts index 8ea8705..4a12397 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 = "2.0.1" + const val VERSION = "2.1.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 988521d..aa8a172 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 @@ -1,8 +1,10 @@ package net.kigawa.hakate.api.state +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job interface StateContext { + val coroutineScope: CoroutineScope fun dispatcher(): StateDispatcher fun dispatch(block: suspend StateContext.() -> Unit): Job fun State.collect(defaultValue: R, block: suspend StateContext.(value: T, prev: R) -> R) = @@ -11,4 +13,5 @@ interface StateContext { fun State.collect(block: suspend StateContext.(value: T) -> Unit) = this.collect(this@StateContext, block) + fun cancel() } \ 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 bd26813..01e6d27 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 @@ -2,6 +2,7 @@ package net.kigawa.hakate.impl.state import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job +import kotlinx.coroutines.cancel import kotlinx.coroutines.launch import net.kigawa.hakate.api.state.StateContext import net.kigawa.hakate.api.state.StateDispatcher @@ -9,16 +10,20 @@ import net.kigawa.hakate.impl.Utl.suspendApply class StateContextImpl( private val dispatcher: StateDispatcher, - private val scope: CoroutineScope, + override val coroutineScope: CoroutineScope, ) : StateContext { override fun dispatcher(): StateDispatcher = dispatcher override fun dispatch( block: suspend StateContext.() -> Unit, ): Job { - return scope.launch { + return coroutineScope.launch { StateContextImpl(dispatcher, this@launch).suspendApply { block() } } } + + override fun cancel() { + coroutineScope.cancel("cancel by StateContext") + } } \ No newline at end of file