1 Star 0 Fork 0

wy2010344/npm_mve

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
filterCacheChildren.js 2.98 KB
一键复制 编辑 原始数据 按行查看 历史
wangyang 提交于 2021-10-15 20:16 . 优化代码
"use strict";
exports.__esModule = true;
exports.filterCacheChildren = void 0;
var childrenBuilder_1 = require("./childrenBuilder");
var util_1 = require("./util");
var CacheViewModel = /** @class */ (function () {
function CacheViewModel(row, life, result) {
this.row = row;
this.life = life;
this.result = result;
this.once = new util_1.OnceInitDestry();
}
CacheViewModel.prototype.init = function () {
this.once.init();
this.resultDestroy = (0, util_1.orRun)(this.result);
};
CacheViewModel.prototype.destroy = function () {
this.once.destroy();
this.life.destroy();
(0, util_1.orRun)(this.resultDestroy);
};
return CacheViewModel;
}());
/**
* 有缓存mvc
* @param array
* @param fun
* @returns
*/
function filterCacheChildren(array, fun) {
return function (parent, me) {
var views = [];
var life = (0, util_1.onceLife)(function () {
var size = views.length;
for (var i = 0; i < size; i++) {
views[i].init();
}
return function () {
var size = views.length;
for (var i = size - 1; i > -1; i--) {
views[i].destroy();
}
views.length = 0;
w.disable();
};
});
var w = util_1.Watcher.ofAfter(array, function (vs) {
if (vs.length < views.length) {
//更新旧数据视图
for (var i = 0; i < vs.length; i++) {
views[i].row(vs[i]);
}
var minLength = vs.length - 1;
for (var i = views.length - 1; i > minLength; i--) {
//销毁
if (life.isInit) {
views[i].destroy();
}
//删除视图
parent.remove(i);
}
views.length = vs.length;
}
else {
//更新旧数据
for (var i = 0; i < views.length; i++) {
views[i].row(vs[i]);
}
//追加新数据
for (var i = views.length; i < vs.length; i++) {
var row = util_1.mve.valueOf(vs[i]);
var lifeModel = (0, util_1.newLifeModel)();
//创建视图
var vm = parent.newChildAt(i);
var cs = fun(lifeModel.me, row, i);
var vx = (0, childrenBuilder_1.baseChildrenBuilder)(lifeModel.me, cs, vm);
var cv = new CacheViewModel(row, lifeModel, vx);
//模型增加
views.push(cv);
//初始化
if (life.isInit) {
cv.init();
}
}
}
});
return life.init;
};
}
exports.filterCacheChildren = filterCacheChildren;
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
TypeScript
1
https://gitee.com/wy2010344/npm_mve.git
[email protected]:wy2010344/npm_mve.git
wy2010344
npm_mve
npm_mve
master

搜索帮助