From 22e7aeab9bd970a0282c94f56e3bc210309dcd66 Mon Sep 17 00:00:00 2001 From: Kirill Shumilov Date: Mon, 4 Feb 2019 14:54:16 +0300 Subject: [PATCH] fix(xod-client, xod-client-electron): stop simulation on closing debugger tab --- .../src/debugger/middleware.js | 11 ++------ packages/xod-client/src/core/middlewares.js | 4 +++ .../src/debugger/outdaterMiddleware.js | 21 ++++++++++++++++ packages/xod-client/src/debugger/reducer.js | 2 ++ packages/xod-client/src/debugger/selectors.js | 5 ++++ packages/xod-client/src/debugger/state.js | 1 + packages/xod-client/src/editor/messages.js | 6 +++++ .../src/editor/stopSimulationMiddleware.js | 25 +++++++++++++++++++ 8 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 packages/xod-client/src/debugger/outdaterMiddleware.js create mode 100644 packages/xod-client/src/editor/stopSimulationMiddleware.js diff --git a/packages/xod-client-electron/src/debugger/middleware.js b/packages/xod-client-electron/src/debugger/middleware.js index a3ff056d3..f0f982647 100644 --- a/packages/xod-client-electron/src/debugger/middleware.js +++ b/packages/xod-client-electron/src/debugger/middleware.js @@ -5,19 +5,12 @@ import { DEBUG_SESSION_STOPPED_ON_TAB_CLOSE } from '../shared/messages'; export default store => next => action => { const state = store.getState(); - const isDebugSession = client.isDebugSession(state); - const prevProject = client.getProject(state); + const isSerialDebugRunning = client.isSerialDebugRunning(state); const result = next(action); - const newProject = client.getProject(store.getState()); - - if (isDebugSession && prevProject !== newProject) { - store.dispatch(client.markDebugSessionOutdated()); - } - // Stop debug session if Debugger tab is closed if ( - isDebugSession && + isSerialDebugRunning && action.type === client.TAB_CLOSE && action.payload.id === 'debugger' ) { diff --git a/packages/xod-client/src/core/middlewares.js b/packages/xod-client/src/core/middlewares.js index 63bcecdd6..3403c217d 100644 --- a/packages/xod-client/src/core/middlewares.js +++ b/packages/xod-client/src/core/middlewares.js @@ -7,6 +7,8 @@ import sidebarsMiddleware from '../editor/sidebarsMiddleware'; import crashReporter from './crashReporterMiddleware'; import hintingMiddleware from '../hinting/middleware'; import domSideeffectsMiddleware from './domSideeffectsMiddleware'; +import outdaterMiddleware from '../debugger/outdaterMiddleware'; +import stopSimulationMiddleware from '../editor/stopSimulationMiddleware'; export default (extraMiddlewares = []) => compose( @@ -17,6 +19,8 @@ export default (extraMiddlewares = []) => sidebarsMiddleware, hintingMiddleware, domSideeffectsMiddleware, + outdaterMiddleware, + stopSimulationMiddleware, ...extraMiddlewares ), devtoolsMiddleware diff --git a/packages/xod-client/src/debugger/outdaterMiddleware.js b/packages/xod-client/src/debugger/outdaterMiddleware.js new file mode 100644 index 000000000..7a0ddefec --- /dev/null +++ b/packages/xod-client/src/debugger/outdaterMiddleware.js @@ -0,0 +1,21 @@ +/** + * Middleware marks Debugger session as outdated on any change of the project. + */ + +import { isDebugSession } from './selectors'; +import { markDebugSessionOutdated } from './actions'; +import { getProject } from '../project/selectors'; + +export default store => next => action => { + const state = store.getState(); + const prevProject = getProject(state); + const result = next(action); + + const newProject = getProject(store.getState()); + + if (isDebugSession(state) && prevProject !== newProject) { + store.dispatch(markDebugSessionOutdated()); + } + + return result; +}; diff --git a/packages/xod-client/src/debugger/reducer.js b/packages/xod-client/src/debugger/reducer.js index 02c9e6416..52a2aceb9 100644 --- a/packages/xod-client/src/debugger/reducer.js +++ b/packages/xod-client/src/debugger/reducer.js @@ -374,6 +374,7 @@ export default (state = initialState, action) => { R.assoc('numberOfSkippedSerialLogLines', 0), R.assoc('nodeIdsMap', invertMap(action.payload.nodeIdsMap)), R.assoc('isRunning', true), + R.assoc('isSerialConnected', true), R.assoc('isOutdated', false), showDebuggerPane )(state); @@ -384,6 +385,7 @@ export default (state = initialState, action) => { R.assoc('numberOfSkippedSerialLogLines', 0), R.assoc('watchNodeValues', {}), R.assoc('nodeIdsMap', {}), + R.assoc('isSerialConnected', false), R.assoc('isRunning', false) )(state); case MARK_DEBUG_SESSION_OUTDATED: diff --git a/packages/xod-client/src/debugger/selectors.js b/packages/xod-client/src/debugger/selectors.js index b16b4b88c..14a086b94 100644 --- a/packages/xod-client/src/debugger/selectors.js +++ b/packages/xod-client/src/debugger/selectors.js @@ -18,6 +18,11 @@ export const isDebuggerVisible = R.compose( getDebuggerState ); +export const isSerialDebugRunning = R.compose( + R.prop('isSerialConnected'), + getDebuggerState +); + export const isDebugSessionOutdated = R.compose( R.prop('isOutdated'), getDebuggerState diff --git a/packages/xod-client/src/debugger/state.js b/packages/xod-client/src/debugger/state.js index 78a6afdf2..907565d34 100644 --- a/packages/xod-client/src/debugger/state.js +++ b/packages/xod-client/src/debugger/state.js @@ -4,6 +4,7 @@ import { LOG_TAB_TYPE } from './constants'; export default { isVisible: false, isRunning: false, + isSerialConnected: false, isOutdated: false, [LOG_TAB_TYPE.INSTALLER]: { log: '', diff --git a/packages/xod-client/src/editor/messages.js b/packages/xod-client/src/editor/messages.js index ba274ba07..9506f1b08 100644 --- a/packages/xod-client/src/editor/messages.js +++ b/packages/xod-client/src/editor/messages.js @@ -35,3 +35,9 @@ export const SIMULATION_ALREADY_RUNNING = { solution: 'Stop the current simulation and try again', persistent: true, }; + +export const SIMULATION_STOPPED_BY_CLOSING_TAB = { + title: 'Simulation stopped', + note: 'You closed the debugger tab.', + persistent: false, +}; diff --git a/packages/xod-client/src/editor/stopSimulationMiddleware.js b/packages/xod-client/src/editor/stopSimulationMiddleware.js new file mode 100644 index 000000000..6d8b140fb --- /dev/null +++ b/packages/xod-client/src/editor/stopSimulationMiddleware.js @@ -0,0 +1,25 @@ +/** + * Middleware stops simulation on close debugger tab + */ + +import { isSimulationRunning } from './selectors'; +import { abortSimulation } from './actions'; +import { TAB_CLOSE } from './actionTypes'; +import { DEBUGGER_TAB_ID } from './constants'; +import { SIMULATION_STOPPED_BY_CLOSING_TAB } from './messages'; +import { addError } from '../messages/actions'; + +export default store => next => action => { + const result = next(action); + + if ( + isSimulationRunning(store.getState()) && + action.type === TAB_CLOSE && + action.payload.id === DEBUGGER_TAB_ID + ) { + store.dispatch(abortSimulation()); + store.dispatch(addError(SIMULATION_STOPPED_BY_CLOSING_TAB)); + } + + return result; +};