carlos
carlos

Reputation: 663

Multiple switchmap, should I unsubscribe?

Here an example:

Is this approach correct? Should I unsubscribe in each switchMap? Should I call takeUntil after each switchMap?

this.sharedSrv.postDetail.pipe(
  switchMap(post => {
    if (post) {
      this.hasPost = true;
      this.post = post;
    }

    this.viewedMainComment = null;
    this.viewedSubComments = [];
    return this.userSrv.getUserAsObservable();
  }),
  takeUntil(this.destroyFirstSwitchMap$), 
  switchMap(user => {
    if (user) {
      if (this.post.user.id == user.id) this.isOwnPost = true;
      this.user = user;
      console.log(user);
      return this.postsSrv.getPostInteraction(this.user.id, this.post.id, this.post.user.id);
    }
    return of(null); 
  }),
  takeUntil(this.destroySecondSwitchMap$),
).subscribe( );

Upvotes: 1

Views: 302

Answers (1)

Matthieu Riegler
Matthieu Riegler

Reputation: 54599

Yes, one takeUntil will end all subscriptions in cascade.

Upvotes: 1

Related Questions