angular rxjs subscription teardown

Renaud

There are different patterns for tearing down subscriptions in angular components. I've been using the Subject / takeUntil pattern, in this form:

export class FooComponent implements OnDestroy {
  private destroy$ = new Subject<void>();
...
  source$.pipe(takeUntil(this.destroy$)).subscribe(...);
...
  ngOnDestroy() {
    this.destroy$.next();
  }
}

Today I've been told to add another line to the ngOnDestroy:

this.destroy$.complete();

After doing a bit of research, it does show up on some online examples and not in others, but either way I'm failing to see the point. Looking at the source code of Subject.complete it will loop through any inner observers and complete these. Assuming we don't subscribe to the destroy$ subject directly, isn't this list of inner observers always supposed to remain empty? If that's the case isn't that call to this.destroy$.complete() redundant?

martin

You don't need to call complete() if you're using destroy$ only for takeUntil() operators.

When destroy$ emits next takeUntil() will complete the chain that will also unsubscribe from destroy$ and thus there will be no subscribers so complete() should not be necessary.

Btw, I've seen people using complete() in this situation as well. I can't find any example right now but in contrast ngrx/component-store is just calling next().

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Angular + rxjs: No provider for Subscription

Getting Error with RxJs "unrecognized teardown 10 added to Subscription"

Angular Routing and RxJs subscription sharing

Angular RxJS Subscription Reference Value

Angular 10, rxjs - looping on subscription?

Angular RxJS Subject subscription and unsubsciption

Rxjs Subscription does not work in a Angular Directive

Angular rxjs: keep subscription after error reached

Angular listen on subscription start of rxjs observable?

Angular 5 RxJS Subscription Timing Issue

Angular 5 & Rxjs: Wait for all subscription

angular - show loading while rxjs subscription waits for data

Angular RxJS Observable: takeUntil vs. unsubscribe with a Subscription

Angular/RxJs When should I unsubscribe from `Subscription`

Type 'Observable<Subscription>' is not assignable to type Observable<MyData> rxjs angular

Angular6 rxjs unsubscribing to timer subscription OnDestroy does not work?

RxJs Subscription does not get fired in the root angular module

Angular2 + RxJS BehaviorSubject subscription not working on all components

Angular 7 Rxjs - In-Flight HTTP request not cancelling for subscription

A better code maintenance for identical subscription handling code [Angular/Rxjs]

RxJS, Angular: Cannot receive result of subscription in other methods

What's the problem in that Angular 7 rxjs subscription from a resolver

Angular rxjs: Delay subscription till other Observable emits

RxJs / Angular: Fetch additional data via subscription inside a map loop

Error method on Angular rxjs subscription not beeing executed at all

Debounce and buffer an rxjs subscription

Rxjs subscription queue

RxJS Subscription and code execution in it

rxjs subscription returning duplicates