Created
March 28, 2019 20:00
-
-
Save tomhermann/6288634bd4b9baa1bba612f272edc95f to your computer and use it in GitHub Desktop.
Debug "Operator" for RxKotlin.
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
package com.zombietank.rx | |
import io.reactivex.* | |
import timber.log.Timber | |
private const val TAG = "rxDebug" | |
private fun log(tag: String) = Timber.tag(tag) | |
fun <T> Observable<T>.debug(tag: String = TAG): Observable<T> { | |
return doOnSubscribe { log(tag).i("Subscribed to ${this.javaClass.simpleName}") } | |
.doOnNext { log(tag).i("On Next: $it") } | |
.doOnError { log(tag).e(it, "Error! ${it.message}") } | |
.doOnComplete { log(tag).i("Completed.") } | |
.doOnDispose { log(tag).i("Disposed.") } | |
} | |
fun <T> Single<T>.debug(tag: String = TAG): Single<T> { | |
return doOnSubscribe { log(tag).i("Subscribed to ${this.javaClass.simpleName}") } | |
.doOnSuccess { log(tag).i("Success: $it") } | |
.doOnError { log(tag).e(it, "Error! ${it.message}") } | |
.doOnDispose { log(tag).i("Disposed.") } | |
} | |
fun <T> Maybe<T>.debug(tag: String = TAG): Maybe<T> { | |
return doOnSubscribe { log(tag).i("Subscribed to ${this.javaClass.simpleName}") } | |
.doOnSuccess { log(tag).i("Success: $it") } | |
.doOnComplete { log(tag).i("Completed.") } | |
.doOnError { log(tag).e(it, "Error! ${it.message}") } | |
.doOnDispose { log(tag).i("Disposed.") } | |
} | |
fun <T> Flowable<T>.debug(tag: String = TAG): Flowable<T> { | |
return doOnSubscribe { log(tag).i("Subscribed to ${this.javaClass.simpleName}") } | |
.doOnNext { log(tag).i("On Next: $it") } | |
.doOnError { log(tag).e(it, "Error! ${it.message}") } | |
.doOnComplete { log(tag).i("Completed.") } | |
.doOnCancel { log(tag).i("Canceled") } | |
.doOnRequest { log(tag).i("Request $it") } | |
} | |
fun Completable.debug(tag: String = TAG): Completable { | |
return doOnSubscribe { log(tag).i("Subscribed to ${this.javaClass.simpleName}") } | |
.doOnComplete { log(tag).i("Completed.") } | |
.doOnError { log(tag).e(it, "Error! ${it.message}") } | |
.doOnDispose { log(tag).i("Disposed.") } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment