refactor(memoizeN): extract support code out of closure (#7805)
This commit is contained in:
@@ -7,32 +7,34 @@ import memoize from "lodash/memoize"
|
|||||||
* storing the result based on the arguments provided to the memoized function.
|
* storing the result based on the arguments provided to the memoized function.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const memoizeN = (fn, resolver = ((...args) => args)) => {
|
const shallowArrayEquals = (a) => (b) => {
|
||||||
const shallowArrayEquals = (a) => (b) => {
|
return Array.isArray(a) && Array.isArray(b)
|
||||||
return Array.isArray(a) && Array.isArray(b)
|
&& a.length === b.length
|
||||||
&& a.length === b.length
|
&& a.every((val, index) => val === b[index])
|
||||||
&& a.every((val, index) => val === b[index])
|
}
|
||||||
|
|
||||||
|
const list = (...args) => args
|
||||||
|
|
||||||
|
class Cache extends Map {
|
||||||
|
delete(key) {
|
||||||
|
const keys = Array.from(this.keys())
|
||||||
|
const foundKey = keys.find(shallowArrayEquals(key))
|
||||||
|
return super.delete(foundKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
class Cache extends Map {
|
get(key) {
|
||||||
delete(key) {
|
const keys = Array.from(this.keys())
|
||||||
const keys = Array.from(this.keys())
|
const foundKey = keys.find(shallowArrayEquals(key))
|
||||||
const foundKey = keys.find(shallowArrayEquals(key))
|
return super.get(foundKey)
|
||||||
return super.delete(foundKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
get(key) {
|
|
||||||
const keys = Array.from(this.keys())
|
|
||||||
const foundKey = keys.find(shallowArrayEquals(key))
|
|
||||||
return super.get(foundKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
has(key) {
|
|
||||||
const keys = Array.from(this.keys())
|
|
||||||
return keys.findIndex(shallowArrayEquals(key)) !== -1
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
has(key) {
|
||||||
|
const keys = Array.from(this.keys())
|
||||||
|
return keys.findIndex(shallowArrayEquals(key)) !== -1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const memoizeN = (fn, resolver = list) => {
|
||||||
const { Cache: OriginalCache } = memoize
|
const { Cache: OriginalCache } = memoize
|
||||||
memoize.Cache = Cache
|
memoize.Cache = Cache
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user