fix(editor): 数据源修改后,依赖重新收集不准确

This commit is contained in:
roymondchen 2025-03-12 14:22:33 +08:00
parent 11c027c71b
commit 9c7d711c16
2 changed files with 36 additions and 14 deletions

View File

@ -502,6 +502,7 @@ export const initServiceEvents = (
isModifyField =
changeRecord.propPath === 'fields' ||
/fields.(\d)+.name/.test(changeRecord.propPath) ||
/fields.(\d)+.defaultValue/.test(changeRecord.propPath) ||
/fields.(\d)+$/.test(changeRecord.propPath);
isModifyMock = changeRecord.propPath === 'mocks';
@ -523,25 +524,35 @@ export const initServiceEvents = (
if (Array.isArray(root?.items)) {
depService.clearIdleTasks();
removeDataSourceTarget(config.id);
initDataSourceDepTarget(config);
let collectIdlePromises: Promise<void[]>[] = [];
if (isModifyField) {
depService.removeTarget(config.id, DepTargetType.DATA_SOURCE);
depService.removeTarget(config.id, DepTargetType.DATA_SOURCE_COND);
depService.addTarget(createDataSourceTarget(config, reactive({})));
depService.addTarget(createDataSourceCondTarget(config, reactive({})));
collectIdlePromises = [
collectIdle(root.items, true, DepTargetType.DATA_SOURCE),
collectIdle(root.items, true, DepTargetType.DATA_SOURCE_COND),
];
} else if (isModifyMock) {
depService.removeTarget(config.id, DepTargetType.DATA_SOURCE);
depService.addTarget(createDataSourceTarget(config, reactive({})));
collectIdlePromises = [collectIdle(root.items, true, DepTargetType.DATA_SOURCE)];
} else if (isModifyMethod) {
depService.removeTarget(config.id, DepTargetType.DATA_SOURCE_METHOD);
depService.addTarget(createDataSourceMethodTarget(config, reactive({})));
collectIdlePromises = [collectIdle(root.items, true, DepTargetType.DATA_SOURCE_METHOD)];
}
Promise.all(collectIdlePromises).then(() => {
updateDataSourceSchema();
updateDsData();
updateStageNodes(root.items);
});
Promise.all(collectIdlePromises)
.then(() => updateDataSourceSchema())
.then(() => updateDsData())
.then(() => updateStageNodes(root.items));
}
} else if (root?.dataSources) {
updateDsData();
@ -616,20 +627,31 @@ export const initServiceEvents = (
}
root.dataSourceCondDeps[target.id] = target.deps;
}
if (target.type === DepTargetType.DATA_SOURCE_METHOD) {
if (!root.dataSourceMethodDeps) {
root.dataSourceMethodDeps = {};
}
root.dataSourceMethodDeps[target.id] = target.deps;
}
};
const targetRemoveHandler = (id: string | number) => {
const targetRemoveHandler = (id: string | number, type: DepTargetType) => {
const root = editorService.get('root');
if (!root) return;
if (root.dataSourceDeps) {
if (root.dataSourceDeps && type === DepTargetType.DATA_SOURCE) {
delete root.dataSourceDeps[id];
}
if (root.dataSourceCondDeps) {
if (root.dataSourceCondDeps && type === DepTargetType.DATA_SOURCE_COND) {
delete root.dataSourceCondDeps[id];
}
if (root.dataSourceMethodDeps && type === DepTargetType.DATA_SOURCE_METHOD) {
delete root.dataSourceMethodDeps[id];
}
};
depService.on('add-target', targetAddHandler);

View File

@ -28,7 +28,7 @@ import BaseService from './BaseService';
export interface DepEvents {
'add-target': [target: Target];
'remove-target': [id: string | number];
'remove-target': [id: string | number, type: string | DepTargetType];
collected: [nodes: MNode[], deep: boolean];
'ds-collected': [nodes: MNode[], deep: boolean];
}
@ -77,7 +77,7 @@ class Dep extends BaseService {
if (!targets) return;
for (const target of Object.values(targets)) {
this.emit('remove-target', target.id);
this.emit('remove-target', target.id, type);
}
}
@ -96,7 +96,7 @@ class Dep extends BaseService {
public removeTarget(id: Id, type: string = DepTargetType.DEFAULT) {
this.watcher.removeTarget(id, type);
this.emit('remove-target', id);
this.emit('remove-target', id, type);
}
public clearTargets() {