-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add subscribeBy extensions for proxy classes. (#127)
* Add subscribeBy extensions for proxy classes. Unfortunately, since the SubscribeProxy classes don't extend the observable class they proxy to, extension functions written for the observable class can't be used. One commonly used set of extensions is RxKotlin's subscribeBy. This commit adds copies of those extensions that work on SubscribeProxies. * Move extensions to sample. * update in response to review. * Remove wildcard import. * Use block bodies for extensions.
- Loading branch information
Showing
2 changed files
with
109 additions
and
8 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
sample/src/main/kotlin/com/uber/autodispose/recipes/subscriberproxies.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package com.uber.autodispose.recipes | ||
|
||
import com.uber.autodispose.CompletableSubscribeProxy | ||
import com.uber.autodispose.FlowableSubscribeProxy | ||
import com.uber.autodispose.MaybeSubscribeProxy | ||
import com.uber.autodispose.ObservableSubscribeProxy | ||
import com.uber.autodispose.SingleSubscribeProxy | ||
import io.reactivex.disposables.Disposable | ||
import io.reactivex.exceptions.OnErrorNotImplementedException | ||
import io.reactivex.plugins.RxJavaPlugins | ||
|
||
/* | ||
* An example of extension functions on the objects returned by `AutoDispose.with`. | ||
* | ||
* AutoDispose returns proxy objects that don't extend Observable or the other reactive classes. This means | ||
* that extensions like RxKotlin's `Observable.subscribeBy` can't be used. However, it's easy to define your | ||
* own. | ||
* | ||
* These extension functions can be called in the following manner: | ||
* | ||
* ``` | ||
* Observable.just(1) | ||
* .autoDisposeWith(this) | ||
* .subscribeBy(onError = { Log.e(it) }) | ||
* ``` | ||
*/ | ||
|
||
private val onNextStub: (Any) -> Unit = {} | ||
private val onErrorStub: (Throwable) -> Unit = { RxJavaPlugins.onError(OnErrorNotImplementedException(it)) } | ||
private val onCompleteStub: () -> Unit = {} | ||
|
||
/** | ||
* Overloaded subscribe function that allows passing named parameters | ||
*/ | ||
fun <T : Any> ObservableSubscribeProxy<T>.subscribeBy( | ||
onError: (Throwable) -> Unit = onErrorStub, | ||
onComplete: () -> Unit = onCompleteStub, | ||
onNext: (T) -> Unit = onNextStub | ||
): Disposable { | ||
return if (onError === onErrorStub && onComplete === onCompleteStub) { | ||
subscribe(onNext) | ||
} else { | ||
subscribe(onNext, onError, onComplete) | ||
} | ||
} | ||
|
||
/** | ||
* Overloaded subscribe function that allows passing named parameters | ||
*/ | ||
fun <T : Any> FlowableSubscribeProxy<T>.subscribeBy( | ||
onError: (Throwable) -> Unit = onErrorStub, | ||
onComplete: () -> Unit = onCompleteStub, | ||
onNext: (T) -> Unit = onNextStub | ||
): Disposable { | ||
return if (onError === onErrorStub && onComplete === onCompleteStub) { | ||
subscribe(onNext) | ||
} else { | ||
subscribe(onNext, onError, onComplete) | ||
} | ||
} | ||
|
||
/** | ||
* Overloaded subscribe function that allows passing named parameters | ||
*/ | ||
fun <T : Any> SingleSubscribeProxy<T>.subscribeBy( | ||
onError: (Throwable) -> Unit = onErrorStub, | ||
onSuccess: (T) -> Unit = onNextStub | ||
): Disposable { | ||
return if (onError === onErrorStub) { | ||
subscribe(onSuccess) | ||
} else { | ||
subscribe(onSuccess, onError) | ||
} | ||
} | ||
|
||
/** | ||
* Overloaded subscribe function that allows passing named parameters | ||
*/ | ||
fun <T : Any> MaybeSubscribeProxy<T>.subscribeBy( | ||
onError: (Throwable) -> Unit = onErrorStub, | ||
onComplete: () -> Unit = onCompleteStub, | ||
onSuccess: (T) -> Unit = onNextStub | ||
): Disposable { | ||
return if (onError === onErrorStub && onComplete === onCompleteStub) { | ||
subscribe(onSuccess) | ||
} else { | ||
subscribe(onSuccess, onError, onComplete) | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Overloaded subscribe function that allows passing named parameters | ||
*/ | ||
fun CompletableSubscribeProxy.subscribeBy( | ||
onError: (Throwable) -> Unit = onErrorStub, | ||
onComplete: () -> Unit = onCompleteStub | ||
): Disposable { | ||
return if (onError === onErrorStub) { | ||
subscribe(onComplete) | ||
} else { | ||
subscribe(onComplete, onError) | ||
} | ||
} |
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