import PluginInstaller from './PluginInstaller' ; import Span from '../trace/span/Span' ; export default interface SwPlugin { readonly module : string ; readonly versions : string ; install ( installer ) : void ; } export const wrapEmit = ( span , ee , doError = true , stop = NaN ) => { const stopIsFunc = typeof stop === 'function' ; const _emit = ee . emit ; Object . defineProperty ( ee , 'emit' , { configurable : true , writable : true , value : ( function ( this ) : any { const event = arguments [ 0 ] ; span . resync ( ) ; try { if ( doError && event === 'error' ) span . error ( arguments [ 1 ] ) ; return _emit . apply ( this , arguments ) ; } catch ( err ) { span . error ( err ) ; throw err ; } finally { if ( stopIsFunc ? stop ( event ) : event === stop ) span . stop ( ) ; else span . async ( ) ; } } ) } ) ; } ; export const wrapCallback = ( span , callback , idxError = false ) => { return function ( this ) { if ( idxError !== false && arguments [ idxError ] ) span . error ( arguments [ idxError ] ) ; span . stop ( ) ; return callback . apply ( this , arguments ) ; } ; } ; export const wrapPromise = ( span , promise ) => { return promise . then ( ( res ) => { span . stop ( ) ; return res ; } , ( err ) => { span . error ( err ) ; span . stop ( ) ; return Promise . reject ( err ) ; } ) ; } ;