Created
May 24, 2016 10:38
-
-
Save fxck/4453f313e86853bccad89e44bec168ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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