untilDestroy moved to extension

This commit is contained in:
Denis Karmyshakov 2018-07-03 15:00:36 +03:00
parent 42750812dd
commit 448501aa06
3 changed files with 23 additions and 30 deletions

View File

@ -14,3 +14,11 @@ inline fun <T> Delegates.observable(
): ReadWriteProperty<Any?, T> = object : ObservableProperty<T>(initialValue) {
override fun afterChange(property: KProperty<*>, oldValue: T, newValue: T) = onChange(newValue)
}
inline fun <T> Delegates.distinctUntilChanged(
initialValue: T,
crossinline onChange: (newValue: T) -> Unit
): ReadWriteProperty<Any?, T> = object : ObservableProperty<T>(initialValue) {
override fun afterChange(property: KProperty<*>, oldValue: T, newValue: T) =
if (newValue != null && oldValue != newValue) onChange(newValue) else Unit
}

View File

@ -24,51 +24,41 @@ open class BaseDestroyable : Destroyable {
*/
fun onDestroy() = subscriptions.dispose()
override fun <T> untilDestroy(
flowable: Flowable<T>,
override fun <T> Flowable<T>.untilDestroy(
onNextAction: (T) -> Unit,
onErrorAction: (Throwable) -> Unit,
onCompletedAction: () -> Unit
): Disposable = flowable
.observeOn(AndroidSchedulers.mainThread())
): Disposable = observeOn(AndroidSchedulers.mainThread())
.subscribe(onNextAction, onErrorAction, onCompletedAction)
.also { subscriptions.add(it) }
override fun <T> untilDestroy(
observable: Observable<T>,
override fun <T> Observable<T>.untilDestroy(
onNextAction: (T) -> Unit,
onErrorAction: (Throwable) -> Unit,
onCompletedAction: () -> Unit
): Disposable = observable
.observeOn(AndroidSchedulers.mainThread())
): Disposable = observeOn(AndroidSchedulers.mainThread())
.subscribe(onNextAction, onErrorAction, onCompletedAction)
.also { subscriptions.add(it) }
override fun <T> untilDestroy(
single: Single<T>,
override fun <T> Single<T>.untilDestroy(
onSuccessAction: (T) -> Unit,
onErrorAction: (Throwable) -> Unit
): Disposable = single
.observeOn(AndroidSchedulers.mainThread())
): Disposable = observeOn(AndroidSchedulers.mainThread())
.subscribe(onSuccessAction, onErrorAction)
.also { subscriptions.add(it) }
override fun untilDestroy(
completable: Completable,
override fun Completable.untilDestroy(
onCompletedAction: () -> Unit,
onErrorAction: (Throwable) -> Unit
): Disposable = completable
.observeOn(AndroidSchedulers.mainThread())
): Disposable = observeOn(AndroidSchedulers.mainThread())
.subscribe(onCompletedAction, onErrorAction)
.also { subscriptions.add(it) }
override fun <T> untilDestroy(
maybe: Maybe<T>,
override fun <T> Maybe<T>.untilDestroy(
onSuccessAction: (T) -> Unit,
onErrorAction: (Throwable) -> Unit,
onCompletedAction: () -> Unit
): Disposable = maybe
.observeOn(AndroidSchedulers.mainThread())
): Disposable = observeOn(AndroidSchedulers.mainThread())
.subscribe(onSuccessAction, onErrorAction, onCompletedAction)
.also { subscriptions.add(it) }

View File

@ -40,8 +40,7 @@ interface Destroyable {
* @param T Type of emitted by observable items;
* @return [Disposable] which is wrapping source observable to unsubscribe from it onDestroy.
*/
fun <T> untilDestroy(
flowable: Flowable<T>,
fun <T> Flowable<T>.untilDestroy(
onNextAction: (T) -> Unit = Functions.emptyConsumer<T>()::accept,
onErrorAction: (Throwable) -> Unit = getActionThrowableForAssertion(Lc.getCodePoint(this, 2)),
onCompletedAction: () -> Unit = Functions.EMPTY_ACTION::run
@ -58,8 +57,7 @@ interface Destroyable {
* @param T Type of emitted by observable items;
* @return [Disposable] which is wrapping source observable to unsubscribe from it onDestroy.
*/
fun <T> untilDestroy(
observable: Observable<T>,
fun <T> Observable<T>.untilDestroy(
onNextAction: (T) -> Unit = Functions.emptyConsumer<T>()::accept,
onErrorAction: (Throwable) -> Unit = getActionThrowableForAssertion(Lc.getCodePoint(this, 2)),
onCompletedAction: () -> Unit = Functions.EMPTY_ACTION::run
@ -76,8 +74,7 @@ interface Destroyable {
* @param T Type of emitted by single items;
* @return [Disposable] which is wrapping source single to unsubscribe from it onDestroy.
*/
fun <T> untilDestroy(
single: Single<T>,
fun <T> Single<T>.untilDestroy(
onSuccessAction: (T) -> Unit = Functions.emptyConsumer<T>()::accept,
onErrorAction: (Throwable) -> Unit = getActionThrowableForAssertion(Lc.getCodePoint(this, 2))
): Disposable
@ -92,8 +89,7 @@ interface Destroyable {
* @param onErrorAction Action which will raise on every [io.reactivex.CompletableEmitter.onError] throwable;
* @return [Disposable] which is wrapping source completable to unsubscribe from it onDestroy.
*/
fun untilDestroy(
completable: Completable,
fun Completable.untilDestroy(
onCompletedAction: () -> Unit = Functions.EMPTY_ACTION::run,
onErrorAction: (Throwable) -> Unit = getActionThrowableForAssertion(Lc.getCodePoint(this, 2))
): Disposable
@ -108,8 +104,7 @@ interface Destroyable {
* @param onErrorAction Action which will raise on every [io.reactivex.MaybeEmitter.onError] throwable;
* @return [Disposable] which is wrapping source maybe to unsubscribe from it onDestroy.
*/
fun <T> untilDestroy(
maybe: Maybe<T>,
fun <T> Maybe<T>.untilDestroy(
onSuccessAction: (T) -> Unit = Functions.emptyConsumer<T>()::accept,
onErrorAction: (Throwable) -> Unit = getActionThrowableForAssertion(Lc.getCodePoint(this, 2)),
onCompletedAction: () -> Unit = Functions.EMPTY_ACTION::run