0

I have an application written in typescript using rxjs where im using flatMap to return a list of 5000 observables, but i don't want them all to be subscribed at the same time.

I tried to use concatMap but it lets my application very slow, since it process one by one in sequence.

Im interesed in creating something like a pool of 10 subscriptions where when one ends the other starts, this will keep only 10 subscriptions active at same time.

I imagine i can create a manual control of that but im interested in knowing if there is a better approach, using some other kind of map or strategy.

justcode
  • 1,562
  • 3
  • 14
  • 25
  • why do you need 5000 observables? can you add more details about what you're trying to achieve? – Anas Dec 20 '18 at 19:07
  • @Anas i have a list of ids from wich a need to grab data throught a bluetooth connection. – justcode Dec 20 '18 at 19:09
  • 7
    Try using `mergeMap` - which takes a `concurrent` parameter that limits concurrency. `concatMap` is implemented using `mergeMap` with a concurrency of 1. – cartant Dec 20 '18 at 21:08
  • @cartant thats exactlly what i was looking for. Actually flatMap is an alias to mergeMap. – justcode Dec 21 '18 at 11:12

1 Answers1

3

The tip of @cartant help me find a solution, for the code below you can use flatMap or mergeMap since they are the same.

this.service.getData(id).flatMap(data => {
  return Observable.of(data);
}, 10).do(value => {
  console.log(value);
})

This code is just for passing the idea how to use the concurrent parameter of flatMap/mergeMap. The number 10 is the concurrent parameter.

justcode
  • 1,562
  • 3
  • 14
  • 25