2020-11-06 13:17:13 +00:00
|
|
|
/**
|
|
|
|
* @author Martin Karkowski
|
|
|
|
* @email m.karkowski@zema.de
|
|
|
|
* @create date 2020-11-06 08:52:55
|
2020-11-12 16:07:05 +00:00
|
|
|
* @modify date 2020-11-12 13:55:43
|
2020-11-06 13:17:13 +00:00
|
|
|
* @desc [description]
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { getSingleton } from "../helpers/singletonMethod";
|
|
|
|
import { IFunctionOptions } from "../types/nope/nopeModule.interface";
|
|
|
|
|
|
|
|
export interface IExportFunctionToDispatcherParameters extends IFunctionOptions {
|
|
|
|
// id: string
|
|
|
|
}
|
|
|
|
|
|
|
|
const container = getSingleton('nopeBackendDispatcher.container', () => {
|
|
|
|
return new Map<string, {
|
|
|
|
uri: string,
|
|
|
|
callback: (...args) => Promise<any>,
|
|
|
|
options: IExportFunctionToDispatcherParameters
|
|
|
|
}>()
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defintion of a Functon.
|
|
|
|
*/
|
|
|
|
export type callable<T> = {
|
|
|
|
(...args): T
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decorator, that will export the Function to a Dispatcher
|
|
|
|
* @param func The Function
|
|
|
|
* @param options The Options.
|
|
|
|
*/
|
|
|
|
export function exportFunctionToDispatcher<T>(func: T, options: IExportFunctionToDispatcherParameters) {
|
2020-11-12 16:07:05 +00:00
|
|
|
// Only add the element if it doesnt exists.
|
2020-11-23 06:09:31 +00:00
|
|
|
if (!container.instance.has(options.id)) {
|
2020-11-12 16:07:05 +00:00
|
|
|
container.instance.set(options.id, {
|
|
|
|
callback: async (...args) => await ((func as any)(...args)),
|
|
|
|
options,
|
|
|
|
uri: options.id || (func as any).name
|
|
|
|
});
|
|
|
|
}
|
2020-11-06 13:17:13 +00:00
|
|
|
return func;
|
|
|
|
}
|