Skip to content

Instantly share code, notes, and snippets.

@tomeustace
Last active November 24, 2020 14:17
Show Gist options
  • Save tomeustace/5a5195eb79dfec3d9f2a227e18f38e26 to your computer and use it in GitHub Desktop.
Save tomeustace/5a5195eb79dfec3d9f2a227e18f38e26 to your computer and use it in GitHub Desktop.
Typescript method decorator with args
/**
* 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