From a3cac844a958c901d2dfb0716c1561597c843334 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Wed, 20 Feb 2019 15:55:52 +0800 Subject: [PATCH] refactor: optimization is endless --- src/driver/connect.ts | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/driver/connect.ts b/src/driver/connect.ts index 89ccde3..bf92a46 100644 --- a/src/driver/connect.ts +++ b/src/driver/connect.ts @@ -1,6 +1,10 @@ import * as React from 'react' -export type Method = (...args: Args extends any[] ? Args : []) => void | Promise +type x = [] extends any[] ? [] : any[] + +export type Method = ( + ...args: Args extends any[] ? Args : any[] +) => void | Promise export type Middleware = ( method: M, args: Parameters, @@ -35,7 +39,7 @@ export type PreDispatch = ( dispatchCallback: (state: State, props: Props) => Props | Promise | void, callback?: () => void, ) => void -export type TriggerOtherMethod = >( +export type TriggerOtherMethod = >( methodCreator: MC, ...args: Parameters> ) => void @@ -43,15 +47,13 @@ export type TriggerOtherMethod = >( export type Dispatch = { set: DispatchState get: PreDispatch - call: TriggerOtherMethod + call: TriggerOtherMethod } -export type MethodCreator = ( - dispatch: Dispatch, -) => Method +export type MethodCreator = (dispatch: Dispatch) => Method type Sources = { - [key: string]: MethodCreator + [key: string]: MethodCreator } type WrappedMethods = { [key: string]: Method @@ -62,14 +64,15 @@ function link(instance: React.Component, sources: Sources): Wr /* [keyof sources] -> wrappedMethods.method */ } const map = new Map< - MethodCreator, + MethodCreator, Method >(/* sources.creator -> wrappedMethods.method */) - const dispatchCall: TriggerOtherMethod = (createMethod, ...otherArgs) => { + const dispatchCall: TriggerOtherMethod = (createMethod, ...otherArgs) => { const isFromSource = sourcesValues.includes(createMethod) if (isFromSource) { const method = map.get(createMethod) + if (!method) throw Error('Method not found') const runnable = applyMiddlewares(method, otherArgs) run(runnable) }