Created
February 25, 2019 16:20
-
-
Save nmccready/1ef5df1d0f455ecad5b2ce99f8d32f8b to your computer and use it in GitHub Desktop.
Lodash memoizeDebounce and throttle extensions
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
import { debounce, throttle } from 'lodash'; | |
import memoize from 'memoizee'; | |
// Memoize to use unique args, and debounce to only get the last call | |
// https://github.com/lodash/lodash/issues/2403#issuecomment-290760787 | |
export const memoizeDebounce = (func, wait = 0, options = {}) => { | |
const { leading, maxWait, trailing, ...memoizeOptions } = options; | |
const mem = memoize( | |
() => debounce(func, wait, { leading, maxWait, trailing }), | |
memoizeOptions | |
); | |
return (...args) => { | |
mem(...args)(...args); | |
}; | |
}; | |
// Memoize to use unique args, and debounce to only get the last call | |
// https://github.com/lodash/lodash/issues/2403#issuecomment-290760787 | |
export const memoizeThrottle = (func, wait = 0, options = {}) => { | |
const { leading, trailing, ...memoizeOptions } = options; | |
const mem = memoize( | |
() => throttle(func, wait, { leading, trailing }), | |
memoizeOptions | |
); | |
return (...args) => { | |
mem(...args)(...args); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment