nope/lib/dispatcher/getLinkedDispatcher.ts

46 lines
1.3 KiB
TypeScript

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