Skip to content

Commit

Permalink
feat(editor,dep): watcher新增方法:清除指定类型的依赖
Browse files Browse the repository at this point in the history
  • Loading branch information
parisma authored and roymondchen committed Dec 21, 2023
1 parent 2dc810a commit d981014
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
25 changes: 24 additions & 1 deletion packages/dep/src/Watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export default class Watcher {
}

/**
* 清除依赖
* 清除所有目标的依赖
* @param nodes 需要清除依赖的节点
*/
public clear(nodes?: Record<string | number, any>[]) {
Expand All @@ -143,6 +143,29 @@ export default class Watcher {
});
}

/**
* 清除指定类型的依赖
* @param type 类型
* @param nodes 需要清除依赖的节点
*/
public clearByType(type: DepTargetType, nodes?: Record<string | number, any>[]) {
const clearedItemsNodeIds: (string | number)[] = [];
const targetList = this.getTargets(type);
Object.values(targetList).forEach((target) => {
if (nodes) {
nodes.forEach((node) => {
target.removeDep(node);
if (Array.isArray(node.items) && node.items.length && !clearedItemsNodeIds.includes(node.id)) {
clearedItemsNodeIds.push(node.id);
this.clear(node.items);
}
});
} else {
target.removeDep();
}
});
}

private collectItem(node: Record<string | number, any>, target: Target, deep = false) {
const collectTarget = (config: Record<string | number, any>, prop = '') => {
const doCollect = (key: string, value: any) => {
Expand Down
4 changes: 4 additions & 0 deletions packages/editor/src/services/dep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class Dep extends BaseService {
return this.watcher.clear(nodes);
}

public clearByType(type: DepTargetType, nodes?: MNode[]) {
return this.watcher.clearByType(type, nodes);
}

public hasTarget(id: Id, type: string = DepTargetType.DEFAULT) {
return this.watcher.hasTarget(id, type);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/services/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ class Editor extends BaseService {
public copyWithRelated(config: MNode | MNode[]): void {
const copyNodes: MNode[] = Array.isArray(config) ? config : [config];
// 关联的组件也一并复制
depService.getTarget(DepTargetType.RELATED_COMP_WHEN_COPY, DepTargetType.RELATED_COMP_WHEN_COPY)?.removeDep();
depService.clearByType(DepTargetType.RELATED_COMP_WHEN_COPY);
depService.collect(copyNodes, true, DepTargetType.RELATED_COMP_WHEN_COPY);
const customTarget = depService.getTarget(
DepTargetType.RELATED_COMP_WHEN_COPY,
Expand Down

0 comments on commit d981014

Please sign in to comment.