import { ErrorMiddleware , Middleware } from '../middlewares' ; export type ChannelMiddlewareTuple = { channel : string ; fn : Middleware ; } ; export type ChannelErrorMiddlewareTuple = { channel : string ; fn : ErrorMiddleware ; } ; export type GenericMiddleware = Middleware | ErrorMiddleware ; export type GenericChannelMiddlewareTuple = ChannelMiddlewareTuple | ChannelErrorMiddlewareTuple ; class GleeRouter { private middlewares : ChannelMiddlewareTuple [ ] ; private outboundMiddlewares : ChannelMiddlewareTuple [ ] ; private errorMiddlewares : ChannelErrorMiddlewareTuple [ ] ; private outboundErrorMiddlewares : ChannelErrorMiddlewareTuple [ ] ; /**
     * Instantiates a GleeRouter.
     */ constructor ( ) { this . middlewares = [ ] ; this . outboundMiddlewares = [ ] ; this . errorMiddlewares = [ ] ; this . outboundErrorMiddlewares = [ ] ; } param { String } channel param { GenericMiddleware } middleware use ( ... middlewares ) ; use ( channel , ... middlewares ) ; use ( channel , ... middlewares ) { const mws = this . middlewaresToChannelMiddlewaresTuples ( channel , ... middlewares ) ; mws . forEach ( mw => { if ( mw . fn . length <= 2 ) { this . addMiddlewares ( [ mw as ChannelMiddlewareTuple ] ) ; } else { this . addErrorMiddlewares ( [ mw as ChannelErrorMiddlewareTuple ] ) ; } } ) ; } param { String } channel param { Function | GleeRouter } . . middlewares useOutbound ( ... middlewares ) ; useOutbound ( channel , ... middlewares ) ; useOutbound ( channel , ... middlewares ) { const mws = this . middlewaresToChannelMiddlewaresTuples ( channel , ... middlewares ) ; mws . forEach ( mw => { if ( mw . fn . length <= 2 ) { this . addOutboundMiddlewares ( [ mw as ChannelMiddlewareTuple ] ) ; } else { this . addOutboundErrorMiddlewares ( [ mw as ChannelErrorMiddlewareTuple ] ) ; } } ) ; } private middlewaresToChannelMiddlewaresTuples ( channel , ... middlewares ) { const realChannel = typeof channel === 'string' ? channel : undefined ; const allMiddlewares = realChannel ? middlewares : [ channel as GenericMiddleware ] . concat ( middlewares ) ; return allMiddlewares . map ( fn => ( { channel : realChannel , fn , } as GenericChannelMiddlewareTuple ) ) ; } return { Array < ChannelMiddlewareTuple > } getMiddlewares ( ) { return this . middlewares ; } return { Array < ChannelMiddlewareTuple > } getOutboundMiddlewares ( ) { return this . outboundMiddlewares ; } return { Array < ChannelErrorMiddlewareTuple > } getErrorMiddlewares ( ) { return this . errorMiddlewares ; } return { Array < ChannelErrorMiddlewareTuple > } getOutboundErrorMiddlewares ( ) { return this . outboundErrorMiddlewares ; } param { Array < GenericChannelMiddlewareTuple > } target param { Array < GenericChannelMiddlewareTuple > } middlewares param { String } channel private private _addMiddlewares ( target , middlewares , channel ) { middlewares . forEach ( mw => { if ( channel ) { const compoundchannel = mw . channel ? `${ channel }/${ mw . channel }` : channel ; target . push ( { ... mw , ... { channel : compoundchannel } } ) ; } else { target . push ( mw ) ; } } ) ; } param { Array < ChannelMiddlewareTuple > } middlewares param { String } channel addMiddlewares ( middlewares , channel ? ) { this . _addMiddlewares ( this . middlewares , middlewares , channel ) ; } param { Array < ChannelMiddlewareTuple > } middlewares param { String } channel addOutboundMiddlewares ( middlewares , channel ? ) { this . _addMiddlewares ( this . outboundMiddlewares , middlewares , channel ) ; } param { Array < ChannelErrorMiddlewareTuple > } errorMiddlewares param { String } channel addErrorMiddlewares ( errorMiddlewares , channel ? ) { this . _addMiddlewares ( this . errorMiddlewares , errorMiddlewares , channel ) ; } param { Array < ChannelErrorMiddlewareTuple > } errorMiddlewares param { String } channel addOutboundErrorMiddlewares ( errorMiddlewares , channel ? ) { this . _addMiddlewares ( this . outboundErrorMiddlewares , errorMiddlewares , channel ) ; } } export default GleeRouter ;