I have a list of observables obs1, obs2, obs3,...,
Each of them can emit a number of items (from mongodb database), I am interested only in the first N items. I want to make sure that queries of my observables are executed only if required. In other words, if obs1, for example, produce more than N, the query behind obs2 should not run, etc.
If I use concat: Observable(obs1, obs2, obs3, ...).concat, all of the queries can run in parallel in mongodb
Basically, I am looking for an operation like obs1.switchIfX(obs2).switchIfX(obs3).....
Where X: less than N items are emitted by current observable.
Any idea how I can implement this requirement in rxscala style?
You could try something like this (untested):
The
maxConcurrentargument ensures thatflattenonly subscribes to one observable at a time, and onceNitems have been emitted,takewill unsubscribe from the upstream observable, so if at that point,obs2orobs3have not yet been subscribed to, they will never be run, as desired.