2016-07-13 12:44:24 -08:00
|
|
|
import rpc from '../rpc';
|
2016-09-21 06:27:11 -08:00
|
|
|
import {keys} from '../utils/object';
|
2016-10-25 04:53:15 -08:00
|
|
|
import findBySession from '../utils/term-groups';
|
2016-07-13 12:44:24 -08:00
|
|
|
import {
|
|
|
|
|
SESSION_ADD,
|
|
|
|
|
SESSION_RESIZE,
|
|
|
|
|
SESSION_REQUEST,
|
2017-09-25 03:55:04 -08:00
|
|
|
SESSION_ADD_DATA,
|
2016-07-13 12:44:24 -08:00
|
|
|
SESSION_PTY_DATA,
|
|
|
|
|
SESSION_PTY_EXIT,
|
|
|
|
|
SESSION_USER_EXIT,
|
|
|
|
|
SESSION_SET_ACTIVE,
|
|
|
|
|
SESSION_CLEAR_ACTIVE,
|
|
|
|
|
SESSION_USER_DATA,
|
2019-09-23 09:37:22 -08:00
|
|
|
SESSION_SET_XTERM_TITLE,
|
|
|
|
|
SESSION_SEARCH,
|
|
|
|
|
SESSION_SEARCH_CLOSE
|
2016-07-13 12:44:24 -08:00
|
|
|
} from '../constants/sessions';
|
2019-10-19 08:59:56 -08:00
|
|
|
import {HyperState, session} from '../hyper';
|
|
|
|
|
import {Dispatch} from 'redux';
|
2016-07-13 12:44:24 -08:00
|
|
|
|
2019-10-19 08:59:56 -08:00
|
|
|
export function addSession({uid, shell, pid, cols, rows, splitDirection, activeUid}: session) {
|
|
|
|
|
return (dispatch: Dispatch<any>, getState: () => HyperState) => {
|
2016-10-03 18:00:50 -08:00
|
|
|
const {sessions} = getState();
|
2017-10-03 14:18:55 -08:00
|
|
|
const now = Date.now();
|
2016-07-13 16:21:23 -08:00
|
|
|
dispatch({
|
2016-07-13 15:32:52 -08:00
|
|
|
type: SESSION_ADD,
|
2016-07-14 15:40:15 -08:00
|
|
|
uid,
|
2016-07-16 14:41:13 -08:00
|
|
|
shell,
|
2016-10-03 18:00:50 -08:00
|
|
|
pid,
|
|
|
|
|
cols,
|
|
|
|
|
rows,
|
|
|
|
|
splitDirection,
|
2019-10-14 16:09:30 -08:00
|
|
|
activeUid: activeUid ? activeUid : sessions.activeUid,
|
2017-10-03 14:18:55 -08:00
|
|
|
now
|
2016-07-13 15:32:52 -08:00
|
|
|
});
|
2016-07-13 12:44:24 -08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
export function requestSession() {
|
2019-10-19 08:59:56 -08:00
|
|
|
return (dispatch: Dispatch<any>, getState: () => HyperState) => {
|
2016-07-13 12:44:24 -08:00
|
|
|
dispatch({
|
|
|
|
|
type: SESSION_REQUEST,
|
|
|
|
|
effect: () => {
|
2018-03-21 03:40:56 -08:00
|
|
|
const {ui} = getState();
|
2019-10-10 11:20:26 -08:00
|
|
|
// the cols and rows from preview session maybe not accurate. so remove.
|
|
|
|
|
const {/*cols, rows,*/ cwd} = ui;
|
|
|
|
|
rpc.emit('new', {cwd});
|
2016-07-13 12:44:24 -08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 08:59:56 -08:00
|
|
|
export function addSessionData(uid: string, data: any) {
|
|
|
|
|
return (dispatch: Dispatch<any>) => {
|
2017-09-25 03:55:04 -08:00
|
|
|
dispatch({
|
|
|
|
|
type: SESSION_ADD_DATA,
|
|
|
|
|
data,
|
|
|
|
|
effect() {
|
2017-10-03 14:18:55 -08:00
|
|
|
const now = Date.now();
|
2017-09-25 03:55:04 -08:00
|
|
|
dispatch({
|
|
|
|
|
type: SESSION_PTY_DATA,
|
|
|
|
|
uid,
|
2017-10-03 14:18:55 -08:00
|
|
|
data,
|
|
|
|
|
now
|
2017-09-25 03:55:04 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2016-07-13 12:44:24 -08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 08:59:56 -08:00
|
|
|
function createExitAction(type: string) {
|
|
|
|
|
return (uid: string) => (dispatch: Dispatch<any>, getState: () => HyperState) => {
|
2016-07-13 12:44:24 -08:00
|
|
|
return dispatch({
|
2016-10-03 18:00:50 -08:00
|
|
|
type,
|
2016-07-13 12:44:24 -08:00
|
|
|
uid,
|
2016-09-21 06:27:11 -08:00
|
|
|
effect() {
|
2016-10-03 18:00:50 -08:00
|
|
|
if (type === SESSION_USER_EXIT) {
|
|
|
|
|
rpc.emit('exit', {uid});
|
2016-07-13 12:44:24 -08:00
|
|
|
}
|
|
|
|
|
|
2016-07-18 14:12:14 -08:00
|
|
|
const sessions = keys(getState().sessions.sessions);
|
2016-10-12 17:35:44 -08:00
|
|
|
if (sessions.length === 0) {
|
2016-07-18 14:12:14 -08:00
|
|
|
window.close();
|
2016-07-13 12:44:24 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-03 18:00:50 -08:00
|
|
|
// we want to distinguish an exit
|
|
|
|
|
// that's UI initiated vs pty initiated
|
|
|
|
|
export const userExitSession = createExitAction(SESSION_USER_EXIT);
|
|
|
|
|
export const ptyExitSession = createExitAction(SESSION_PTY_EXIT);
|
2016-07-13 12:44:24 -08:00
|
|
|
|
2019-10-19 08:59:56 -08:00
|
|
|
export function setActiveSession(uid: string) {
|
|
|
|
|
return (dispatch: Dispatch<any>) => {
|
2016-07-13 12:44:24 -08:00
|
|
|
dispatch({
|
|
|
|
|
type: SESSION_SET_ACTIVE,
|
2016-11-04 09:52:03 -08:00
|
|
|
uid
|
2016-07-13 12:44:24 -08:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
export function clearActiveSession() {
|
2016-07-13 12:44:24 -08:00
|
|
|
return {
|
|
|
|
|
type: SESSION_CLEAR_ACTIVE
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 08:59:56 -08:00
|
|
|
export function setSessionXtermTitle(uid: string, title: string) {
|
2016-07-13 12:44:24 -08:00
|
|
|
return {
|
|
|
|
|
type: SESSION_SET_XTERM_TITLE,
|
|
|
|
|
uid,
|
|
|
|
|
title
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 08:59:56 -08:00
|
|
|
export function resizeSession(uid: string, cols: number, rows: number) {
|
|
|
|
|
return (dispatch: Dispatch<any>, getState: () => HyperState) => {
|
2016-10-03 18:00:50 -08:00
|
|
|
const {termGroups} = getState();
|
2019-10-19 08:59:56 -08:00
|
|
|
const group = findBySession(termGroups, uid)!;
|
2016-10-03 18:00:50 -08:00
|
|
|
const isStandaloneTerm = !group.parentUid && !group.children.length;
|
2017-10-03 14:18:55 -08:00
|
|
|
const now = Date.now();
|
2016-10-03 18:00:50 -08:00
|
|
|
dispatch({
|
|
|
|
|
type: SESSION_RESIZE,
|
|
|
|
|
uid,
|
|
|
|
|
cols,
|
|
|
|
|
rows,
|
|
|
|
|
isStandaloneTerm,
|
2017-10-03 14:18:55 -08:00
|
|
|
now,
|
2016-10-03 18:00:50 -08:00
|
|
|
effect() {
|
|
|
|
|
rpc.emit('resize', {uid, cols, rows});
|
|
|
|
|
}
|
|
|
|
|
});
|
2016-07-13 12:44:24 -08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 08:59:56 -08:00
|
|
|
export function onSearch(uid: string) {
|
|
|
|
|
return (dispatch: Dispatch<any>, getState: () => HyperState) => {
|
2019-09-23 09:37:22 -08:00
|
|
|
const targetUid = uid || getState().sessions.activeUid;
|
|
|
|
|
dispatch({
|
|
|
|
|
type: SESSION_SEARCH,
|
|
|
|
|
uid: targetUid
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 08:59:56 -08:00
|
|
|
export function closeSearch(uid: string) {
|
|
|
|
|
return (dispatch: Dispatch<any>, getState: () => HyperState) => {
|
2019-09-23 09:37:22 -08:00
|
|
|
const targetUid = uid || getState().sessions.activeUid;
|
|
|
|
|
dispatch({
|
|
|
|
|
type: SESSION_SEARCH_CLOSE,
|
|
|
|
|
uid: targetUid
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 08:59:56 -08:00
|
|
|
export function sendSessionData(uid: string, data: any, escaped: any) {
|
|
|
|
|
return (dispatch: Dispatch<any>, getState: () => HyperState) => {
|
2016-08-04 05:43:57 -08:00
|
|
|
dispatch({
|
|
|
|
|
type: SESSION_USER_DATA,
|
|
|
|
|
data,
|
2016-09-21 06:27:11 -08:00
|
|
|
effect() {
|
2016-10-10 02:26:47 -08:00
|
|
|
// If no uid is passed, data is sent to the active session.
|
2016-08-04 05:43:57 -08:00
|
|
|
const targetUid = uid || getState().sessions.activeUid;
|
2017-06-14 08:12:03 -08:00
|
|
|
|
|
|
|
|
rpc.emit('data', {uid: targetUid, data, escaped});
|
2016-08-04 05:43:57 -08:00
|
|
|
}
|
|
|
|
|
});
|
2016-07-13 12:44:24 -08:00
|
|
|
};
|
|
|
|
|
}
|