Unsubscribe techniques draft

Last modified 2 years ago / Edit on Github
Danger icon
The last modifications of this post were around 2 years ago, some information may be outdated!
Danger icon
This is a draft, the content is not complete and of poor quality!

For HTTP Calls, no need to unsubscribe.

Using a list subscriptions

import { Subscription } from 'rxjs';
private subscriptions: Subscription[] = [];
this.subscriptions.push(
// subscriptions
);
ngOnDestroy(): void {
this.subscriptions.forEach((subscription: Subscription) => subscription.unsubscribe());
}

Using takeUntil, takeWhile

👉 takeUntil - Learn RxJS
👉takeWhile - Learn RxJS

import { debounceTime, distinctUntilChanged, filter, takeUntil } from 'rxjs/operators';
private destroy$ = new Subject();
ngOnDestroy(){
this.destroy$.next();
this.destroy$.complete();
}
// someInput: FormControl = new FormControl('');
this.someInput.valueChanges
.pipe(
debounceTime(1000),
distinctUntilChanged(),
takeUntil(this.destroy$)
).subscribe(val => {...});

💬 Comments

Support Thi Support Thi