Alexander Black
01/08/2022, 12:59 AMoverride 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.Joakim Forslund
01/11/2022, 7:31 PMAlexander Black
01/11/2022, 7:51 PMCoroutineScope(Job() + Dispatcher.Unconfined)
or sub in my new singlethread dispatcher for Dispatcher.Unconfined.