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 collect the items from the source into a list, check its size in a
flatMap
operator and switch to another source if the length is not enough:If you don't want to get more than N items, you can specify
o.take(n).toList()
instead.