nope/lib/communication/bridge.ts

611 lines
18 KiB
TypeScript
Raw Normal View History

/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2020-11-06 08:52:36
2021-04-12 05:09:47 +00:00
* @modify date 2021-03-24 07:58:29
* @desc [description]
*/
2021-03-19 18:17:39 +00:00
2021-03-19 13:01:51 +00:00
import { EventEmitter } from "events";
2021-03-19 18:17:39 +00:00
import * as Logger from "js-logger";
import { ILogger } from "js-logger";
2021-03-19 13:01:51 +00:00
import { generateId } from "../helpers/idMethods";
2021-03-22 18:02:24 +00:00
import { copy } from "../helpers/objectMethods";
2021-03-19 13:01:51 +00:00
import { getNopeLogger } from "../logger/getLogger";
import { LoggerLevel } from "../logger/nopeLogger";
import { NopeObservable } from "../observables/nopeObservable";
2021-03-19 13:01:51 +00:00
import {
ICommunicationBridge,
2021-03-22 18:02:24 +00:00
ICommunicationInterface,
2021-03-24 06:50:36 +00:00
ICommunicationMirror,
IEmitter,
ValidEventTypesOfMirror
2021-03-19 13:01:51 +00:00
} from "../types/nope/nopeCommunication.interface";
import { INopeObservable } from "../types/nope/nopeObservable.interface";
2021-03-19 13:01:51 +00:00
const EMITTING_METHODS: Array<keyof ICommunicationInterface> = [
"emitAurevoir",
"emitBonjour",
"emitNewInstanceGeneratorsAvailable",
"emitNewInstancesAvailable",
"emitNewObersvablesAvailable",
"emitNewServicesAvailable",
2020-12-05 01:28:33 +00:00
"emitStatusUpdate",
2021-03-19 13:01:51 +00:00
"emitTaskCancelation"
];
const EMITTING_SPECIFIC_METHODS: Array<keyof ICommunicationInterface> = [
"emitEvent",
"emitRpcRequest",
"emitRpcResponse"
];
const ON_METHODS: Array<keyof ICommunicationInterface> = [
"onAurevoir",
"onBonjour",
"onNewInstanceGeneratorsAvailable",
"onNewInstancesAvailable",
"onNewObservablesAvailable",
"onNewServicesAvailable",
2020-12-05 01:28:33 +00:00
"onStatusUpdate",
2021-01-12 15:40:54 +00:00
"onTaskCancelation"
];
2021-03-19 13:01:51 +00:00
const ON_SPECIFIC_METHODS: Array<keyof ICommunicationInterface> = [
"onEvent",
"onRpcRequest",
"onRpcResponse"
];
2021-04-14 09:58:41 +00:00
const UNSPECIFIC_TO_METHODS: {
[index: string]: {
emit: keyof ICommunicationInterface;
subscribe: keyof ICommunicationInterface;
};
} = {
2021-04-20 18:49:15 +00:00
Aurevoir: {
2021-04-14 09:58:41 +00:00
emit: "emitAurevoir",
subscribe: "onAurevoir"
},
2021-04-20 18:49:15 +00:00
Bonjour: {
2021-04-14 09:58:41 +00:00
emit: "emitBonjour",
subscribe: "onBonjour"
},
NewInstancesAvailable: {
emit: "emitNewInstancesAvailable",
subscribe: "onNewInstancesAvailable"
},
NewInstanceGeneratorsAvailable: {
emit: "emitNewInstanceGeneratorsAvailable",
subscribe: "onNewInstanceGeneratorsAvailable"
},
NewObersvablesAvailable: {
emit: "emitNewObersvablesAvailable",
subscribe: "onNewObservablesAvailable"
},
NewServicesAvailable: {
emit: "emitNewServicesAvailable",
subscribe: "onNewServicesAvailable"
},
StatusUpdate: {
emit: "emitStatusUpdate",
subscribe: "onStatusUpdate"
},
TaskCancelation: {
emit: "emitTaskCancelation",
subscribe: "onTaskCancelation"
}
};
2021-03-19 13:01:51 +00:00
2021-04-14 09:58:41 +00:00
const LAYER_METHOD_TO_EVENT: {
[P in keyof ICommunicationInterface]: string;
} = {
2021-03-19 13:01:51 +00:00
// Specific:
onEvent: "event/",
2021-04-20 18:49:15 +00:00
emitEvent: "event/",
onRpcRequest: "",
emitRpcRequest: "",
onRpcResponse: "",
emitRpcResponse: ""
2021-03-19 13:01:51 +00:00
};
2021-04-14 09:58:41 +00:00
for (const eventName in UNSPECIFIC_TO_METHODS) {
const data = UNSPECIFIC_TO_METHODS[eventName];
LAYER_METHOD_TO_EVENT[data.emit as any] = eventName;
LAYER_METHOD_TO_EVENT[data.subscribe as any] = eventName;
}
// Define additionally a Mapping between the Events
const MIRROR_METHOD_TO_EVENT: {
2021-03-24 06:50:36 +00:00
[P in keyof ICommunicationInterface]: ValidEventTypesOfMirror;
2021-04-14 09:58:41 +00:00
} = copy(LAYER_METHOD_TO_EVENT) as any;
2021-03-24 06:50:36 +00:00
2021-04-20 18:49:15 +00:00
MIRROR_METHOD_TO_EVENT.onEvent = "Event";
MIRROR_METHOD_TO_EVENT.emitEvent = "Event";
MIRROR_METHOD_TO_EVENT.onRpcRequest = "RpcRequest";
MIRROR_METHOD_TO_EVENT.emitRpcRequest = "RpcRequest";
MIRROR_METHOD_TO_EVENT.onRpcResponse = "RpcResponse";
MIRROR_METHOD_TO_EVENT.emitRpcResponse = "RpcResponse";
2021-03-22 18:02:24 +00:00
const MIRROR_EVENT_TO_EMIT: {
2021-03-24 06:50:36 +00:00
[P in keyof ValidEventTypesOfMirror]: keyof ICommunicationInterface;
2021-04-14 09:58:41 +00:00
} = {} as any;
2021-03-22 18:02:24 +00:00
// Define all Events, that must be considered in a mirror.
2021-04-14 09:58:41 +00:00
Object.getOwnPropertyNames(MIRROR_METHOD_TO_EVENT).map((method) => {
2021-03-24 06:50:36 +00:00
if (method.startsWith("emit")) {
MIRROR_EVENT_TO_EMIT[
2021-04-14 09:58:41 +00:00
MIRROR_METHOD_TO_EVENT[method]
2021-03-24 06:50:36 +00:00
] = method as keyof ICommunicationInterface;
}
});
2021-03-22 18:02:24 +00:00
2021-03-19 13:01:51 +00:00
const OFF_METHODS: Array<keyof ICommunicationInterface> = [
"offEvent",
"offRpcRequest",
"offRpcResponse"
];
//@ts-ignore Ignore the Interface. Its implemented manually
2021-03-19 18:17:39 +00:00
export class Bridge implements ICommunicationBridge {
public connected: INopeObservable<boolean>;
public considerConnection = true;
2021-03-12 07:34:22 +00:00
public allowServiceRedundancy = false;
public ownDispatcherId: string;
2021-03-19 13:01:51 +00:00
public id: string;
protected _logger: ILogger;
protected _internalEmitter: EventEmitter;
protected _layers: Map<
2021-03-12 07:40:31 +00:00
string,
{
2021-03-19 13:01:51 +00:00
layer: ICommunicationInterface;
considerConnection: boolean;
forwardData: boolean;
2021-03-12 07:40:31 +00:00
}
>;
2021-03-12 07:34:22 +00:00
2021-03-24 06:50:36 +00:00
protected _mirrors: Map<
ICommunicationMirror,
{ considerConnection: boolean }
>;
2021-03-22 18:02:24 +00:00
2021-03-19 18:17:39 +00:00
protected _callbacks: Map<
string, // Method
Array<(...args) => any>
>;
protected _specificCallbacks: Array<{
callback: (...args) => any;
method: keyof ICommunicationInterface;
event: string;
}>;
2021-03-19 18:17:39 +00:00
/**
* Creates an instance of Bridge.
* @param {*} [id=generateId()] The ID. (this can be adapted later and is only used to simplify debugging)
* @param {string} [loggerName="bridge"] The Name of the Logger.
* @param {LoggerLevel} [level="info"] The Level of the Logger.
* @memberof Bridge
*/
constructor(
id = generateId(),
loggerName = "bridge",
level: LoggerLevel = "info"
) {
2021-03-19 13:01:51 +00:00
this._internalEmitter = new EventEmitter();
this._callbacks = new Map();
2021-03-19 18:17:39 +00:00
this._specificCallbacks = [];
2021-03-19 13:01:51 +00:00
this._layers = new Map();
2021-03-24 06:50:36 +00:00
this._mirrors = new Map();
2021-03-19 18:17:39 +00:00
this._logger = getNopeLogger(loggerName, level);
2021-03-19 13:01:51 +00:00
this.id = id;
2021-03-19 13:01:51 +00:00
const _this = this;
2021-03-12 07:34:22 +00:00
2021-03-19 13:01:51 +00:00
// Now we use the loops to define the functions,
// required by the interface.
for (const method of ON_METHODS) {
2021-03-19 18:17:39 +00:00
this[method] = async (cb) => {
_this._on(method, cb);
2021-03-19 13:01:51 +00:00
};
2021-03-12 07:34:22 +00:00
}
2021-03-19 13:01:51 +00:00
for (const method of ON_SPECIFIC_METHODS) {
2021-03-19 18:17:39 +00:00
this[method] = async (event: string, cb) => {
_this._onSpecific(method, event, cb);
2021-03-19 13:01:51 +00:00
};
2021-03-19 12:19:06 +00:00
}
2021-03-19 13:01:51 +00:00
for (const method of OFF_METHODS) {
2021-03-19 18:17:39 +00:00
this[method] = async (event: string, cb) => {
_this._off(cb);
2021-03-19 13:01:51 +00:00
};
2021-03-12 07:34:22 +00:00
}
2021-03-19 13:01:51 +00:00
for (const method of EMITTING_METHODS) {
2021-04-14 09:58:41 +00:00
const eventName = LAYER_METHOD_TO_EVENT[method];
2021-03-19 18:17:39 +00:00
this[method] = async (data) => {
2021-05-13 08:04:51 +00:00
_this._emit(eventName, null, data);
2021-03-19 13:01:51 +00:00
};
}
2021-03-19 13:01:51 +00:00
for (const method of EMITTING_SPECIFIC_METHODS) {
2021-03-22 18:02:24 +00:00
this[method] = async (event: string, data) => {
_this._emitSpecific(method, null, event, data);
2021-03-12 07:34:22 +00:00
};
2021-03-19 13:01:51 +00:00
}
2021-03-12 07:34:22 +00:00
2021-03-19 13:01:51 +00:00
this.connected = new NopeObservable();
this.connected.setContent(false);
2021-03-12 07:34:22 +00:00
2021-03-19 13:01:51 +00:00
// Add a custom handler for the connect flag.
// the Flag is defined as true, if every socket
// is connected.
this.connected.getter = () => {
for (const data of _this._layers.values()) {
2021-04-12 05:09:47 +00:00
if (data.considerConnection && !data.layer.connected.getContent()) {
2021-03-19 13:01:51 +00:00
return false;
2021-04-12 05:09:47 +00:00
}
2021-03-12 07:34:22 +00:00
}
2021-04-12 05:09:47 +00:00
for (const [mirror, { considerConnection }] of _this._mirrors.entries()) {
if (considerConnection && !mirror.connected.getContent()) {
2021-03-24 06:50:36 +00:00
return false;
2021-04-12 05:09:47 +00:00
}
2021-03-24 06:50:36 +00:00
}
2021-03-19 13:01:51 +00:00
return true;
};
2021-03-12 07:34:22 +00:00
}
/**
2021-03-19 13:01:51 +00:00
* Helper Function, which will internally subscribe to the Events of the Layer.
2021-03-12 07:34:22 +00:00
*
* @protected
2021-03-19 13:01:51 +00:00
* @param {ICommunicationInterface} layer The Layer to consinder, on this layer, we will subscribe to the events
* @param {keyof ICommunicationInterface} method The method used for subscription
2021-03-19 18:17:39 +00:00
* @param {string} event The name of the Event
2021-03-19 13:01:51 +00:00
* @param {boolean} forwardData Flag, showing whether data will be forwarded or not.
* @memberof BridgeV2
2021-03-12 07:34:22 +00:00
*/
2021-03-19 13:01:51 +00:00
protected _subscribeToCallback(
2021-03-12 07:34:22 +00:00
layer: ICommunicationInterface,
2021-03-19 13:01:51 +00:00
method: keyof ICommunicationInterface,
2021-03-19 18:17:39 +00:00
event: string,
2021-03-19 13:01:51 +00:00
forwardData: boolean
): void {
2021-03-12 07:34:22 +00:00
const _this = this;
2021-04-20 18:49:15 +00:00
if (ON_SPECIFIC_METHODS.includes(method)) {
2021-03-19 18:17:39 +00:00
layer[method as any](event, (data) => {
2021-03-19 13:01:51 +00:00
// Define the Method to Forward data.
2021-03-19 18:17:39 +00:00
_this._internalEmitter.emit(event, data);
2021-03-12 07:34:22 +00:00
2021-03-19 13:01:51 +00:00
// Now we are able to iterate over the Methods and forward the content
// but only if the Layer forwards the content
if (forwardData) {
2021-04-12 05:09:47 +00:00
_this._emitSpecific(method, layer, event, data);
2021-03-19 13:01:51 +00:00
}
2021-03-19 18:17:39 +00:00
}).catch((error) => {
_this._logger.error("failed subscribing", method);
_this._logger.error(error);
2021-03-19 13:01:51 +00:00
});
} else {
// Subscribe to the Event.
2021-03-19 18:17:39 +00:00
layer[method as any]((data) => {
2021-03-19 13:01:51 +00:00
// Define the Method to Forward data.
2021-04-14 09:58:41 +00:00
_this._internalEmitter.emit(LAYER_METHOD_TO_EVENT[method], data);
2021-03-19 13:01:51 +00:00
// Now we are able to iterate over the Methods and forward the content
// but only if the Layer forwards the content
if (forwardData) {
2021-04-12 05:09:47 +00:00
_this._emit(method, layer, data);
2021-03-19 13:01:51 +00:00
}
2021-03-19 18:17:39 +00:00
}).catch((error) => {
_this._logger.error("failed subscribing", method);
_this._logger.error(error);
2021-03-19 13:01:51 +00:00
});
}
}
2021-03-12 07:34:22 +00:00
2021-03-19 18:17:39 +00:00
protected _on(method: keyof ICommunicationInterface, cb): void {
// Determine the Corresponding Event, that should be used
// to subescribe to.
2021-04-14 09:58:41 +00:00
const event = LAYER_METHOD_TO_EVENT[method];
2021-03-19 18:17:39 +00:00
2021-03-19 13:01:51 +00:00
// Subscribe on the Event
this._internalEmitter.setMaxListeners(
this._internalEmitter.getMaxListeners() + 1
);
2021-03-19 18:17:39 +00:00
if (this._logger.enabledFor(Logger.DEBUG) && event !== "StatusUpdate") {
this._logger.debug("subscribe to", event);
// If logging is enable, we subscribe to that.
const _this = this;
this._internalEmitter.on(event, (data) => {
_this._logger.debug("received", event, data);
});
}
2021-03-19 13:01:51 +00:00
this._internalEmitter.on(event, cb);
2021-03-12 07:34:22 +00:00
2021-03-19 18:17:39 +00:00
// Store the Unspecific callbacks
2021-03-19 13:01:51 +00:00
if (!this._callbacks.has(method)) {
2021-03-19 18:17:39 +00:00
this._callbacks.set(method, [cb]);
2021-03-19 13:01:51 +00:00
} else {
2021-03-19 18:17:39 +00:00
this._callbacks.get(method).push(cb);
2021-03-12 07:34:22 +00:00
}
2021-03-19 18:17:39 +00:00
2021-03-19 13:01:51 +00:00
// Iterate over the Layers and on the connected Layers,
// subscribe the methods.
for (const data of this._layers.values()) {
if (data.layer.connected.getContent()) {
2021-03-19 18:17:39 +00:00
this._subscribeToCallback(data.layer, method, event, data.forwardData);
2021-03-19 13:01:51 +00:00
}
2021-03-12 07:34:22 +00:00
}
}
2021-03-19 18:17:39 +00:00
protected _onSpecific(
2021-03-19 13:01:51 +00:00
method: keyof ICommunicationInterface,
event: string,
2021-03-19 18:17:39 +00:00
callback
2021-03-19 13:01:51 +00:00
): void {
2021-03-19 18:17:39 +00:00
// Determine the event that we are using internally.
// It is a mix between the method alias channel and
// the event.
2021-04-14 09:58:41 +00:00
const eventToUse = LAYER_METHOD_TO_EVENT[method] + event;
2021-03-19 18:17:39 +00:00
// Subscribe on the Event
this._internalEmitter.setMaxListeners(
this._internalEmitter.getMaxListeners() + 1
);
2021-03-22 18:02:24 +00:00
2021-03-19 18:17:39 +00:00
if (this._logger.enabledFor(Logger.DEBUG) && event !== "StatusUpdate") {
this._logger.debug("subscribe ", method, "specifically on", eventToUse);
const _this = this;
this._internalEmitter.on(eventToUse, (data) => {
_this._logger.debug("received", event, data);
});
}
this._internalEmitter.on(eventToUse, callback);
// Add the callbacks.
this._specificCallbacks.push({
callback,
method,
event
});
// Iterate over the Layers and on the connected Layers,
// subscribe the methods.
for (const data of this._layers.values()) {
if (data.layer.connected.getContent()) {
this._subscribeToCallback(data.layer, method, event, data.forwardData);
}
}
}
protected _off(cb): void {
// Determine the event that we are using internally.
// It is a mix between the method alias channel and
// the event.
for (let i = 0; i < this._specificCallbacks.length; i++) {
if (this._specificCallbacks[i].callback == cb) {
this._specificCallbacks.splice(i, 1);
break;
}
2021-03-19 13:01:51 +00:00
}
2020-12-05 01:28:33 +00:00
2021-03-19 13:01:51 +00:00
// Theoretically we must remove the subscriptions.
// But we wont do this, because if the connection has
// been lost, or the Layer has been removed, the stored
// callbacks wont be called any more.
}
2021-03-19 13:01:51 +00:00
protected _emit(
2021-04-14 09:58:41 +00:00
eventName: string,
2021-03-24 06:50:36 +00:00
toExclude: ICommunicationInterface | ICommunicationMirror = null,
2021-03-19 18:17:39 +00:00
dataToSend: any
): void {
2021-04-20 18:49:15 +00:00
if (this._logger.enabledFor(Logger.WARN) && eventName !== "StatusUpdate") {
2021-04-14 09:58:41 +00:00
this._logger.debug("emitting", eventName, dataToSend);
2021-03-19 18:17:39 +00:00
}
// Emit the Event on the internal Layer.
2021-04-14 09:58:41 +00:00
this._internalEmitter.emit(eventName, dataToSend);
2021-03-19 18:17:39 +00:00
const _this = this;
2021-04-14 09:58:41 +00:00
const _method = UNSPECIFIC_TO_METHODS[eventName].emit;
2021-03-19 18:17:39 +00:00
// Iterate over the Layers.
for (const data of this._layers.values()) {
// If the Layer has been conneced
2021-03-22 18:02:24 +00:00
if (data.layer !== toExclude && data.layer.connected.getContent()) {
// Only Publish the Data, on which we are forwarding
2021-04-14 09:58:41 +00:00
data.layer[_method as any](dataToSend).catch((error) => {
_this._logger.error("failed executing", _method);
2021-03-19 18:17:39 +00:00
_this._logger.error(error);
});
}
}
2021-03-22 18:02:24 +00:00
2021-03-24 06:50:36 +00:00
for (const mirror of this._mirrors.keys()) {
if (mirror != toExclude && mirror.connected.getContent()) {
2021-04-14 09:58:41 +00:00
mirror.emit(eventName, dataToSend);
2021-03-22 18:02:24 +00:00
}
}
2021-03-19 18:17:39 +00:00
}
protected _emitSpecific(
2021-03-19 13:01:51 +00:00
method: keyof ICommunicationInterface,
2021-03-24 06:50:36 +00:00
toExclude: ICommunicationInterface | ICommunicationMirror = null,
2021-03-19 13:01:51 +00:00
event: string,
2021-03-19 18:17:39 +00:00
dataToSend: any
2021-03-19 13:01:51 +00:00
): void {
2021-03-19 18:17:39 +00:00
if (this._logger.enabledFor(Logger.DEBUG) && event !== "StatusUpdate") {
this._logger.debug(
"emitting",
2021-04-14 09:58:41 +00:00
LAYER_METHOD_TO_EVENT[method] + event,
2021-03-19 18:17:39 +00:00
dataToSend
);
}
2021-03-19 13:01:51 +00:00
// Emit the Event on the internal Layer.
2021-04-14 09:58:41 +00:00
this._internalEmitter.emit(
LAYER_METHOD_TO_EVENT[method] + event,
dataToSend
);
2021-03-19 18:17:39 +00:00
const _this = this;
2021-03-19 13:01:51 +00:00
// Iterate over the Layers.
for (const data of this._layers.values()) {
// If the Layer has been conneced
2021-03-22 18:02:24 +00:00
if (data.layer !== toExclude && data.layer.connected.getContent()) {
2021-03-19 18:17:39 +00:00
data.layer[method as any](event, dataToSend).catch((error) => {
_this._logger.error("failed executing", method);
_this._logger.error(error);
});
}
}
2021-03-22 18:02:24 +00:00
2021-03-24 06:50:36 +00:00
for (const mirror of this._mirrors.keys()) {
if (mirror != toExclude && mirror.connected.getContent()) {
2021-04-14 09:58:41 +00:00
mirror.emit(MIRROR_METHOD_TO_EVENT[method], {
2021-03-22 18:02:24 +00:00
name: event,
data: dataToSend
});
}
}
}
2021-03-19 13:01:51 +00:00
public async addLayer(
2021-01-12 15:40:54 +00:00
layer: ICommunicationInterface,
2021-03-19 13:01:51 +00:00
forwardData = false,
considerConnection = false
): Promise<void> {
if (!this._layers.has(layer.id)) {
// Store the Layers:
this._layers.set(layer.id, {
layer,
considerConnection,
forwardData
});
2020-12-05 01:28:33 +00:00
2021-03-19 13:01:51 +00:00
// Forward the Events of the Layer
// being connected to our aggregated
// state
const _this = this;
layer.connected.subscribe(() => {
_this.connected.forcePublish();
});
2021-03-01 13:32:43 +00:00
2021-03-19 13:01:51 +00:00
// Wait until the Layer is connected.
await layer.connected.waitFor((value) => value);
2021-03-12 07:34:22 +00:00
2021-03-19 18:17:39 +00:00
// Register all know unspecific methods
for (const [method, cbs] of this._callbacks.entries()) {
for (const callback of cbs) {
layer[method as any](callback);
2020-12-05 01:28:33 +00:00
}
}
2021-03-19 18:17:39 +00:00
// Register all know specific methods
for (const { method, event, callback } of this._specificCallbacks) {
layer[method as any](event, callback);
}
}
}
public async removeLayer(layer: ICommunicationInterface): Promise<void> {
if (this._layers.has(layer.id)) {
this._layers.delete(layer.id);
}
}
2021-03-22 18:02:24 +00:00
/**
* Adds a Mirror to the System.
*
2021-04-12 05:09:47 +00:00
* @param {IEmitter} mirror the Mirror to use.
2021-03-22 18:02:24 +00:00
* @return {*} {Promise<void>}
* @memberof Bridge
*/
2021-03-24 06:50:36 +00:00
public addMirror(
mirror: ICommunicationMirror,
considerConnection = true
): void {
2021-03-22 18:02:24 +00:00
if (!this._mirrors.has(mirror)) {
2021-03-24 06:50:36 +00:00
// Add the Mirror
this._mirrors.set(mirror, {
considerConnection
});
const _this = this;
2021-04-12 05:09:47 +00:00
// Subscribe for the Connected Flag.
if (considerConnection) {
// We just forward the Signal
mirror.connected.subscribe(() => _this.connected.forcePublish());
}
2021-03-24 06:50:36 +00:00
2021-04-12 05:09:47 +00:00
// Now we wait until the connection has been established first time,
// then, we will add our subscriptions.
mirror.connected
.waitFor((value) => value)
.then(() => {
for (const eventName of ValidEventTypesOfMirror) {
2021-04-14 09:58:41 +00:00
if (UNSPECIFIC_TO_METHODS[eventName]) {
2021-04-12 05:09:47 +00:00
mirror.on(eventName as ValidEventTypesOfMirror, (data) => {
2021-04-14 09:58:41 +00:00
// Unless it is a Status-Update, we will log it.
2021-04-12 05:09:47 +00:00
if (
_this._logger.enabledFor(Logger.DEBUG) &&
eventName !== "StatusUpdate"
) {
_this._logger.debug(
"received data from mirror on",
2021-04-14 09:58:41 +00:00
eventName,
". Forwarding event to",
UNSPECIFIC_TO_METHODS[eventName].emit
2021-04-12 05:09:47 +00:00
);
}
// Now Forward the Data.
2021-04-14 09:58:41 +00:00
_this._emit(eventName, mirror, data);
2021-04-12 05:09:47 +00:00
});
} else {
mirror.on(eventName as ValidEventTypesOfMirror, (data) => {
// If logging is enabled => Forward this information
if (
_this._logger.enabledFor(Logger.DEBUG) &&
eventName !== "StatusUpdate"
) {
_this._logger.debug(
"received data from mirror on",
eventName,
data.name
);
}
// Now emit the data on the channel
_this._emitSpecific(
MIRROR_EVENT_TO_EMIT[eventName],
mirror,
data.name,
data.data
);
});
2021-03-24 06:50:36 +00:00
}
2021-04-12 05:09:47 +00:00
}
});
2021-03-22 18:02:24 +00:00
}
}
2021-04-12 05:09:47 +00:00
/**
* Removes a Mirror, from the system.
*
* @param {ICommunicationMirror} mirror
* @return {*} {boolean}
* @memberof Bridge
*/
2021-03-24 06:50:36 +00:00
public removeMirror(mirror: ICommunicationMirror): boolean {
2021-03-22 18:02:24 +00:00
return this._mirrors.delete(mirror);
}
}