Created
May 27, 2019 12:43
-
-
Save gotev/022ec6a7b41a12ddd2270c3fd7320507 to your computer and use it in GitHub Desktop.
Optional Subject
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
import io.reactivex.subjects.BehaviorSubject | |
class Optional<T>(private val value: T? = null) { | |
companion object { | |
fun <T> subject() = BehaviorSubject.createDefault(Optional<T>()) | |
} | |
fun isPresent() = value != null | |
fun value() = value | |
} | |
fun <T> BehaviorSubject<Optional<T>>.currentValue() = value?.value() | |
fun <T> BehaviorSubject<Optional<T>>.isSet() = value?.isPresent() ?: false | |
fun <T> BehaviorSubject<Optional<T>>.clearValue() = onNext(Optional()) | |
fun <T> BehaviorSubject<Optional<T>>.setValue(value: T) = onNext(Optional(value)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Swift: