Created
October 22, 2019 09:48
-
-
Save bodia-uz/51036e6f6a2128edbb3bc09c1d367a28 to your computer and use it in GitHub Desktop.
withLogger
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
function mPatch(object, fnProperty, { onBefore, onAfter }) { | |
const originFn = object[fnProperty]; | |
object[fnProperty] = (...args) => { | |
if (typeof onBefore === 'function') { | |
onBefore(...args); | |
} | |
const returnValue = originFn(...args); | |
if (typeof onAfter === 'function') { | |
onAfter(returnValue); | |
} | |
return returnValue; | |
}; | |
return () => { | |
object[fnProperty] = originFn; | |
}; | |
} | |
function withLogger(object, fnProperty) { | |
return mPatch(object, fnProperty, { | |
onBefore: args => console.log(`${fnProperty} args:`, args), | |
onAfter: returnValue => console.log(`${fnProperty} returnValue`, returnValue) | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment