diff --git a/src/driver/core/FileExplorer.ts b/src/driver/core/FileExplorer.ts index d67423e..73036e3 100644 --- a/src/driver/core/FileExplorer.ts +++ b/src/driver/core/FileExplorer.ts @@ -48,7 +48,9 @@ type Task = () => void const tasksAfterRender: (Task)[] = [] let visibleNodesGenerator: VisibleNodesGenerator -const init: MethodCreator = dispatch => () => +type BoundMethodCreator = MethodCreator + +const init: BoundMethodCreator = dispatch => () => dispatch.call(setStateText, 'Fetching File List...') const githubSubModuleURLRegex = { @@ -124,9 +126,7 @@ function handleParsed(root: TreeNode, parsed: Parsed) { }) } -const setUpTree: MethodCreator< - Props, - ConnectorState, +const setUpTree: BoundMethodCreator< [Pick] > = dispatch => async ({ treeData, metaData, compressSingletonFolder, accessToken }) => { if (!treeData) return @@ -157,27 +157,21 @@ const setUpTree: MethodCreator< dispatch.call(goTo, URLHelper.getCurrentPath(metaData.branchName)) } -const execAfterRender: MethodCreator = dispatch => () => { +const execAfterRender: BoundMethodCreator = dispatch => () => { for (const task of tasksAfterRender) { task() } tasksAfterRender.length = 0 } -const setStateText: MethodCreator< - Props, - ConnectorState, - [ConnectorState['stateText']] -> = dispatch => (text: string) => +const setStateText: BoundMethodCreator<[ConnectorState['stateText']]> = dispatch => ( + text: string, +) => dispatch.set({ stateText: text, }) -const handleKeyDown: MethodCreator< - Props, - ConnectorState, - [React.KeyboardEvent] -> = dispatch => event => { +const handleKeyDown: BoundMethodCreator<[React.KeyboardEvent]> = dispatch => event => { const { searched, visibleNodes } = dispatch.get() if (!visibleNodes) return const { nodes, focusedNode, expandedNodes, depths } = visibleNodes @@ -282,19 +276,16 @@ const handleKeyDown: MethodCreator< } } -const onFocusSearchBar: MethodCreator = dispatch => () => - dispatch.call(focusNode, null, false) +const onFocusSearchBar: BoundMethodCreator = dispatch => () => dispatch.call(focusNode, null, false) -const handleSearchKeyChange: MethodCreator< - Props, - ConnectorState, +const handleSearchKeyChange: BoundMethodCreator< [React.FormEvent] > = dispatch => async event => { const searchKey = event.currentTarget.value await dispatch.call(search, searchKey) } -const search: MethodCreator = dispatch => { +const search: BoundMethodCreator<[string]> = dispatch => { let i = 0 return async searchKey => { dispatch.set({ searchKey }) @@ -307,7 +298,7 @@ const search: MethodCreator = dispatch => { } } -const goTo: MethodCreator = dispatch => async currentPath => { +const goTo: BoundMethodCreator<[string[]]> = dispatch => async currentPath => { await visibleNodesGenerator.search('') tasksAfterRender.push(() => { const nodeExpandedTo = visibleNodesGenerator.expandTo(currentPath.join('/')) @@ -319,15 +310,12 @@ const goTo: MethodCreator = dispatch => async dispatch.set({ searchKey: '', searched: false }) } -const setExpand: MethodCreator = dispatch => ( - node, - expand = false, -) => { +const setExpand: BoundMethodCreator<[TreeNode, boolean]> = dispatch => (node, expand = false) => { visibleNodesGenerator.setExpand(node, expand) dispatch.call(focusNode, node, false) } -const toggleNodeExpansion: MethodCreator = dispatch => ( +const toggleNodeExpansion: BoundMethodCreator<[TreeNode, boolean]> = dispatch => ( node, skipScrollToNode, ) => { @@ -336,7 +324,7 @@ const toggleNodeExpansion: MethodCreator = dispatch => ( +const focusNode: BoundMethodCreator<[TreeNode | null, boolean]> = dispatch => ( node: TreeNode | null, skipScroll = false, ) => { @@ -346,7 +334,7 @@ const focusNode: MethodCreator = dispatch => node => { +const onNodeClick: BoundMethodCreator<[TreeNode]> = dispatch => node => { if (node.type === 'tree') { dispatch.call(toggleNodeExpansion, node, true) } else if (node.type === 'blob') { @@ -359,7 +347,7 @@ const onNodeClick: MethodCreator = dispatch = } } -const updateVisibleNodes: MethodCreator = dispatch => () => { +const updateVisibleNodes: BoundMethodCreator = dispatch => () => { const { visibleNodes } = visibleNodesGenerator dispatch.set({ visibleNodes }) } diff --git a/src/driver/core/SideBar.ts b/src/driver/core/SideBar.ts index 2631dde..38feb3c 100644 --- a/src/driver/core/SideBar.ts +++ b/src/driver/core/SideBar.ts @@ -54,7 +54,9 @@ export type ConnectorState = { setCompressSingleton: GetCreatedMethod } -const init: MethodCreator = dispatch => async () => { +type BoundMethodCreator = MethodCreator + +const init: BoundMethodCreator = dispatch => async () => { const { initializingPromise } = dispatch.get() if (initializingPromise) await initializingPromise @@ -156,7 +158,7 @@ const init: MethodCreator = dispatch => async () => { } } -const handleError: MethodCreator = dispatch => async err => { +const handleError: BoundMethodCreator<[Error]> = dispatch => async err => { if (err.message === EMPTY_PROJECT) { dispatch.call(setError, 'This project seems to be empty.') } else if (err.message === BLOCKED_PROJECT) { @@ -176,7 +178,7 @@ const handleError: MethodCreator = dispatch => a } } -const onPJAXEnd: MethodCreator = dispatch => () => { +const onPJAXEnd: BoundMethodCreator = dispatch => () => { const { metaData, copyFileButton, copySnippetButton } = dispatch.get() DOMHelper.unmountTopProgressBar() DOMHelper.decorateGitHubPageContent({ copyFileButton, copySnippetButton }) @@ -185,7 +187,7 @@ const onPJAXEnd: MethodCreator = dispatch => () => { dispatch.call(setMetaData, mergedMetaData) } -const onKeyDown: MethodCreator = dispatch => e => { +const onKeyDown: BoundMethodCreator<[KeyboardEvent]> = dispatch => e => { const { toggleShowSideBarShortcut } = dispatch.get() if (toggleShowSideBarShortcut) { const keys = keyHelper.parseEvent(e) @@ -195,41 +197,31 @@ const onKeyDown: MethodCreator = dispatc } } -const toggleShowSideBar: MethodCreator = dispatch => () => +const toggleShowSideBar: BoundMethodCreator = dispatch => () => dispatch.call(setShouldShow, !dispatch.get().shouldShow) -const setShouldShow: MethodCreator< - Props, - ConnectorState, +const setShouldShow: BoundMethodCreator< [ConnectorState['shouldShow']] > = dispatch => shouldShow => { dispatch.set({ shouldShow }, shouldShow ? DOMHelper.focusFileExplorer : undefined) DOMHelper.setBodyIndent(shouldShow) } -const setError: MethodCreator< - Props, - ConnectorState, - [ConnectorState['error']] -> = dispatch => error => { +const setError: BoundMethodCreator<[ConnectorState['error']]> = dispatch => error => { dispatch.set({ error }) dispatch.call(setShouldShow, false) } -const toggleShowSettings: MethodCreator = dispatch => () => +const toggleShowSettings: BoundMethodCreator = dispatch => () => dispatch.set(({ showSettings }) => ({ showSettings: !showSettings, })) -const setShowSettings: MethodCreator< - Props, - ConnectorState, +const setShowSettings: BoundMethodCreator< [ConnectorState['showSettings']] > = dispatch => showSettings => dispatch.set({ showSettings }) -const onAccessTokenChange: MethodCreator< - Props, - ConnectorState, +const onAccessTokenChange: BoundMethodCreator< [ConnectorState['accessToken']] > = dispatch => accessToken => { dispatch.set({ accessToken }) @@ -239,37 +231,26 @@ const onAccessTokenChange: MethodCreator< } } -const onShortcutChange: MethodCreator< - Props, - ConnectorState, +const onShortcutChange: BoundMethodCreator< [ConnectorState['toggleShowSideBarShortcut']] > = dispatch => shortcut => dispatch.set({ toggleShowSideBarShortcut: shortcut }) -const setMetaData: MethodCreator< - Props, - ConnectorState, - [ConnectorState['metaData']] -> = dispatch => metaData => dispatch.set({ metaData }) +const setMetaData: BoundMethodCreator<[ConnectorState['metaData']]> = dispatch => metaData => + dispatch.set({ metaData }) -const setCompressSingleton: MethodCreator< - Props, - ConnectorState, +const setCompressSingleton: BoundMethodCreator< [ConnectorState['compressSingletonFolder']] > = dispatch => compressSingletonFolder => dispatch.set({ compressSingletonFolder }) -const setCopyFile: MethodCreator< - Props, - ConnectorState, +const setCopyFile: BoundMethodCreator< [ConnectorState['copyFileButton']] > = dispatch => copyFileButton => dispatch.set({ copyFileButton }) -const setCopySnippet: MethodCreator< - Props, - ConnectorState, +const setCopySnippet: BoundMethodCreator< [ConnectorState['copySnippetButton']] > = dispatch => copySnippetButton => dispatch.set({ copySnippetButton }) -const useListeners: MethodCreator = dispatch => { +const useListeners: BoundMethodCreator<[boolean]> = dispatch => { const $onPJAXEnd = dispatch.call.bind(dispatch, onPJAXEnd) const $onKeyDown = dispatch.call.bind(dispatch, onKeyDown) return on => {