import EventEmitter from 'events' ; import GleeConnection from './connection' ; type MessageHeaders = { [ key ] : any ; } ; interface IGleeMessageConstructor { payload ? : any ; headers ? : MessageHeaders ; channel ? : string ; serverName ? : string ; connection ? : GleeConnection ; broadcast ? : boolean ; } interface IReply { payload ? : any ; headers ? : { [ key ] : any ; } ; channel ? : string ; } class GleeMessage extends EventEmitter { private _payload : any ; private _headers : { [ key ] : string ; } ; private _channel : string ; private _serverName : string ; private _connection : GleeConnection ; private _broadcast : boolean ; private _inbound : boolean ; private _outbound : boolean ; private _params : { [ key ] : string ; } ; param options param { Any } options . payload param { Object } options . headers param { String } options . channel param { String } options . serverName param { GleeConnection } options . connection param { Boolean } options . broadcast constructor ( { payload , headers , channel , serverName , connection , broadcast = false , } ) { super ( ) ; if ( payload ) this . _payload = payload ; if ( headers ) this . _headers = headers ; if ( channel ) this . _channel = channel ; if ( serverName ) this . _serverName = serverName ; if ( connection ) this . _connection = connection ; if ( broadcast ) this . _broadcast = ! ! broadcast ; } get payload ( ) : any { return this . _payload ; } set payload ( value ) { this . _payload = value ; } get headers ( ) : { [ key ] : string ; } { return this . _headers ; } set headers ( value ) { this . _headers = value ; } get channel ( ) : string { return this . _channel ; } set channel ( value ) { this . _channel = value ; } get serverName ( ) : string { return this . _serverName ; } set serverName ( value ) { this . _serverName = value ; } get connection ( ) : GleeConnection { return this . _connection ; } set connection ( value ) { this . _connection = value ; } get broadcast ( ) : boolean { return this . _broadcast ; } get params ( ) : { [ key ] : string ; } { return this . _params ; } set params ( value ) { this . _params = value ; } param options param { Any } options . payload param { Object | null } options . headers param { String } options . channel reply ( { payload , headers , channel } ) { if ( payload ) this . _payload = payload ; if ( headers !== undefined ) { if ( headers === null ) { this . _headers = undefined ; } else { this . _headers = headers ; } } if ( channel !== undefined ) { if ( typeof channel === 'string' ) { this . _channel = channel ; } else { return console . error ( 'GleeMessage.reply(): when specified, "channel" must be a string.' ) ; } } this . send ( ) ; } /**
     * Makes the message suitable only for the inbound pipeline.
     */ setInbound ( ) { this . _inbound = true ; this . _outbound = false ; } /**
     * Makes the message suitable only for the outbound pipeline.
     */ setOutbound ( ) { this . _inbound = false ; this . _outbound = true ; } /**
     * Checks if it's an inbound message.
     */ isInbound ( ) { return this . _inbound && ! this . _outbound ; } /**
     * Checks if it's an outbound message.
     */ isOutbound ( ) { return this . _outbound && ! this . _inbound ; } /**
     * Tells Glee to send the message.
     */ send ( ) { this . emit ( 'send' , this ) ; } } export default GleeMessage ;