nope/lib/communication/eventLayer.ts

120 lines
3.5 KiB
TypeScript
Raw Normal View History

2020-11-06 08:10:30 +00:00
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2020-11-06 08:52:36
* @modify date 2020-11-11 16:56:24
2020-11-06 08:10:30 +00:00
* @desc [description]
*/
2020-08-21 16:38:21 +00:00
import { EventEmitter } from "events";
2020-09-17 06:05:45 +00:00
import { Logger } from "winston";
import { NopeObservable } from "../observables/nopeObservable";
2020-11-09 08:46:06 +00:00
import { IAvailableInstanceGeneratorsMsg, IAvailableServicesMsg, IAvailableTopicsMsg, ICommunicationInterface, IExternalEventMsg, IRequestTaskMsg, IResponseTaskMsg, ITaskCancelationMsg } from "../types/nope/nopeCommunication.interface";
import { INopeObservable } from "../types/nope/nopeObservable.interface";
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 {
connected: INopeObservable<boolean>;
2020-11-23 06:09:31 +00:00
protected _emitter = new EventEmitter();
constructor(
2020-11-23 06:09:31 +00:00
public readonly subscriptionMode: "individual" | "generic" = "individual",
public readonly resultSharing: "individual" | "generic" = "individual",
protected _logger?: Logger
2020-11-23 06:09:31 +00:00
) {
this.connected = new NopeObservable();
this.connected.setContent(true);
}
2020-11-23 06:09:31 +00:00
2020-11-04 16:33:25 +00:00
async onTaskCancelation(cb: (msg: ITaskCancelationMsg) => void) {
2020-11-23 06:09:31 +00:00
this._emitter.on("cancel", cb);
2020-11-04 16:33:25 +00:00
}
async emitTaskCancelation(msg: ITaskCancelationMsg) {
2020-11-23 06:09:31 +00:00
this._emitter.emit("cancel", msg);
2020-11-04 16:33:25 +00:00
}
async onAurevoir(cb: (dispatcher: string) => void) {
2020-11-23 06:09:31 +00:00
this._emitter.on("aurevoir", cb);
2020-11-04 16:33:25 +00:00
}
async emitAurevoir(dispatcher: string) {
2020-11-23 06:09:31 +00:00
this._emitter.emit("aurevoir", dispatcher);
2020-11-04 16:33:25 +00:00
}
2020-11-04 16:33:25 +00:00
async emitNewInstanceGeneratorsAvailable(generators: IAvailableInstanceGeneratorsMsg) {
2020-11-23 06:09:31 +00:00
this._emitter.emit("generators", generators);
}
2020-11-04 16:33:25 +00:00
async onNewInstanceGeneratorsAvailable(cb: (generators: IAvailableInstanceGeneratorsMsg) => void) {
2020-11-23 06:09:31 +00:00
this._emitter.on("generators", cb);
}
2020-11-23 06:09:31 +00:00
async emitRpcRequest(name: string, request: IRequestTaskMsg) {
2020-09-13 10:35:04 +00:00
this._emitter.emit(name, request);
}
2020-11-23 06:09:31 +00:00
async emitRpcResult(name: string, result: IResponseTaskMsg) {
this._emitter.emit(name, result);
2020-09-13 10:35:04 +00:00
}
2020-11-23 06:09:31 +00:00
async onRpcResult(name: string, cb: (result: IResponseTaskMsg) => void) {
2020-09-13 10:35:04 +00:00
this._emitter.on(name, cb);
}
2020-11-23 06:09:31 +00:00
async offRpcResponse(name: string, cb: (result: IResponseTaskMsg) => void) {
2020-09-13 10:35:04 +00:00
this._emitter.off(name, cb);
}
2020-11-23 06:09:31 +00:00
async onRpcRequest(name: string, cb: (data: IRequestTaskMsg) => void) {
2020-09-13 10:35:04 +00:00
this._emitter.on(name, cb);
}
2020-11-23 06:09:31 +00:00
async offRpcRequest(name: string, cb: (data: IRequestTaskMsg) => void) {
2020-09-13 10:35:04 +00:00
this._emitter.off(name, cb);
}
2020-11-23 06:09:31 +00:00
async emitNewServicesAvailable(services: IAvailableServicesMsg) {
this._emitter.emit("services", services);
2020-09-13 10:35:04 +00:00
}
2020-10-13 13:18:25 +00:00
async onNewServicesAvailable(cb: (services: IAvailableServicesMsg) => void) {
2020-11-23 06:09:31 +00:00
this._emitter.on("services", cb);
}
2020-11-23 06:09:31 +00:00
async onBonjour(cb: (dispatcher: string) => void) {
this._emitter.on("bonjour", cb);
2020-09-17 06:05:45 +00:00
}
2020-11-23 06:09:31 +00:00
async emitBonjour(dispatcher: string) {
this._emitter.emit("bonjour", dispatcher);
2020-09-17 06:05:45 +00:00
}
2020-11-23 06:09:31 +00:00
async emitNewTopicsAvailable(topics: IAvailableTopicsMsg) {
this._emitter.emit("topics", topics);
2020-09-17 06:05:45 +00:00
}
2020-10-13 13:18:25 +00:00
async onNewTopicsAvailable(cb: (topics: IAvailableTopicsMsg) => void) {
2020-11-23 06:09:31 +00:00
this._emitter.on("topics", cb);
2020-09-17 06:05:45 +00:00
}
2020-11-23 06:09:31 +00:00
async onEvent(event: string, cb: (data: IExternalEventMsg) => void) {
this._emitter.on("event_" + event, cb);
2020-09-17 06:05:45 +00:00
}
2020-11-23 06:09:31 +00:00
async emitEvent(event: string, data: IExternalEventMsg) {
this._emitter.emit("event_" + event, data);
2020-09-17 06:05:45 +00:00
}
2020-11-23 06:09:31 +00:00
async offEvent(event: string, cb: (data: IExternalEventMsg) => void) {
this._emitter.off("event_" + event, cb);
2020-09-17 06:05:45 +00:00
}
2020-08-21 16:38:21 +00:00
}