smartsocket/ts/smartsocket.classes.socketrequest.ts

74 lines
2.1 KiB
TypeScript
Raw Normal View History

2016-08-09 09:42:21 +00:00
import * as plugins from "./smartsocket.plugins";
// import interfaces
2016-08-09 16:22:30 +00:00
import { ISocketFunctionCall } from "./smartsocket.classes.socketfunction";
2016-08-09 09:42:21 +00:00
// import classes
import { Objectmap } from "lik";
import { SocketFunction } from "./smartsocket.classes.socketfunction";
2016-08-09 16:22:30 +00:00
import { SocketConnection } from "./smartsocket.classes.socketconnection";
2016-08-09 09:42:21 +00:00
// export interfaces
export type TSocketRequestStatus = "new" | "pending" | "finished";
export type TSocketRequestSide = "requesting" | "responding";
2016-08-09 16:22:30 +00:00
/**
2016-08-09 21:37:25 +00:00
* interface of constructor of class SocketRequest
2016-08-09 16:22:30 +00:00
*/
2016-08-09 09:42:21 +00:00
export interface SocketRequestConstructorOptions {
side: TSocketRequestSide;
2016-08-09 16:22:30 +00:00
originSocketConnection:SocketConnection;
shortId: string;
funcCallData?: ISocketFunctionCall;
2016-08-09 09:42:21 +00:00
};
2016-08-09 21:37:25 +00:00
/**
* request object that is sent initially and may or may not receive a response
*/
export interface ISocketRequestDataObject {
funcCallData:ISocketFunctionCall;
shortId:string;
responseTimeout?:number;
};
2016-08-09 09:42:21 +00:00
//export objects
export let allRequestingSocketRequests = new Objectmap<SocketRequest>();
export let allRespondingSocketRequests = new Objectmap<SocketRequest>();
// export classes
export class SocketRequest {
status: TSocketRequestStatus = "new";
side: TSocketRequestSide;
shortid: string;
2016-08-09 16:22:30 +00:00
originSocketConnection:SocketConnection;
requestData: ISocketRequestDataObject;
2016-08-09 21:37:25 +00:00
done = plugins.q.defer();
2016-08-09 09:42:21 +00:00
constructor(optionsArg: SocketRequestConstructorOptions) {
this.side = optionsArg.side;
2016-08-09 16:22:30 +00:00
this.shortid = optionsArg.shortId;
2016-08-09 09:42:21 +00:00
if(this.side === "requesting"){
allRequestingSocketRequests.add(this);
} else {
allRespondingSocketRequests.add(this);
};
2016-08-09 21:37:25 +00:00
// build request and response dataArg
this.requestData = {
funcCallData:optionsArg.funcCallData,
shortId:optionsArg.shortId
}
2016-08-09 09:42:21 +00:00
};
2016-08-09 21:37:25 +00:00
/**
*
*/
dispatch(){
this.originSocketConnection.socket.emit("function",this.requestData);
return this.done.promise;
};
2016-08-09 21:37:25 +00:00
handleResponse(responseDataArg:ISocketRequestDataObject){
this.done.resolve(responseDataArg);
2016-08-09 09:42:21 +00:00
}
2016-08-09 21:37:25 +00:00
createResponse(){
2016-08-09 21:37:25 +00:00
}
2016-08-09 09:42:21 +00:00
};