Skip to content

Instantly share code, notes, and snippets.

@VovaStelmashchuk
Last active December 20, 2018 12:38
Show Gist options
  • Save VovaStelmashchuk/302f2320dd9d11958c324015677fc293 to your computer and use it in GitHub Desktop.
Save VovaStelmashchuk/302f2320dd9d11958c324015677fc293 to your computer and use it in GitHub Desktop.
/**
* The method add default method [handleError] for handle onError evert from the ObservableSource,
* and add disposable from this subscribe to CompositeDisposable [compositeDisposable]
*
* @param onNext - the function you have designed to accept emissions from the ObservableSource
*/
protected fun <T> Observable<T>.safeSubscribe(onNext: (T) -> Unit, onError: (Throwable) -> Unit = ::handleError) {
this.subscribe(onNext, onError)
.addToCompositeDisposable()
}
/**
* The method add default method [handleError] for handle onError evert from the SingleSource,
* and add disposable from this subscribe to CompositeDisposable [compositeDisposable]
*
* @param onNext - the function you have designed to accept emissions from the SingleSource
*/
protected fun <T> Single<T>.safeSubscribe(onNext: (T) -> Unit, onError: (Throwable) -> Unit = ::handleError) {
this.subscribe(onNext, onError)
.addToCompositeDisposable()
}
protected fun Completable.safeSubscribe(onNext: () -> Unit, onError: (Throwable) -> Unit = ::handleError) {
this.subscribe(onNext, onError)
.addToCompositeDisposable()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment