123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- "use strict";
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = void 0;
- exports.sameDerivativeOption = sameDerivativeOption;
- var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
- var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
- var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
- // ================================== Cache ==================================
- 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 = exports.default = /*#__PURE__*/function () {
- function ThemeCache() {
- (0, _classCallCheck2.default)(this, ThemeCache);
- (0, _defineProperty2.default)(this, "cache", void 0);
- (0, _defineProperty2.default)(this, "keys", void 0);
- (0, _defineProperty2.default)(this, "cacheCallTimes", void 0);
- this.cache = new Map();
- this.keys = [];
- this.cacheCallTimes = 0;
- }
- (0, _createClass2.default)(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 = (_cache = cache) === null || _cache === void 0 || (_cache = _cache.map) === null || _cache === void 0 ? void 0 : _cache.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 = (0, _slicedToArray2.default)(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 = (0, _slicedToArray2.default)(_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;
- }();
- (0, _defineProperty2.default)(ThemeCache, "MAX_CACHE_SIZE", 20);
- (0, _defineProperty2.default)(ThemeCache, "MAX_CACHE_OFFSET", 5);
|