Skip to content

Instantly share code, notes, and snippets.

@gpeal
Created March 14, 2021 02:34
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gpeal/7505f6308e20a0dd59c2fd2141e19159 to your computer and use it in GitHub Desktop.
Save gpeal/7505f6308e20a0dd59c2fd2141e19159 to your computer and use it in GitHub Desktop.
Unique Observable
/**
* Like Delegates.observable except it only calls the callback when the value actually changes.
*/
public inline fun <T> uniqueObservable(initialValue: T, emitInitial: Boolean = false, crossinline onChange: (value: T) -> Unit): ReadWriteProperty<Any?, T> {
if (emitInitial) onChange(initialValue)
return object : ObservableProperty<T>(initialValue) {
override fun afterChange(property: KProperty<*>, oldValue: T, newValue: T) {
if (oldValue != newValue) onChange(newValue)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment