nope/lib/dispatcher/nopeDispatcher.ts

119 lines
3.2 KiB
TypeScript
Raw Normal View History

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