| 
									
										
										
										
											2022-03-14 22:40:55 +01:00
										 |  |  | import * as plugins from './smartsocket.plugins.js'; | 
					
						
							| 
									
										
										
										
											2016-08-07 15:37:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | // import classes
 | 
					
						
							| 
									
										
										
										
											2022-03-14 22:40:55 +01:00
										 |  |  | import { SocketConnection } from './smartsocket.classes.socketconnection.js'; | 
					
						
							|  |  |  | import { Smartsocket } from './smartsocket.classes.smartsocket.js'; | 
					
						
							|  |  |  | import { SmartsocketClient } from './smartsocket.classes.smartsocketclient.js'; | 
					
						
							| 
									
										
										
										
											2016-08-07 15:37:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-09 11:42:21 +02:00
										 |  |  | // export interfaces
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-09 18:22:30 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * interface of the contructor options of class SocketFunction | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-09-24 18:04:11 +00:00
										 |  |  | export interface ISocketFunctionConstructorOptions< | 
					
						
							|  |  |  |   T extends plugins.typedrequestInterfaces.ITypedRequest | 
					
						
							|  |  |  | > { | 
					
						
							| 
									
										
										
										
											2019-09-10 00:21:47 +02:00
										 |  |  |   funcName: T['method']; | 
					
						
							| 
									
										
										
										
											2019-09-09 23:58:32 +02:00
										 |  |  |   funcDef: TFuncDef<T>; | 
					
						
							| 
									
										
										
										
											2017-07-07 22:02:19 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-08-07 15:37:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-09 18:22:30 +02:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2016-08-12 03:22:36 +02:00
										 |  |  |  * interface of the Socket Function call, in other words the object that routes a call to a function | 
					
						
							| 
									
										
										
										
											2016-08-09 18:22:30 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-09-24 18:04:11 +00:00
										 |  |  | export interface ISocketFunctionCallDataRequest< | 
					
						
							|  |  |  |   T extends plugins.typedrequestInterfaces.ITypedRequest | 
					
						
							|  |  |  | > { | 
					
						
							| 
									
										
										
										
											2019-09-09 23:58:32 +02:00
										 |  |  |   funcName: T['method']; | 
					
						
							|  |  |  |   funcDataArg: T['request']; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * interface of the Socket Function call, in other words the object that routes a call to a function | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-09-24 18:04:11 +00:00
										 |  |  | export interface ISocketFunctionCallDataResponse< | 
					
						
							|  |  |  |   T extends plugins.typedrequestInterfaces.ITypedRequest | 
					
						
							|  |  |  | > { | 
					
						
							| 
									
										
										
										
											2019-09-09 23:58:32 +02:00
										 |  |  |   funcName: T['method']; | 
					
						
							|  |  |  |   funcDataArg: T['response']; | 
					
						
							| 
									
										
										
										
											2017-07-07 22:02:19 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-08-12 05:17:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * interface for function definition of SocketFunction | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-09-24 18:04:11 +00:00
										 |  |  | export type TFuncDef<T extends plugins.typedrequestInterfaces.ITypedRequest> = ( | 
					
						
							|  |  |  |   dataArg: T['request'], | 
					
						
							|  |  |  |   connectionArg: SocketConnection | 
					
						
							|  |  |  | ) => PromiseLike<T['response']>; | 
					
						
							| 
									
										
										
										
											2016-08-09 18:22:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-09 11:42:21 +02:00
										 |  |  | // export classes
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2016-08-09 18:22:30 +02:00
										 |  |  |  * class that respresents a function that can be transparently called using a SocketConnection | 
					
						
							| 
									
										
										
										
											2016-08-09 11:42:21 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-09-09 23:58:32 +02:00
										 |  |  | export class SocketFunction<T extends plugins.typedrequestInterfaces.ITypedRequest> { | 
					
						
							| 
									
										
										
										
											2019-08-12 22:31:40 +02:00
										 |  |  |   // STATIC
 | 
					
						
							| 
									
										
										
										
											2019-09-09 23:58:32 +02:00
										 |  |  |   public static getSocketFunctionByName<Q extends plugins.typedrequestInterfaces.ITypedRequest>( | 
					
						
							| 
									
										
										
										
											2019-08-12 22:46:57 +02:00
										 |  |  |     smartsocketRefArg: Smartsocket | SmartsocketClient, | 
					
						
							|  |  |  |     functionNameArg: string | 
					
						
							| 
									
										
										
										
											2019-09-09 23:58:32 +02:00
										 |  |  |   ): SocketFunction<Q> { | 
					
						
							| 
									
										
										
										
											2022-01-18 17:10:46 +01:00
										 |  |  |     return smartsocketRefArg.socketFunctions.findSync((socketFunctionArg) => { | 
					
						
							| 
									
										
										
										
											2019-08-12 22:31:40 +02:00
										 |  |  |       return socketFunctionArg.name === functionNameArg; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // INSTANCE
 | 
					
						
							|  |  |  |   public name: string; | 
					
						
							| 
									
										
										
										
											2019-09-09 23:58:32 +02:00
										 |  |  |   public funcDef: TFuncDef<T>; | 
					
						
							| 
									
										
										
										
											2016-08-08 18:20:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-07 22:02:19 +02:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * the constructor for SocketFunction | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2019-09-09 23:58:32 +02:00
										 |  |  |   constructor(optionsArg: ISocketFunctionConstructorOptions<T>) { | 
					
						
							| 
									
										
										
										
											2018-03-15 02:29:40 +01:00
										 |  |  |     this.name = optionsArg.funcName; | 
					
						
							|  |  |  |     this.funcDef = optionsArg.funcDef; | 
					
						
							| 
									
										
										
										
											2017-07-07 22:02:19 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-08-08 18:20:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-07 22:02:19 +02:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * invokes the function of this SocketFunction | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2020-09-24 18:04:11 +00:00
										 |  |  |   public async invoke( | 
					
						
							|  |  |  |     dataArg: ISocketFunctionCallDataRequest<T>, | 
					
						
							|  |  |  |     socketConnectionArg: SocketConnection | 
					
						
							|  |  |  |   ): Promise<ISocketFunctionCallDataResponse<T>> { | 
					
						
							| 
									
										
										
										
											2017-07-07 22:02:19 +02:00
										 |  |  |     if (dataArg.funcName === this.name) { | 
					
						
							| 
									
										
										
										
											2019-09-09 23:58:32 +02:00
										 |  |  |       const funcResponseData: ISocketFunctionCallDataResponse<T> = { | 
					
						
							|  |  |  |         funcName: this.name, | 
					
						
							| 
									
										
										
										
											2020-09-24 18:04:11 +00:00
										 |  |  |         funcDataArg: await this.funcDef(dataArg.funcDataArg, socketConnectionArg), | 
					
						
							| 
									
										
										
										
											2019-09-09 23:58:32 +02:00
										 |  |  |       }; | 
					
						
							|  |  |  |       return funcResponseData; | 
					
						
							| 
									
										
										
										
											2017-07-07 22:02:19 +02:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2018-03-15 02:29:40 +01:00
										 |  |  |       throw new Error("SocketFunction.name does not match the data argument's .name!"); | 
					
						
							| 
									
										
										
										
											2016-08-08 18:20:00 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-07-07 22:02:19 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | } |