nope/lib/decorators/functionDecorators.ts

51 lines
1.2 KiB
TypeScript
Raw Normal View History

/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2020-11-06 08:52:55
* @modify date 2020-12-02 08:56:37
* @desc [description]
*/
import { getSingleton } from "../helpers/singletonMethod";
import { IFunctionOptions } from "../types/nope/nopeModule.interface";
2022-01-26 12:03:03 +00:00
export type IexportFunctionAsNopeServiceParameters = IFunctionOptions;
const container = getSingleton("nopeBackendDispatcher.container", () => {
return new Map<
string,
{
uri: string;
callback: (...args) => Promise<any>;
2022-01-26 12:03:03 +00:00
options: IexportFunctionAsNopeServiceParameters;
}
>();
});
/**
* 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.
*/
2022-01-26 12:03:03 +00:00
export function exportFunctionAsNopeService<T>(
func: T,
2022-01-26 12:03:03 +00:00
options: IexportFunctionAsNopeServiceParameters
) {
// Only add the element if it doesnt exists.
2020-11-23 06:09:31 +00:00
if (!container.instance.has(options.id)) {
container.instance.set(options.id, {
callback: async (...args) => await (func as any)(...args),
options,
2021-12-04 07:25:26 +00:00
uri: options.id || (func as any).name,
});
}
return func;
}