Skip to content

Instantly share code, notes, and snippets.

@rohozhnikoff
Created June 14, 2018 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rohozhnikoff/435fcdc70bf6dcafd9f3498dc49a7bda to your computer and use it in GitHub Desktop.
Save rohozhnikoff/435fcdc70bf6dcafd9f3498dc49a7bda to your computer and use it in GitHub Desktop.
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