nope/lib/dispatcher/nopeDispatcher.ts
Martin Karkowski d785f72667 Merge branch 'dev' of https://github.com/anti-held-333/nope-backend into lib
# Conflicts:
#	lib/cli/runNopeBackend.ts
#	lib/dispatcher/nopeDispatcher.ts
#	lib/types/nope/nopeDispatcher.interface.ts
#	modules/wamo/src/wamo.basemodule.module.ts
#	modules/wamo/src/wamo.converter.module.ts
#	modules/wamo/src/wamo.lineManager.module.ts
#	modules/wamo/src/wamo.transportManager.simple.module.ts
2022-01-16 20:38:45 +01:00

116 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,
IEventCallback,
INopeDispatcher,
INopeDispatcherDescription,
INopeObserver,
} 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 {
public get masterExists(): boolean {
return false;
}
// See interface description
public pushData<T = unknown>(
path: string,
content: T,
options: Partial<IEventAdditionalData> = {}
): void {
return this.dataDistributor.pushData(path, content, options);
}
// See interface description
public pullData<T = unknown, D = null>(path: string, _default: D = null): T {
return this.dataDistributor.pullData<T, D>(path, _default);
}
// See interface description
public subscribeToEvent<T = unknown>(
event: string,
subscription: IEventCallback<T>
): INopeObserver {
return this.eventDistributor.registerSubscription(event, subscription);
}
// See interface description
public emitEvent<T>(
eventName: string,
data: T,
options: Partial<IEventAdditionalData> = {}
) {
this.eventDistributor.emit(eventName, data, options);
}
// See interface description
public 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.originalData.values()) {
hosts.add(info.host.name);
}
return Array.from(hosts);
}
// See interface description
public toDescription(): INopeDispatcherDescription {
return Object.assign(this.connectivityManager.info, {
isMaster: this.connectivityManager.isMaster,
instances: this.instanceManager.instances.data.getContent(),
services: [],
events: this.eventDistributor.emitters,
properties: this.dataDistributor.emitters,
// Show the data.
data: this.dataDistributor.pullData("", {}),
});
}
}