From d232ec27c0258e5159cdb5325011a0f5bff2d67d Mon Sep 17 00:00:00 2001 From: Labhansh Agrawal Date: Sun, 20 Oct 2019 13:38:48 +0530 Subject: [PATCH] Typings fixes in terms and sessions --- lib/hyper.d.ts | 24 ++++++++++----------- lib/reducers/sessions.ts | 9 ++++---- lib/reducers/term-groups.ts | 43 +++++++++++++++++++++---------------- 3 files changed, 41 insertions(+), 35 deletions(-) diff --git a/lib/hyper.d.ts b/lib/hyper.d.ts index 10cf2c1f..dfa258c8 100644 --- a/lib/hyper.d.ts +++ b/lib/hyper.d.ts @@ -8,7 +8,7 @@ declare global { } export type ITermGroup = { - uid: string | null; + uid: string; sessionUid: string | null; parentUid: string | null; direction: string | null; @@ -108,20 +108,20 @@ export type uiState = { export type IUiReducer = Reducer>; export type session = { cleared: boolean; - cols: null; - pid: null; - resizeAt: number; - rows: null; + cols: number | null; + pid: number | null; + resizeAt?: number; + rows: number | null; search: boolean; shell: string; title: string; uid: string; - url: null; - splitDirection: string; - activeUid: string; + url: string | null; + splitDirection?: string; + activeUid?: string; }; export type sessionState = { - sessions: Record>; + sessions: Record; activeUid: string | null; }; @@ -144,9 +144,9 @@ export type hyperPlugin = { mapTermsState: any; middleware: any; onRendererWindow: any; - reduceSessions: any; - reduceTermGroups: any; - reduceUI: any; + reduceSessions: ISessionReducer; + reduceTermGroups: ITermGroupReducer; + reduceUI: IUiReducer; }; import rootReducer from './reducers/index'; diff --git a/lib/reducers/sessions.ts b/lib/reducers/sessions.ts index dfc58c03..fe837669 100644 --- a/lib/reducers/sessions.ts +++ b/lib/reducers/sessions.ts @@ -13,15 +13,15 @@ import { SESSION_SEARCH, SESSION_SEARCH_CLOSE } from '../constants/sessions'; -import {sessionState} from '../hyper'; +import {sessionState, session} from '../hyper'; const initialState: ImmutableType = Immutable({ sessions: {}, activeUid: null }); -function Session(obj: Immutable.DeepPartial) { - return Immutable({ +function Session(obj: Immutable.DeepPartial) { + const x: session = { uid: '', title: '', cols: null, @@ -31,7 +31,8 @@ function Session(obj: Immutable.DeepPartial) { search: false, shell: '', pid: null - }).merge(obj); + }; + return Immutable(x).merge(obj); } function deleteSession(state: ImmutableType, uid: string) { diff --git a/lib/reducers/term-groups.ts b/lib/reducers/term-groups.ts index aa9c68ac..2c46ed49 100644 --- a/lib/reducers/term-groups.ts +++ b/lib/reducers/term-groups.ts @@ -14,14 +14,15 @@ const initialState = Immutable({ }); function TermGroup(obj: Immutable.DeepPartial) { - return Immutable({ - uid: null, + const x: ITermGroup = { + uid: '', sessionUid: null, parentUid: null, direction: null, sizes: null, children: [] - } as ITermGroup).merge(obj); + }; + return Immutable(x).merge(obj); } // Recurse upwards until we find a root term group (no parent). @@ -40,8 +41,8 @@ const setActiveGroup = (state: ImmutableType, action: {uid: string}) } const childGroup = findBySession(state, action.uid)!; - const rootGroup = findRootGroup(state.termGroups, childGroup.uid!); - return state.set('activeRootGroup', rootGroup.uid).setIn(['activeSessions', rootGroup.uid as string], action.uid); + const rootGroup = findRootGroup(state.termGroups, childGroup.uid); + return state.set('activeRootGroup', rootGroup.uid).setIn(['activeSessions', rootGroup.uid], action.uid); }; // Reduce existing sizes to fit a new split: @@ -65,7 +66,7 @@ const removalRebalance = (oldSizes: ImmutableType, index: number) => { ); }; -const splitGroup = (state: ImmutableType, action: {splitDirection: any; uid: any; activeUid: any}) => { +const splitGroup = (state: ImmutableType, action: {splitDirection: any; uid: string; activeUid: any}) => { const {splitDirection, uid, activeUid} = action; const activeGroup = findBySession(state, activeUid)!; // If we're splitting in the same direction as the current active @@ -90,7 +91,7 @@ const splitGroup = (state: ImmutableType, action: {splitDirection: a parentUid: parentGroup.uid }); - state = state.setIn(['termGroups', newSession.uid as string], newSession); + state = state.setIn(['termGroups', newSession.uid], newSession); if (parentGroup.sessionUid) { const existingSession = TermGroup({ uid: uuid.v4(), @@ -98,22 +99,22 @@ const splitGroup = (state: ImmutableType, action: {splitDirection: a parentUid: parentGroup.uid }); - return state.setIn(['termGroups', existingSession.uid as string], existingSession).setIn( - ['termGroups', parentGroup.uid!], + return state.setIn(['termGroups', existingSession.uid], existingSession).setIn( + ['termGroups', parentGroup.uid], parentGroup.merge({ sessionUid: '', direction: splitDirection, - children: [existingSession.uid!, newSession.uid!] + children: [existingSession.uid, newSession.uid] }) ); } const {children} = parentGroup; // Insert the new child pane right after the active one: - const index = children.indexOf(activeGroup.uid!) + 1; - const newChildren = [...children.slice(0, index).asMutable(), newSession.uid!, ...children.slice(index).asMutable()]; + const index = children.indexOf(activeGroup.uid) + 1; + const newChildren = [...children.slice(0, index).asMutable(), newSession.uid, ...children.slice(index).asMutable()]; state = state.setIn( - ['termGroups', parentGroup.uid!], + ['termGroups', parentGroup.uid], parentGroup.merge({ direction: splitDirection, children: newChildren @@ -122,7 +123,7 @@ const splitGroup = (state: ImmutableType, action: {splitDirection: a if (parentGroup.sizes) { const newSizes = insertRebalance(parentGroup.sizes, index); - state = state.setIn(['termGroups', parentGroup.uid!, 'sizes'], newSizes); + state = state.setIn(['termGroups', parentGroup.uid, 'sizes'], newSizes); } return state; @@ -131,19 +132,23 @@ const splitGroup = (state: ImmutableType, action: {splitDirection: a // Replace the parent by the given child in the tree, // used when we remove another child and we're left // with a one-to-one mapping between parent and child. -const replaceParent = (state: ImmutableType, parent: ImmutableType, child: {uid: any}) => { +const replaceParent = ( + state: ImmutableType, + parent: ImmutableType, + child: ImmutableType +) => { if (parent.parentUid) { const parentParent = state.termGroups[parent.parentUid]; // If the parent we're replacing has a parent, // we need to change the uid in its children array // with `child`: - const newChildren = parentParent.children.map((uid: any) => (uid === parent.uid ? child.uid : uid)); + const newChildren = parentParent.children.map((uid: string) => (uid === parent.uid ? child.uid : uid)); - state = state.setIn(['termGroups', parentParent.uid!, 'children'], newChildren); + state = state.setIn(['termGroups', parentParent.uid, 'children'], newChildren); } else { // This means the given child will be // a root group, so we need to set it up as such: - const newSessions = state.activeSessions.without(parent.uid!).set(child.uid, state.activeSessions[parent.uid!]); + const newSessions = state.activeSessions.without(parent.uid).set(child.uid, state.activeSessions[parent.uid]); state = state .set('activeTermGroup', child.uid) @@ -152,7 +157,7 @@ const replaceParent = (state: ImmutableType, parent: ImmutableType