import { EventEmitter } from "events"; import { Logger } from "winston"; import { IAvailableInstanceGenerators, IAvailableServicesMsg, IAvailableTopicsMsg, ICommunicationInterface, IExternalEventMsg, IRequestTaskMsg, IResponseTaskMsg } from "../types/communication.interface"; /** * A Communication Layer for the Dispatchers. * Here, only a Events are used. * * @export * @class EventLayer * @implements {ICommunicationInterface} */ export class EventLayer implements ICommunicationInterface { constructor( public readonly subscriptionMode: 'individual' | 'generic', public readonly resultSharing: 'individual' | 'generic', protected _logger?: Logger ){ } async emitNewInstanceGeneratorsAvailable(generators: IAvailableInstanceGenerators) { this._emitter.emit('generators',generators); } async onNewInstanceGeneratorsAvailable(cb: (generators: IAvailableInstanceGenerators) => void) { this._emitter.on('generators',cb) } async emitRpcRequest(name: string, request: IRequestTaskMsg){ this._emitter.emit(name, request); } async emitRpcResult(name: string, result: IResponseTaskMsg){ this._emitter.emit(name,result); } async onRpcResult(name: string, cb: (result: IResponseTaskMsg) => void){ this._emitter.on(name, cb); } async offRpcResponse(name: string, cb: (result: IResponseTaskMsg) => void){ this._emitter.off(name, cb); } async onRpcRequest(name: string, cb: (data: IRequestTaskMsg) => void){ this._emitter.on(name, cb); } async offRpcRequest(name: string, cb: (data: IRequestTaskMsg) => void){ this._emitter.off(name, cb); } async emitNewServicesAvailable(services: IAvailableServicesMsg){ this._emitter.emit('services', services) } async onNewServicesAvailable(cb: (services: IAvailableServicesMsg) => void) { this._emitter.on('services', cb); } async onBonjour(cb: (dispatcher: string) => void){ this._emitter.on('bonjour', cb); } async emitBonjour(dispatcher: string){ this._emitter.emit('bonjour', dispatcher); } async emitNewTopicsAvailable(topics: IAvailableTopicsMsg){ this._emitter.emit('topics',topics) } async onNewTopicsAvailable(cb: (topics: IAvailableTopicsMsg) => void) { this._emitter.on('topics',cb) } async onEvent(event: string, cb: (data: IExternalEventMsg) => void){ this._emitter.on('event_'+event, cb); } async emitEvent(event: string, data: IExternalEventMsg){ this._emitter.emit('event_'+event, data) } async offEvent(event: string, cb: (data: IExternalEventMsg) => void){ this._emitter.off('event_'+event, cb); } protected _emitter = new EventEmitter(); }