/**
 * callback帮助类
 */ export class CallbackHelper { private callbackQueue : Array < ( value : any , resolve : ( value ? : any ) => void , reject : ( reason ? : any ) => void ) => void > = new Array ( ) ; private isExecuteCallbackQueue = false ; private executeValue : any ; private isExecuteReject = false ; private isReject = false ; private rejectValue : any ; private rejectCallback : ( ( value : any ) => void ) | undefined ; constructor ( executor ) { executor ( this . resolve . bind ( this ) , this . reject . bind ( this ) ) ; } public static builder ( executor ) { return new CallbackHelper ( executor ) ; } public then ( executor ) { this . callbackQueue . push ( executor ) ; if ( this . isExecuteCallbackQueue ) { this . resolve ( this . executeValue ) ; } return this ; } public catch ( executor ) { this . rejectCallback = executor ; if ( this . isReject ) { this . reject ( this . rejectValue ) ; } return this ; } private resolve = ( value ? ) => { if ( this . isExecuteReject ) { return ; } this . isExecuteCallbackQueue = true ; this . executeValue = value ; let executor = this . callbackQueue . shift ( ) ; if ( ! executor ) { return ; } this . executeValue = undefined ; this . isExecuteCallbackQueue = false ; executor ( value , this . resolve , this . reject ) ; } ; private reject = ( reason ? ) => { this . isReject = true ; this . rejectValue = reason ; this . isExecuteReject = true ; if ( this . rejectCallback ) { this . isReject = false ; this . rejectCallback ( reason ) ; } } ; }