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 = "3.1.0"
const val VERSION = "3.2.0"
}

group = Conf.GROUP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,4 +19,5 @@ interface StateContext {
fun merge(other: StateContext): StateContext {
return MergedStateContextImpl(this, other)
}
fun newStateContext(): StateContext
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -37,4 +39,11 @@ class MergedStateContextImpl(
first.cancel()
second.cancel()
}

override fun newStateContext(): StateContext {
return StateContextImpl(
dispatcher(),
CoroutineScope(coroutineScope.newCoroutineContext(EmptyCoroutineContext))
)
}
}
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)))
}
}