now handling responses
This commit is contained in:
		| @@ -3,7 +3,7 @@ import * as helpers from "./smartsocket.helpers"; | |||||||
|  |  | ||||||
| // import classes | // import classes | ||||||
| import { SocketFunction } from "./smartsocket.classes.socketfunction"; | import { SocketFunction } from "./smartsocket.classes.socketfunction"; | ||||||
| import { SocketRequest, ISocketRequestDataObject } from "./smartsocket.classes.socketrequest"; | import { SocketRequest, ISocketRequestDataObject, allSocketRequests } from "./smartsocket.classes.socketrequest"; | ||||||
| import { SocketRole } from "./smartsocket.classes.socketrole"; | import { SocketRole } from "./smartsocket.classes.socketrole"; | ||||||
|  |  | ||||||
| // export interfaces | // export interfaces | ||||||
| @@ -96,7 +96,8 @@ export class SocketConnection { | |||||||
|                 }; |                 }; | ||||||
|             }); |             }); | ||||||
|             this.socket.on("functionResponse", (dataArg:ISocketRequestDataObject) => { |             this.socket.on("functionResponse", (dataArg:ISocketRequestDataObject) => { | ||||||
|                  |                 let targetSocketRequest = helpers.getSocketRequestById(dataArg.shortId); | ||||||
|  |                 targetSocketRequest.handleResponse(dataArg); | ||||||
|             }) |             }) | ||||||
|         } else { |         } else { | ||||||
|             done.reject("socket needs to be authenticated first"); |             done.reject("socket needs to be authenticated first"); | ||||||
|   | |||||||
| @@ -6,9 +6,6 @@ import { SocketRole } from "./smartsocket.classes.socketrole"; | |||||||
|  |  | ||||||
| // export interfaces | // export interfaces | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * interface of the contructor options of class SocketFunction |  * interface of the contructor options of class SocketFunction | ||||||
|  */ |  */ | ||||||
| @@ -24,6 +21,13 @@ export interface ISocketFunctionConstructorOptions { | |||||||
| export interface ISocketFunctionCall { | export interface ISocketFunctionCall { | ||||||
|     funcName:string; |     funcName:string; | ||||||
|     funcDataArg:any; |     funcDataArg:any; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * interface for function definition of SocketFunction | ||||||
|  |  */ | ||||||
|  | export interface IFuncDef { | ||||||
|  |     (dataArg:any):PromiseLike<any> | ||||||
| } | } | ||||||
|  |  | ||||||
| // export objects | // export objects | ||||||
| @@ -36,7 +40,7 @@ export let allSocketFunctions = new Objectmap<SocketFunction>(); | |||||||
|  */ |  */ | ||||||
| export class SocketFunction { | export class SocketFunction { | ||||||
|     name: string; |     name: string; | ||||||
|     func: any; |     funcDef: IFuncDef; | ||||||
|     roles: SocketRole[]; |     roles: SocketRole[]; | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
| @@ -44,7 +48,7 @@ export class SocketFunction { | |||||||
|      */ |      */ | ||||||
|     constructor(optionsArg: ISocketFunctionConstructorOptions) { |     constructor(optionsArg: ISocketFunctionConstructorOptions) { | ||||||
|         this.name = optionsArg.funcName; |         this.name = optionsArg.funcName; | ||||||
|         this.func = optionsArg.funcDef; |         this.funcDef = optionsArg.funcDef; | ||||||
|         this.roles = optionsArg.allowedRoles; |         this.roles = optionsArg.allowedRoles; | ||||||
|         for (let socketRoleArg of this.roles){ |         for (let socketRoleArg of this.roles){ | ||||||
|             this._notifyRole(socketRoleArg); |             this._notifyRole(socketRoleArg); | ||||||
| @@ -62,9 +66,17 @@ export class SocketFunction { | |||||||
|     /** |     /** | ||||||
|      * invokes the function of this SocketFunction |      * invokes the function of this SocketFunction | ||||||
|      */ |      */ | ||||||
|     invoke(dataArg:any):plugins.q.Promise<any> { |     invoke(dataArg:ISocketFunctionCall):plugins.q.Promise<any> { | ||||||
|         let done = plugins.q.defer(); |         let done = plugins.q.defer(); | ||||||
|  |         if(dataArg.funcName === this.name){ | ||||||
|  |             this.funcDef(dataArg.funcDataArg) | ||||||
|  |                 .then((resultData:any) => { | ||||||
|  |                     done.resolve(resultData); | ||||||
|  |                 }); | ||||||
|  |                  | ||||||
|  |         } else { | ||||||
|  |             throw new Error("SocketFunction.name does not match the data argument's .name!"); | ||||||
|  |         } | ||||||
|         return done.promise;         |         return done.promise;         | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,4 +1,5 @@ | |||||||
| import * as plugins from "./smartsocket.plugins"; | import * as plugins from "./smartsocket.plugins"; | ||||||
|  | import * as helpers from "./smartsocket.helpers"; | ||||||
|  |  | ||||||
| // import interfaces | // import interfaces | ||||||
| import { ISocketFunctionCall } from "./smartsocket.classes.socketfunction"; | import { ISocketFunctionCall } from "./smartsocket.classes.socketfunction"; | ||||||
| @@ -17,7 +18,7 @@ export type TSocketRequestSide = "requesting" | "responding"; | |||||||
|  */ |  */ | ||||||
| export interface SocketRequestConstructorOptions { | export interface SocketRequestConstructorOptions { | ||||||
|     side: TSocketRequestSide; |     side: TSocketRequestSide; | ||||||
|     originSocketConnection:SocketConnection; |     originSocketConnection: SocketConnection; | ||||||
|     shortId: string; |     shortId: string; | ||||||
|     funcCallData?: ISocketFunctionCall; |     funcCallData?: ISocketFunctionCall; | ||||||
| }; | }; | ||||||
| @@ -26,52 +27,47 @@ export interface SocketRequestConstructorOptions { | |||||||
|  * request object that is sent initially and may or may not receive a response |  * request object that is sent initially and may or may not receive a response | ||||||
|  */ |  */ | ||||||
| export interface ISocketRequestDataObject { | export interface ISocketRequestDataObject { | ||||||
|     funcCallData:ISocketFunctionCall; |     funcCallData: ISocketFunctionCall; | ||||||
|     shortId:string; |     shortId: string; | ||||||
|     responseTimeout?:number; |     responseTimeout?: number; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| //export objects | //export objects | ||||||
| export let allRequestingSocketRequests = new Objectmap<SocketRequest>(); | export let allSocketRequests = new Objectmap<SocketRequest>(); | ||||||
| export let allRespondingSocketRequests = new Objectmap<SocketRequest>(); |  | ||||||
|  |  | ||||||
| // export classes | // export classes | ||||||
| export class SocketRequest { | export class SocketRequest { | ||||||
|     status: TSocketRequestStatus = "new"; |     status: TSocketRequestStatus = "new"; | ||||||
|     side: TSocketRequestSide; |     side: TSocketRequestSide; | ||||||
|     shortid: string; |     shortid: string; | ||||||
|     originSocketConnection:SocketConnection; |     originSocketConnection: SocketConnection; | ||||||
|     funcCallData: ISocketFunctionCall |     funcCallData: ISocketFunctionCall | ||||||
|     done = plugins.q.defer(); |     done = plugins.q.defer(); | ||||||
|     constructor(optionsArg: SocketRequestConstructorOptions) { |     constructor(optionsArg: SocketRequestConstructorOptions) { | ||||||
|         this.side = optionsArg.side; |         this.side = optionsArg.side; | ||||||
|         this.shortid = optionsArg.shortId; |         this.shortid = optionsArg.shortId; | ||||||
|         this.funcCallData = optionsArg.funcCallData; |         this.funcCallData = optionsArg.funcCallData; | ||||||
|         if(this.side === "requesting"){ |         allSocketRequests.add(this); | ||||||
|             allRequestingSocketRequests.add(this); |  | ||||||
|         } else { |  | ||||||
|             allRespondingSocketRequests.add(this); |  | ||||||
|         }; |  | ||||||
|     }; |     }; | ||||||
|      |  | ||||||
|     // requesting -------------------------- |     // requesting -------------------------- | ||||||
|      |  | ||||||
|     /** |     /** | ||||||
|      * dispatches a socketrequest from the requesting to the receiving side |      * dispatches a socketrequest from the requesting to the receiving side | ||||||
|      */ |      */ | ||||||
|     dispatch(){ |     dispatch() { | ||||||
|         let requestData:ISocketRequestDataObject = { |         let requestData: ISocketRequestDataObject = { | ||||||
|             funcCallData:this.funcCallData, |             funcCallData: this.funcCallData, | ||||||
|             shortId:this.shortid |             shortId: this.shortid | ||||||
|         } |         } | ||||||
|         this.originSocketConnection.socket.emit("function",requestData); |         this.originSocketConnection.socket.emit("function", requestData); | ||||||
|         return this.done.promise; |         return this.done.promise; | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * handles the response that is received by the requesting side |      * handles the response that is received by the requesting side | ||||||
|      */ |      */ | ||||||
|     private _handleResponse(responseDataArg:ISocketRequestDataObject){ |     handleResponse(responseDataArg: ISocketRequestDataObject) { | ||||||
|         this.done.resolve(responseDataArg); |         this.done.resolve(responseDataArg); | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -80,7 +76,15 @@ export class SocketRequest { | |||||||
|     /** |     /** | ||||||
|      * creates the response on the responding side |      * creates the response on the responding side | ||||||
|      */ |      */ | ||||||
|     createResponse(){ |     createResponse() { | ||||||
|          |         let targetSocketFunction: SocketFunction = helpers.getSocketFunctionByName(this.funcCallData.funcName); | ||||||
|  |         targetSocketFunction.invoke(this.funcCallData) | ||||||
|  |             .then((resultData) => { | ||||||
|  |                 let requestData: ISocketRequestDataObject = { | ||||||
|  |                     funcCallData: resultData, | ||||||
|  |                     shortId: this.shortid | ||||||
|  |                 } | ||||||
|  |                 this.originSocketConnection.socket.emit("functionResponse",requestData); | ||||||
|  |             }); | ||||||
|     } |     } | ||||||
| }; | }; | ||||||
|   | |||||||
| @@ -4,18 +4,27 @@ import * as plugins from "./smartsocket.plugins"; | |||||||
| import { Smartsocket } from "./smartsocket.classes.smartsocket"; | import { Smartsocket } from "./smartsocket.classes.smartsocket"; | ||||||
| import { SocketFunction, allSocketFunctions } from "./smartsocket.classes.socketfunction"; | import { SocketFunction, allSocketFunctions } from "./smartsocket.classes.socketfunction"; | ||||||
| import { SocketConnection } from "./smartsocket.classes.socketconnection"; | import { SocketConnection } from "./smartsocket.classes.socketconnection"; | ||||||
|  | import { SocketRequest, allSocketRequests, TSocketRequestSide } from "./smartsocket.classes.socketrequest"; | ||||||
| import { SocketRole, allSocketRoles } from "./smartsocket.classes.socketrole"; | import { SocketRole, allSocketRoles } from "./smartsocket.classes.socketrole"; | ||||||
|  |  | ||||||
| // SocketFunction helpers | // SocketFunction helpers | ||||||
| export let getSocketFunctionByName = (functionNameArg:string) => { | export let getSocketFunctionByName = (functionNameArg: string):SocketFunction => { | ||||||
|     return allSocketFunctions.find((socketFunctionArg) => { return socketFunctionArg.name === functionNameArg}); |     return allSocketFunctions.find((socketFunctionArg) => { return socketFunctionArg.name === functionNameArg }); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // SocketRequest helpers | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * get corresponding Socketrequest instance by shortId | ||||||
|  |  */ | ||||||
|  | export let getSocketRequestById = (shortIdArg:string,requestSide?:TSocketRequestSide):SocketRequest => { | ||||||
|  |     return allSocketRequests.find((socketRequestArg) => {return socketRequestArg.shortid === shortIdArg}) | ||||||
|  | } | ||||||
|  |  | ||||||
| // SocketRole helpers | // SocketRole helpers | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * get corresponding SocketRequest instance by name |  * get corresponding SocketRole instance by name | ||||||
|  */ |  */ | ||||||
| export let getSocketRoleByName = (socketRoleNameArg: string): SocketRole => { | export let getSocketRoleByName = (socketRoleNameArg: string): SocketRole => { | ||||||
|     return allSocketRoles.find((socketRoleArg) => { return socketRoleArg.name === socketRoleNameArg }) |     return allSocketRoles.find((socketRoleArg) => { return socketRoleArg.name === socketRoleNameArg }) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user