mirror of
https://github.com/EnixCoda/Gitako.git
synced 2026-03-11 08:54:44 +00:00
refactor: shorten MethodCreator
This commit is contained in:
parent
682cab762c
commit
76fe90a181
2 changed files with 37 additions and 68 deletions
|
|
@ -48,7 +48,9 @@ type Task = () => void
|
|||
const tasksAfterRender: (Task)[] = []
|
||||
let visibleNodesGenerator: VisibleNodesGenerator
|
||||
|
||||
const init: MethodCreator<Props, ConnectorState> = dispatch => () =>
|
||||
type BoundMethodCreator<Args = []> = MethodCreator<Props, ConnectorState, Args>
|
||||
|
||||
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<Props, 'treeData' | 'metaData' | 'compressSingletonFolder' | 'accessToken'>]
|
||||
> = 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<Props, ConnectorState> = 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<Props, ConnectorState> = 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<HTMLInputElement>]
|
||||
> = dispatch => async event => {
|
||||
const searchKey = event.currentTarget.value
|
||||
await dispatch.call(search, searchKey)
|
||||
}
|
||||
|
||||
const search: MethodCreator<Props, ConnectorState, [string]> = dispatch => {
|
||||
const search: BoundMethodCreator<[string]> = dispatch => {
|
||||
let i = 0
|
||||
return async searchKey => {
|
||||
dispatch.set({ searchKey })
|
||||
|
|
@ -307,7 +298,7 @@ const search: MethodCreator<Props, ConnectorState, [string]> = dispatch => {
|
|||
}
|
||||
}
|
||||
|
||||
const goTo: MethodCreator<Props, ConnectorState, [string[]]> = 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<Props, ConnectorState, [string[]]> = dispatch => async
|
|||
dispatch.set({ searchKey: '', searched: false })
|
||||
}
|
||||
|
||||
const setExpand: MethodCreator<Props, ConnectorState, [TreeNode, boolean]> = 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<Props, ConnectorState, [TreeNode, boolean]> = dispatch => (
|
||||
const toggleNodeExpansion: BoundMethodCreator<[TreeNode, boolean]> = dispatch => (
|
||||
node,
|
||||
skipScrollToNode,
|
||||
) => {
|
||||
|
|
@ -336,7 +324,7 @@ const toggleNodeExpansion: MethodCreator<Props, ConnectorState, [TreeNode, boole
|
|||
tasksAfterRender.push(DOMHelper.focusFileExplorer)
|
||||
}
|
||||
|
||||
const focusNode: MethodCreator<Props, ConnectorState, [TreeNode | null, boolean]> = dispatch => (
|
||||
const focusNode: BoundMethodCreator<[TreeNode | null, boolean]> = dispatch => (
|
||||
node: TreeNode | null,
|
||||
skipScroll = false,
|
||||
) => {
|
||||
|
|
@ -346,7 +334,7 @@ const focusNode: MethodCreator<Props, ConnectorState, [TreeNode | null, boolean]
|
|||
dispatch.call(updateVisibleNodes)
|
||||
}
|
||||
|
||||
const onNodeClick: MethodCreator<Props, ConnectorState, [TreeNode]> = 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<Props, ConnectorState, [TreeNode]> = dispatch =
|
|||
}
|
||||
}
|
||||
|
||||
const updateVisibleNodes: MethodCreator<Props, ConnectorState> = dispatch => () => {
|
||||
const updateVisibleNodes: BoundMethodCreator = dispatch => () => {
|
||||
const { visibleNodes } = visibleNodesGenerator
|
||||
dispatch.set({ visibleNodes })
|
||||
}
|
||||
|
|
|
|||
|
|
@ -54,7 +54,9 @@ export type ConnectorState = {
|
|||
setCompressSingleton: GetCreatedMethod<typeof setCompressSingleton>
|
||||
}
|
||||
|
||||
const init: MethodCreator<Props, ConnectorState> = dispatch => async () => {
|
||||
type BoundMethodCreator<Args = []> = MethodCreator<Props, ConnectorState, Args>
|
||||
|
||||
const init: BoundMethodCreator = dispatch => async () => {
|
||||
const { initializingPromise } = dispatch.get()
|
||||
if (initializingPromise) await initializingPromise
|
||||
|
||||
|
|
@ -156,7 +158,7 @@ const init: MethodCreator<Props, ConnectorState> = dispatch => async () => {
|
|||
}
|
||||
}
|
||||
|
||||
const handleError: MethodCreator<Props, ConnectorState, [Error]> = 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<Props, ConnectorState, [Error]> = dispatch => a
|
|||
}
|
||||
}
|
||||
|
||||
const onPJAXEnd: MethodCreator<Props, ConnectorState> = dispatch => () => {
|
||||
const onPJAXEnd: BoundMethodCreator = dispatch => () => {
|
||||
const { metaData, copyFileButton, copySnippetButton } = dispatch.get()
|
||||
DOMHelper.unmountTopProgressBar()
|
||||
DOMHelper.decorateGitHubPageContent({ copyFileButton, copySnippetButton })
|
||||
|
|
@ -185,7 +187,7 @@ const onPJAXEnd: MethodCreator<Props, ConnectorState> = dispatch => () => {
|
|||
dispatch.call(setMetaData, mergedMetaData)
|
||||
}
|
||||
|
||||
const onKeyDown: MethodCreator<Props, ConnectorState, [KeyboardEvent]> = 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<Props, ConnectorState, [KeyboardEvent]> = dispatc
|
|||
}
|
||||
}
|
||||
|
||||
const toggleShowSideBar: MethodCreator<Props, ConnectorState> = 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<Props, ConnectorState> = 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<Props, ConnectorState, [boolean]> = dispatch => {
|
||||
const useListeners: BoundMethodCreator<[boolean]> = dispatch => {
|
||||
const $onPJAXEnd = dispatch.call.bind(dispatch, onPJAXEnd)
|
||||
const $onKeyDown = dispatch.call.bind(dispatch, onKeyDown)
|
||||
return on => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue