143 lines
4.9 KiB
JavaScript
143 lines
4.9 KiB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
|
|
import _createClass from "@babel/runtime/helpers/esm/createClass";
|
|
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
// ================================== Cache ==================================
|
|
|
|
export function sameDerivativeOption(left, right) {
|
|
if (left.length !== right.length) {
|
|
return false;
|
|
}
|
|
for (var i = 0; i < left.length; i++) {
|
|
if (left[i] !== right[i]) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
var ThemeCache = /*#__PURE__*/function () {
|
|
function ThemeCache() {
|
|
_classCallCheck(this, ThemeCache);
|
|
_defineProperty(this, "cache", void 0);
|
|
_defineProperty(this, "keys", void 0);
|
|
_defineProperty(this, "cacheCallTimes", void 0);
|
|
this.cache = new Map();
|
|
this.keys = [];
|
|
this.cacheCallTimes = 0;
|
|
}
|
|
_createClass(ThemeCache, [{
|
|
key: "size",
|
|
value: function size() {
|
|
return this.keys.length;
|
|
}
|
|
}, {
|
|
key: "internalGet",
|
|
value: function internalGet(derivativeOption) {
|
|
var _cache2, _cache3;
|
|
var updateCallTimes = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
var cache = {
|
|
map: this.cache
|
|
};
|
|
derivativeOption.forEach(function (derivative) {
|
|
if (!cache) {
|
|
cache = undefined;
|
|
} else {
|
|
var _cache, _cache$map;
|
|
cache = (_cache = cache) === null || _cache === void 0 ? void 0 : (_cache$map = _cache.map) === null || _cache$map === void 0 ? void 0 : _cache$map.get(derivative);
|
|
}
|
|
});
|
|
if ((_cache2 = cache) !== null && _cache2 !== void 0 && _cache2.value && updateCallTimes) {
|
|
cache.value[1] = this.cacheCallTimes++;
|
|
}
|
|
return (_cache3 = cache) === null || _cache3 === void 0 ? void 0 : _cache3.value;
|
|
}
|
|
}, {
|
|
key: "get",
|
|
value: function get(derivativeOption) {
|
|
var _this$internalGet;
|
|
return (_this$internalGet = this.internalGet(derivativeOption, true)) === null || _this$internalGet === void 0 ? void 0 : _this$internalGet[0];
|
|
}
|
|
}, {
|
|
key: "has",
|
|
value: function has(derivativeOption) {
|
|
return !!this.internalGet(derivativeOption);
|
|
}
|
|
}, {
|
|
key: "set",
|
|
value: function set(derivativeOption, value) {
|
|
var _this = this;
|
|
// New cache
|
|
if (!this.has(derivativeOption)) {
|
|
if (this.size() + 1 > ThemeCache.MAX_CACHE_SIZE + ThemeCache.MAX_CACHE_OFFSET) {
|
|
var _this$keys$reduce = this.keys.reduce(function (result, key) {
|
|
var _result = _slicedToArray(result, 2),
|
|
callTimes = _result[1];
|
|
if (_this.internalGet(key)[1] < callTimes) {
|
|
return [key, _this.internalGet(key)[1]];
|
|
}
|
|
return result;
|
|
}, [this.keys[0], this.cacheCallTimes]),
|
|
_this$keys$reduce2 = _slicedToArray(_this$keys$reduce, 1),
|
|
targetKey = _this$keys$reduce2[0];
|
|
this.delete(targetKey);
|
|
}
|
|
this.keys.push(derivativeOption);
|
|
}
|
|
var cache = this.cache;
|
|
derivativeOption.forEach(function (derivative, index) {
|
|
if (index === derivativeOption.length - 1) {
|
|
cache.set(derivative, {
|
|
value: [value, _this.cacheCallTimes++]
|
|
});
|
|
} else {
|
|
var cacheValue = cache.get(derivative);
|
|
if (!cacheValue) {
|
|
cache.set(derivative, {
|
|
map: new Map()
|
|
});
|
|
} else if (!cacheValue.map) {
|
|
cacheValue.map = new Map();
|
|
}
|
|
cache = cache.get(derivative).map;
|
|
}
|
|
});
|
|
}
|
|
}, {
|
|
key: "deleteByPath",
|
|
value: function deleteByPath(currentCache, derivatives) {
|
|
var cache = currentCache.get(derivatives[0]);
|
|
if (derivatives.length === 1) {
|
|
var _cache$value;
|
|
if (!cache.map) {
|
|
currentCache.delete(derivatives[0]);
|
|
} else {
|
|
currentCache.set(derivatives[0], {
|
|
map: cache.map
|
|
});
|
|
}
|
|
return (_cache$value = cache.value) === null || _cache$value === void 0 ? void 0 : _cache$value[0];
|
|
}
|
|
var result = this.deleteByPath(cache.map, derivatives.slice(1));
|
|
if ((!cache.map || cache.map.size === 0) && !cache.value) {
|
|
currentCache.delete(derivatives[0]);
|
|
}
|
|
return result;
|
|
}
|
|
}, {
|
|
key: "delete",
|
|
value: function _delete(derivativeOption) {
|
|
// If cache exists
|
|
if (this.has(derivativeOption)) {
|
|
this.keys = this.keys.filter(function (item) {
|
|
return !sameDerivativeOption(item, derivativeOption);
|
|
});
|
|
return this.deleteByPath(this.cache, derivativeOption);
|
|
}
|
|
return undefined;
|
|
}
|
|
}]);
|
|
return ThemeCache;
|
|
}();
|
|
_defineProperty(ThemeCache, "MAX_CACHE_SIZE", 20);
|
|
_defineProperty(ThemeCache, "MAX_CACHE_OFFSET", 5);
|
|
export { ThemeCache as default }; |