From c71b24ebeadea3f5d131f26db964d8da655c764b Mon Sep 17 00:00:00 2001 From: Labhansh Agrawal Date: Sat, 28 Dec 2019 16:09:17 +0530 Subject: [PATCH] improve reducer typings --- lib/hyper.d.ts | 21 ++++++++++++--------- lib/reducers/index.ts | 16 ++++++++++++---- lib/reducers/sessions.ts | 8 +++++--- lib/reducers/term-groups.ts | 21 +++++++-------------- lib/reducers/ui.ts | 6 ++++-- 5 files changed, 40 insertions(+), 32 deletions(-) diff --git a/lib/hyper.d.ts b/lib/hyper.d.ts index d6af5f2a..a1908699 100644 --- a/lib/hyper.d.ts +++ b/lib/hyper.d.ts @@ -1,4 +1,3 @@ -import {Reducer} from 'redux'; import {Immutable} from 'seamless-immutable'; declare global { @@ -26,10 +25,8 @@ export type ITermState = { activeRootGroup: string | null; }; -export type ITermGroupReducer = Reducer, any>; - export type uiState = { - _lastUpdate: null; + _lastUpdate: number | null; activeUid: string | null; activityMarkers: Record; backgroundColor: string; @@ -77,8 +74,8 @@ export type uiState = { macOptionSelectionMode: string; maximized: boolean; messageDismissable: null | boolean; - messageText: null; - messageURL: null; + messageText: string | null; + messageURL: string | null; modifierKeys: { altIsMeta: boolean; cmdIsMeta: boolean; @@ -107,7 +104,6 @@ export type uiState = { webGLRenderer: boolean; }; -export type IUiReducer = Reducer>; export type session = { cleared: boolean; cols: number | null; @@ -115,7 +111,7 @@ export type session = { resizeAt?: number; rows: number | null; search: boolean; - shell: string; + shell: string | null; title: string; uid: string; url: string | null; @@ -128,7 +124,14 @@ export type sessionState = { write?: any; }; -export type ISessionReducer = Reducer>; +export {ITermGroupReducer} from './reducers/term-groups'; +import {ITermGroupReducer} from './reducers/term-groups'; + +export {IUiReducer} from './reducers/ui'; +import {IUiReducer} from './reducers/ui'; + +export {ISessionReducer} from './reducers/sessions'; +import {ISessionReducer} from './reducers/sessions'; export type hyperPlugin = { getTabProps: any; diff --git a/lib/reducers/index.ts b/lib/reducers/index.ts index 85c5cc10..74228dac 100644 --- a/lib/reducers/index.ts +++ b/lib/reducers/index.ts @@ -1,9 +1,17 @@ import {combineReducers} from 'redux'; -import ui from './ui'; -import sessions from './sessions'; -import termGroups from './term-groups'; +import ui, {IUiReducer} from './ui'; +import sessions, {ISessionReducer} from './sessions'; +import termGroups, {ITermGroupReducer} from './term-groups'; +import {HyperActions} from '../hyper'; -export default combineReducers({ +export default combineReducers< + { + ui: ReturnType; + sessions: ReturnType; + termGroups: ReturnType; + }, + HyperActions +>({ ui, sessions, termGroups diff --git a/lib/reducers/sessions.ts b/lib/reducers/sessions.ts index fe837669..060a6fdf 100644 --- a/lib/reducers/sessions.ts +++ b/lib/reducers/sessions.ts @@ -13,7 +13,7 @@ import { SESSION_SEARCH, SESSION_SEARCH_CLOSE } from '../constants/sessions'; -import {sessionState, session} from '../hyper'; +import {sessionState, session, HyperActions} from '../hyper'; const initialState: ImmutableType = Immutable({ sessions: {}, @@ -36,14 +36,14 @@ function Session(obj: Immutable.DeepPartial) { } function deleteSession(state: ImmutableType, uid: string) { - return state.updateIn(['sessions'], (sessions: ImmutableType) => { + return state.updateIn(['sessions'], (sessions: typeof state['sessions']) => { const sessions_ = sessions.asMutable(); delete sessions_[uid]; return sessions_; }); } -const reducer = (state: ImmutableType = initialState, action: any) => { +const reducer = (state: ImmutableType = initialState, action: HyperActions) => { switch (action.type) { case SESSION_ADD: return state.set('activeUid', action.uid).setIn( @@ -135,4 +135,6 @@ const reducer = (state: ImmutableType = initialState, action: any) } }; +export type ISessionReducer = typeof reducer; + export default decorateSessionsReducer(reducer); diff --git a/lib/reducers/term-groups.ts b/lib/reducers/term-groups.ts index 2c46ed49..5ebba197 100644 --- a/lib/reducers/term-groups.ts +++ b/lib/reducers/term-groups.ts @@ -1,10 +1,10 @@ import uuid from 'uuid'; import Immutable, {Immutable as ImmutableType} from 'seamless-immutable'; import {TERM_GROUP_EXIT, TERM_GROUP_RESIZE} from '../constants/term-groups'; -import {SESSION_ADD, SESSION_SET_ACTIVE} from '../constants/sessions'; +import {SESSION_ADD, SESSION_SET_ACTIVE, SessionAddAction} from '../constants/sessions'; import findBySession from '../utils/term-groups'; import {decorateTermGroupsReducer} from '../utils/plugins'; -import {ITermGroup, ITermState, ITermGroups} from '../hyper'; +import {ITermGroup, ITermState, ITermGroups, HyperActions} from '../hyper'; const MIN_SIZE = 0.05; const initialState = Immutable({ @@ -66,9 +66,9 @@ const removalRebalance = (oldSizes: ImmutableType, index: number) => { ); }; -const splitGroup = (state: ImmutableType, action: {splitDirection: any; uid: string; activeUid: any}) => { +const splitGroup = (state: ImmutableType, action: SessionAddAction) => { const {splitDirection, uid, activeUid} = action; - const activeGroup = findBySession(state, activeUid)!; + const activeGroup = findBySession(state, activeUid!)!; // If we're splitting in the same direction as the current active // group's parent - or if it's the first split for that group - // we want the parent to get another child: @@ -197,16 +197,7 @@ const resizeGroup = (state: ImmutableType, uid: any, sizes: number[] return state.setIn(['termGroups', uid, 'sizes'], sizes); }; -const reducer = ( - state = initialState, - action: { - splitDirection: any; - uid: any; - activeUid: any; - type: any; - sizes: any; - } -) => { +const reducer = (state = initialState, action: HyperActions) => { switch (action.type) { case SESSION_ADD: { if (action.splitDirection) { @@ -236,4 +227,6 @@ const reducer = ( } }; +export type ITermGroupReducer = typeof reducer; + export default decorateTermGroupsReducer(reducer); diff --git a/lib/reducers/ui.ts b/lib/reducers/ui.ts index baf5f309..cc0163fd 100644 --- a/lib/reducers/ui.ts +++ b/lib/reducers/ui.ts @@ -24,7 +24,7 @@ import { SESSION_SET_CWD } from '../constants/sessions'; import {UPDATE_AVAILABLE} from '../constants/updater'; -import {uiState} from '../hyper'; +import {uiState, HyperActions} from '../hyper'; const allowedCursorShapes = new Set(['BEAM', 'BLOCK', 'UNDERLINE']); const allowedCursorBlinkValues = new Set([true, false]); @@ -114,7 +114,7 @@ const initial: ImmutableType = Immutable({ const currentWindow = remote.getCurrentWindow(); -const reducer = (state = initial, action: any) => { +const reducer = (state = initial, action: HyperActions) => { let state_ = state; let isMax; //eslint-disable-next-line default-case @@ -450,4 +450,6 @@ const reducer = (state = initial, action: any) => { return state_; }; +export type IUiReducer = typeof reducer; + export default decorateUIReducer(reducer);