One of the things I've come to appreciate about Reactive Streams is the ability to catch a cancel event and handle it appropriately in your stream. A cancel event can be a timeout or a manual disposal of your async process, but whatever the case you are given the opportunity to deal with it.
fun doLongRunning(): Single<Result> {
return Single.create { emitter ->
val longProcess = /* whatever */
emitter.onSuccess(longProcess_result)
emitter.setCancellable {
cleanup(longProcess)
}
}
}
doLongRunning()
.timeout(5_secs)
.subscribe({ result ->
}, { error ->
})