refactor: named value from dispatch.get

This commit is contained in:
EnixCoda 2020-10-28 00:38:08 +08:00
parent 72ddfefb19
commit 0548c614be
No known key found for this signature in database
GPG key ID: 0C1A07377913A1DD
3 changed files with 23 additions and 12 deletions

View file

@ -31,7 +31,7 @@ function run<M extends Method>([method, args]: [M, Parameters<M>]) {
}
export type DispatchState<Props, State> = React.Component<Props, State>['setState']
export type GetState<Props, State> = () => [State, Props]
export type GetState<Props, State> = () => { state: State; props: Props }
export type TriggerOtherMethod<Props, State> = <Args extends any[]>(
methodCreator: MethodCreator<Props, State, Args>,
...args: Parameters<ReturnType<MethodCreator<Props, State, Args>>>
@ -75,7 +75,7 @@ function link<P, S>(instance: React.Component<P, S>, sources: Sources<P, S>): Wr
const dispatchState: DispatchState<P, S> = (updater, callback) => {
instance.setState(updater, callback)
}
const prepareState: GetState<P, S> = () => [instance.state, instance.props]
const prepareState: GetState<P, S> = () => ({ state: instance.state, props: instance.props })
const dispatch: Dispatch<P, S> = {
call: dispatchCall,
get: prepareState,

View file

@ -80,7 +80,10 @@ export const setUpTree: BoundMethodCreator<[
}
export const handleKeyDown: BoundMethodCreator<[React.KeyboardEvent]> = dispatch => event => {
const [{ searched, visibleNodes }, { loadWithPJAX }] = dispatch.get()
const {
state: { searched, visibleNodes },
props: { loadWithPJAX },
} = dispatch.get()
if (!visibleNodes) return
const { nodes, focusedNode, expandedNodes } = visibleNodes
function handleVerticalMove(index: number) {
@ -227,18 +230,19 @@ export const onNodeClick: BoundMethodCreator<[
if (preventDefault) event.preventDefault()
if (node.type === 'tree') {
const [
,
{
const {
props: {
config: { recursiveToggleFolder },
},
] = dispatch.get()
} = dispatch.get()
const recursive =
(recursiveToggleFolder === 'shift' && event.shiftKey) ||
(recursiveToggleFolder === 'alt' && event.altKey)
dispatch.call(toggleNodeExpansion, node, { recursive })
} else if (node.type === 'blob') {
const [, { loadWithPJAX }] = dispatch.get()
const {
props: { loadWithPJAX },
} = dispatch.get()
dispatch.call(focusNode, node)
if (node.url && !node.url.includes('#')) {
loadWithPJAX(node.url)

View file

@ -35,7 +35,9 @@ export type ConnectorState = {
type BoundMethodCreator<Args extends any[] = []> = MethodCreator<Props, ConnectorState, Args>
export const init: BoundMethodCreator = dispatch => async () => {
const [{ initializingPromise }] = dispatch.get()
const {
state: { initializingPromise },
} = dispatch.get()
if (initializingPromise) await initializingPromise
let done: any = null // cannot use type `(() => void) | null` here
@ -59,7 +61,9 @@ export const init: BoundMethodCreator = dispatch => async () => {
})
dispatch.call(setMetaData, metaData)
const [, { configContext }] = dispatch.get()
const {
props: { configContext },
} = dispatch.get()
const { access_token: accessToken } = configContext.val
if (!metaData.userName || !metaData.repoName) return
@ -146,7 +150,7 @@ export const handleError: BoundMethodCreator<[Error]> = dispatch => async err =>
) {
dispatch.set({ errorDueToAuth: true })
} else if (err.message === errors.CONNECTION_BLOCKED) {
const [, props] = dispatch.get()
const { props } = dispatch.get()
if (props.configContext.val.access_token) {
dispatch.call(setError, `Cannot connect to ${platformName}.`)
} else {
@ -162,7 +166,10 @@ export const handleError: BoundMethodCreator<[Error]> = dispatch => async err =>
}
export const toggleShowSideBar: BoundMethodCreator = dispatch => () => {
const [{ shouldShow }, { configContext }] = dispatch.get()
const {
state: { shouldShow },
props: { configContext },
} = dispatch.get()
dispatch.call(setShouldShow, !shouldShow)
const {