Last active
November 24, 2020 14:17
-
-
Save tomeustace/5a5195eb79dfec3d9f2a227e18f38e26 to your computer and use it in GitHub Desktop.
Typescript method decorator with args
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
/** | |
* Decorator to ensure widgets unsubscribe and clean up subscriptions on each new subscription. | |
*/ | |
export function Unsubscribe<T>(): Function { | |
return function(targetClass, functionName: string, descriptor): Function { | |
const source = descriptor.value; | |
descriptor.value = function(...args): void { | |
if (this.dataSubscription) { | |
this.dataSubscription.unsubscribe(); | |
} | |
return source.apply(this, args); | |
} | |
return descriptor; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment