Skip to content

Instantly share code, notes, and snippets.

@christopherbauer
Last active April 30, 2023 22:54
Show Gist options
  • Save christopherbauer/b37379a9dc4f9685df4f64ef7642ba95 to your computer and use it in GitHub Desktop.
Save christopherbauer/b37379a9dc4f9685df4f64ef7642ba95 to your computer and use it in GitHub Desktop.
/**
* Class method decorator for counting the number of calls
* @param originalMethod
* @param context
* @returns Decorator function
*/
export const Counter = (
originalMethod: Function,
context: ClassMethodDecoratorContext
) => {
const methodName = String(context.name);
let count = 0;
function ReplacementMethod(this: any, ...args: any[]) {
count++;
console.log(`${methodName} called ${count}x`);
return originalMethod.call(this, ...args);
}
return ReplacementMethod;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment