Skip to content

Instantly share code, notes, and snippets.

@DjangoLC
Created November 30, 2020 04:05
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 DjangoLC/e2c8f7e096a827db5aef974e97e87130 to your computer and use it in GitHub Desktop.
Save DjangoLC/e2c8f7e096a827db5aef974e97e87130 to your computer and use it in GitHub Desktop.
open class Event<out T>(private val content: T) {
var hasBeenHandled = false
private set // Allow external read but not write
/**
* Returns the content and prevents its use again.
*/
fun getContentIfNotHandled(): T? {
return if (hasBeenHandled) {
null
} else {
hasBeenHandled = true
content
}
}
/**
* Returns the content, even if it's already been handled.
*/
fun peekContent(): T = content
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment