useSingletonCache.js 613 B

1234567891011121314151617
  1. import * as React from 'react';
  2. import isEqual from "rc-util/es/isEqual";
  3. /**
  4. * Singleton cache will only take latest `cacheParams` as key
  5. * and return the result for callback matching.
  6. */
  7. export default function useSingletonCache() {
  8. const cacheRef = React.useRef([null, null]);
  9. const getCache = (cacheKeys, callback) => {
  10. const filteredKeys = cacheKeys.map(item => item instanceof HTMLElement || Number.isNaN(item) ? '' : item);
  11. if (!isEqual(cacheRef.current[0], filteredKeys)) {
  12. cacheRef.current = [filteredKeys, callback()];
  13. }
  14. return cacheRef.current[1];
  15. };
  16. return getCache;
  17. }