import { getRandomHash } from './utils.functions' ; import { Message , MessageQueue , MessageType , messageSource } from './messaging.definitions' ; export class MessagePipeBackend { private _messageQueue ; private _sendMessage ; private _createQueueAlertMessage ; private _handlers : Array < ( message : Message < any > ) => void > = [ ] ; constructor ( injectables ) { this . _messageQueue = injectables . messageQueue ; this . _sendMessage = injectables . sendMessage ; this . _createQueueAlertMessage = injectables . createQueueAlertMessage ; } public createMessage < T > ( type , content ) { return { messageSource , messageId : getRandomHash ( ) , messageType : type , content : content } ; } public addHandler ( handler ) { this . _handlers . push ( handler ) ; } public handleIncomingMessage ( message ) { this . _handlers . forEach ( handler => handler ( message ) ) ; } public sendSimpleMessage ( message ) { return this . _sendMessage ( message ) ; } public sendQueuedMessage ( message ) { this . _messageQueue . enqueue ( message ) ; return this . _sendMessage ( this . _createQueueAlertMessage ( ) ) ; } }