refactor(memoize): get rid of memoizee prod dependency (#7799)
The memoizee dependency was replaced with specialization of lodash.memoize. Refs #7694
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { objectify, isFunc, normalizeArray, deeplyStripKey } from "core/utils"
|
||||
import XML from "xml"
|
||||
import memoizee from "memoizee"
|
||||
import isEmpty from "lodash/isEmpty"
|
||||
import { objectify, isFunc, normalizeArray, deeplyStripKey } from "core/utils"
|
||||
|
||||
import memoizeN from "../../../helpers/memoizeN"
|
||||
|
||||
const primitives = {
|
||||
"string": () => "string",
|
||||
@@ -602,6 +603,8 @@ export const createXMLExample = (schema, config, o) => {
|
||||
export const sampleFromSchema = (schema, config, o) =>
|
||||
sampleFromSchemaGeneric(schema, config, o, false)
|
||||
|
||||
export const memoizedCreateXMLExample = memoizee(createXMLExample)
|
||||
const resolver = (arg1, arg2, arg3) => [arg1, JSON.stringify(arg2), JSON.stringify(arg3)]
|
||||
|
||||
export const memoizedSampleFromSchema = memoizee(sampleFromSchema)
|
||||
export const memoizedCreateXMLExample = memoizeN(createXMLExample, resolver)
|
||||
|
||||
export const memoizedSampleFromSchema = memoizeN(sampleFromSchema, resolver)
|
||||
|
||||
46
src/helpers/memoizeN.js
Normal file
46
src/helpers/memoizeN.js
Normal file
@@ -0,0 +1,46 @@
|
||||
import memoize from "lodash/memoize"
|
||||
|
||||
/**
|
||||
* This function is extension on top of lodash.memoize.
|
||||
* It uses all the arguments of the `fn` as the cache key instead of just the first one.
|
||||
* If resolver is provided, it determines the cache key for
|
||||
* storing the result based on the arguments provided to the memoized function.
|
||||
*/
|
||||
|
||||
const memoizeN = (fn, resolver = ((...args) => args)) => {
|
||||
const shallowArrayEquals = (a) => (b) => {
|
||||
return Array.isArray(a) && Array.isArray(b)
|
||||
&& a.length === b.length
|
||||
&& a.every((val, index) => val === b[index])
|
||||
}
|
||||
|
||||
class Cache extends Map {
|
||||
delete(key) {
|
||||
const keys = Array.from(this.keys())
|
||||
const foundKey = keys.find(shallowArrayEquals(key))
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
const { Cache: OriginalCache } = memoize
|
||||
memoize.Cache = Cache
|
||||
|
||||
const memoized = memoize(fn, resolver)
|
||||
|
||||
memoize.Cache = OriginalCache
|
||||
|
||||
return memoized
|
||||
}
|
||||
|
||||
export default memoizeN
|
||||
Reference in New Issue
Block a user