2016-08-07 13:37:52 +00:00
|
|
|
import * as plugins from "./smartsocket.plugins";
|
|
|
|
|
|
|
|
// import classes
|
2016-08-12 01:22:36 +00:00
|
|
|
import { Objectmap } from "lik";
|
2016-08-07 13:37:52 +00:00
|
|
|
import { SocketRole } from "./smartsocket.classes.socketrole";
|
|
|
|
|
2016-08-09 09:42:21 +00:00
|
|
|
// export interfaces
|
|
|
|
|
2016-08-09 16:22:30 +00:00
|
|
|
/**
|
|
|
|
* interface of the contructor options of class SocketFunction
|
|
|
|
*/
|
2016-08-12 01:22:36 +00:00
|
|
|
export interface ISocketFunctionConstructorOptions {
|
2016-08-09 16:22:30 +00:00
|
|
|
funcName: string;
|
|
|
|
funcDef: any;
|
|
|
|
allowedRoles: SocketRole[]; // all roles that are allowed to execute a SocketFunction
|
2016-08-07 13:37:52 +00:00
|
|
|
};
|
|
|
|
|
2016-08-09 16:22:30 +00:00
|
|
|
/**
|
2016-08-12 01:22:36 +00:00
|
|
|
* interface of the Socket Function call, in other words the object that routes a call to a function
|
2016-08-09 16:22:30 +00:00
|
|
|
*/
|
|
|
|
export interface ISocketFunctionCall {
|
|
|
|
funcName:string;
|
|
|
|
funcDataArg:any;
|
2016-08-12 03:17:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* interface for function definition of SocketFunction
|
|
|
|
*/
|
|
|
|
export interface IFuncDef {
|
|
|
|
(dataArg:any):PromiseLike<any>
|
2016-08-09 16:22:30 +00:00
|
|
|
}
|
|
|
|
|
2016-08-12 01:22:36 +00:00
|
|
|
// export objects
|
|
|
|
export let allSocketFunctions = new Objectmap<SocketFunction>();
|
|
|
|
|
2016-08-09 09:42:21 +00:00
|
|
|
// export classes
|
|
|
|
|
|
|
|
/**
|
2016-08-09 16:22:30 +00:00
|
|
|
* class that respresents a function that can be transparently called using a SocketConnection
|
2016-08-09 09:42:21 +00:00
|
|
|
*/
|
2016-08-07 13:37:52 +00:00
|
|
|
export class SocketFunction {
|
|
|
|
name: string;
|
2016-08-12 03:17:13 +00:00
|
|
|
funcDef: IFuncDef;
|
2016-08-07 13:37:52 +00:00
|
|
|
roles: SocketRole[];
|
2016-08-08 16:20:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the constructor for SocketFunction
|
|
|
|
*/
|
2016-08-12 01:22:36 +00:00
|
|
|
constructor(optionsArg: ISocketFunctionConstructorOptions) {
|
2016-08-09 16:22:30 +00:00
|
|
|
this.name = optionsArg.funcName;
|
2016-08-12 03:17:13 +00:00
|
|
|
this.funcDef = optionsArg.funcDef;
|
2016-08-09 16:22:30 +00:00
|
|
|
this.roles = optionsArg.allowedRoles;
|
2016-08-08 16:20:00 +00:00
|
|
|
for (let socketRoleArg of this.roles){
|
|
|
|
this._notifyRole(socketRoleArg);
|
2016-08-12 01:22:36 +00:00
|
|
|
};
|
|
|
|
allSocketFunctions.add(this); // map instance with Objectmap
|
2016-08-07 13:37:52 +00:00
|
|
|
};
|
2016-08-08 16:20:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* notifies a role about access to this SocketFunction
|
|
|
|
*/
|
|
|
|
private _notifyRole(socketRoleArg:SocketRole){
|
|
|
|
socketRoleArg.addSocketFunction(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-09 09:42:21 +00:00
|
|
|
* invokes the function of this SocketFunction
|
2016-08-08 16:20:00 +00:00
|
|
|
*/
|
2016-08-12 03:17:13 +00:00
|
|
|
invoke(dataArg:ISocketFunctionCall):plugins.q.Promise<any> {
|
2016-08-09 16:22:30 +00:00
|
|
|
let done = plugins.q.defer();
|
2016-08-12 03:17:13 +00:00
|
|
|
if(dataArg.funcName === this.name){
|
|
|
|
this.funcDef(dataArg.funcDataArg)
|
|
|
|
.then((resultData:any) => {
|
2016-08-14 23:38:28 +00:00
|
|
|
let funcResponseData:ISocketFunctionCall = {
|
|
|
|
funcName:this.name,
|
|
|
|
funcDataArg:resultData
|
|
|
|
}
|
|
|
|
done.resolve(funcResponseData);
|
2016-08-12 03:17:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
throw new Error("SocketFunction.name does not match the data argument's .name!");
|
|
|
|
}
|
2016-08-09 16:22:30 +00:00
|
|
|
return done.promise;
|
2016-08-08 16:20:00 +00:00
|
|
|
};
|
|
|
|
|
2016-08-07 13:37:52 +00:00
|
|
|
}
|