porting files in lib/ actions, reducers and utils/plugins to ts (#3887)

* add type definitions

* rename files in lib/actions

* rename files from lib/reducers to ts

* renamed plugins.js to ts

* Add hyper types

* Fix ts errors in lib/reducers

* Fix ts errors in lib/actions

* Fix ts errors in plugins.ts
This commit is contained in:
Labhansh Agrawal 2019-10-19 22:29:56 +05:30 committed by Benjamin Staneck
parent 27c9cc3892
commit 537c746c75
14 changed files with 479 additions and 280 deletions

View file

@ -1,12 +1,13 @@
import rpc from '../rpc';
import INIT from '../constants';
import {Dispatch} from 'redux';
export default function init() {
return dispatch => {
return (dispatch: Dispatch<any>) => {
dispatch({
type: INIT,
effect: () => {
rpc.emit('init');
rpc.emit('init', null);
}
});
};

View file

@ -1,13 +1,13 @@
import {NOTIFICATION_MESSAGE, NOTIFICATION_DISMISS} from '../constants/notifications';
export function dismissNotification(id) {
export function dismissNotification(id: string) {
return {
type: NOTIFICATION_DISMISS,
id
};
}
export function addNotificationMessage(text, url = null, dismissable = true) {
export function addNotificationMessage(text: string, url: string | null = null, dismissable = true) {
return {
type: NOTIFICATION_MESSAGE,
text,

View file

@ -16,9 +16,11 @@ import {
SESSION_SEARCH,
SESSION_SEARCH_CLOSE
} from '../constants/sessions';
import {HyperState, session} from '../hyper';
import {Dispatch} from 'redux';
export function addSession({uid, shell, pid, cols, rows, splitDirection, activeUid}) {
return (dispatch, getState) => {
export function addSession({uid, shell, pid, cols, rows, splitDirection, activeUid}: session) {
return (dispatch: Dispatch<any>, getState: () => HyperState) => {
const {sessions} = getState();
const now = Date.now();
dispatch({
@ -36,7 +38,7 @@ export function addSession({uid, shell, pid, cols, rows, splitDirection, activeU
}
export function requestSession() {
return (dispatch, getState) => {
return (dispatch: Dispatch<any>, getState: () => HyperState) => {
dispatch({
type: SESSION_REQUEST,
effect: () => {
@ -49,8 +51,8 @@ export function requestSession() {
};
}
export function addSessionData(uid, data) {
return dispatch => {
export function addSessionData(uid: string, data: any) {
return (dispatch: Dispatch<any>) => {
dispatch({
type: SESSION_ADD_DATA,
data,
@ -67,8 +69,8 @@ export function addSessionData(uid, data) {
};
}
function createExitAction(type) {
return uid => (dispatch, getState) => {
function createExitAction(type: string) {
return (uid: string) => (dispatch: Dispatch<any>, getState: () => HyperState) => {
return dispatch({
type,
uid,
@ -91,8 +93,8 @@ function createExitAction(type) {
export const userExitSession = createExitAction(SESSION_USER_EXIT);
export const ptyExitSession = createExitAction(SESSION_PTY_EXIT);
export function setActiveSession(uid) {
return dispatch => {
export function setActiveSession(uid: string) {
return (dispatch: Dispatch<any>) => {
dispatch({
type: SESSION_SET_ACTIVE,
uid
@ -106,7 +108,7 @@ export function clearActiveSession() {
};
}
export function setSessionXtermTitle(uid, title) {
export function setSessionXtermTitle(uid: string, title: string) {
return {
type: SESSION_SET_XTERM_TITLE,
uid,
@ -114,10 +116,10 @@ export function setSessionXtermTitle(uid, title) {
};
}
export function resizeSession(uid, cols, rows) {
return (dispatch, getState) => {
export function resizeSession(uid: string, cols: number, rows: number) {
return (dispatch: Dispatch<any>, getState: () => HyperState) => {
const {termGroups} = getState();
const group = findBySession(termGroups, uid);
const group = findBySession(termGroups, uid)!;
const isStandaloneTerm = !group.parentUid && !group.children.length;
const now = Date.now();
dispatch({
@ -134,8 +136,8 @@ export function resizeSession(uid, cols, rows) {
};
}
export function onSearch(uid) {
return (dispatch, getState) => {
export function onSearch(uid: string) {
return (dispatch: Dispatch<any>, getState: () => HyperState) => {
const targetUid = uid || getState().sessions.activeUid;
dispatch({
type: SESSION_SEARCH,
@ -144,8 +146,8 @@ export function onSearch(uid) {
};
}
export function closeSearch(uid) {
return (dispatch, getState) => {
export function closeSearch(uid: string) {
return (dispatch: Dispatch<any>, getState: () => HyperState) => {
const targetUid = uid || getState().sessions.activeUid;
dispatch({
type: SESSION_SEARCH_CLOSE,
@ -154,8 +156,8 @@ export function closeSearch(uid) {
};
}
export function sendSessionData(uid, data, escaped) {
return (dispatch, getState) => {
export function sendSessionData(uid: string, data: any, escaped: any) {
return (dispatch: Dispatch<any>, getState: () => HyperState) => {
dispatch({
type: SESSION_USER_DATA,
data,

View file

@ -10,9 +10,12 @@ import {SESSION_REQUEST} from '../constants/sessions';
import findBySession from '../utils/term-groups';
import getRootGroups from '../selectors';
import {setActiveSession, ptyExitSession, userExitSession} from './sessions';
import {Dispatch} from 'redux';
import {ITermState, ITermGroup, HyperState} from '../hyper';
import {Immutable} from 'seamless-immutable';
function requestSplit(direction) {
return activeUid => (dispatch, getState) => {
function requestSplit(direction: string) {
return (activeUid: string) => (dispatch: Dispatch<any>, getState: () => HyperState): void => {
dispatch({
type: SESSION_REQUEST,
effect: () => {
@ -30,7 +33,7 @@ function requestSplit(direction) {
export const requestVerticalSplit = requestSplit(DIRECTION.VERTICAL);
export const requestHorizontalSplit = requestSplit(DIRECTION.HORIZONTAL);
export function resizeTermGroup(uid, sizes) {
export function resizeTermGroup(uid: string, sizes: number[]) {
return {
uid,
type: TERM_GROUP_RESIZE,
@ -38,8 +41,8 @@ export function resizeTermGroup(uid, sizes) {
};
}
export function requestTermGroup(activeUid) {
return (dispatch, getState) => {
export function requestTermGroup(activeUid: string) {
return (dispatch: Dispatch<any>, getState: () => HyperState) => {
dispatch({
type: TERM_GROUP_REQUEST,
effect: () => {
@ -55,8 +58,8 @@ export function requestTermGroup(activeUid) {
};
}
export function setActiveGroup(uid) {
return (dispatch, getState) => {
export function setActiveGroup(uid: string) {
return (dispatch: Dispatch<any>, getState: () => HyperState) => {
const {termGroups} = getState();
dispatch(setActiveSession(termGroups.activeSessions[uid]));
};
@ -65,12 +68,12 @@ export function setActiveGroup(uid) {
// When we've found the next group which we want to
// set as active (after closing something), we also need
// to find the first child group which has a sessionUid.
const findFirstSession = (state, group) => {
const findFirstSession = (state: Immutable<ITermState>, group: Immutable<ITermGroup>): string | undefined => {
if (group.sessionUid) {
return group.sessionUid;
}
for (const childUid of group.children) {
for (const childUid of group.children.asMutable()) {
const child = state.termGroups[childUid];
// We want to find the *leftmost* session,
// even if it's nested deep down:
@ -81,14 +84,14 @@ const findFirstSession = (state, group) => {
}
};
const findPrevious = (list, old) => {
const findPrevious = <T>(list: T[], old: T) => {
const index = list.indexOf(old);
// If `old` was the first item in the list,
// choose the other item available:
return index ? list[index - 1] : list[1];
};
const findNextSessionUid = (state, group) => {
const findNextSessionUid = (state: Immutable<ITermState>, group: Immutable<ITermGroup>) => {
// If we're closing a root group (i.e. a whole tab),
// the next group needs to be a root group as well:
if (state.activeRootGroup === group.uid) {
@ -97,13 +100,13 @@ const findNextSessionUid = (state, group) => {
return findFirstSession(state, nextGroup);
}
const {children} = state.termGroups[group.parentUid];
const nextUid = findPrevious(children, group.uid);
return findFirstSession(state, state.termGroups[nextUid]);
const {children} = state.termGroups[group.parentUid!];
const nextUid = findPrevious(children.asMutable(), group.uid);
return findFirstSession(state, state.termGroups[nextUid!]);
};
export function ptyExitTermGroup(sessionUid) {
return (dispatch, getState) => {
export function ptyExitTermGroup(sessionUid: string) {
return (dispatch: Dispatch<any>, getState: () => HyperState) => {
const {termGroups} = getState();
const group = findBySession(termGroups, sessionUid);
// This might have already been closed:
@ -115,10 +118,10 @@ export function ptyExitTermGroup(sessionUid) {
type: TERM_GROUP_EXIT,
uid: group.uid,
effect: () => {
const activeSessionUid = termGroups.activeSessions[termGroups.activeRootGroup];
const activeSessionUid = termGroups.activeSessions[termGroups.activeRootGroup!];
if (Object.keys(termGroups.termGroups).length > 1 && activeSessionUid === sessionUid) {
const nextSessionUid = findNextSessionUid(termGroups, group);
dispatch(setActiveSession(nextSessionUid));
dispatch(setActiveSession(nextSessionUid!));
}
dispatch(ptyExitSession(sessionUid));
@ -127,8 +130,8 @@ export function ptyExitTermGroup(sessionUid) {
};
}
export function userExitTermGroup(uid) {
return (dispatch, getState) => {
export function userExitTermGroup(uid: string) {
return (dispatch: Dispatch<any>, getState: () => HyperState) => {
const {termGroups} = getState();
dispatch({
type: TERM_GROUP_EXIT,
@ -138,13 +141,13 @@ export function userExitTermGroup(uid) {
if (Object.keys(termGroups.termGroups).length <= 1) {
// No need to attempt finding a new active session
// if this is the last one we've got:
return dispatch(userExitSession(group.sessionUid));
return dispatch(userExitSession(group.sessionUid!));
}
const activeSessionUid = termGroups.activeSessions[termGroups.activeRootGroup];
const activeSessionUid = termGroups.activeSessions[termGroups.activeRootGroup!];
if (termGroups.activeRootGroup === uid || activeSessionUid === group.sessionUid) {
const nextSessionUid = findNextSessionUid(termGroups, group);
dispatch(setActiveSession(nextSessionUid));
dispatch(setActiveSession(nextSessionUid!));
}
if (group.sessionUid) {
@ -160,13 +163,13 @@ export function userExitTermGroup(uid) {
}
export function exitActiveTermGroup() {
return (dispatch, getState) => {
return (dispatch: Dispatch<any>, getState: () => HyperState) => {
dispatch({
type: TERM_GROUP_EXIT_ACTIVE,
effect() {
const {sessions, termGroups} = getState();
const {uid} = findBySession(termGroups, sessions.activeUid);
dispatch(userExitTermGroup(uid));
const {uid} = findBySession(termGroups, sessions.activeUid!)!;
dispatch(userExitTermGroup(uid!));
}
});
};

View file

@ -28,11 +28,14 @@ import {
import {setActiveGroup} from './term-groups';
import parseUrl from 'parse-url';
import {Dispatch} from 'redux';
import {HyperState} from '../hyper';
import {Stats} from 'fs';
const {stat} = window.require('fs');
export function openContextMenu(uid, selection) {
return (dispatch, getState) => {
export function openContextMenu(uid: string, selection: any) {
return (dispatch: Dispatch<any>, getState: () => HyperState) => {
dispatch({
type: UI_CONTEXTMENU_OPEN,
uid,
@ -48,7 +51,7 @@ export function openContextMenu(uid, selection) {
}
export function increaseFontSize() {
return (dispatch, getState) => {
return (dispatch: Dispatch<any>, getState: () => HyperState) => {
dispatch({
type: UI_FONT_SIZE_INCR,
effect() {
@ -65,7 +68,7 @@ export function increaseFontSize() {
}
export function decreaseFontSize() {
return (dispatch, getState) => {
return (dispatch: Dispatch<any>, getState: () => HyperState) => {
dispatch({
type: UI_FONT_SIZE_DECR,
effect() {
@ -89,7 +92,7 @@ export function resetFontSize() {
}
export function setFontSmoothing() {
return dispatch => {
return (dispatch: Dispatch<any>) => {
setTimeout(() => {
const devicePixelRatio = window.devicePixelRatio;
const fontSmoothing = devicePixelRatio < 2 ? 'subpixel-antialiased' : 'antialiased';
@ -110,18 +113,21 @@ export function windowGeometryUpdated() {
// Find all sessions that are below the given
// termGroup uid in the hierarchy:
const findChildSessions = (termGroups, uid) => {
const findChildSessions = (termGroups: any, uid: string): string[] => {
const group = termGroups[uid];
if (group.sessionUid) {
return [uid];
}
return group.children.reduce((total, childUid) => total.concat(findChildSessions(termGroups, childUid)), []);
return group.children.reduce(
(total: string[], childUid: string) => total.concat(findChildSessions(termGroups, childUid)),
[]
);
};
// Get the index of the next or previous group,
// depending on the movement direction:
const getNeighborIndex = (groups, uid, type) => {
const getNeighborIndex = (groups: string[], uid: string, type: string) => {
if (type === UI_MOVE_NEXT_PANE) {
return (groups.indexOf(uid) + 1) % groups.length;
}
@ -129,21 +135,21 @@ const getNeighborIndex = (groups, uid, type) => {
return (groups.indexOf(uid) + groups.length - 1) % groups.length;
};
function moveToNeighborPane(type) {
return () => (dispatch, getState) => {
function moveToNeighborPane(type: string) {
return () => (dispatch: Dispatch<any>, getState: () => HyperState) => {
dispatch({
type,
effect() {
const {sessions, termGroups} = getState();
const {uid} = findBySession(termGroups, sessions.activeUid);
const childGroups = findChildSessions(termGroups.termGroups, termGroups.activeRootGroup);
const {uid} = findBySession(termGroups, sessions.activeUid!)!;
const childGroups = findChildSessions(termGroups.termGroups, termGroups.activeRootGroup!);
if (childGroups.length === 1) {
//eslint-disable-next-line no-console
console.log('ignoring move for single group');
} else {
const index = getNeighborIndex(childGroups, uid, type);
const index = getNeighborIndex(childGroups, uid!, type);
const {sessionUid} = termGroups.termGroups[childGroups[index]];
dispatch(setActiveSession(sessionUid));
dispatch(setActiveSession(sessionUid!));
}
}
});
@ -153,13 +159,13 @@ function moveToNeighborPane(type) {
export const moveToNextPane = moveToNeighborPane(UI_MOVE_NEXT_PANE);
export const moveToPreviousPane = moveToNeighborPane(UI_MOVE_PREV_PANE);
const getGroupUids = state => {
const getGroupUids = (state: HyperState) => {
const rootGroups = getRootGroups(state);
return rootGroups.map(({uid}) => uid);
};
export function moveLeft() {
return (dispatch, getState) => {
return (dispatch: Dispatch<any>, getState: () => HyperState) => {
dispatch({
type: UI_MOVE_LEFT,
effect() {
@ -180,7 +186,7 @@ export function moveLeft() {
}
export function moveRight() {
return (dispatch, getState) => {
return (dispatch: Dispatch<any>, getState: () => HyperState) => {
dispatch({
type: UI_MOVE_RIGHT,
effect() {
@ -200,8 +206,8 @@ export function moveRight() {
};
}
export function moveTo(i) {
return (dispatch, getState) => {
export function moveTo(i: number | 'last') {
return (dispatch: Dispatch<any>, getState: () => HyperState) => {
if (i === 'last') {
// Finding last tab index
const {termGroups} = getState().termGroups;
@ -217,11 +223,11 @@ export function moveTo(i) {
const state = getState();
const groupUids = getGroupUids(state);
const uid = state.termGroups.activeRootGroup;
if (uid === groupUids[i]) {
if (uid === groupUids[i as number]) {
//eslint-disable-next-line no-console
console.log('ignoring same uid');
} else if (groupUids[i]) {
dispatch(setActiveGroup(groupUids[i]));
} else if (groupUids[i as number]) {
dispatch(setActiveGroup(groupUids[i as number]));
} else {
//eslint-disable-next-line no-console
console.log('ignoring inexistent index', i);
@ -231,8 +237,8 @@ export function moveTo(i) {
};
}
export function windowMove(window) {
return dispatch => {
export function windowMove(window: any) {
return (dispatch: Dispatch<any>) => {
dispatch({
type: UI_WINDOW_MOVE,
window,
@ -244,7 +250,7 @@ export function windowMove(window) {
}
export function windowGeometryChange() {
return dispatch => {
return (dispatch: Dispatch<any>) => {
dispatch({
type: UI_WINDOW_MOVE,
effect() {
@ -254,12 +260,12 @@ export function windowGeometryChange() {
};
}
export function openFile(path) {
return dispatch => {
export function openFile(path: string) {
return (dispatch: Dispatch<any>) => {
dispatch({
type: UI_OPEN_FILE,
effect() {
stat(path, (err, stats) => {
stat(path, (err: any, stats: Stats) => {
if (err) {
notify('Unable to open path', `"${path}" doesn't exist.`, {error: err});
} else {
@ -271,7 +277,7 @@ export function openFile(path) {
}
rpc.once('session add', ({uid}) => {
rpc.once('session data', () => {
dispatch(sendSessionData(uid, command));
dispatch(sendSessionData(uid, command, null));
});
});
}
@ -294,12 +300,12 @@ export function leaveFullScreen() {
};
}
export function openSSH(url) {
return dispatch => {
export function openSSH(url: string) {
return (dispatch: Dispatch<any>) => {
dispatch({
type: UI_OPEN_SSH_URL,
effect() {
let parsedUrl = parseUrl(url, true);
const parsedUrl = parseUrl(url, true);
let command = parsedUrl.protocol + ' ' + (parsedUrl.user ? `${parsedUrl.user}@` : '') + parsedUrl.resource;
if (parsedUrl.port) command += ' -p ' + parsedUrl.port;
@ -308,7 +314,7 @@ export function openSSH(url) {
rpc.once('session add', ({uid}) => {
rpc.once('session data', () => {
dispatch(sendSessionData(uid, command));
dispatch(sendSessionData(uid, command, null));
});
});
@ -318,8 +324,8 @@ export function openSSH(url) {
};
}
export function execCommand(command, fn, e) {
return dispatch =>
export function execCommand(command: any, fn: any, e: any) {
return (dispatch: Dispatch<any>) =>
dispatch({
type: UI_COMMAND_EXEC,
command,