nope/lib/communication/eventLayer.ts

175 lines
4.3 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-12-03 16:34:27
2020-11-06 08:10:30 +00:00
* @desc [description]
*/
2020-08-21 16:38:21 +00:00
import { EventEmitter } from "events";
import { ILogger } from "js-logger";
import { NopeObservable } from "../observables/nopeObservable";
import {
IAvailableInstanceGeneratorsMsg,
IAvailableInstancesMsg,
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?: ILogger
2020-11-23 06:09:31 +00:00
) {
this.connected = new NopeObservable();
this.connected.setContent(true);
}
2020-11-23 06:09:31 +00:00
async onNewInstancesAvailable(
cb: (instances: IAvailableInstancesMsg) => void
): Promise<void> {
this._emitter.on("newInstancesAvailable", cb);
}
async emitNewInstancesAvailable(
instances: IAvailableInstancesMsg
): Promise<void> {
this._emitter.emit("newInstancesAvailable", instances);
}
async onTaskCancelation(
cb: (msg: ITaskCancelationMsg) => void
): Promise<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): Promise<void> {
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): Promise<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): Promise<void> {
2020-11-23 06:09:31 +00:00
this._emitter.emit("aurevoir", dispatcher);
2020-11-04 16:33:25 +00:00
}
async emitNewInstanceGeneratorsAvailable(
generators: IAvailableInstanceGeneratorsMsg
): Promise<void> {
2020-11-23 06:09:31 +00:00
this._emitter.emit("generators", generators);
}
async onNewInstanceGeneratorsAvailable(
cb: (generators: IAvailableInstanceGeneratorsMsg) => void
): Promise<void> {
2020-11-23 06:09:31 +00:00
this._emitter.on("generators", cb);
}
async emitRpcRequest(name: string, request: IRequestTaskMsg): Promise<void> {
2020-09-13 10:35:04 +00:00
this._emitter.emit(name, request);
}
async emitRpcResult(name: string, result: IResponseTaskMsg): Promise<void> {
2020-11-23 06:09:31 +00:00
this._emitter.emit(name, result);
2020-09-13 10:35:04 +00:00
}
async onRpcResult(
name: string,
cb: (result: IResponseTaskMsg) => void
): Promise<void> {
2020-09-13 10:35:04 +00:00
this._emitter.on(name, cb);
}
async offRpcResponse(
name: string,
cb: (result: IResponseTaskMsg) => void
): Promise<void> {
2020-09-13 10:35:04 +00:00
this._emitter.off(name, cb);
}
async onRpcRequest(
name: string,
cb: (data: IRequestTaskMsg) => void
): Promise<void> {
2020-09-13 10:35:04 +00:00
this._emitter.on(name, cb);
}
async offRpcRequest(
name: string,
cb: (data: IRequestTaskMsg) => void
): Promise<void> {
2020-09-13 10:35:04 +00:00
this._emitter.off(name, cb);
}
async emitNewServicesAvailable(
services: IAvailableServicesMsg
): Promise<void> {
2020-11-23 06:09:31 +00:00
this._emitter.emit("services", services);
2020-09-13 10:35:04 +00:00
}
async onNewServicesAvailable(
cb: (services: IAvailableServicesMsg) => void
): Promise<void> {
2020-11-23 06:09:31 +00:00
this._emitter.on("services", cb);
}
async onBonjour(cb): Promise<void> {
2020-11-23 06:09:31 +00:00
this._emitter.on("bonjour", cb);
2020-09-17 06:05:45 +00:00
}
async emitBonjour(msg): Promise<void> {
this._emitter.emit("bonjour", msg);
2020-09-17 06:05:45 +00:00
}
async emitNewObersvablesAvailable(
topics: IAvailableTopicsMsg
): Promise<void> {
2020-11-23 06:09:31 +00:00
this._emitter.emit("topics", topics);
2020-09-17 06:05:45 +00:00
}
async onNewObservablesAvailable(
cb: (topics: IAvailableTopicsMsg) => void
): Promise<void> {
2020-11-23 06:09:31 +00:00
this._emitter.on("topics", cb);
2020-09-17 06:05:45 +00:00
}
async onEvent(
event: string,
cb: (data: IExternalEventMsg) => void
): Promise<void> {
2020-11-23 06:09:31 +00:00
this._emitter.on("event_" + event, cb);
2020-09-17 06:05:45 +00:00
}
async emitEvent(event: string, data: IExternalEventMsg): Promise<void> {
2020-11-23 06:09:31 +00:00
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
): Promise<void> {
2020-11-23 06:09:31 +00:00
this._emitter.off("event_" + event, cb);
2020-09-17 06:05:45 +00:00
}
}