Skip to content

Instantly share code, notes, and snippets.

@fxck
Created May 24, 2016 10:38
Show Gist options
  • Save fxck/4453f313e86853bccad89e44bec168ce to your computer and use it in GitHub Desktop.
Save fxck/4453f313e86853bccad89e44bec168ce to your computer and use it in GitHub Desktop.
const DESTROY = 'ngOnDestroy';
export function Lifecycle(name?: string): PropertyDecorator {
return function(target, prop: string) {
const namex = !name ? prop.slice(0, -1) : name;
const handler = target[namex];
const onDestroy = target[DESTROY];
if (namex !== DESTROY) {
Object.defineProperty(target, DESTROY, {
configurable: true,
enumerable: true,
writable: true,
value: function(change) {
if (onDestroy) {
onDestroy.call(this, change);
}
this[prop].complete();
}
});
}
Object.defineProperty(target, name, {
configurable: true,
enumerable: true,
writable: true,
value: function(change) {
if (handler) {
handler.call(this, change);
}
let p = this[prop];
if (namex !== DESTROY) {
p.next();
} else {
p.next();
p.complete();
}
}
});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment