useUniqueMemo.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = void 0;
  7. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  8. var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  9. var _react = _interopRequireDefault(require("react"));
  10. const BEAT_LIMIT = 1000 * 60 * 10;
  11. /**
  12. * A helper class to map keys to values.
  13. * It supports both primitive keys and object keys.
  14. */
  15. let ArrayKeyMap = /*#__PURE__*/function () {
  16. function ArrayKeyMap() {
  17. (0, _classCallCheck2.default)(this, ArrayKeyMap);
  18. this.map = new Map();
  19. // Use WeakMap to avoid memory leak
  20. this.objectIDMap = new WeakMap();
  21. this.nextID = 0;
  22. this.lastAccessBeat = new Map();
  23. // We will clean up the cache when reach the limit
  24. this.accessBeat = 0;
  25. }
  26. return (0, _createClass2.default)(ArrayKeyMap, [{
  27. key: "set",
  28. value: function set(keys, value) {
  29. // New set will trigger clear
  30. this.clear();
  31. // Set logic
  32. const compositeKey = this.getCompositeKey(keys);
  33. this.map.set(compositeKey, value);
  34. this.lastAccessBeat.set(compositeKey, Date.now());
  35. }
  36. }, {
  37. key: "get",
  38. value: function get(keys) {
  39. const compositeKey = this.getCompositeKey(keys);
  40. const cache = this.map.get(compositeKey);
  41. this.lastAccessBeat.set(compositeKey, Date.now());
  42. this.accessBeat += 1;
  43. return cache;
  44. }
  45. }, {
  46. key: "getCompositeKey",
  47. value: function getCompositeKey(keys) {
  48. const ids = keys.map(key => {
  49. if (key && typeof key === 'object') {
  50. return `obj_${this.getObjectID(key)}`;
  51. }
  52. return `${typeof key}_${key}`;
  53. });
  54. return ids.join('|');
  55. }
  56. }, {
  57. key: "getObjectID",
  58. value: function getObjectID(obj) {
  59. if (this.objectIDMap.has(obj)) {
  60. return this.objectIDMap.get(obj);
  61. }
  62. const id = this.nextID;
  63. this.objectIDMap.set(obj, id);
  64. this.nextID += 1;
  65. return id;
  66. }
  67. }, {
  68. key: "clear",
  69. value: function clear() {
  70. if (this.accessBeat > 10000) {
  71. const now = Date.now();
  72. this.lastAccessBeat.forEach((beat, key) => {
  73. if (now - beat > BEAT_LIMIT) {
  74. this.map.delete(key);
  75. this.lastAccessBeat.delete(key);
  76. }
  77. });
  78. this.accessBeat = 0;
  79. }
  80. }
  81. }]);
  82. }();
  83. const uniqueMap = new ArrayKeyMap();
  84. /**
  85. * Like `useMemo`, but this hook result will be shared across all instances.
  86. */
  87. function useUniqueMemo(memoFn, deps) {
  88. return _react.default.useMemo(() => {
  89. const cachedValue = uniqueMap.get(deps);
  90. if (cachedValue) {
  91. return cachedValue;
  92. }
  93. const newValue = memoFn();
  94. uniqueMap.set(deps, newValue);
  95. return newValue;
  96. }, deps);
  97. }
  98. var _default = exports.default = useUniqueMemo;