nope/lib/communication/layers/EventCommunicationInterface.ts

102 lines
2.8 KiB
TypeScript
Raw Normal View History

2021-03-22 19:24:15 +00:00
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2021-03-22 19:03:15
* @modify date 2022-01-10 15:56:50
2021-03-22 19:24:15 +00:00
* @desc [description]
*/
2021-08-17 15:52:46 +00:00
import { EventEmitter } from "events";
2021-03-22 19:24:15 +00:00
import { ILogger } from "js-logger";
import { generateId } from "../../helpers/idMethods";
import { DEBUG } from "../../logger/index.browser";
2021-08-04 16:17:23 +00:00
import { NopeObservable } from "../../observables/nopeObservable";
2021-03-22 19:24:15 +00:00
import {
EventnameToEventType,
ICommunicationInterface,
2021-03-22 19:24:15 +00:00
IEmitter,
} from "../../types/nope";
2021-03-22 19:24:15 +00:00
import { INopeObservable } from "../../types/nope/nopeObservable.interface";
2021-04-12 05:09:47 +00:00
/**
* A Basic Mirror, used to share the events in a Mirror Style.
2021-12-04 07:25:26 +00:00
* This Layer should not be used directly. this should only
2021-04-12 05:09:47 +00:00
* be extended.
*
* @export
* @class EventMirror
* @implements {ICommunicationMirror}
*/
export class EventCommunicationInterface implements ICommunicationInterface {
2021-04-12 05:09:47 +00:00
/**
* Function which will be used to subscribe Data
*
* @param {(symbol | ValidEventTypesOfMirror)} event The Event to listen to
* @param {(...args: any[]) => void} listener The "Listener" (A Callback, which will be called)
* @memberof EventMirror
*/
async on<T extends keyof EventnameToEventType>(
eventname: T,
cb: (data: EventnameToEventType[T]) => void
): Promise<void> {
this._emitter.on(eventname, cb);
if (eventname !== "StatusChanged" && this._logger?.enabledFor(DEBUG)) {
this._emitter.on(eventname, (...args) => {
2022-01-18 07:01:50 +00:00
this._logger.debug("received", "'" + eventname + "'", ...args);
});
}
2021-03-22 19:24:15 +00:00
}
2021-04-12 05:09:47 +00:00
/**
* Function, which will be used to emit data
*
* @param {ValidEventTypesOfMirror} event the name fo the event to emit something
* @param {*} data the data to emit
* @memberof EventMirror
*/
async emit<T extends keyof EventnameToEventType>(
eventname: T,
data: EventnameToEventType[T]
): Promise<void> {
this._emitter.emit(eventname, data);
2021-03-22 19:24:15 +00:00
}
2021-04-12 05:09:47 +00:00
/**
* Flag, showing whether the Mirror is connected or not.
*
* @type {INopeObservable<boolean>}
* @memberof EventMirror
*/
2021-03-22 19:24:15 +00:00
connected: INopeObservable<boolean>;
2021-10-19 08:01:00 +00:00
async dispose(): Promise<void> {
// Disposes the Emitter.
(this._emitter as any)?.removeAllListeners();
}
2021-04-12 05:09:47 +00:00
/**
* Creates an instance of EventMirror.
2021-12-04 07:25:26 +00:00
* @param {IEmitter} [_emitter=new EventEmitter() as any] The Type of Emitter to use.
2021-04-12 05:09:47 +00:00
* @param {ILogger} [_logger] a Logger
* @memberof EventMirror
*/
2021-03-22 19:24:15 +00:00
constructor(
protected _emitter: IEmitter = new EventEmitter() as any,
2021-11-12 07:57:03 +00:00
protected _logger?: ILogger,
public readonly receivesOwnMessages: boolean = true
2021-03-22 19:24:15 +00:00
) {
this.connected = new NopeObservable<boolean>();
2021-08-17 15:52:46 +00:00
this.connected.setContent(true);
this.id = generateId();
2021-03-22 19:24:15 +00:00
}
2022-07-17 20:26:33 +00:00
detailListeners(
type: "event" | "rpc" | "data" | "response",
listeners: string[]
) {
throw new Error("Method not implemented.");
}
public id: string;
2021-03-22 19:24:15 +00:00
}