diff --git a/ts/smartsocket.classes.socketconnection.ts b/ts/smartsocket.classes.socketconnection.ts index 3da7804..a096abe 100644 --- a/ts/smartsocket.classes.socketconnection.ts +++ b/ts/smartsocket.classes.socketconnection.ts @@ -3,7 +3,7 @@ import * as helpers from "./smartsocket.helpers"; // import classes 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"; // export interfaces @@ -96,7 +96,8 @@ export class SocketConnection { }; }); this.socket.on("functionResponse", (dataArg:ISocketRequestDataObject) => { - + let targetSocketRequest = helpers.getSocketRequestById(dataArg.shortId); + targetSocketRequest.handleResponse(dataArg); }) } else { done.reject("socket needs to be authenticated first"); diff --git a/ts/smartsocket.classes.socketfunction.ts b/ts/smartsocket.classes.socketfunction.ts index 601e3cd..5b69b7e 100644 --- a/ts/smartsocket.classes.socketfunction.ts +++ b/ts/smartsocket.classes.socketfunction.ts @@ -6,9 +6,6 @@ import { SocketRole } from "./smartsocket.classes.socketrole"; // export interfaces - - - /** * interface of the contructor options of class SocketFunction */ @@ -24,6 +21,13 @@ export interface ISocketFunctionConstructorOptions { export interface ISocketFunctionCall { funcName:string; funcDataArg:any; +}; + +/** + * interface for function definition of SocketFunction + */ +export interface IFuncDef { + (dataArg:any):PromiseLike } // export objects @@ -36,7 +40,7 @@ export let allSocketFunctions = new Objectmap(); */ export class SocketFunction { name: string; - func: any; + funcDef: IFuncDef; roles: SocketRole[]; /** @@ -44,7 +48,7 @@ export class SocketFunction { */ constructor(optionsArg: ISocketFunctionConstructorOptions) { this.name = optionsArg.funcName; - this.func = optionsArg.funcDef; + this.funcDef = optionsArg.funcDef; this.roles = optionsArg.allowedRoles; for (let socketRoleArg of this.roles){ this._notifyRole(socketRoleArg); @@ -62,9 +66,17 @@ export class SocketFunction { /** * invokes the function of this SocketFunction */ - invoke(dataArg:any):plugins.q.Promise { + invoke(dataArg:ISocketFunctionCall):plugins.q.Promise { 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; }; diff --git a/ts/smartsocket.classes.socketrequest.ts b/ts/smartsocket.classes.socketrequest.ts index afccdb5..4b484dc 100644 --- a/ts/smartsocket.classes.socketrequest.ts +++ b/ts/smartsocket.classes.socketrequest.ts @@ -1,4 +1,5 @@ import * as plugins from "./smartsocket.plugins"; +import * as helpers from "./smartsocket.helpers"; // import interfaces import { ISocketFunctionCall } from "./smartsocket.classes.socketfunction"; @@ -17,7 +18,7 @@ export type TSocketRequestSide = "requesting" | "responding"; */ export interface SocketRequestConstructorOptions { side: TSocketRequestSide; - originSocketConnection:SocketConnection; + originSocketConnection: SocketConnection; shortId: string; funcCallData?: ISocketFunctionCall; }; @@ -26,52 +27,47 @@ export interface SocketRequestConstructorOptions { * request object that is sent initially and may or may not receive a response */ export interface ISocketRequestDataObject { - funcCallData:ISocketFunctionCall; - shortId:string; - responseTimeout?:number; + funcCallData: ISocketFunctionCall; + shortId: string; + responseTimeout?: number; }; //export objects -export let allRequestingSocketRequests = new Objectmap(); -export let allRespondingSocketRequests = new Objectmap(); +export let allSocketRequests = new Objectmap(); // export classes export class SocketRequest { status: TSocketRequestStatus = "new"; side: TSocketRequestSide; shortid: string; - originSocketConnection:SocketConnection; + originSocketConnection: SocketConnection; funcCallData: ISocketFunctionCall done = plugins.q.defer(); constructor(optionsArg: SocketRequestConstructorOptions) { this.side = optionsArg.side; this.shortid = optionsArg.shortId; this.funcCallData = optionsArg.funcCallData; - if(this.side === "requesting"){ - allRequestingSocketRequests.add(this); - } else { - allRespondingSocketRequests.add(this); - }; + allSocketRequests.add(this); }; - + // requesting -------------------------- - + /** * dispatches a socketrequest from the requesting to the receiving side */ - dispatch(){ - let requestData:ISocketRequestDataObject = { - funcCallData:this.funcCallData, - shortId:this.shortid + dispatch() { + let requestData: ISocketRequestDataObject = { + funcCallData: this.funcCallData, + shortId: this.shortid } - this.originSocketConnection.socket.emit("function",requestData); + this.originSocketConnection.socket.emit("function", requestData); return this.done.promise; }; /** * handles the response that is received by the requesting side */ - private _handleResponse(responseDataArg:ISocketRequestDataObject){ + handleResponse(responseDataArg: ISocketRequestDataObject) { this.done.resolve(responseDataArg); } @@ -80,7 +76,15 @@ export class SocketRequest { /** * 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); + }); } }; diff --git a/ts/smartsocket.helpers.ts b/ts/smartsocket.helpers.ts index 1fb3268..1adf351 100644 --- a/ts/smartsocket.helpers.ts +++ b/ts/smartsocket.helpers.ts @@ -4,18 +4,27 @@ import * as plugins from "./smartsocket.plugins"; import { Smartsocket } from "./smartsocket.classes.smartsocket"; import { SocketFunction, allSocketFunctions } from "./smartsocket.classes.socketfunction"; import { SocketConnection } from "./smartsocket.classes.socketconnection"; +import { SocketRequest, allSocketRequests, TSocketRequestSide } from "./smartsocket.classes.socketrequest"; import { SocketRole, allSocketRoles } from "./smartsocket.classes.socketrole"; // SocketFunction helpers -export let getSocketFunctionByName = (functionNameArg:string) => { - return allSocketFunctions.find((socketFunctionArg) => { return socketFunctionArg.name === functionNameArg}); +export let getSocketFunctionByName = (functionNameArg: string):SocketFunction => { + 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 /** - * get corresponding SocketRequest instance by name + * get corresponding SocketRole instance by name */ export let getSocketRoleByName = (socketRoleNameArg: string): SocketRole => { return allSocketRoles.find((socketRoleArg) => { return socketRoleArg.name === socketRoleNameArg })