UNPKG

vscroll

Version:
147 lines 5.33 kB
import { __read, __spreadArray } from "tslib"; import { SizeStrategy } from '../../inputs/index'; var SizesRecalculation = /** @class */ (function () { function SizesRecalculation() { this.reset(); } SizesRecalculation.prototype.reset = function () { this.newItems = []; this.oldItems = []; this.removed = []; }; return SizesRecalculation; }()); export { SizesRecalculation }; var DefaultSize = /** @class */ (function () { function DefaultSize(itemSize, sizeStrategy) { this.itemSize = itemSize; this.sizeStrategy = sizeStrategy; this.sizeMap = new Map(); this.recalculation = new SizesRecalculation(); } DefaultSize.prototype.reset = function (force) { if (force) { this.constantSize = this.itemSize; this.frequentSize = this.itemSize; this.averageSize = this.itemSize; this.averageSizeFloat = this.itemSize; this.sizeMap.clear(); } this.recalculation.reset(); }; DefaultSize.prototype.get = function () { switch (this.sizeStrategy) { case SizeStrategy.Average: return this.averageSize; case SizeStrategy.Frequent: return this.frequentSize; default: return this.constantSize; } }; DefaultSize.prototype.recalculateAverageSize = function (cacheSize) { var _a = this.recalculation, oldItems = _a.oldItems, newItems = _a.newItems, removed = _a.removed; if (oldItems.length) { var oldSize = oldItems.reduce(function (acc, item) { return acc + item.size; }, 0); var newSize = oldItems.reduce(function (acc, item) { return acc + item.newSize; }, 0); var averageSize = this.averageSizeFloat || 0; this.averageSizeFloat = averageSize - (oldSize - newSize) / (cacheSize - newItems.length); } if (newItems.length) { var newSize = newItems.reduce(function (acc, item) { return acc + item.size; }, 0); var averageSize = this.averageSizeFloat || 0; this.averageSizeFloat = ((cacheSize - newItems.length) * averageSize + newSize) / cacheSize; } if (removed.length) { var removedSize = removed.reduce(function (acc, item) { return acc + item.size; }, 0); var averageSize = this.averageSizeFloat || 0; this.averageSizeFloat = ((cacheSize + removed.length) * averageSize - removedSize) / cacheSize; } this.averageSize = Math.round(this.averageSizeFloat); }; DefaultSize.prototype.recalculateFrequentSize = function () { var _this = this; var _a = this.recalculation, oldItems = _a.oldItems, newItems = _a.newItems, removed = _a.removed; var oldFrequentSizeCount = this.sizeMap.get(this.frequentSize); if (newItems.length) { newItems.forEach(function (_a) { var size = _a.size; return _this.sizeMap.set(size, (_this.sizeMap.get(size) || 0) + 1); }); } if (oldItems.length) { oldItems.forEach(function (_a) { var size = _a.size; return _this.sizeMap.set(size, Math.max((_this.sizeMap.get(size) || 0) - 1, 0)); }); oldItems.forEach(function (_a) { var s = _a.newSize; return _this.sizeMap.set(s, (_this.sizeMap.get(s) || 0) + 1); }); } if (removed.length) { removed.forEach(function (_a) { var size = _a.size; return _this.sizeMap.set(size, Math.max((_this.sizeMap.get(size) || 0) - 1, 0)); }); } var sorted = __spreadArray([], __read(this.sizeMap.entries()), false).sort(function (a, b) { return b[1] - a[1]; }); var mostFrequentCount = sorted[0][1]; var listEqual = sorted.filter(function (i) { return i[1] === mostFrequentCount; }); if (listEqual.length > 1 && listEqual.find(function (i) { return i[0] === oldFrequentSizeCount; })) { // if there are more than 1 most frequent sizes, but the old one is present return; } this.frequentSize = sorted[0][0]; }; DefaultSize.prototype.recalculate = function (cacheSize) { if (this.sizeStrategy === SizeStrategy.Constant) { return false; } var _a = this.recalculation, oldItems = _a.oldItems, newItems = _a.newItems, removed = _a.removed; if (!oldItems.length && !newItems.length && !removed.length) { return false; } var oldValue = this.get(); if (!cacheSize) { this.reset(true); } else { if (this.sizeStrategy === SizeStrategy.Average) { this.recalculateAverageSize(cacheSize); } else { this.recalculateFrequentSize(); } this.recalculation.reset(); } return this.get() !== oldValue; }; DefaultSize.prototype.setExisted = function (oldSize, newSize) { if (this.sizeStrategy !== SizeStrategy.Constant) { this.recalculation.oldItems.push({ size: oldSize, newSize: newSize }); } }; DefaultSize.prototype.setNew = function (size) { if (this.sizeStrategy !== SizeStrategy.Constant) { this.recalculation.newItems.push({ size: size }); } else { if (!this.constantSize) { this.constantSize = size; } } }; DefaultSize.prototype.setRemoved = function (size) { if (this.sizeStrategy !== SizeStrategy.Constant) { this.recalculation.removed.push({ size: size }); } }; return DefaultSize; }()); export { DefaultSize }; //# sourceMappingURL=defaultSize.js.map