I'd like to implement "scheduler inheritance" as part of an RxJava2-using API. I want consumers of my API to be able to think in terms of building a single processing chain rather than a DAG, even though, internally, new events are being teed in as an implementation detail.
I don't see any way to do the equivalent of:
observable
.flatMap {
val scheduler = Schedulers().current!!
someOtherObservable
.observeOn(scheduler)
}
Is there some other way to inherit a scheduler?
More Context
I have a pipeline like:
compositeDisposable += Environment
.lookupDeviceInfo()
.subscribeOn(scheduler)
.flatMap { deviceInfo ->
Device(deviceId = deviceInfo.id)
.sendCommand()
.subscribe(
{ result -> /*process result*/ },
{ e -> /*log error*/ })
To the consumer, this looks like they pushed all the work onto the specified scheduler
: events from lookupDeviceInfo()
get vectored to a worker from that scheduler, and they expect to stick on that worker.
In practice, they have a bug, because sendCommand()
tees in events from another event source as an implementation detail:
sendMessageSingle(deviceId, payload)
.flatMap { sentMessageId ->
responseObservable
.filter { it.messageId == sentMessageId }
.firstOrError()
}
Events stream in from responseObservable
, but none of those events get vectored to the specified scheduler
, because that got applied upstream.