import { ActionReturn , ActionType , ActionsType } from './types' ; import { isFn } from './utils' ; export interface Sub < Actions > { ( actions ) : any ; } export type CmdType < Actions > = Sub < Actions > [ ] ; param task param args param succeedAction param failedAction export function ofFn < A , T , State , Actions > ( task , args , succeedAction ? , failedAction ? ) ; export function ofFn < A , T , State , Actions > ( task ? , succeedAction ? , failedAction ? ) ; export function ofFn < A , T , State , Actions > ( task ? , args ? , succeedAction ? , failedAction ? ) { if ( ! task ) { return [ ] as CmdType < Actions > ; } if ( isFn ( args ) ) { failedAction = succeedAction as any ; succeedAction = args as any ; args = void 0 ; } const fn = ( ) => { const result = task ( args ) ; if ( succeedAction ) { succeedAction ( result ) ; } return result ; } ; return [ _ => { if ( failedAction ) { try { return fn ( ) ; } catch ( e ) { console . error ( e ) ; failedAction ( e ) ; } } else { return fn ( ) ; } } ] ; } param task param args param succeedAction param failedAction export function ofPromise < A , T , State , Actions > ( task , args , succeedAction ? , failedAction ? ) ; export function ofPromise < A , T , State , Actions > ( task , succeedAction ? , failedAction ? ) ; export function ofPromise < A , T , State , Actions > ( task , args ? , succeedAction ? , failedAction ? ) { if ( ! task ) { return [ ] as CmdType < Actions > ; } if ( isFn ( args ) ) { failedAction = succeedAction as any ; succeedAction = args as any ; args = void 0 ; } return [ _ => task ( args ) . then ( res => { try { succeedAction && succeedAction ( res ) ; } catch ( error ) { console . error ( error ) ; } } ) . catch ( err => { console . error ( err ) ; failedAction && failedAction ( err ) ; } ) ] ; } param sub export const ofSub = < Actions > ( ... subs ) : CmdType < Actions > => subs ; const _concat = Array . prototype . concat ; param cmds export const batch = < Actions > ( ... cmds ) : CmdType < Actions > => _concat . apply ( [ ] , _concat . apply ( [ ] , cmds ) ) ; param map param cmd export const map = < Actions , SubActions > ( map , cmd ) : CmdType < Actions > => cmd . map ( sub => actions => sub ( map ( actions ) ) ) ; /**
 * Empty command
 */ export const none = ( [ ] as Array < Sub < any > > ) ; export const Cmd = { none , ofFn , ofPromise , ofSub , batch , map , } ; export default Cmd ;