jpush-ui
Version:
极光大数据前端Angular组件(^6.1.0)
235 lines • 7.54 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var pager_model_1 = require("./pager.model");
// 缓存控制器,支持多维度
var
// 缓存控制器,支持多维度
PageCache = /** @class */ (function () {
function PageCache() {
this.cache = {};
this.total = 0;
this.hasData = false;
this.markers = {};
this.key = 0;
}
PageCache.prototype.addToList = function (rows, start) {
// What this does is actually replacement.
var list = this.getList();
if (list) {
if (list.length < start) {
for (var i = list.length; i < start; i++) {
list[i] = {};
}
}
// 设备index
rows.forEach(function (item, index) {
if (typeof item === 'object') {
item.index = start + index;
}
});
list.splice.apply(list, [start, rows.length].concat(rows));
this.hasData = true;
}
// set markers
var markers = this.markers[this.key];
var end = start + rows.length;
for (var i = start; i < end; i++) {
markers[i] = true;
}
};
PageCache.prototype.prepend = function (arr) {
var list = this.getList();
if (list) {
list.splice.apply(list, [0, 0].concat(arr));
list.forEach(function (item, index) {
if (typeof item === 'object') {
item.index = index;
}
});
this.total += arr.length;
}
};
PageCache.prototype.append = function (arr) {
var list = this.getList();
if (list) {
var oriLen_1 = list.length;
list.splice.apply(list, [oriLen_1, 0].concat(arr));
arr.forEach(function (item, index) {
if (typeof item === 'object') {
item.index = index + oriLen_1;
}
});
this.total += arr.length;
}
};
PageCache.prototype.reset = function () {
this.total = 0;
this.cache = {};
};
PageCache.prototype.setTotal = function (total) {
this.total = total;
};
PageCache.prototype.setCurKey = function (key) {
this.key = key;
};
// 获取全部的列表(key)
// 获取全部的列表(key)
PageCache.prototype.getList =
// 获取全部的列表(key)
function () {
var list;
list = this.cache[this.key];
if (!list) {
this.cache[this.key] = list = [];
this.markers[this.key] = [];
}
return list;
};
PageCache.prototype.getListOf = function (span) {
var list = this.getList();
if (list) {
return list.slice(span.start, span.end);
}
return null;
};
PageCache.prototype.delFromList = function (opt) {
if (!opt || opt.index === undefined && !opt.item) {
return -1;
}
var list = this.getList();
if (opt.index !== undefined) {
return this.removeItem(opt.index, list);
}
else {
if (opt.item) {
var index = list.indexOf(opt.item);
if (!index) {
return -1;
}
else {
return this.removeItem(index, list);
}
}
else {
return -1;
}
}
};
// 查看接下去的页面有没有缓存,没有的话,返回需要加载的start和end
// 查看接下去的页面有没有缓存,没有的话,返回需要加载的start和end
PageCache.prototype.checkCache =
// 查看接下去的页面有没有缓存,没有的话,返回需要加载的start和end
function (start, end, leastFetch) {
var list = this.getList();
if (list) {
if (list.length > 0) {
// 这时total肯定有值
if (end > this.total) {
end = this.total;
}
return this.getRealSeg(start, end, leastFetch);
}
else {
// 第一次请求
return { start: start, end: end };
}
}
};
PageCache.prototype.isExist = function (index) {
var markers = this.markers[this.key];
return markers && markers[index];
};
// 删除,同时调整index
// 删除,同时调整index
PageCache.prototype.removeItem =
// 删除,同时调整index
function (index, list) {
if (!list || list.length <= 0) {
return;
}
var deleted = list.splice(index, 1);
var len = list.length;
for (var i = index; i < len; i++) {
var item = list[i];
if (item) {
item.index = i;
}
}
// 更新marker
var markers = this.markers[this.key];
if (markers) {
markers.splice(index, 1);
}
return {
index: index,
data: deleted
};
};
PageCache.prototype.getRealSeg = function (start, end, leastFetch) {
// 计算分段
var segs = 0; // 分段数
var markers = this.markers[this.key];
if (markers.length < start) {
return { start: start, end: end };
}
var flag = false;
var tmpStart = start;
var tmpEnd = end;
var tmpCnt = 0;
var firstStart; // 第一次检测到的分段的开头
var lastEnd = end; // 第一次检测到的分段的开头
for (var i = start; i < end; i++) {
if (segs > 1) {
// 分段超过1个
lastEnd = tmpEnd;
// 从后往前找最后一个有数据的地方
for (var j = end - 1; j > i; j--) {
if (markers[j - 1] === undefined) {
lastEnd = j;
break;
}
}
// console.log(lastEnd);
return {
start: firstStart,
// 此时的firstStart不可能为空
end: lastEnd
};
}
var marker = markers[i];
if (marker === undefined) {
if (!flag) {
// 出现undefined的时候,tmpStart才会变化
tmpStart = i;
if (!firstStart) {
firstStart = tmpStart;
}
segs++;
}
flag = true;
tmpEnd = i + 1; // tmpEnd总指向最后一个没数据的index+1
}
else {
tmpCnt++;
flag = false;
}
}
if (tmpCnt === end - start) {
// 全都存在
return null;
}
if (leastFetch && tmpEnd === end) {
if (tmpEnd - tmpStart <= leastFetch) {
var pageSize = leastFetch / pager_model_1.LEAST_FETCHING_PERCENT;
tmpEnd += pageSize; // 多取一页
}
}
return {
start: tmpStart,
end: tmpEnd
};
};
return PageCache;
}());
exports.PageCache = PageCache;
//# sourceMappingURL=cache.helper.js.map