nope/lib/dispatcher/nopeDispatcher.ts
2022-01-10 07:52:05 +01:00

121 lines
3.1 KiB
TypeScript

/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2020-10-12 18:52:00
* @modify date 2021-10-19 09:15:25
* @desc [description]
*/
import { comparePatternAndPath } from "../helpers/pathMatchingMethods";
import {
IEventAdditionalData,
INopeDispatcher,
INopeObserver,
IEventCallback,
INopeDispatcherDescription,
} from "../types/nope/index";
import { NopeCore } from "./Core";
/**
* A Dispatcher to perform a function on a Remote
* Dispatcher. Therefore a Task is created and forwarded
* to the remote.
*
* @export
* @class nopeDispatcher
*/
export class NopeDispatcher extends NopeCore implements INopeDispatcher {
// See interface description
public get isMaster(): boolean {
return false;
}
public get masterExists(): boolean {
return false;
}
// See interface description
pushData<T = unknown>(
path: string,
content: T,
options: IEventAdditionalData = {}
): void {
return this.dataDistributor.pushData(path, content, options);
}
// See interface description
pullData<T = unknown, D = null>(path: string, _default: D = null): T {
return this.dataDistributor.pullData<T, D>(path, _default);
}
// See interface description
subscribeToEvent<T = unknown>(
event: string,
subscription: IEventCallback<T>
): INopeObserver {
return this.eventDistributor.registerSubscription(event, subscription);
}
// See interface description
emitEvent<T>(
eventName: string,
data: T,
options: Partial<IEventAdditionalData> = {}
) {
this.eventDistributor.emit(eventName, data, options);
}
// See interface description
query(
pattern: string,
type: "instances" | "services" | "properties" | "events"
): string[] {
let items: string[] = [];
switch (type) {
case "instances":
items = this.instanceManager.instances.data
.getContent()
.map((item) => item.identifier);
break;
case "services":
items = this.rpcManager.services.data.getContent();
break;
case "properties":
items = this.dataDistributor.publishers.data.getContent();
break;
case "events":
items = this.eventDistributor.publishers.data.getContent();
break;
default:
throw Error("Invalid Type-Parameter");
}
return items.filter(
(item) => comparePatternAndPath(pattern, item).affected
);
}
// See interface description
public getAllHosts(): string[] {
const hosts = new Set<string>();
for (const info of this.connectivityManager.dispatchers.data.getContent()) {
hosts.add(info.host.name);
}
return Array.from(hosts);
}
// See interface description
public toDescription(): INopeDispatcherDescription {
return Object.assign(this.connectivityManager.info, {
isMaster: this.isMaster,
instances: this.instanceManager.instances.data.getContent(),
services: [],
events: this.eventDistributor.emitters,
properties: this.dataDistributor.emitters,
// Show the data.
data: this.dataDistributor.pullData("", {}),
});
}
}