now handling responses

This commit is contained in:
Philipp Kunz 2016-08-12 05:17:13 +02:00
parent da510eb87a
commit 3afede95fc
4 changed files with 60 additions and 34 deletions

View File

@ -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");

View File

@ -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<any>
}
// export objects
@ -36,7 +40,7 @@ export let allSocketFunctions = new Objectmap<SocketFunction>();
*/
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<any> {
invoke(dataArg:ISocketFunctionCall):plugins.q.Promise<any> {
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;
};

View File

@ -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,32 +27,27 @@ 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<SocketRequest>();
export let allRespondingSocketRequests = new Objectmap<SocketRequest>();
export let allSocketRequests = new Objectmap<SocketRequest>();
// 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 --------------------------
@ -59,19 +55,19 @@ export class SocketRequest {
/**
* 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);
});
}
};

View File

@ -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 })