feat(dep): 性能优化,收集时节点配置中id与name不需要做判断

This commit is contained in:
roymondchen 2025-01-14 16:22:04 +08:00
parent ad2545c2e2
commit b215062177
2 changed files with 28 additions and 21 deletions

View File

@ -103,9 +103,9 @@ export default class Watcher {
* target * target
*/ */
public clearTargets() { public clearTargets() {
Object.keys(this.targetsList).forEach((key) => { for (const key of Object.keys(this.targetsList)) {
delete this.targetsList[key]; delete this.targetsList[key];
}); }
} }
/** /**
@ -137,9 +137,9 @@ export default class Watcher {
if (!type && !target.isCollectByDefault) { if (!type && !target.isCollectByDefault) {
return; return;
} }
nodes.forEach((node) => { for (const node of nodes) {
cb({ node, target }); cb({ node, target });
}); }
}, },
type, type,
); );
@ -161,7 +161,7 @@ export default class Watcher {
const clearedItemsNodeIds: (string | number)[] = []; const clearedItemsNodeIds: (string | number)[] = [];
traverseTarget(targetsList, (target) => { traverseTarget(targetsList, (target) => {
if (nodes) { if (nodes) {
nodes.forEach((node) => { for (const node of nodes) {
target.removeDep(node[this.idProp]); target.removeDep(node[this.idProp]);
if ( if (
@ -172,7 +172,7 @@ export default class Watcher {
clearedItemsNodeIds.push(node[this.idProp]); clearedItemsNodeIds.push(node[this.idProp]);
this.clear(node[this.childrenProp]); this.clear(node[this.childrenProp]);
} }
}); }
} else { } else {
target.removeDep(); target.removeDep();
} }
@ -202,26 +202,29 @@ export default class Watcher {
key: fullKey, key: fullKey,
}); });
} else if (!keyIsItems && Array.isArray(value)) { } else if (!keyIsItems && Array.isArray(value)) {
value.forEach((item, index) => { for (let i = 0, l = value.length; i < l; i++) {
const item = value[i];
if (isObject(item)) { if (isObject(item)) {
collectTarget(item, `${fullKey}[${index}]`); collectTarget(item, `${fullKey}[${i}]`);
}
} }
});
} else if (isObject(value)) { } else if (isObject(value)) {
collectTarget(value, fullKey); collectTarget(value, fullKey);
} }
if (keyIsItems && deep && Array.isArray(value)) { if (keyIsItems && deep && Array.isArray(value)) {
value.forEach((child) => { for (const child of value) {
this.collectItem(child, target, depExtendedData, deep); this.collectItem(child, target, depExtendedData, deep);
}); }
} }
}; };
Object.entries(config).forEach(([key, value]) => { for (const [key, value] of Object.entries(config)) {
if (typeof value === 'undefined' || value === '') return; if (typeof value === 'undefined' || value === '') continue;
if (key === 'id' || key === 'name') continue;
doCollect(key, value); doCollect(key, value);
}); }
}; };
collectTarget(node); collectTarget(node);
@ -230,9 +233,9 @@ export default class Watcher {
public removeTargetDep(target: Target, node: TargetNode, key?: string | number) { public removeTargetDep(target: Target, node: TargetNode, key?: string | number) {
target.removeDep(node[this.idProp], key); target.removeDep(node[this.idProp], key);
if (typeof key === 'undefined' && Array.isArray(node[this.childrenProp]) && node[this.childrenProp].length) { if (typeof key === 'undefined' && Array.isArray(node[this.childrenProp]) && node[this.childrenProp].length) {
node[this.childrenProp].forEach((item: TargetNode) => { for (const item of node[this.childrenProp] as TargetNode[]) {
this.removeTargetDep(target, item, key); this.removeTargetDep(target, item, key);
}); }
} }
} }
} }

View File

@ -185,6 +185,10 @@ export const isDataSourceTarget = (
value: any, value: any,
hasArray = false, hasArray = false,
) => { ) => {
if (!value || !['string', 'object'].includes(typeof value)) {
return false;
}
if (`${key}`.startsWith(NODE_CONDS_KEY)) { if (`${key}`.startsWith(NODE_CONDS_KEY)) {
return false; return false;
} }
@ -285,12 +289,12 @@ export const traverseTarget = (
cb: (target: Target) => void, cb: (target: Target) => void,
type?: DepTargetType | string, type?: DepTargetType | string,
) => { ) => {
Object.values(targetsList).forEach((targets) => { for (const targets of Object.values(targetsList)) {
Object.values(targets).forEach((target) => { for (const target of Object.values(targets)) {
if (type && target.type !== type) { if (type && target.type !== type) {
return; continue;
} }
cb(target); cb(target);
}); }
}); }
}; };