Created
March 14, 2021 02:34
-
-
Save gpeal/7505f6308e20a0dd59c2fd2141e19159 to your computer and use it in GitHub Desktop.
Unique Observable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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