import { OnDestroy } from '@angular/core'; import { Subscription } from 'rxjs'; export abstract class UnsubscribableComponent implements OnDestroy { public set loading(value: boolean) { this._loading = value; } public get loading(): boolean { return this._loading; } /** * Please, look there for more information * https://medium.com/@benlesh/rxjs-dont-unsubscribe-6753ed4fda87 * * Such proposal is more recommended, but for 2-way compability, * the approach with subscribers has been left. * * Approach using isAlive property can be achieved by using the * rxjs#takeWhile(() => this.isAlive); */ public isAlive: boolean = true; protected subscribers: Array = []; private _loading: boolean = false; public ngOnDestroy(): void { this.isAlive = false; this.clearSubscriptions(); } protected clearSubscriptions(): void { for (const subscription of this.subscribers) { if (subscription && !subscription.closed) { subscription.unsubscribe(); } } this.subscribers.length = 0; } }