2020-08-25 22:11:26 +00:00
|
|
|
import { getSingleton } from "../helpers/singletonMethod";
|
2020-09-11 07:06:49 +00:00
|
|
|
import { nopeObservable } from "../observables/nopeObservable";
|
2020-08-25 22:11:26 +00:00
|
|
|
|
2020-08-21 14:49:48 +00:00
|
|
|
// Symbols for the Property Registery:
|
2020-08-23 07:16:14 +00:00
|
|
|
const _registeredDispatcherMethods_ = Symbol('_registeredDispatcherMethods_');
|
|
|
|
const _registeredDispatcherParams_ = Symbol('_registeredDispatcherParams_');
|
2020-08-21 14:49:48 +00:00
|
|
|
|
|
|
|
// Interfaces for the Class
|
2020-08-23 07:16:14 +00:00
|
|
|
export interface IExportToDispatcherParameters {
|
2020-08-25 10:33:33 +00:00
|
|
|
uri?: string,
|
2020-08-21 14:49:48 +00:00
|
|
|
}
|
|
|
|
|
2020-08-23 07:16:14 +00:00
|
|
|
export interface IExportMethodToDispatcherParameters {
|
2020-09-11 07:06:49 +00:00
|
|
|
// Different uri (instead of using the name)
|
2020-09-07 18:12:19 +00:00
|
|
|
uri?: string,
|
2020-09-11 07:06:49 +00:00
|
|
|
// Contains Either the Property Name or the idx name.
|
|
|
|
deletableCallbacks?: Array<number> | Array<string>
|
2020-08-25 22:11:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface IExportFunctionToDispatcherParameters {
|
2020-09-11 07:06:49 +00:00
|
|
|
uri?: string,
|
|
|
|
// Contains Either the Property Name or the idx name.
|
|
|
|
deletableCallbacks?: Array<number> | Array<string>
|
2020-08-21 14:49:48 +00:00
|
|
|
}
|
|
|
|
|
2020-08-23 07:16:14 +00:00
|
|
|
export interface IExportPropertyToDispatcherParameters {
|
2020-08-25 10:33:33 +00:00
|
|
|
uri?: string,
|
2020-08-21 14:49:48 +00:00
|
|
|
readonly?: boolean
|
|
|
|
}
|
|
|
|
|
2020-09-07 18:12:19 +00:00
|
|
|
const container = getSingleton('nopeBackendDispatcher.container', () => {
|
2020-08-25 22:11:26 +00:00
|
|
|
return {
|
|
|
|
methods: new Map<string, {
|
|
|
|
uri: string,
|
|
|
|
callback: (...args) => Promise<any>,
|
|
|
|
options: IExportMethodToDispatcherParameters
|
|
|
|
}>(),
|
|
|
|
parameters: new Map<string, {
|
|
|
|
uri: string,
|
2020-09-11 07:06:49 +00:00
|
|
|
item: nopeObservable<any>,
|
2020-08-25 22:11:26 +00:00
|
|
|
options: IExportPropertyToDispatcherParameters
|
2020-09-07 18:12:19 +00:00
|
|
|
}>(),
|
|
|
|
functions: new Map<string, {
|
|
|
|
uri: string,
|
|
|
|
callback: (...args) => Promise<any>,
|
|
|
|
options: IExportFunctionToDispatcherParameters
|
2020-08-25 22:11:26 +00:00
|
|
|
}>()
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-08-21 14:49:48 +00:00
|
|
|
/**
|
|
|
|
* Decorator used to export a Class API over openAPI
|
|
|
|
* @param options
|
|
|
|
*/
|
2020-08-23 07:16:14 +00:00
|
|
|
export function exportsElementsToDispatcher(options: IExportToDispatcherParameters) {
|
2020-08-21 14:49:48 +00:00
|
|
|
return function <T extends { new(...args: any[]): {} }>(Base: T) {
|
|
|
|
return class extends Base {
|
|
|
|
constructor(...args: any[]) {
|
|
|
|
super(...args);
|
|
|
|
|
2020-08-21 20:42:11 +00:00
|
|
|
const _this = this as any;
|
|
|
|
|
2020-08-21 14:49:48 +00:00
|
|
|
// extract the Registered Methods of the Class.
|
2020-08-23 07:16:14 +00:00
|
|
|
const registeredMethods = Base.prototype[_registeredDispatcherMethods_] as Map<string, IExportMethodToDispatcherParameters>;
|
2020-08-24 11:34:48 +00:00
|
|
|
const registeredParams = Base.prototype[_registeredDispatcherParams_] as Map<string, IExportPropertyToDispatcherParameters>;
|
2020-08-21 14:49:48 +00:00
|
|
|
|
|
|
|
// Online if they are present, iterate over them
|
2020-08-21 20:42:11 +00:00
|
|
|
if (registeredMethods) {
|
2020-09-11 07:06:49 +00:00
|
|
|
registeredMethods.forEach((_options, methodName) => {
|
2020-08-25 22:11:26 +00:00
|
|
|
// Register the Methods
|
2020-09-11 07:06:49 +00:00
|
|
|
const uri = (options.uri || Base.prototype.name) + (_options.uri || methodName);
|
2020-08-25 22:11:26 +00:00
|
|
|
container.instance.methods.set(uri, {
|
|
|
|
callback: async (...args) => _this[methodName](...args),
|
|
|
|
uri,
|
|
|
|
options
|
2020-08-21 20:42:11 +00:00
|
|
|
});
|
2020-08-25 22:11:26 +00:00
|
|
|
});
|
2020-08-21 14:49:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Online if they are present, iterate over them
|
2020-08-21 20:42:11 +00:00
|
|
|
if (registeredParams) {
|
2020-09-11 07:06:49 +00:00
|
|
|
registeredParams.forEach((_options, parameterName) => {
|
2020-08-25 22:11:26 +00:00
|
|
|
// Register the Methods
|
2020-09-11 07:06:49 +00:00
|
|
|
const uri = (options.uri || Base.prototype.name) + (_options.uri || parameterName);
|
2020-08-25 22:11:26 +00:00
|
|
|
container.instance.parameters.set(uri, {
|
2020-09-11 07:06:49 +00:00
|
|
|
item: _this[parameterName],
|
2020-08-25 22:11:26 +00:00
|
|
|
uri,
|
|
|
|
options
|
2020-08-21 20:42:11 +00:00
|
|
|
});
|
2020-08-25 22:11:26 +00:00
|
|
|
});
|
2020-08-21 14:49:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decorator, used to export the Method.
|
|
|
|
* @param options
|
|
|
|
*/
|
2020-08-25 10:33:33 +00:00
|
|
|
export function exportMethodToDispatcher(options: IExportMethodToDispatcherParameters) {
|
2020-08-21 14:49:48 +00:00
|
|
|
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
|
2020-08-23 07:16:14 +00:00
|
|
|
target[_registeredDispatcherMethods_] = target[_registeredDispatcherMethods_] || new Map<string, IExportMethodToDispatcherParameters>();
|
2020-08-21 14:49:48 +00:00
|
|
|
// Here we just add some information that class decorator will use
|
2020-08-23 07:16:14 +00:00
|
|
|
target[_registeredDispatcherMethods_].set(propertyKey, options);
|
2020-08-21 14:49:48 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decorator, will create a POST and GET api for the Parameter.
|
|
|
|
* @param options
|
|
|
|
*/
|
2020-08-25 10:33:33 +00:00
|
|
|
export function exportPropertyToDispatcher(options: IExportPropertyToDispatcherParameters) {
|
2020-08-21 14:49:48 +00:00
|
|
|
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
|
2020-08-23 07:16:14 +00:00
|
|
|
target[_registeredDispatcherParams_] = target[_registeredDispatcherParams_] || new Map<string, IExportPropertyToDispatcherParameters>();
|
2020-08-21 14:49:48 +00:00
|
|
|
// Here we just add some information that class decorator will use
|
2020-08-23 07:16:14 +00:00
|
|
|
target[_registeredDispatcherParams_].set(propertyKey, options);
|
2020-08-21 14:49:48 +00:00
|
|
|
};
|
2020-08-25 22:11:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2020-09-07 18:12:19 +00:00
|
|
|
export function exportFunctionToDispatcher<T>(func: T, options: IExportFunctionToDispatcherParameters) {
|
2020-08-25 22:11:26 +00:00
|
|
|
|
2020-09-07 18:12:19 +00:00
|
|
|
container.instance.functions.set(options.uri, {
|
|
|
|
callback: async (...args) => await ((func as any)(...args)),
|
2020-08-25 22:11:26 +00:00
|
|
|
options,
|
2020-09-07 18:12:19 +00:00
|
|
|
uri: options.uri || (func as any).name
|
2020-08-25 22:11:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return func;
|
|
|
|
}
|