Last active
April 30, 2023 22:54
-
-
Save christopherbauer/b37379a9dc4f9685df4f64ef7642ba95 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* 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