From ab6200ebeea6e486e2ec1e7caa89295e9998885b Mon Sep 17 00:00:00 2001 From: Paulo Cesar Date: Sun, 25 Dec 2016 08:13:09 -0200 Subject: [PATCH] meaningful function names and better next/throw --- redux-actions/index.d.ts | 66 +++++++++++++++++----------- redux-actions/redux-actions-tests.ts | 16 +++++-- 2 files changed, 53 insertions(+), 29 deletions(-) diff --git a/redux-actions/index.d.ts b/redux-actions/index.d.ts index 54f373ead0..bd195c1c18 100644 --- a/redux-actions/index.d.ts +++ b/redux-actions/index.d.ts @@ -23,49 +23,65 @@ declare namespace ReduxActions { } interface ReducerMap { - [actionType: string]: Reducer; + [actionType: string]: Reducer | ReducerNextThrow; } + interface ReducerMapMeta { + [actionType: string]: Reducer | ReducerNextThrow; + } + + interface ReducerNextThrow { + next?(state: State, action: Action): State; + throw?(state: State, action: Action): State; + } + + interface ReducerNextThrowMeta { + next?(state: State, action: ActionMeta): State; + throw?(state: State, action: ActionMeta): State; + } + + type ActionFunctions = ActionFunction0> | ActionFunction1> | ActionFunction2> | ActionFunction3> | ActionFunction4> | ActionFunctionAny> + type Reducer = (state: State, action: Action) => State; type ReducerMeta = (state: State, action: ActionMeta) => State; /** argument inferring borrowed from lodash definitions */ - type Function0 = () => R; - type Function1 = (t1: T1) => R; - type Function2 = (t1: T1, t2: T2) => R; - type Function3 = (t1: T1, t2: T2, t3: T3) => R; - type Function4 = (t1: T1, t2: T2, t3: T3, t4: T4) => R; - type FunctionAny = (...args: any[]) => R; + type ActionFunction0 = () => R; + type ActionFunction1 = (t1: T1) => R; + type ActionFunction2 = (t1: T1, t2: T2) => R; + type ActionFunction3 = (t1: T1, t2: T2, t3: T3) => R; + type ActionFunction4 = (t1: T1, t2: T2, t3: T3, t4: T4) => R; + type ActionFunctionAny = (...args: any[]) => R; export function createAction( actionType: string, - payloadCreator: Function0 - ): Function0>; + payloadCreator: ActionFunction0 + ): ActionFunction0>; export function createAction( actionType: string, - payloadCreator: Function1 - ): Function1>; + payloadCreator: ActionFunction1 + ): ActionFunction1>; export function createAction( actionType: string, - payloadCreator: Function2 - ): Function2>; + payloadCreator: ActionFunction2 + ): ActionFunction2>; export function createAction( actionType: string, - payloadCreator: Function3 - ): Function3>; + payloadCreator: ActionFunction3 + ): ActionFunction3>; export function createAction( actionType: string, - payloadCreator: Function4 - ): Function4>; + payloadCreator: ActionFunction4 + ): ActionFunction4>; export function createAction( actionType: string - ): FunctionAny>; + ): ActionFunctionAny>; export function createAction( actionType: string, @@ -74,26 +90,26 @@ declare namespace ReduxActions { ): (...args: any[]) => ActionMeta; export function handleAction( - actionType: string | Function0> | Function1> | Function2> | Function3> | Function4> | FunctionAny>, - reducer: Reducer | ReducerMap + actionType: string | ActionFunctions, + reducer: Reducer | ReducerNextThrow ): Reducer; export function handleAction( actionType: { toString(): string }, - reducer: ReducerMeta | ReducerMap + reducer: ReducerMeta | ReducerNextThrowMeta ): Reducer; export function handleActions( reducerMap: ReducerMap, - initialState?: StateAndPayload + initialState: StateAndPayload ): Reducer; export function handleActions( reducerMap: ReducerMap, - initialState?: State + initialState: State ): Reducer; export function combineActions( - ...actionTypes: Array<{ toString(): string }> - ): { toString(): string }; + ...actionTypes: Array> + ): Array>; } diff --git a/redux-actions/redux-actions-tests.ts b/redux-actions/redux-actions-tests.ts index 65ef3282ed..f5a2d41e18 100644 --- a/redux-actions/redux-actions-tests.ts +++ b/redux-actions/redux-actions-tests.ts @@ -34,13 +34,17 @@ state = actionHandlerWithReduceMap(0, multiplyAction(10)); const actionsHandler = ReduxActions.handleActions({ 'INCREMENT': (state: number, action: ReduxActions.Action) => state + action.payload, 'MULTIPLY': (state: number, action: ReduxActions.Action) => state * action.payload -}); +}, 0); state = actionsHandler(0, { type: 'INCREMENT' }); const actionsHandlerWithInitialState = ReduxActions.handleActions({ - 'INCREMENT': (state: number, action: ReduxActions.Action) => state + action.payload, - 'MULTIPLY': (state: number, action: ReduxActions.Action) => state * action.payload + 'INCREMENT': { + next: (state: number, action: ReduxActions.Action) => state + action.payload, + }, + 'MULTIPLY': { + next: (state: number, action: ReduxActions.Action) => state * action.payload + } }, 0); state = actionsHandlerWithInitialState(0, { type: 'INCREMENT' }); @@ -123,7 +127,11 @@ ReduxActions.handleAction(act3, (state, action) => { return { hello: action.payload.s } }) -/* can't do this until it lands in 2.2 +ReduxActions.handleAction(ReduxActions.combineActions(act, act3, act2), () => { + +}) + +/* can't do this until it lands in 2.2, HKTs ReduxActions.handleAction(act, (state, action) => { action.payload === 'hello' return {}