ThemeCache.js 5.3 KB

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