Skip to content

Instantly share code, notes, and snippets.

@eyeahs
Forked from JoseAlcerreca/EventObserver.kt
Created November 1, 2018 13:51
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 eyeahs/5a895103d76cf2669bd309c951830eff to your computer and use it in GitHub Desktop.
Save eyeahs/5a895103d76cf2669bd309c951830eff to your computer and use it in GitHub Desktop.
An Observer for Events, simplifying the pattern of checking if the Event's content has already been handled.
/**
* An [Observer] for [Event]s, simplifying the pattern of checking if the [Event]'s content has
* already been handled.
*
* [onEventUnhandledContent] is *only* called if the [Event]'s contents has not been handled.
*/
class EventObserver<T>(private val onEventUnhandledContent: (T) -> Unit) : Observer<Event<T>> {
override fun onChanged(event: Event<T>?) {
event?.getContentIfNotHandled()?.let { value ->
onEventUnhandledContent(value)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment