In an Angular 7 Component, I use the RxJS takeUntil() to properly unsubscribe on observable subscriptions.
- What happens when the
this.destroy$.next()
is missing in the methodngOnDestroy
(see sample below)? Will it still unsubscribe properly? - What happens when the
this.destroy$.complete()
is missing in the methodngOnDestroy
(see sample below)? Will it still unsubscribe properly? - Is there any way to enforce that pattern with takeUntil() to unsubscribe is properly used (e.g. tslint rule, npm package)?
@Component({
selector: 'app-flights',
templateUrl: './flights.component.html'
})
export class FlightsComponent implements OnDestroy, OnInit {
private readonly destroy$ = new Subject();
public flights: FlightModel[];
constructor(private readonly flightService: FlightService) { }
ngOnInit() {
this.flightService.getAll()
.pipe(takeUntil(this.destroy$))
.subscribe(flights => this.flights = flights);
}
ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}
}