Hi i need some guidance on how to define this extension function to in my Android base ViewModel so ...
t
Hi i need some guidance on how to define this extension function to in my Android base ViewModel so that I only have to define the actor parameters once (e.g.
actor(start = CoroutineStart.LAZY, context = actorJob, capacity = Channel.UNLIMITED)
) and simply pass a block of code from each of my modules ViewModels that extend my base ViewModel...
currently I have multiple modules each with their own ViewModel and they all define an actor as follows:-
Copy code
val reduceChannel: SendChannel<Actionable> = viewModelScope.actor(start = CoroutineStart.LAZY, context = actorJob, capacity = Channel.UNLIMITED) {
    for (action in channel) {
        when (action) {
            is ModuleN_FirstAction -> doActionOne() // Suspending function

            is ModuleN_SecondAction -> doActionTwo()  // Suspending function
            is ModuleN_ThirdAction -> doActionThree()  // Suspending function
            ... // More of the same, different for each module
        }
    }
}
what I would like to refactor is that my Base ViewModel defines the
Copy code
viewModelScope.actor(start = CoroutineStart.LAZY, context = actorJob, capacity = Channel.UNLIMITED) { eachModuleCodeBlock() }
once and i simply pass each modules code block to this base function
So far I have this in my base viewModel
Copy code
fun frank(reducer: (Actionable) -> Unit) = viewModelScope.actor(start = CoroutineStart.LAZY, context = actorJob, capacity = Channel.UNLIMITED) {
    for (action in channel) {
        reducer(action)
    }
}
however when I call frank as follows:-
Copy code
frank { action ->
    when (action) {
            is ModuleN_FirstAction -> doActionOne() // Suspending function

            is ModuleN_SecondAction -> doActionTwo()  // Suspending function
            is ModuleN_ThirdAction -> doActionThree()  // Suspending function
            ... // More of the same, different for each module
        }
}
the compliler complains "Suspension functions can only be called within coroutine body how can I achive the desired result?
z
add
suspend
to reducer:
fun frank(reducer: suspend (Actionable) -> Unit)