I am struggling a bit with some fundamentals I think. I am wondering what is a proper solution for the following:
I am creating some sort of Redux architecture, and am now at the part where I am handling effects. These effects should be handled in separate coroutines. Now, the question is as follows. I can implement my
EffectDispatcher
two ways:
// as a suspending function
interface EffectDispatcher<E> {
suspend fun dispatchEffect(effect: E)
}
// return a job
interface EffectDispatcher<E> {
val scope: CoroutineScope
fun dispatchEffect(effect: E): Job
}
As far as I understand:
In the suspending case (1st) it is the responsibility of the callee to provide a coroutinescope, and in the case of the 2nd the EffectDispatcher itself has a scope in which the effects launch and the callee gets a reference to the dispatched effect job, maybe to cancel it or something, or can cancel every running job with
scope
.
Is this correct? As my preference goes I'd go for the 2nd option.
EDIT:
Option number 3 would be to have
EffectDispatcher<E>
implement
CoroutineScope
. I like that one best actually.