Probably an rx newbee question.
If I have two rx streams, which have some common parts, is there a possibility to extract and to reuse them?
Here is pseudo code example:
someManager.getInfo(id) returns a Single<SometInfo>
This fun takes an id and should return just a single with status
fun getStatus(id: String): Single<Status> =
someManager.getInfo(id)
.flattenAsObservable { it.items }
.map { getSomeProp(it) } // from here
.toList()
.map { getStatus(it) }
.doOnSuccess { getStatusRelay(id).accept(it) } // until here it's common
and this one doesn't take an id but already an object and should check if a status is ok
fun isStatusOk(info: SomeInfo): Single<Boolean> =
Observable.fromIterable(info.items)
.map { getSomeProp(it) } // from here
.toList()
.map { getStatus(it) }
.doOnSuccess { getStatusRelay(id).accept(it) } // until here it's common
.map { it == Status.OK }
Thank you in advance
Phoenix Wang suggested Transformers. I tried them and after some experiments it seems I got it working.
I created a SingleTransformer:
And then I can refactor
and this too
Hopefully it helps someone else.