ThemeCache.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
  2. import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
  3. import _createClass from "@babel/runtime/helpers/esm/createClass";
  4. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  5. // ================================== Cache ==================================
  6. export function sameDerivativeOption(left, right) {
  7. if (left.length !== right.length) {
  8. return false;
  9. }
  10. for (var i = 0; i < left.length; i++) {
  11. if (left[i] !== right[i]) {
  12. return false;
  13. }
  14. }
  15. return true;
  16. }
  17. var ThemeCache = /*#__PURE__*/function () {
  18. function ThemeCache() {
  19. _classCallCheck(this, ThemeCache);
  20. _defineProperty(this, "cache", void 0);
  21. _defineProperty(this, "keys", void 0);
  22. _defineProperty(this, "cacheCallTimes", void 0);
  23. this.cache = new Map();
  24. this.keys = [];
  25. this.cacheCallTimes = 0;
  26. }
  27. _createClass(ThemeCache, [{
  28. key: "size",
  29. value: function size() {
  30. return this.keys.length;
  31. }
  32. }, {
  33. key: "internalGet",
  34. value: function internalGet(derivativeOption) {
  35. var _cache2, _cache3;
  36. var updateCallTimes = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  37. var cache = {
  38. map: this.cache
  39. };
  40. derivativeOption.forEach(function (derivative) {
  41. if (!cache) {
  42. cache = undefined;
  43. } else {
  44. var _cache;
  45. cache = (_cache = cache) === null || _cache === void 0 || (_cache = _cache.map) === null || _cache === void 0 ? void 0 : _cache.get(derivative);
  46. }
  47. });
  48. if ((_cache2 = cache) !== null && _cache2 !== void 0 && _cache2.value && updateCallTimes) {
  49. cache.value[1] = this.cacheCallTimes++;
  50. }
  51. return (_cache3 = cache) === null || _cache3 === void 0 ? void 0 : _cache3.value;
  52. }
  53. }, {
  54. key: "get",
  55. value: function get(derivativeOption) {
  56. var _this$internalGet;
  57. return (_this$internalGet = this.internalGet(derivativeOption, true)) === null || _this$internalGet === void 0 ? void 0 : _this$internalGet[0];
  58. }
  59. }, {
  60. key: "has",
  61. value: function has(derivativeOption) {
  62. return !!this.internalGet(derivativeOption);
  63. }
  64. }, {
  65. key: "set",
  66. value: function set(derivativeOption, value) {
  67. var _this = this;
  68. // New cache
  69. if (!this.has(derivativeOption)) {
  70. if (this.size() + 1 > ThemeCache.MAX_CACHE_SIZE + ThemeCache.MAX_CACHE_OFFSET) {
  71. var _this$keys$reduce = this.keys.reduce(function (result, key) {
  72. var _result = _slicedToArray(result, 2),
  73. callTimes = _result[1];
  74. if (_this.internalGet(key)[1] < callTimes) {
  75. return [key, _this.internalGet(key)[1]];
  76. }
  77. return result;
  78. }, [this.keys[0], this.cacheCallTimes]),
  79. _this$keys$reduce2 = _slicedToArray(_this$keys$reduce, 1),
  80. targetKey = _this$keys$reduce2[0];
  81. this.delete(targetKey);
  82. }
  83. this.keys.push(derivativeOption);
  84. }
  85. var cache = this.cache;
  86. derivativeOption.forEach(function (derivative, index) {
  87. if (index === derivativeOption.length - 1) {
  88. cache.set(derivative, {
  89. value: [value, _this.cacheCallTimes++]
  90. });
  91. } else {
  92. var cacheValue = cache.get(derivative);
  93. if (!cacheValue) {
  94. cache.set(derivative, {
  95. map: new Map()
  96. });
  97. } else if (!cacheValue.map) {
  98. cacheValue.map = new Map();
  99. }
  100. cache = cache.get(derivative).map;
  101. }
  102. });
  103. }
  104. }, {
  105. key: "deleteByPath",
  106. value: function deleteByPath(currentCache, derivatives) {
  107. var cache = currentCache.get(derivatives[0]);
  108. if (derivatives.length === 1) {
  109. var _cache$value;
  110. if (!cache.map) {
  111. currentCache.delete(derivatives[0]);
  112. } else {
  113. currentCache.set(derivatives[0], {
  114. map: cache.map
  115. });
  116. }
  117. return (_cache$value = cache.value) === null || _cache$value === void 0 ? void 0 : _cache$value[0];
  118. }
  119. var result = this.deleteByPath(cache.map, derivatives.slice(1));
  120. if ((!cache.map || cache.map.size === 0) && !cache.value) {
  121. currentCache.delete(derivatives[0]);
  122. }
  123. return result;
  124. }
  125. }, {
  126. key: "delete",
  127. value: function _delete(derivativeOption) {
  128. // If cache exists
  129. if (this.has(derivativeOption)) {
  130. this.keys = this.keys.filter(function (item) {
  131. return !sameDerivativeOption(item, derivativeOption);
  132. });
  133. return this.deleteByPath(this.cache, derivativeOption);
  134. }
  135. return undefined;
  136. }
  137. }]);
  138. return ThemeCache;
  139. }();
  140. _defineProperty(ThemeCache, "MAX_CACHE_SIZE", 20);
  141. _defineProperty(ThemeCache, "MAX_CACHE_OFFSET", 5);
  142. export { ThemeCache as default };