type TCallback < T = Partial < TaroGeneral . CallbackResult > > = ( res : T ) => Promise < void > | void ; interface IMethodParam < T = Partial < TaroGeneral . CallbackResult > > { name : string ; success ? : TCallback < T > ; fail ? : TCallback ; complete ? : TCallback ; } export class MethodHandler < T = Partial < TaroGeneral . CallbackResult > > { methodName : string ; protected __success ? : TCallback < T > ; protected __fail ? : TCallback ; protected __complete ? : TCallback ; constructor ( { name , success , fail , complete } ) { this . methodName = name ; this . __success = success ; this . __fail = fail ; this . __complete = complete ; } success < U = Record < string , unknown > > ( res = { } , resolve = Promise . resolve . bind ( Promise ) ) { if ( ! res . errMsg ) { res . errMsg = `${ this . methodName }:ok` ; } typeof this . __success === 'function' && this . __success ( res as T ) ; typeof this . __complete === 'function' && this . __complete ( res ) ; return resolve ( res as Required < T & U & TaroGeneral . CallbackResult > ) ; } fail < U = Record < string , unknown > > ( res = { } , reject = Promise . reject . bind ( Promise ) ) { if ( ! res . errMsg ) { res . errMsg = `${ this . methodName }:fail` ; } else { res . errMsg = `${ this . methodName }:fail ${ res . errMsg }` ; } console . error ( res . errMsg ) ; typeof this . __fail === 'function' && this . __fail ( res ) ; typeof this . __complete === 'function' && this . __complete ( res ) ; return reject ( res ) ; } } type TCallbackManagerParam = ( ... arr : unknown [ ] ) => void ; interface ICallbackManagerOption { callback ? : TCallbackManagerParam ; ctx ? : any ; [ key ] : unknown ; } type TCallbackManagerListItem = ( TCallbackManagerParam | ICallbackManagerOption ) ; type TCallbackManagerList = TCallbackManagerListItem [ ] ; export class CallbackManager { callbacks : TCallbackManagerList = [ ] ; param { { callback : function , ctx : any } | function } opt add ( opt ? ) { if ( opt ) this . callbacks . push ( opt ) ; } param { { callback : function , ctx : any } | function } opt remove ( opt ? ) { if ( opt ) { let pos = - 1 ; this . callbacks . forEach ( ( callback , k ) => { if ( callback === opt ) { pos = k ; } } ) ; if ( pos > - 1 ) { this . callbacks . splice ( pos , 1 ) ; } } } return { number } count ( ) { return this . callbacks . length ; } param { ... any } args trigger ( ... args ) { this . callbacks . forEach ( opt => { if ( typeof opt === 'function' ) { opt ( ... args ) ; } else { const { callback , ctx } = opt ; typeof callback === 'function' && callback . call ( ctx , ... args ) ; } } ) ; } }