I'm wondering if anyone has advice on how to do As...
# redux-kotlin
a
I'm wondering if anyone has advice on how to do Async Actions where the dispatch ordering is always sequential? I wrote a request middleware
Copy code
override fun invoke(store: Store<AppState>): (next: Dispatcher) -> (action: Any) -> Any {
        return { next ->
            { action ->

                if (action is Request) coroutineScope.launch {
                    action.execute(store.getState, store.dispatch, api, db)
                }

                next(action)

            }
        }
    }
which just dispatches async requests on its own coroutineScope which is very useful for making API/DB fetches, but the problem I run into from time to time is if I want to update the store state and then dispatch another action from within an action sometimes the state updating action gets executed after the next request action, which might depend on the state being updated. Maybe my approach is wrong, but that's kinda what I'm struggling with. example usecase: BootAppRequest: step one: get the logged in user data from the db step two: update the store with the user data step three: This step is new dispatched action. request some other user data from API which depends on user data being in the store. as you can see from the usecase above the sequence of actions is important. I could execute step three in a function, but it's being used in several places in the UI and for good code reusability it makes more sense to be a dispatched action. I really want the request action to get handled on a background thread so I don't potentially impact the UI thread Anyway any advice would be helpful.
j
Out of curiousity, how are you setting up a proper coroutineScope for that?
a
I tried it a few different ways, I’ve done a new singlethreaded dispatcher, as well as Dispatcher.Unconfined. like
CoroutineScope(Job() +  Dispatcher.Unconfined)
or sub in my new singlethread dispatcher for Dispatcher.Unconfined.