From 85936cd9e8d3c54311a016cefa007e2de76cda38 Mon Sep 17 00:00:00 2001 From: Joffrey Bion Date: Sun, 30 Apr 2023 23:17:06 +0200 Subject: 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. --- .../org/luxons/sevenwonders/ui/redux/sagas/SagasFramework.kt | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'sw-ui/src') 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( ) { private lateinit var context: SagaContext - private val actions = MutableSharedFlow(extraBufferCapacity = 16) + private val actions = MutableSharedFlow(extraBufferCapacity = Channel.UNLIMITED) fun createMiddleware(): Middleware = ::sagasMiddleware @@ -31,9 +32,12 @@ class SagaManager( 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.() -> Unit): Job { -- cgit