nope/lib/dispatcher/getLinkedDispatcher.ts

57 lines
1.5 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
2021-08-17 15:52:46 +00:00
* @modify date 2021-08-11 10:34:12
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";
2021-09-03 05:42:37 +00:00
import {
INopeDispatcher,
2021-12-04 07:25:26 +00:00
INopeDispatcherOptions,
2021-09-03 05:42:37 +00:00
} from "../types/nope/nopeDispatcher.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
*/
2021-09-03 05:42:37 +00:00
export function getLinkedDispatcher(
options: INopeDispatcherOptions
): INopeDispatcher {
// 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.
2020-11-23 06:09:31 +00:00
const container = getSingleton("nopeBackendDispatcher.container", () => {
return new Map<
string,
{
uri: string;
callback: (...args) => Promise<any>;
options: IExportFunctionToDispatcherParameters;
}
>();
2020-11-23 06:09:31 +00:00
});
// 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, {
2021-12-04 07:25:26 +00:00
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;
}