Created
March 29, 2019 09:24
-
-
Save leontastic/6187abde255bd5f93dfef8996d8080d7 to your computer and use it in GitHub Desktop.
memoizer that only remembers the most recent input
This file contains 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
// memoizer that only remembers the most recent input | |
const memoizeOne = <Arguments extends any[], Output = any>( | |
fn: (...args: Arguments) => Output, | |
resolver = (...args: Arguments) => args[0], | |
) => { | |
let resolution: any; | |
let value: Output; | |
return (...args: Arguments) => { | |
const newResolution = resolver(...args); | |
if (!resolution || resolution !== newResolution) { | |
resolution = newResolution; | |
value = fn(...args); | |
} | |
return value; | |
}; | |
}; | |
export default memoizeOne; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment