nope/lib/dispatcher/getLinkedDispatcher.ts
Martin Karkowski a5b82ebb9e adding mqtt
2020-11-15 20:11:25 +01:00

49 lines
1.4 KiB
TypeScript

/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2020-08-25 23:27:28
* @modify date 2020-11-14 17:43:41
* @desc [description]
*/
import { IExportFunctionToDispatcherParameters } from "../decorators/dispatcherDecorators";
import { getSingleton } from "../helpers/singletonMethod";
import { INopeDispatcherOptions } from "../types/nope/nopeCommunication.interface";
import { getDispatcher } from "./getDispatcher";
/**
* Returns a Dispatcher.
* @param options
*/
export function getLinkedDispatcher(options: INopeDispatcherOptions) {
// Create the Dispatcher Instance.
const dispatcher = getDispatcher(options);
// Define a Container, which contains all functions.
const container = getSingleton('nopeBackendDispatcher.container', () => {
return new Map<string, {
uri: string,
callback: (...args) => Promise<any>,
options: IExportFunctionToDispatcherParameters
}>()
});
// If the Dispatcher has been connected, register all functions.
dispatcher.ready.waitFor(value => value === true).then(() => {
if (dispatcher.ready.getContent()){
// Iterate over the Functions
for (const [uri, settings] of container.instance.entries()) {
dispatcher.registerFunction(settings.callback, {
id: uri
});
}
} else {
// Failed to Setup the Container.
}
});
return dispatcher;
}