Skip to content

Instantly share code, notes, and snippets.

@emartynov
Created April 5, 2018 08:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emartynov/bf5aeb1828def440db34e858217e965d to your computer and use it in GitHub Desktop.
Save emartynov/bf5aeb1828def440db34e858217e965d to your computer and use it in GitHub Desktop.
inline fun completable(
crossinline action: () -> Unit,
crossinline finally: () -> Unit
): Completable {
return Completable.create { emitter ->
try {
action()
emitter.onComplete()
} catch (t: Throwable) {
// Attempts to emit the specified {@code Throwable} error if the downstream
// hasn't cancelled the sequence or is otherwise terminated, returning false
// if the emission is not allowed to happen due to lifecycle restrictions.
// <p>
// Unlike {@link #onError(Throwable)}, the {@code RxJavaPlugins.onError} is not called
// if the error could not be delivered.
emitter.tryOnError(t)
} finally {
finally()
}
}
}
@Test
fun `Completable do not crash when terminated`() {
var ex: Throwable? = null
RxJavaPlugins.setErrorHandler { t -> ex = t }
Completable.mergeArray(
completable {
throw IOException("test1")
}.subscribeOn(Schedulers.io()),
completable {
throw IOException("test2")
}.subscribeOn(Schedulers.io())
).onErrorComplete()
.blockingAwait()
RxJavaPlugins.setErrorHandler(null)
ex?.let {
throw it
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment