Reputation: 270
I have an observable which emits some id and i have to make an another subscription which uses the emitted ids. What is the best way to handle this subscription. I know a solution like subscribing the second observable inside the the first observable subscription. Like below -
observable1$.subsctipe(id=>{
this.store.pipe(select(some_selector, id)).subscribe(value=>{
//some business
})
})
Is this solution has any bad effect like memory leaks? And is there any other better way to do this?
Upvotes: 0
Views: 930
Reputation: 6887
The bad effect of this thing is that you have two subscriptions to keep track of. You have to unsubscribe both to clean up.
observable1$.pipe(mergeMap(id =>
this.store.pipe(select(some_selector, id))
).subscribe(value => {
//some business
})
Better do it like this. Then you have only one subscription to release if the component destroys. This is not the case with your current implementation.
I also assume that you dont want to keep listening to your store with the old id
if observable1$
has already emittet a new id
. If thats the case, you can replace mergeMap
with switchMap
. Btw flatMap
is an alias for mergeMap
.
Upvotes: 2