You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
flow-playform-react/src/store/ideContainer.ts

249 lines
7.6 KiB
TypeScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import { createSlice } from '@reduxjs/toolkit';
import {
createDefaultAppRuntimeState,
getCurrentAppKey,
} from '@/utils/flow/runtime';
// 定义初始状态类型
interface IDEContainerState {
info: any;
menuData: any;
flowData: any;
canvasDataMap: any;
projectComponentData: any;
currentAppData: any;
eventListOld: any;
eventList: any;
eventTopicList: any;
logBarStatus?: boolean;
socketId: string;
nodeStatusMap: Record<string, string>; // 节点状态映射
isRunning: boolean; // 是否正在运行
// 应用运行状态和日志数据按应用ID隔离存储
appRuntimeData: Record<
string,
{
nodeStatusMap: Record<string, string>;
isRunning: boolean;
isPaused: boolean;
logs: any[];
runId: string;
eventSendNodeList: any[]; // [{nodeID:topic}]
eventlisteneList: any[]; // [{nodeID:topic}]
}
>;
gloBalVarList: any;
// 组件开发相关状态
componentCoding: {
localProjectPath: string; // code-server路径
name: string; // 组件名称
projectId: string; // 组件标识
id: string; // 组件id
};
}
// 初始状态
const initialState: IDEContainerState = {
info: {}, // 项目信息
menuData: {}, // 菜单数据
flowData: {}, // 编排数据,即流程图的渲染数据
canvasDataMap: {}, // 每个画布的缓存信息
projectComponentData: {}, // 工程下的组件列表
currentAppData: {}, // 当前选中的应用数据
eventListOld: [], // 工程下的事件列表
eventList: [], // 工程下的事件列表
eventTopicList: [], // 应用编排使用的事件名和topic列表
logBarStatus: false,
socketId: '', // 工程的socketId
nodeStatusMap: {}, // 初始化节点状态映射
isRunning: false, // 默认未运行
appRuntimeData: {}, // 按应用ID隔离的应用运行状态和日志数据
gloBalVarList: [], // 工程级全局变量列表
componentCoding: {
localProjectPath: '',
name: '',
projectId: '',
id: '',
},
};
// 创建切片
const ideContainerSlice = createSlice({
name: 'ideContainer',
initialState,
reducers: {
updateInfo(state, action) {
state.info = action.payload;
},
updateMenuData(state, action) {
state.menuData = action.payload;
},
updateFlowData(state, action) {
state.flowData = { ...state.flowData, ...action.payload };
},
updateCanvasDataMap(state, action) {
state.canvasDataMap = { ...state.canvasDataMap, ...action.payload };
},
updateProjectComponentData(state, action) {
state.projectComponentData = {
...state.projectComponentData,
...action.payload,
};
},
updateCurrentAppData(state, action) {
state.currentAppData = action.payload;
// 切换应用时,同步全局 nodeStatusMap 为新应用的节点状态
const newAppKey = getCurrentAppKey(action.payload);
if (newAppKey && state.appRuntimeData[newAppKey]) {
state.nodeStatusMap = {
...state.appRuntimeData[newAppKey].nodeStatusMap,
};
state.isRunning = state.appRuntimeData[newAppKey].isRunning;
} else {
// 如果新应用没有运行时数据,清空节点状态
state.nodeStatusMap = {};
state.isRunning = false;
}
},
updateEventList(state, action) {
state.eventList = action.payload;
},
updateEventListOld(state, action) {
state.eventListOld = action.payload;
},
updateEventTopicList(state, action) {
state.eventTopicList = action.payload;
},
updateLogBarStatus(state, action) {
state.logBarStatus = action.payload;
},
updateSocketId(state, action) {
state.socketId = action.payload;
},
updateGlobalVarList(state, action) {
state.gloBalVarList = action.payload;
},
// 更新节点状态
updateNodeStatus: (state, { payload }) => {
const { nodeId, status, actionType, appId } = payload;
// 如果提供了 appId优先使用提供的 appId否则使用当前激活的应用
const targetAppKey = appId || getCurrentAppKey(state.currentAppData);
// 更新全局 nodeStatusMap仅当是当前激活应用时更新用于 UI 显示)
const currentAppKey = getCurrentAppKey(state.currentAppData);
if (targetAppKey === currentAppKey) {
state.nodeStatusMap[nodeId] = status;
}
// 更新目标应用的节点状态
if (targetAppKey) {
if (!state.appRuntimeData[targetAppKey]) {
state.appRuntimeData[targetAppKey] = createDefaultAppRuntimeState();
}
state.appRuntimeData[targetAppKey].nodeStatusMap[nodeId] = status;
}
},
// 重置节点状态
resetNodeStatus: (state) => {
state.nodeStatusMap = {};
// 同时重置当前应用的节点状态
const appKey = getCurrentAppKey(state.currentAppData);
if (appKey && state.appRuntimeData[appKey]) {
state.appRuntimeData[appKey].nodeStatusMap = {};
}
},
// 更新运行状态
updateIsRunning: (state, { payload }) => {
state.isRunning = payload;
// 同时更新当前应用的运行状态
const appKey = getCurrentAppKey(state.currentAppData);
if (appKey) {
if (!state.appRuntimeData[appKey]) {
state.appRuntimeData[appKey] = createDefaultAppRuntimeState();
}
state.appRuntimeData[appKey].isRunning = payload;
}
},
// 更新暂停状态
updateIsPaused: (state, { payload }) => {
const appKey = getCurrentAppKey(state.currentAppData);
if (appKey) {
if (!state.appRuntimeData[appKey]) {
state.appRuntimeData[appKey] = createDefaultAppRuntimeState();
}
state.appRuntimeData[appKey].isPaused = payload;
}
},
// 添加运行id
updateRuntimeId: (state, { payload }) => {
const appKey = getCurrentAppKey(state.currentAppData);
if (!state.appRuntimeData[appKey]) {
state.appRuntimeData[appKey] = createDefaultAppRuntimeState();
}
state.appRuntimeData[appKey].runId = payload;
},
// 更新事件节点列表
updateEventNodeList: (state, { payload }) => {
const appKey = getCurrentAppKey(state.currentAppData);
if (!state.appRuntimeData[appKey]) {
state.appRuntimeData[appKey] = createDefaultAppRuntimeState();
}
state.appRuntimeData[appKey] = {
...state.appRuntimeData[appKey],
...payload,
};
},
// 添加运行日志
addRuntimeLog: (state, { payload }) => {
const { log, appId } = payload;
if (!state.appRuntimeData[appId]) {
state.appRuntimeData[appId] = createDefaultAppRuntimeState();
}
state.appRuntimeData[appId].logs.push(log);
},
// 清空指定应用的运行日志
clearRuntimeLogs: (state, { payload }) => {
const { appId } = payload;
if (state.appRuntimeData[appId]) {
state.appRuntimeData[appId].logs = [];
}
},
// 更新组件编码路径
updateComponentCodingPath(state, action) {
state.componentCoding = { ...action.payload };
},
},
});
// 导出动作 creators
export const {
updateInfo,
updateMenuData,
updateFlowData,
updateCanvasDataMap,
updateProjectComponentData,
updateCurrentAppData,
updateEventList,
updateEventListOld,
updateEventTopicList,
updateLogBarStatus,
updateSocketId,
updateGlobalVarList,
updateNodeStatus,
resetNodeStatus,
updateIsRunning,
updateIsPaused,
updateRuntimeId,
updateEventNodeList,
addRuntimeLog,
clearRuntimeLogs,
updateComponentCodingPath,
} = ideContainerSlice.actions;
// 默认导出 reducer
export default ideContainerSlice.reducer;