import { Middleware, PayloadAction } from '@reduxjs/toolkit'; import prepare, { unprepare } from '../domain/exam/prepare'; import { BusinessFlowState, setBusinessFlow } from './BusinessFlowSlice'; import { setFeedbackOpen } from './exam/largeScreenSlice'; let continueBusinessFlow = ''; const businessFlowMiddlewareLogic: Middleware = (store) => (next) => (action: PayloadAction) => { //const result = next(action); // console.log( // `[businessFlowMiddleware] Action dispatched: ${action.type} ${action.payload}` // ); if (action.type !== setBusinessFlow.type) { return next(action); // Only handle setBusinessFlow actions } const state = store.getState().BusinessFlow as BusinessFlowState; const currentKey = state.currentKey; console.log(`[businessFlowMiddleware] currentKey: ${currentKey}`); console.log( `[businessFlowMiddleware] Current business flow is now: ${state.currentKey} ; Last key was: ${state.lastKey}` ); //进入检查 if (currentKey === 'exam') { prepare(); } if (isFromExamToView(action.payload, currentKey)) { //从检查进入图像处理,有可能是曝光导致的,怎么知道呢?看发生器状态 if (store.getState().generatorMonitor.acquisitionState === 1) { //发生器正在采集 console.log( `[businessFlowMiddleware] Exiting exam flow, but generator is still acquiring.` ); return next(action); // 发生器正在采集,不能退出 } else { //非曝光导致的从检查进入处理 // 说明从检查退出 , 执行清理 console.log( `[businessFlowMiddleware] Exiting exam flow, last key was: ${state.lastKey}` ); unprepare(); } } if ( isExitingExam(action.payload, currentKey) && action.payload !== 'exitExamCompleted' && action.payload !== 'exitExamSuspended' ) { console.log( `[businessFlowMiddleware] Exiting exam flow to go to : ${action.payload}` ); const exposureStatus = store.getState().bodyPositionList.exposureStatus; console.log( `[businessFlowMiddleware] Exposure status: ${exposureStatus}` ); if (exposureStatus === 'Half Exposed') { store.dispatch(setFeedbackOpen(true)); //显示反馈框 // store.dispatch({ type: 'SET_CONTINUE_BUSINESS_FLOW', payload: currentKey }); continueBusinessFlow = action.payload; // 保存退出检查时,要去哪个业务流程 return; //阻止退出exam } else if (exposureStatus === 'Fully Exposed') { // Notify backend console.log( '[businessFlowMiddleware] Fully Exposed, notifying backend' ); // Proceed with the action return next(action); } else if (exposureStatus === 'Not Exposed') { // Proceed with the action return next(action); } } //退出检查的中间过程 if (action.payload === 'exitExamCompleted') { // Notify backend with different interfaces for completed and suspended console.log( `[businessFlowMiddleware] Notifying backend for ${action.payload}` ); // todo 把所有未曝光的体位从study删除掉--服务端和slice。 return next({ ...action, payload: continueBusinessFlow }); } if (action.payload === 'exitExamSuspended') { // Notify backend with different interfaces for completed and suspended 暂时不通知后端 console.log( `[businessFlowMiddleware] Notifying backend for ${action.payload}` ); return next({ ...action, payload: continueBusinessFlow }); } next(action); }; /** * 从检查到图像处理 */ function isFromExamToView(currentAction: string, currentKey: string): boolean { return currentAction === 'view' && currentKey === 'exam'; } function isExitingExam(currentAction: string, currentKey: string): boolean { console.log( `[businessFlowMiddleware] Checking if exiting exam: ${currentAction} vs ${currentKey}` ); return currentAction !== 'exam' && currentKey === 'exam'; } export default businessFlowMiddlewareLogic;