Skip to content

Instantly share code, notes, and snippets.

@masimplo
Created October 3, 2018 08:10
Show Gist options
  • Save masimplo/01b8f9206b795ba5cf10115c3a362702 to your computer and use it in GitHub Desktop.
Save masimplo/01b8f9206b795ba5cf10115c3a362702 to your computer and use it in GitHub Desktop.
Angular Destroyable component decorator
import { Observable } from 'rxjs/Observable';
export function Destroyable() {
return (target) => {
const originalOnDestroy: Function = target.prototype.ngOnDestroy;
// tslint:disable-next-line:only-arrow-functions
target.prototype.ngOnDestroy = function() {
if (!('_componentDestroyed$' in this || !(this._componentDestroyed$ instanceof Observable))) {
throw new Error('Destroyable components must define _componentDestroyed$ property');
}
if (this._componentDestroyed$) {
this._componentDestroyed$.next();
this._componentDestroyed$.complete();
}
this._componentDestroyed$ = null;
if (originalOnDestroy)
originalOnDestroy.apply(this, arguments);
};
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment