diff --git a/lib/actions/header.js b/lib/actions/header.js index c0ec61d6fab5..a6debbe90e26 100644 --- a/lib/actions/header.js +++ b/lib/actions/header.js @@ -1,7 +1,7 @@ import { CLOSE_TAB, CHANGE_TAB } from '../constants/tabs'; import { UI_WINDOW_MAXIMIZE, UI_WINDOW_UNMAXIMIZE } from '../constants/ui'; import rpc from '../rpc'; -import { exitTermGroup, setActiveGroup } from './term-groups'; +import { userExitTermGroup, setActiveGroup } from './term-groups'; export function closeTab (uid) { return (dispatch, getState) => { @@ -9,7 +9,7 @@ export function closeTab (uid) { type: CLOSE_TAB, uid, effect () { - dispatch(exitTermGroup(uid)); + dispatch(userExitTermGroup(uid)); } }); }; diff --git a/lib/actions/sessions.js b/lib/actions/sessions.js index 84bf4da6b9f2..fd7559cd2f99 100644 --- a/lib/actions/sessions.js +++ b/lib/actions/sessions.js @@ -78,15 +78,16 @@ export function addSessionData (uid, data) { }; } -export function sessionExit (uid) { - return (dispatch, getState) => { +function createExitAction (type) { + return (uid) => (dispatch, getState) => { return dispatch({ - type: SESSION_PTY_EXIT, + type, uid, effect () { - // we reiterate the same logic as below - // for SESSION_USER_EXIT since the exit - // could happen pty side or optimistic + if (type === SESSION_USER_EXIT) { + rpc.emit('exit', { uid }); + } + const sessions = keys(getState().sessions.sessions); if (!sessions.length) { window.close(); @@ -98,21 +99,8 @@ export function sessionExit (uid) { // we want to distinguish an exit // that's UI initiated vs pty initiated -export function userExitSession (uid) { - return (dispatch, getState) => { - return dispatch({ - type: SESSION_USER_EXIT, - uid, - effect () { - rpc.emit('exit', { uid }); - const sessions = keys(getState().sessions.sessions); - if (!sessions.length) { - window.close(); - } - } - }); - }; -} +export const userExitSession = createExitAction(SESSION_USER_EXIT); +export const ptyExitSession = createExitAction(SESSION_PTY_EXIT); export function setActiveSession (uid) { return (dispatch, getState) => { diff --git a/lib/actions/term-groups.js b/lib/actions/term-groups.js index 6389bf8eb0db..37998c2839dc 100644 --- a/lib/actions/term-groups.js +++ b/lib/actions/term-groups.js @@ -7,7 +7,7 @@ import { TERM_GROUP_EXIT_ACTIVE } from '../constants/term-groups'; import { SESSION_REQUEST } from '../constants/sessions'; -import { setActiveSession, userExitSession } from './sessions'; +import { setActiveSession, ptyExitSession, userExitSession } from './sessions'; import { findBySession } from '../utils/term-groups'; import { getRootGroups } from '../selectors'; @@ -97,7 +97,29 @@ const findNextSessionUid = (state, group) => { return findFirstSession(state, state.termGroups[nextUid]); }; -export function exitTermGroup (uid) { +export function ptyExitTermGroup (sessionUid) { + return (dispatch, getState) => { + const { termGroups } = getState(); + const group = findBySession(termGroups, sessionUid); + // This might have already been closed: + if (!group) return dispatch(ptyExitSession(sessionUid)); + dispatch({ + type: TERM_GROUP_EXIT, + uid: group.uid, + effect: () => { + const activeSessionUid = termGroups.activeSessions[termGroups.activeRootGroup]; + if (Object.keys(termGroups.termGroups).length > 1 && activeSessionUid === sessionUid) { + const nextSessionUid = findNextSessionUid(termGroups, group); + dispatch(setActiveSession(nextSessionUid)); + } + + dispatch(ptyExitSession(sessionUid)); + } + }); + }; +} + +export function userExitTermGroup (uid) { return (dispatch, getState) => { const { termGroups } = getState(); dispatch({ @@ -121,7 +143,7 @@ export function exitTermGroup (uid) { dispatch(userExitSession(group.sessionUid)); } else { group.children.forEach(childUid => { - dispatch(exitTermGroup(childUid)); + dispatch(userExitTermGroup(childUid)); }); } } @@ -129,14 +151,14 @@ export function exitTermGroup (uid) { }; } -export function exitActiveTermGroup (uid) { +export function exitActiveTermGroup () { return (dispatch, getState) => { dispatch({ type: TERM_GROUP_EXIT_ACTIVE, effect () { const { sessions, termGroups } = getState(); const { uid } = findBySession(termGroups, sessions.activeUid); - dispatch(exitTermGroup(uid)); + dispatch(userExitTermGroup(uid)); } }); }; diff --git a/lib/index.js b/lib/index.js index 6949b42da728..25b8fb4d2369 100644 --- a/lib/index.js +++ b/lib/index.js @@ -66,7 +66,7 @@ rpc.on('session title', ({ uid, title }) => { }); rpc.on('session exit', ({ uid }) => { - store_.dispatch(sessionActions.sessionExit(uid)); + store_.dispatch(termGroupActions.ptyExitTermGroup(uid)); }); rpc.on('termgroup close req', () => {