UNPKG

recoil

Version:

Recoil - A state management library for React

78 lines (69 loc) 2.07 kB
/** * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @emails oncall+recoil * @flow strict-local * @format */ 'use strict'; import type { CachePolicy, EqualityPolicy, EvictionPolicy, } from './Recoil_CachePolicy'; import type {TreeCacheImplementation} from './Recoil_TreeCacheImplementationType'; const {TreeCache} = require('./Recoil_TreeCache'); const treeCacheLRU = require('./Recoil_treeCacheLRU'); const err = require('recoil-shared/util/Recoil_err'); const nullthrows = require('recoil-shared/util/Recoil_nullthrows'); const stableStringify = require('recoil-shared/util/Recoil_stableStringify'); const defaultPolicy = { equality: 'reference', eviction: 'keep-all', maxSize: Infinity, }; function treeCacheFromPolicy<T>( { equality = defaultPolicy.equality, eviction = defaultPolicy.eviction, maxSize = defaultPolicy.maxSize, }: CachePolicy = defaultPolicy, name?: string, ): TreeCacheImplementation<T> { const valueMapper = getValueMapper(equality); return getTreeCache(eviction, maxSize, valueMapper, name); } function getValueMapper(equality: EqualityPolicy): mixed => mixed { switch (equality) { case 'reference': return val => val; case 'value': return val => stableStringify(val); } throw err(`Unrecognized equality policy ${equality}`); } function getTreeCache<T>( eviction: EvictionPolicy, maxSize: ?number, mapNodeValue: mixed => mixed, name?: string, ): TreeCacheImplementation<T> { switch (eviction) { case 'keep-all': // $FlowFixMe[method-unbinding] return new TreeCache<T>({name, mapNodeValue}); case 'lru': return treeCacheLRU<T>({ name, maxSize: nullthrows(maxSize), mapNodeValue, }); case 'most-recent': return treeCacheLRU<T>({name, maxSize: 1, mapNodeValue}); } throw err(`Unrecognized eviction policy ${eviction}`); } module.exports = treeCacheFromPolicy;