Unsubscribe techniques draft

Last modified 15 days ago / Edit on Github
Warning
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 => {...});