nope/lib/communication/eventLayer.ts

31 lines
848 B
TypeScript
Raw Normal View History

2020-08-21 16:38:21 +00:00
import { EventEmitter } from "events";
import { ICommunicationInterface } from "../dispatcher/nopeDispatcher";
import { getLogger } from "../logger/getLogger";
2020-08-21 16:38:21 +00:00
2020-08-23 07:21:03 +00:00
/**
2020-09-11 07:59:23 +00:00
* A Communication Layer for the Dispatchers.
* Here, only a Events are used.
*
* @export
* @class EventLayer
* @implements {ICommunicationInterface}
2020-08-23 07:21:03 +00:00
*/
2020-08-21 16:38:21 +00:00
export class EventLayer implements ICommunicationInterface {
off(event: string, cb: (...args: any[]) => void) {
this._emitter.off(event, cb);
}
2020-08-21 16:38:21 +00:00
protected _emitter = new EventEmitter();
protected _logger = getLogger('info', 'Event-Layer');
on(event: string, cb: (...args: any[]) => void) {
// this._logger.debug('subscribed ' + event);
2020-08-21 16:38:21 +00:00
this._emitter.on(event, cb);
}
send(event: string, data: any): void {
// this._logger.debug('emitting ' + event);
this._emitter.emit(event, data)
2020-08-21 16:38:21 +00:00
}
}