BlackDice/b3-chief

View on GitHub
src/core/ObjectCache.js

Summary

Maintainability
A
0 mins
Test Coverage
const cacheIdentity = (value) => value

function createObjectCache(factory, getCacheKey = cacheIdentity) {
    const cache = (getCacheKey === cacheIdentity
        ? new WeakMap()
        : new Map()
    )

    const getOrCreateCacheItem = (cacheObject, arg1, arg2, arg3) => {
        const cacheKey = getCacheKey(cacheObject)
        let cacheItem = cache.get(cacheKey)
        if (cacheItem === undefined) {
            cacheItem = factory(cacheObject, arg1, arg2, arg3)
            cache.set(cacheKey, cacheItem)
        }
        return cacheItem
    }

    return getOrCreateCacheItem
}

export default createObjectCache