private var fetching: Boolean = false
private fun fetch() {
if(fetching) return
fetching = true
launch {
_fetch something that takes long_
fetching = false
}
}
I often write methods as the one above, that are there to request a long-running action if it is not already runnning. I wondered if there was a construct that could simplify this or something could be implemented to reduce this boilerplate?
Synchronization isn’t an issue for me, since this method will always be called from the same thread.
I think you sort of missed my point, because the idea was not to process incoming things, but to simply request a refresh. If that refresh is already taking place, it should simply return instead of creating a new refresh.
Also: I can’t really find documentation on Actor and Mutex, except for the KDoc in the code.
This is my solution now:
class Refresher(private val runnable: () -> Unit) {
private var refreshing = false
fun refresh() {
if (refreshing) return
refreshing = true
launch {
runnable()
refreshing = false
}
}
}