type Middleware = ( ctx : any , next : Middleware ) => any ; export function compose ( middleware ) { return function ( context , next ? ) { return dispatch ( 0 ) ; function dispatch ( i ) { let fn = middleware [ i ] ; if ( i === middleware . length && next ) { fn = next ; } if ( ! fn ) return Promise . resolve ( ) ; try { return Promise . resolve ( fn ( context , dispatch . bind ( null , i + 1 ) ) ) ; } catch ( err ) { return Promise . reject ( err ) ; } } } ; } export function delay ( time ) { return new Promise ( ( resolve ) => { setTimeout ( ( ) => { resolve ( ) ; } , time ) ; } ) ; } export function throttle ( fn , timeout ) { let timer = - 1 ; return function ( ... args ) { if ( timer > 0 ) { return ; } timer = setTimeout ( ( ) => { fn ( ... args ) ; timer = - 1 ; } , timeout ) ; } ; }