From 8948ca4f9b2761839a5fafb282eef33712e5a6e7 Mon Sep 17 00:00:00 2001 From: Martin Karkowski Date: Fri, 21 Aug 2020 22:42:11 +0200 Subject: [PATCH] fixing typ errors, adding callbackfunction to the Method. --- lib/callDispatcher.ts | 22 +++++++++++++++++++ lib/decorators.ts | 50 +++++++++++++++++++++---------------------- 2 files changed, 47 insertions(+), 25 deletions(-) diff --git a/lib/callDispatcher.ts b/lib/callDispatcher.ts index a2b5edd..48f7466 100644 --- a/lib/callDispatcher.ts +++ b/lib/callDispatcher.ts @@ -264,4 +264,26 @@ export class CallDispatcher { this.clearTasks(); this.unregisterAll(); } +} + +declare const global; +declare const window; + +/** + * Function to extract a Singleton Dispatcher + * @param uuid The Unique Id of the Dispatcher + * @param communicator The provided communicator + */ +export function getDispatcher(uuid: string, communicator: ICommunicationInterface){ + + // Get the global variable + const _global = typeof global !== "undefined" ? global : window; + const _accessor = '__singletons__'+uuid; + const _hasDispatcher = _global[_accessor] !== undefined; + + if (!_hasDispatcher){ + _global[_accessor] = new CallDispatcher(communicator); + } + + return _global[_accessor] as CallDispatcher } \ No newline at end of file diff --git a/lib/decorators.ts b/lib/decorators.ts index 72135d8..ffda3e4 100644 --- a/lib/decorators.ts +++ b/lib/decorators.ts @@ -1,6 +1,6 @@ // Symbols for the Property Registery: -const _registedMethods_ = Symbol('_registedMethods_'); -const _registedParams_ = Symbol('_registedParams_'); +const _registeredMethods_ = Symbol('_registedMethods_'); +const _registeredParams_ = Symbol('_registedParams_'); // Interfaces for the Class export interface IExportApiParameters { @@ -30,33 +30,33 @@ export function eportApi(options: IExportApiParameters) { constructor(...args: any[]) { super(...args); + const _this = this as any; + // Adding the Path Option. - (this as any).__path = options.url; + _this.__path = options.url; // extract the Registered Methods of the Class. - const registedMethods = Base.prototype[_registedMethods_] as Map; - const registedParams = Base.prototype[_registedParams_] as Map; + const registeredMethods = Base.prototype[_registeredMethods_] as Map; + const registeredParams = Base.prototype[_registeredParams_] as Map; // Online if they are present, iterate over them - if (registedMethods) { - registedMethods.forEach((options, methodName) => { - - // Provide the code that should be exectuted for every - // Registered Function - unicorn.test.push([methodName, options]); - console.log(unicorn.test) - }); + if (registeredMethods) { + _this.__dispatchRegisterdMethods = (cb: (methodName: string, callback: (...args) => Promise, options: IExportMethodParameters) => void) => { + registeredMethods.forEach((options, methodName) => { + // Callback the Method + cb(methodName, async (...args) => _this[methodName](...args), options); + }); + } } // Online if they are present, iterate over them - if (registedParams) { - registedParams.forEach((options, parameterName) => { - - // Provide the code that should be exectuted for every - // Registered Function - unicorn.test.push([parameterName, options]); - console.log(unicorn.test) - }); + if (registeredParams) { + _this.__dispatchRegisterdParams = (cb: (methodName: string, callback: (...args) => Promise, options: IExportPropertyParameters) => void) => { + registeredParams.forEach((options, methodName) => { + // Callback the Method + cb(methodName, async (...args) => _this[methodName](...args), options); + }); + } } } }; @@ -69,9 +69,9 @@ export function eportApi(options: IExportApiParameters) { */ export function exportMethod(options: IExportMethodParameters = {}) { return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) { - target[_registedMethods_] = target[_registedMethods_] || new Map(); + target[_registeredMethods_] = target[_registeredMethods_] || new Map(); // Here we just add some information that class decorator will use - target[_registedMethods_].set(propertyKey, options); + target[_registeredMethods_].set(propertyKey, options); }; } @@ -81,8 +81,8 @@ export function exportMethod(options: IExportMethodParameters = {}) { */ export function exportProperty(options: IExportPropertyParameters = {}) { return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) { - target[_registedParams_] = target[_registedParams_] || new Map(); + target[_registeredParams_] = target[_registeredParams_] || new Map(); // Here we just add some information that class decorator will use - target[_registedParams_].set(propertyKey, options); + target[_registeredParams_].set(propertyKey, options); }; } \ No newline at end of file