diff options
author | Joffrey Bion <joffrey.bion@gmail.com> | 2023-04-30 23:17:06 +0200 |
---|---|---|
committer | Joffrey Bion <joffrey.bion@gmail.com> | 2023-04-30 23:17:06 +0200 |
commit | 85936cd9e8d3c54311a016cefa007e2de76cda38 (patch) | |
tree | 54690a93ab19ea11280923eb86880d26a16e6658 | |
parent | Cleanup experimental annotations (diff) | |
download | seven-wonders-85936cd9e8d3c54311a016cefa007e2de76cda38.tar.gz seven-wonders-85936cd9e8d3c54311a016cefa007e2de76cda38.tar.bz2 seven-wonders-85936cd9e8d3c54311a016cefa007e2de76cda38.zip |
Remove unnecessary GlobalScope usage
We used GlobalScope to emit redux actions to the shared flow of actions.
This means we technically had an unlimited buffer of actions but we used
the coroutines queue to represent it instead of just defining the flow
as such in the first place.
-rw-r--r-- | sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/SagasFramework.kt | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/SagasFramework.kt b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/SagasFramework.kt index ce05ca15..05c03b13 100644 --- a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/SagasFramework.kt +++ b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/SagasFramework.kt @@ -1,6 +1,7 @@ package org.luxons.sevenwonders.ui.redux.sagas import kotlinx.coroutines.* +import kotlinx.coroutines.channels.* import kotlinx.coroutines.flow.* import redux.Middleware import redux.MiddlewareApi @@ -11,7 +12,7 @@ class SagaManager<S, A : RAction, R>( ) { private lateinit var context: SagaContext<S, A, R> - private val actions = MutableSharedFlow<A>(extraBufferCapacity = 16) + private val actions = MutableSharedFlow<A>(extraBufferCapacity = Channel.UNLIMITED) fun createMiddleware(): Middleware<S, A, R, A, R> = ::sagasMiddleware @@ -31,9 +32,12 @@ class SagaManager<S, A : RAction, R>( monitor?.invoke(action) } - @OptIn(DelicateCoroutinesApi::class) // Ok because almost never suspends - if it does, we have bigger problems private fun handleAction(action: A) { - GlobalScope.launch { actions.emit(action) } + val emitted = actions.tryEmit(action) + if (!emitted) { + // should never happen since our buffer is 'unlimited' (in reality it's Int.MAX_VALUE) + error("Couldn't dispatch redux action, buffer is full") + } } fun launchSaga(coroutineScope: CoroutineScope, saga: suspend SagaContext<S, A, R>.() -> Unit): Job { |