add auto unsubscribe component

This commit is contained in:
Dan Percic 2021-07-29 13:19:01 +03:00
parent 004d821e31
commit 29995f481c
2 changed files with 29 additions and 0 deletions

View File

@ -1,3 +1,4 @@
export * from './lib/common-ui.module';
export * from './lib/buttons/icon-button/icon-button.component';
export * from './lib/buttons/icon-button/icon-button-type.model';
export * from './lib/base/auto-unsubscribe.component';

View File

@ -0,0 +1,28 @@
import { Component, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs';
/**
* Inherit this class when you need to subscribe to observables in your components
*/
@Component({ template: '' })
export abstract class AutoUnsubscribeComponent implements OnDestroy {
private readonly _subscriptions = new Subscription();
/**
* Call this method when you want to subscribe to an observable
* @param subscription - the new subscription to add to subscriptions array
*/
set addSubscription(subscription: Subscription) {
this._subscriptions.closed = false;
this._subscriptions.add(subscription);
}
/**
* This method unsubscribes active subscriptions
* If you implement OnDestroy in a component that inherits AutoUnsubscribeComponent,
* then you must explicitly call super.ngOnDestroy()
*/
ngOnDestroy(): void {
this._subscriptions.unsubscribe();
}
}