Created
June 14, 2018 20:08
-
-
Save rohozhnikoff/435fcdc70bf6dcafd9f3498dc49a7bda 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
export default function memoizeOnce(callback, isChanged) { | |
let firstTime = true; | |
let lastFirstArg, lastRes; | |
return function memoizedOnce() { | |
if (firstTime) { | |
lastRes = callback.apply(null, arguments); | |
lastFirstArg = arguments[0]; | |
firstTime = false; | |
} else if ( | |
typeof isChanged === 'function' | |
? isChanged(arguments[0], lastFirstArg) | |
: arguments[0] !== lastFirstArg | |
) { | |
lastRes = callback.apply(null, arguments); | |
lastFirstArg = arguments[0]; | |
} | |
return lastRes; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment