nope/lib/dispatcher/getLinkedDispatcher.ts

49 lines
1.4 KiB
TypeScript
Raw Normal View History

2020-08-25 22:11:26 +00:00
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2020-08-25 23:27:28
2020-11-15 19:11:25 +00:00
* @modify date 2020-11-14 17:43:41
2020-08-25 22:11:26 +00:00
* @desc [description]
*/
2020-11-06 13:18:10 +00:00
import { IExportFunctionToDispatcherParameters } from "../decorators/dispatcherDecorators";
2020-08-25 22:11:26 +00:00
import { getSingleton } from "../helpers/singletonMethod";
2020-11-15 19:11:25 +00:00
import { INopeDispatcherOptions } from "../types/nope/nopeCommunication.interface";
2020-11-06 13:18:10 +00:00
import { getDispatcher } from "./getDispatcher";
2020-08-25 22:11:26 +00:00
/**
* Returns a Dispatcher.
* @param options
2020-08-25 22:11:26 +00:00
*/
2020-11-15 19:11:25 +00:00
export function getLinkedDispatcher(options: INopeDispatcherOptions) {
2020-08-25 22:11:26 +00:00
// Create the Dispatcher Instance.
2020-11-06 13:18:10 +00:00
const dispatcher = getDispatcher(options);
2020-08-25 22:11:26 +00:00
// 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.
}
});
2020-09-10 16:21:01 +00:00
2020-08-25 22:11:26 +00:00
return dispatcher;
}