| 
									
										
										
										
											2018-03-15 02:29:40 +01:00
										 |  |  | import * as plugins from './smartsocket.plugins'; | 
					
						
							| 
									
										
										
										
											2016-08-07 15:37:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | // import classes
 | 
					
						
							| 
									
										
										
										
											2019-01-30 03:14:02 +01:00
										 |  |  | import { Objectmap } from '@pushrocks/lik'; | 
					
						
							| 
									
										
										
										
											2018-03-15 02:29:40 +01:00
										 |  |  | import { SocketRole } from './smartsocket.classes.socketrole'; | 
					
						
							| 
									
										
										
										
											2019-05-02 11:46:36 +02:00
										 |  |  | import { SocketConnection } from './smartsocket.classes.socketconnection'; | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-08-12 03:22:36 +02:00
										 |  |  | export interface ISocketFunctionConstructorOptions { | 
					
						
							| 
									
										
										
										
											2018-03-15 02:29:40 +01:00
										 |  |  |   funcName: string; | 
					
						
							| 
									
										
										
										
											2019-05-02 11:46:36 +02:00
										 |  |  |   funcDef: TFuncDef; | 
					
						
							| 
									
										
										
										
											2018-03-15 02:29:40 +01:00
										 |  |  |   allowedRoles: SocketRole[]; // all roles that are allowed to execute a SocketFunction
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  |  */ | 
					
						
							|  |  |  | export interface ISocketFunctionCall { | 
					
						
							| 
									
										
										
										
											2018-03-15 02:29:40 +01:00
										 |  |  |   funcName: string; | 
					
						
							|  |  |  |   funcDataArg: any; | 
					
						
							| 
									
										
										
										
											2017-07-07 22:02:19 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-08-12 05:17:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * interface for function definition of SocketFunction | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-05-02 11:46:36 +02:00
										 |  |  | export type TFuncDef = (dataArg: any, connectionArg: SocketConnection) => PromiseLike<any>; | 
					
						
							| 
									
										
										
										
											2016-08-09 18:22:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-12 03:22:36 +02:00
										 |  |  | // export objects
 | 
					
						
							| 
									
										
										
										
											2018-03-15 02:29:40 +01:00
										 |  |  | export let allSocketFunctions = new Objectmap<SocketFunction>(); | 
					
						
							| 
									
										
										
										
											2016-08-12 03:22:36 +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
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-08-07 15:37:52 +02:00
										 |  |  | export class SocketFunction { | 
					
						
							| 
									
										
										
										
											2018-03-15 02:29:40 +01:00
										 |  |  |   name: string; | 
					
						
							| 
									
										
										
										
											2019-05-02 11:46:36 +02:00
										 |  |  |   funcDef: TFuncDef; | 
					
						
							| 
									
										
										
										
											2018-03-15 02:29:40 +01:00
										 |  |  |   roles: SocketRole[]; | 
					
						
							| 
									
										
										
										
											2016-08-08 18:20:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-07 22:02:19 +02:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * the constructor for SocketFunction | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2018-03-15 02:29:40 +01:00
										 |  |  |   constructor(optionsArg: ISocketFunctionConstructorOptions) { | 
					
						
							|  |  |  |     this.name = optionsArg.funcName; | 
					
						
							|  |  |  |     this.funcDef = optionsArg.funcDef; | 
					
						
							|  |  |  |     this.roles = optionsArg.allowedRoles; | 
					
						
							| 
									
										
										
										
											2017-07-07 22:02:19 +02:00
										 |  |  |     for (let socketRoleArg of this.roles) { | 
					
						
							| 
									
										
										
										
											2018-03-15 02:29:40 +01:00
										 |  |  |       this._notifyRole(socketRoleArg); | 
					
						
							| 
									
										
										
										
											2017-07-07 22:02:19 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-15 02:29:40 +01:00
										 |  |  |     allSocketFunctions.add(this); // map instance with Objectmap
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2019-05-02 11:46:36 +02:00
										 |  |  |   invoke(dataArg: ISocketFunctionCall, socketConnectionArg: SocketConnection): Promise<any> { | 
					
						
							| 
									
										
										
										
											2019-01-30 03:14:02 +01:00
										 |  |  |     let done = plugins.smartpromise.defer(); | 
					
						
							| 
									
										
										
										
											2017-07-07 22:02:19 +02:00
										 |  |  |     if (dataArg.funcName === this.name) { | 
					
						
							| 
									
										
										
										
											2019-05-02 11:46:36 +02:00
										 |  |  |       this.funcDef(dataArg.funcDataArg, socketConnectionArg).then((resultData: any) => { | 
					
						
							| 
									
										
										
										
											2018-03-15 02:29:40 +01:00
										 |  |  |         let funcResponseData: ISocketFunctionCall = { | 
					
						
							|  |  |  |           funcName: this.name, | 
					
						
							|  |  |  |           funcDataArg: resultData | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |         done.resolve(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
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-15 02:29:40 +01:00
										 |  |  |     return done.promise; | 
					
						
							| 
									
										
										
										
											2017-07-07 22:02:19 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-08-08 18:20:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-07 22:02:19 +02:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * notifies a role about access to this SocketFunction | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2018-03-15 02:29:40 +01:00
										 |  |  |   private _notifyRole(socketRoleArg: SocketRole) { | 
					
						
							|  |  |  |     socketRoleArg.addSocketFunction(this); | 
					
						
							| 
									
										
										
										
											2017-07-07 22:02:19 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | } |