CacheMap.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
  2. import _createClass from "@babel/runtime/helpers/esm/createClass";
  3. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  4. // Firefox has low performance of map.
  5. var CacheMap = /*#__PURE__*/function () {
  6. function CacheMap() {
  7. _classCallCheck(this, CacheMap);
  8. _defineProperty(this, "maps", void 0);
  9. // Used for cache key
  10. // `useMemo` no need to update if `id` not change
  11. _defineProperty(this, "id", 0);
  12. _defineProperty(this, "diffRecords", new Map());
  13. this.maps = Object.create(null);
  14. }
  15. _createClass(CacheMap, [{
  16. key: "set",
  17. value: function set(key, value) {
  18. // Record prev value
  19. this.diffRecords.set(key, this.maps[key]);
  20. this.maps[key] = value;
  21. this.id += 1;
  22. }
  23. }, {
  24. key: "get",
  25. value: function get(key) {
  26. return this.maps[key];
  27. }
  28. /**
  29. * CacheMap will record the key changed.
  30. * To help to know what's update in the next render.
  31. */
  32. }, {
  33. key: "resetRecord",
  34. value: function resetRecord() {
  35. this.diffRecords.clear();
  36. }
  37. }, {
  38. key: "getRecord",
  39. value: function getRecord() {
  40. return this.diffRecords;
  41. }
  42. }]);
  43. return CacheMap;
  44. }();
  45. export default CacheMap;