import { ThunkAction } from 'redux-thunk' ; import { State , Action } from './types' ; type ActionCreatorFunction = ( ) => ThunkAction < void , State , void , Action > ; export type ActionCreator = ActionCreatorFunction & { enabled : ( state : State ) => boolean ; } ; export const makeActionCreator = ( actionCreatorFunction , enabled = ( ) => true ) : ActionCreator => { const actionCreator = ( ) => actionCreatorFunction ( ) ; actionCreator . enabled = enabled ; return wrapActionCreator ( actionCreator , ( dispatch , getState ) => { if ( enabled ( getState ( ) ) ) { dispatch ( ) ; } } ) ; } ; export const wrapActionCreator = ( actionCreator , wrapper ) : ActionCreator => { const wrappedActionCreator = ( ) => ( dispatch , getState ) => wrapper ( ( ) => dispatch ( actionCreator ( ) ) , getState ) ; wrappedActionCreator . enabled = actionCreator . enabled ; return wrappedActionCreator ; } ;