Skip to content

Commit

Permalink
Merge pull request #196 from vivid-money/fix-command-dispatching
Browse files Browse the repository at this point in the history
execute commands on IO dispatcher
  • Loading branch information
sergei-lapin authored Mar 23, 2023
2 parents 685b0e0 + 18510e2 commit 02a76f6
Showing 1 changed file with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package vivid.money.elmslie.coroutines

import kotlinx.coroutines.*
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Unconfined
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.flowOn
import vivid.money.elmslie.core.config.ElmslieConfig
import kotlinx.coroutines.launch
import vivid.money.elmslie.core.disposable.Disposable
import vivid.money.elmslie.core.store.DefaultActor
import vivid.money.elmslie.core.store.StateReducer
Expand All @@ -28,10 +31,10 @@ class ElmStoreCompat<Event : Any, State : Any, Effect : Any, Command : Any>(
@Suppress("TooGenericExceptionCaught", "RethrowCaughtException")
private fun <Command : Any, Event : Any> Actor<Command, Event>.toActor() =
DefaultActor<Command, Event> { command, onEvent, onError ->
val job = GlobalScope.launch(Dispatchers.Unconfined) {
val job = GlobalScope.launch(Unconfined) {
try {
execute(command)
.flowOn(ElmslieConfig.backgroundExecutor.asCoroutineDispatcher())
.flowOn(IO)
.collect { event -> onEvent(event) }
} catch (t: CancellationException) {
throw t
Expand Down

0 comments on commit 02a76f6

Please sign in to comment.