Tobias Preuss
01/27/2024, 9:53 PMViewModel
private extensions function into a public, standalone function which can be used by all ViewModels in a project?
private fun <E> SendChannel<E>.sendOneTimeEvent(event: E) {
viewModelScope.launch {
send(event)
}
}
I thought, it might work by leveraging the experimental context receivers feature -Xcontext-receivers
.
context(ViewModel)
fun <E> SendChannel<E>.sendOneTimeEvent(event: E) {
viewModelScope.launch {
send(event)
}
}
But this raises an error at call site:
> No required context receiver found: Cxt { context(androidx.lifecycle.ViewModel) public fun E kotlinx.coroutines.channels.SendChannelE.sendOneTimeEvent(event: E)Dominaezzz
01/28/2024, 11:05 PMlaunch
just for a send
seems incorrect to me. Why not just have an unbounded channel and use trySend
?Dominaezzz
01/28/2024, 11:05 PMviewModelScope
.