nope/lib/communication/layers/eventLayer.ts

298 lines
7.6 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
2021-11-12 07:57:03 +00:00
* @modify date 2021-11-10 16:52:40
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";
2021-03-22 19:24:15 +00:00
import { generateId } from "../../helpers/idMethods";
import { NopeObservable } from "../../observables/nopeObservable";
import {
IAvailableInstanceGeneratorsMsg,
IAvailableInstancesMsg,
IAvailableServicesMsg,
IAvailableTopicsMsg,
ICommunicationInterface,
2021-03-19 13:26:38 +00:00
IEmitter,
2021-08-17 15:52:46 +00:00
IExecutingTaskMsg,
IExternalEventMsg,
2021-11-12 07:57:03 +00:00
IExternalPropertyChangedMsg,
IRequestTaskMsg,
IResponseTaskMsg,
2021-08-17 15:52:46 +00:00
IRpcUnregisterMsg,
ITaskCancelationMsg
2021-03-22 19:24:15 +00:00
} from "../../types/nope/nopeCommunication.interface";
import { IDispatcherInfo } from "../../types/nope/nopeDispatcher.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.
2021-04-12 05:09:47 +00:00
* Here, only a Events are used. This layer represents
* the basic layer. If transmits the events using
* internal emitters.
2020-09-11 07:59:23 +00:00
*
* @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 {
2021-04-12 05:09:47 +00:00
/**
* Flag, showing whether the Mirror is connected or not.
*
* @type {INopeObservable<boolean>}
* @memberof EventLayer
*/
connected: INopeObservable<boolean>;
2021-04-12 05:09:47 +00:00
/**
* Flag, to enable, that this layer will be used in a bridge,
* to show, whether the bridge is connected or not.
*
* @memberof EventLayer
*/
considerConnection = true;
2021-04-12 05:09:47 +00:00
2021-03-19 13:26:38 +00:00
allowServiceRedundancy = false;
/**
* Creaetes an Event Emitter-based Communication Layer:
2020-12-30 18:53:48 +00:00
* @param _emitter
* @param _logger
*/
constructor(
2020-12-05 01:28:33 +00:00
protected _emitter: IEmitter = new EventEmitter(),
protected _logger?: ILogger
2020-11-23 06:09:31 +00:00
) {
this.connected = new NopeObservable();
this.connected.setContent(true);
2021-03-12 07:47:30 +00:00
this.id = generateId();
2021-04-12 05:09:47 +00:00
this._subscribing = new Map();
}
2021-03-19 13:26:38 +00:00
2021-11-12 07:57:03 +00:00
allowsServiceRedundancy: boolean;
receivesOwnMessages: boolean;
2021-03-12 07:47:30 +00:00
id: string;
2020-11-23 06:09:31 +00:00
2021-04-12 05:09:47 +00:00
protected _subscribing: Map<string, Set<(...args) => any>>;
/**
* Helper-Function, which will publish the subescribe events
*
* @protected
* @memberof EventLayer
*/
protected _publishSubscriptions(): void {
const activeSubscriptions = Array.from(this._subscribing.entries())
.filter((entry) => {
return entry[1].size > 0;
})
.map((entry) => entry[0]);
this._emitter.emit("subscribing", activeSubscriptions);
}
protected async _on(event: string, cb): Promise<void> {
if (typeof (await this._emitter.getMaxListeners) === "function") {
await this._emitter.setMaxListeners(
(await this._emitter.getMaxListeners()) + 1
);
2020-12-30 18:53:48 +00:00
}
2021-04-12 05:09:47 +00:00
// Store the Event we are publishing
if (!this._subscribing.has(event)) {
this._subscribing.set(event, new Set());
}
this._subscribing.get(event).add(cb);
this._publishSubscriptions();
2020-12-30 18:53:48 +00:00
this._emitter.on(event, cb);
}
2021-04-12 05:09:47 +00:00
protected async _emit(name: string, data: any): Promise<void> {
this._emitter.emit("emitted", { name, data });
}
protected async _off(name, cb): Promise<void> {
this._off(name, cb);
// Store the Event we are publishing
if (!this._subscribing.has(name)) {
this._subscribing.set(name, new Set());
}
this._subscribing.get(name).delete(cb);
this._publishSubscriptions();
}
2020-12-05 01:28:33 +00:00
async emitStatusUpdate(status: IDispatcherInfo): Promise<void> {
2021-04-12 05:09:47 +00:00
await this._emit("statusUdpate", status);
2020-12-05 01:28:33 +00:00
}
2020-12-30 18:53:48 +00:00
2020-12-05 01:28:33 +00:00
async onStatusUpdate(cb: (status: IDispatcherInfo) => void): Promise<void> {
2021-04-12 05:09:47 +00:00
await this._on("statusUdpate", cb);
2020-12-05 01:28:33 +00:00
}
async onNewInstancesAvailable(
cb: (instances: IAvailableInstancesMsg) => void
): Promise<void> {
2021-04-12 05:09:47 +00:00
await this._on("newInstancesAvailable", cb);
}
async emitNewInstancesAvailable(
instances: IAvailableInstancesMsg
): Promise<void> {
2021-04-12 05:09:47 +00:00
await this._emit("newInstancesAvailable", instances);
}
async onTaskCancelation(
cb: (msg: ITaskCancelationMsg) => void
): Promise<void> {
2021-04-12 05:09:47 +00:00
await this._on("cancel", cb);
2020-11-04 16:33:25 +00:00
}
async emitTaskCancelation(msg: ITaskCancelationMsg): Promise<void> {
2021-04-12 05:09:47 +00:00
await this._emit("cancel", msg);
2020-11-04 16:33:25 +00:00
}
async onAurevoir(cb: (dispatcher: string) => void): Promise<void> {
2021-04-12 05:09:47 +00:00
await this._on("aurevoir", cb);
2020-11-04 16:33:25 +00:00
}
async emitAurevoir(dispatcher: string): Promise<void> {
2021-04-12 05:09:47 +00:00
await this._emit("aurevoir", dispatcher);
2020-11-04 16:33:25 +00:00
}
async emitNewInstanceGeneratorsAvailable(
generators: IAvailableInstanceGeneratorsMsg
): Promise<void> {
2021-04-12 05:09:47 +00:00
await this._emit("generators", generators);
}
async onNewInstanceGeneratorsAvailable(
cb: (generators: IAvailableInstanceGeneratorsMsg) => void
): Promise<void> {
2021-04-12 05:09:47 +00:00
await this._on("generators", cb);
}
async emitRpcRequest(name: string, request: IRequestTaskMsg): Promise<void> {
2021-04-12 05:09:47 +00:00
await this._emit(name, request);
2020-09-13 10:35:04 +00:00
}
2020-12-05 01:28:33 +00:00
async emitRpcResponse(name: string, result: IResponseTaskMsg): Promise<void> {
2021-04-12 05:09:47 +00:00
await this._emit(name, result);
2020-09-13 10:35:04 +00:00
}
async onRpcResponse(
name: string,
cb: (result: IResponseTaskMsg) => void
): Promise<void> {
2021-04-12 05:09:47 +00:00
await this._on(name, cb);
2020-09-13 10:35:04 +00:00
}
async offRpcResponse(
name: string,
cb: (result: IResponseTaskMsg) => void
): Promise<void> {
2021-04-12 05:09:47 +00:00
await this._off(name, cb);
2020-09-13 10:35:04 +00:00
}
async onRpcRequest(
name: string,
cb: (data: IRequestTaskMsg) => void
): Promise<void> {
2021-04-12 05:09:47 +00:00
await this._on(name, cb);
2020-09-13 10:35:04 +00:00
}
async offRpcRequest(
name: string,
cb: (data: IRequestTaskMsg) => void
): Promise<void> {
2021-04-12 05:09:47 +00:00
await this._off(name, cb);
2020-09-13 10:35:04 +00:00
}
async emitNewServicesAvailable(
services: IAvailableServicesMsg
): Promise<void> {
2021-04-12 05:09:47 +00:00
await this._emit("services", services);
2020-09-13 10:35:04 +00:00
}
async onNewServicesAvailable(
cb: (services: IAvailableServicesMsg) => void
): Promise<void> {
2021-04-12 05:09:47 +00:00
await this._on("services", cb);
}
async onBonjour(cb): Promise<void> {
2021-04-12 05:09:47 +00:00
await this._on("bonjour", cb);
2020-09-17 06:05:45 +00:00
}
async emitBonjour(msg): Promise<void> {
2021-04-12 05:09:47 +00:00
await this._emit("bonjour", msg);
2020-09-17 06:05:45 +00:00
}
2021-08-04 12:56:37 +00:00
async emitNewObservablesAvailable(
topics: IAvailableTopicsMsg
): Promise<void> {
2021-04-12 05:09:47 +00:00
await this._emit("topics", topics);
2020-09-17 06:05:45 +00:00
}
async onNewObservablesAvailable(
cb: (topics: IAvailableTopicsMsg) => void
): Promise<void> {
2021-04-12 05:09:47 +00:00
await this._on("topics", cb);
2020-09-17 06:05:45 +00:00
}
async onEvent(
event: string,
cb: (data: IExternalEventMsg) => void
): Promise<void> {
2021-11-12 07:57:03 +00:00
await this._on(`Event:${event}`, cb);
2020-09-17 06:05:45 +00:00
}
async emitEvent(event: string, data: IExternalEventMsg): Promise<void> {
2021-11-12 07:57:03 +00:00
await this._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> {
2021-11-12 07:57:03 +00:00
await this._off(`Event:${event}`, cb);
2020-09-17 06:05:45 +00:00
}
2021-08-17 15:52:46 +00:00
async onUnregisterRpc(cb: (msg: IRpcUnregisterMsg) => void): Promise<void> {
await this._on("RpcUnregister", cb);
}
async emitUnregisterRpc(data: IRpcUnregisterMsg): Promise<void> {
await this._emit("RpcUnregister", data);
}
async onExecutingTasks(cb: (msg: IExecutingTaskMsg) => void): Promise<void> {
await this._on("ExecutingTask", cb);
}
async emitExecutingTasks(data: IExecutingTaskMsg): Promise<void> {
await this._emit("ExecutingTask", data);
}
2021-11-12 07:57:03 +00:00
async onPropertyChange(name: string, cb: (data: IExternalPropertyChangedMsg<unknown>) => void): Promise<void> {
await this._on(`PropertyChange:${name}`, cb);
}
async emitPropertyChange(name: string, data: IExternalPropertyChangedMsg<unknown>): Promise<void> {
await this._emit(`PropertyChange:${name}`, data);
}
async offPropertyChange(name: string, cb: (data: IExternalPropertyChangedMsg<unknown>) => void): Promise<void> {
await this._off(`PropertyChange:${name}`, cb);
}
2021-03-19 13:26:38 +00:00
async dispose(): Promise<void> {
this.connected.dispose();
}
}