添加反向树加载功能,重构节点和树的选择逻辑

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
2026-04-17 19:30:19 +08:00
parent 65d99bb7a8
commit 1de4f9db8d
15 changed files with 611 additions and 139 deletions

View File

@@ -0,0 +1,49 @@
import type { GraphContainer } from '../../graph';
import { findNodeConnections, findNodeParameters, findTreeNodeInstances } from '../api';
import type { TreeTemplateRow } from '../api';
import { buildGraphFromReverseTreeRows } from './reverse-tree';
import type { ReverseTreeRows } from './reverse-tree';
import { loadNodeTemplatesOnce, loadTemplateParameterDefsOnce } from '../template-metadata-loader';
// 已反演过的树直接返回缓存图,无需重复请求
const graphCache = new Map<number, GraphContainer>();
export const loadReverseTreeGraph = async (treeId: number): Promise<GraphContainer | null> => {
const cached = graphCache.get(treeId);
if (cached) {
return cached;
}
// 模板类接口只请求一次,树级接口每次切树请求
const [nodeTemplatesResponse, templateParameterDefs, instancesResponse, connectionsResponse] =
await Promise.all([
loadNodeTemplatesOnce(),
loadTemplateParameterDefsOnce(),
findTreeNodeInstances({ treeId }),
findNodeConnections({ treeId }),
]);
const parametersResponse = await findNodeParameters({ treeId });
const rows: ReverseTreeRows = {
templates: (nodeTemplatesResponse.data ?? []).map(t => ({
id: t.id,
type: t.type ?? 'action',
name: t.name ?? '',
multiable: t.multiable,
})) as TreeTemplateRow[],
templateParameterDefs: templateParameterDefs,
nodeInstances: instancesResponse.rows ?? [],
connections: connectionsResponse.rows ?? [],
nodeParameters: parametersResponse.rows ?? [],
};
const graph = buildGraphFromReverseTreeRows(treeId, rows);
if (graph) {
graphCache.set(treeId, graph);
}
return graph;
};
export const invalidateReverseTreeCache = (treeId: number): void => {
graphCache.delete(treeId);
};