add auto unsubscribe component
This commit is contained in:
parent
004d821e31
commit
29995f481c
@ -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';
|
||||
|
||||
28
src/lib/base/auto-unsubscribe.component.ts
Normal file
28
src/lib/base/auto-unsubscribe.component.ts
Normal 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();
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user