Skip to content

Commit

Permalink
split panes: handle PTY exits
Browse files Browse the repository at this point in the history
  • Loading branch information
ekmartin committed Oct 3, 2016
1 parent 5191cf2 commit b2ca957
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 29 deletions.
4 changes: 2 additions & 2 deletions lib/actions/header.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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) => {
dispatch({
type: CLOSE_TAB,
uid,
effect () {
dispatch(exitTermGroup(uid));
dispatch(userExitTermGroup(uid));
}
});
};
Expand Down
30 changes: 9 additions & 21 deletions lib/actions/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) => {
Expand Down
32 changes: 27 additions & 5 deletions lib/actions/term-groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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({
Expand All @@ -121,22 +143,22 @@ export function exitTermGroup (uid) {
dispatch(userExitSession(group.sessionUid));
} else {
group.children.forEach(childUid => {
dispatch(exitTermGroup(childUid));
dispatch(userExitTermGroup(childUid));
});
}
}
});
};
}

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));
}
});
};
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit b2ca957

Please sign in to comment.