ThemeCache.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. exports.sameDerivativeOption = sameDerivativeOption;
  7. function sameDerivativeOption(left, right) {
  8. if (left.length !== right.length) {
  9. return false;
  10. }
  11. for (let i = 0; i < left.length; i++) {
  12. if (left[i] !== right[i]) {
  13. return false;
  14. }
  15. }
  16. return true;
  17. }
  18. class ThemeCache {
  19. constructor() {
  20. this.cache = new Map();
  21. this.keys = [];
  22. this.cacheCallTimes = 0;
  23. }
  24. size() {
  25. return this.keys.length;
  26. }
  27. internalGet(derivativeOption) {
  28. let updateCallTimes = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  29. let cache = {
  30. map: this.cache
  31. };
  32. derivativeOption.forEach(derivative => {
  33. var _a;
  34. if (!cache) {
  35. cache = undefined;
  36. } else {
  37. cache = (_a = cache === null || cache === void 0 ? void 0 : cache.map) === null || _a === void 0 ? void 0 : _a.get(derivative);
  38. }
  39. });
  40. if ((cache === null || cache === void 0 ? void 0 : cache.value) && updateCallTimes) {
  41. cache.value[1] = this.cacheCallTimes++;
  42. }
  43. return cache === null || cache === void 0 ? void 0 : cache.value;
  44. }
  45. get(derivativeOption) {
  46. var _a;
  47. return (_a = this.internalGet(derivativeOption, true)) === null || _a === void 0 ? void 0 : _a[0];
  48. }
  49. has(derivativeOption) {
  50. return !!this.internalGet(derivativeOption);
  51. }
  52. set(derivativeOption, value) {
  53. // New cache
  54. if (!this.has(derivativeOption)) {
  55. if (this.size() + 1 > ThemeCache.MAX_CACHE_SIZE + ThemeCache.MAX_CACHE_OFFSET) {
  56. const [targetKey] = this.keys.reduce((result, key) => {
  57. const [, callTimes] = result;
  58. if (this.internalGet(key)[1] < callTimes) {
  59. return [key, this.internalGet(key)[1]];
  60. }
  61. return result;
  62. }, [this.keys[0], this.cacheCallTimes]);
  63. this.delete(targetKey);
  64. }
  65. this.keys.push(derivativeOption);
  66. }
  67. let cache = this.cache;
  68. derivativeOption.forEach((derivative, index) => {
  69. if (index === derivativeOption.length - 1) {
  70. cache.set(derivative, {
  71. value: [value, this.cacheCallTimes++]
  72. });
  73. } else {
  74. const cacheValue = cache.get(derivative);
  75. if (!cacheValue) {
  76. cache.set(derivative, {
  77. map: new Map()
  78. });
  79. } else if (!cacheValue.map) {
  80. cacheValue.map = new Map();
  81. }
  82. cache = cache.get(derivative).map;
  83. }
  84. });
  85. }
  86. deleteByPath(currentCache, derivatives) {
  87. var _a;
  88. const cache = currentCache.get(derivatives[0]);
  89. if (derivatives.length === 1) {
  90. if (!cache.map) {
  91. currentCache.delete(derivatives[0]);
  92. } else {
  93. currentCache.set(derivatives[0], {
  94. map: cache.map
  95. });
  96. }
  97. return (_a = cache.value) === null || _a === void 0 ? void 0 : _a[0];
  98. }
  99. const result = this.deleteByPath(cache.map, derivatives.slice(1));
  100. if ((!cache.map || cache.map.size === 0) && !cache.value) {
  101. currentCache.delete(derivatives[0]);
  102. }
  103. return result;
  104. }
  105. delete(derivativeOption) {
  106. // If cache exists
  107. if (this.has(derivativeOption)) {
  108. this.keys = this.keys.filter(item => !sameDerivativeOption(item, derivativeOption));
  109. return this.deleteByPath(this.cache, derivativeOption);
  110. }
  111. return undefined;
  112. }
  113. }
  114. exports.default = ThemeCache;
  115. ThemeCache.MAX_CACHE_SIZE = 20;
  116. ThemeCache.MAX_CACHE_OFFSET = 5;