Skip to content

Instantly share code, notes, and snippets.

@mrmitew
Created August 5, 2018 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrmitew/6b6b70e231fa40dbf0e16bffc1590974 to your computer and use it in GitHub Desktop.
Save mrmitew/6b6b70e231fa40dbf0e16bffc1590974 to your computer and use it in GitHub Desktop.
Set a click listener on a view, backed up by an actor that discards all clicks until the suspending action returns
/**
* Set a click listener on a view and back it up by an actor that allows
* execution of a single action block at a time. Subsequent requests will be discarded until action returns.
*/
fun View.setOnRendezvousClickListener(action: suspend (View) -> Unit) {
val eventActor = actor<View>(UI, capacity = 0) {
for (event in channel) action(event)
}
setOnClickListener {
eventActor.offer(it)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment