Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <T, R> State<T>.collect(defaultValue: R, block: suspend StateContext.(value: T, prev: R) -> R) =
Expand All @@ -11,4 +13,5 @@ interface StateContext {
fun <T> State<T>.collect(block: suspend StateContext.(value: T) -> Unit) =
this.collect(this@StateContext, block)

fun cancel()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@ 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
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")
}
}