Alexey Demedeckiy
06/13/2020, 7:08 PMclass Store {
private val channel = Channel<Action>()
suspend func dispatch(action: Action) {
channel.send(action)
}
init {
GlobalScope.launch {
for (action in channel) {
reduce(action)
notifyObservers()
}
}
}
}
Sadly, this code doesn't work - actions are not reducer.
Small detail: If i will explicitly set Dispatchers.Main in init and in dispatch - it work, but defeats my purpose.
My goal is to move action processing of main thread. notifyObservers
will build Props
for each active ViewModel
and publish.Zach Klippenstein (he/him) [MOD]
06/13/2020, 7:12 PMactions are not reducedCan you elaborate? Are they not being sent on the channel? Not being received from the channel? Is
reduce
not working as expected?Zach Klippenstein (he/him) [MOD]
06/13/2020, 7:12 PMGlobalScope
.Alexey Demedeckiy
06/13/2020, 7:17 PMdispatch
remains suspended, reduce
never called as nothing was send to the channel.
Why? And what should I use instead? I just want to offload processing from UI thread to not block UI while I calculate next props.vaskir
06/13/2020, 7:21 PMvaskir
06/13/2020, 7:23 PMasync(Dispatchers.Default) { reduce(action) }.await()
Alexey Demedeckiy
06/13/2020, 7:24 PMAlexey Demedeckiy
06/13/2020, 7:25 PMZach Klippenstein (he/him) [MOD]
06/13/2020, 7:26 PMZach Klippenstein (he/him) [MOD]
06/13/2020, 7:26 PMvaskir
06/13/2020, 7:27 PMvaskir
06/13/2020, 7:28 PMZach Klippenstein (he/him) [MOD]
06/13/2020, 7:29 PMvaskir
06/13/2020, 7:29 PMvaskir
06/13/2020, 7:29 PMvaskir
06/13/2020, 7:30 PMAlexey Demedeckiy
06/13/2020, 7:30 PMAlexey Demedeckiy
06/13/2020, 7:31 PMvaskir
06/13/2020, 7:31 PMdispatch
, who calls the Store ctor?)Alexey Demedeckiy
06/13/2020, 7:32 PMdispatch
is called by UI - whenever something happens - new action is sended to dispatch. Store
ctor is created on application start - it is single instacnce per applicationAlexey Demedeckiy
06/13/2020, 7:33 PMAlexey Demedeckiy
06/13/2020, 7:34 PMreduce
change global mutable state, so I need to protect it from parallel mutation - that's why sequential order is a mustZach Klippenstein (he/him) [MOD]
06/13/2020, 7:34 PMchannel.send
function get invoked?Alexey Demedeckiy
06/13/2020, 7:36 PMfunc dispatch(action: Action) {
log("Sending")
channel.send(action)
log("Sended")
}
I see single "Sending" message but not "Sended"Zach Klippenstein (he/him) [MOD]
06/13/2020, 7:40 PMfunc
so I know you're not copy/pasting 😜Alexey Demedeckiy
06/13/2020, 7:41 PMsuspend init
? Should I use MainScope?Zach Klippenstein (he/him) [MOD]
06/13/2020, 7:44 PMprivate val scope = CoroutineScope(Dispatchers.Default)
In your class and use that. That makes it explicit, and also means if you need to ever dispose your Store
class (eg to reset state between tests) you can just cancel that scope.Alexey Demedeckiy
06/13/2020, 7:44 PMZach Klippenstein (he/him) [MOD]
06/13/2020, 7:45 PMAlexey Demedeckiy
06/13/2020, 7:47 PMRobert Jaros
06/13/2020, 7:52 PMAlexey Demedeckiy
06/13/2020, 8:00 PM