fix(core): update

This commit is contained in:
Philipp Kunz 2019-05-02 11:46:36 +02:00
parent a63f14da47
commit 1b20aa5992
5 changed files with 23 additions and 21 deletions

View File

@ -42,4 +42,4 @@
"tslint-config-prettier": "^1.18.0"
},
"private": false
}
}

View File

@ -39,7 +39,7 @@ tap.test('should add a socketrole', async () => {
tap.test('should register a new Function', async () => {
testSocketFunction1 = new smartsocket.SocketFunction({
allowedRoles: [testSocketRole1],
funcDef: async dataArg => {
funcDef: async (dataArg, socketConnectionArg) => {
return dataArg;
},
funcName: 'testFunction1'

View File

@ -83,8 +83,8 @@ export class Smartsocket {
shortId: plugins.shortid.generate(),
side: 'requesting'
});
socketRequest.dispatch().then((dataArg: ISocketFunctionCall) => {
done.resolve(dataArg.funcDataArg);
socketRequest.dispatch().then((dataArg2: ISocketFunctionCall) => {
done.resolve(dataArg2.funcDataArg);
});
const result = await done.promise;
return result;

View File

@ -3,6 +3,7 @@ import * as plugins from './smartsocket.plugins';
// import classes
import { Objectmap } from '@pushrocks/lik';
import { SocketRole } from './smartsocket.classes.socketrole';
import { SocketConnection } from './smartsocket.classes.socketconnection';
// export interfaces
@ -11,7 +12,7 @@ import { SocketRole } from './smartsocket.classes.socketrole';
*/
export interface ISocketFunctionConstructorOptions {
funcName: string;
funcDef: any;
funcDef: TFuncDef;
allowedRoles: SocketRole[]; // all roles that are allowed to execute a SocketFunction
}
@ -26,9 +27,7 @@ export interface ISocketFunctionCall {
/**
* interface for function definition of SocketFunction
*/
export interface IFuncDef {
(dataArg: any): PromiseLike<any>;
}
export type TFuncDef = (dataArg: any, connectionArg: SocketConnection) => PromiseLike<any>;
// export objects
export let allSocketFunctions = new Objectmap<SocketFunction>();
@ -40,7 +39,7 @@ export let allSocketFunctions = new Objectmap<SocketFunction>();
*/
export class SocketFunction {
name: string;
funcDef: IFuncDef;
funcDef: TFuncDef;
roles: SocketRole[];
/**
@ -59,10 +58,10 @@ export class SocketFunction {
/**
* invokes the function of this SocketFunction
*/
invoke(dataArg: ISocketFunctionCall): Promise<any> {
invoke(dataArg: ISocketFunctionCall, socketConnectionArg: SocketConnection): Promise<any> {
let done = plugins.smartpromise.defer();
if (dataArg.funcName === this.name) {
this.funcDef(dataArg.funcDataArg).then((resultData: any) => {
this.funcDef(dataArg.funcDataArg, socketConnectionArg).then((resultData: any) => {
let funcResponseData: ISocketFunctionCall = {
funcName: this.name,
funcDataArg: resultData

View File

@ -38,12 +38,12 @@ export let allSocketRequests = new Objectmap<SocketRequest>();
// export classes
export class SocketRequest {
status: TSocketRequestStatus = 'new';
side: TSocketRequestSide;
shortid: string;
originSocketConnection: SocketConnection;
funcCallData: ISocketFunctionCall;
done = plugins.smartpromise.defer();
public status: TSocketRequestStatus = 'new';
public side: TSocketRequestSide;
public shortid: string;
public originSocketConnection: SocketConnection;
public funcCallData: ISocketFunctionCall;
public done = plugins.smartpromise.defer();
constructor(optionsArg: SocketRequestConstructorOptions) {
this.side = optionsArg.side;
this.shortid = optionsArg.shortId;
@ -57,7 +57,7 @@ export class SocketRequest {
/**
* dispatches a socketrequest from the requesting to the receiving side
*/
dispatch() {
public dispatch() {
let requestData: ISocketRequestDataObject = {
funcCallData: this.funcCallData,
shortId: this.shortid
@ -69,7 +69,7 @@ export class SocketRequest {
/**
* handles the response that is received by the requesting side
*/
handleResponse(responseDataArg: ISocketRequestDataObject) {
public handleResponse(responseDataArg: ISocketRequestDataObject) {
plugins.smartlog.defaultLogger.log('info', 'handling response!');
this.done.resolve(responseDataArg.funcCallData);
allSocketRequests.remove(this);
@ -85,12 +85,15 @@ export class SocketRequest {
this.funcCallData.funcName
);
if (!targetSocketFunction) {
defaultLogger.log('warn', `There is no SocketFunction defined for ${this.funcCallData.funcName}`);
defaultLogger.log(
'warn',
`There is no SocketFunction defined for ${this.funcCallData.funcName}`
);
defaultLogger.log('warn', `So now response is being sent.`);
return;
}
plugins.smartlog.defaultLogger.log('info', `invoking ${targetSocketFunction.name}`);
targetSocketFunction.invoke(this.funcCallData).then(resultData => {
targetSocketFunction.invoke(this.funcCallData, this.originSocketConnection).then(resultData => {
plugins.smartlog.defaultLogger.log('info', 'got resultData. Sending it to requesting party.');
let requestData: ISocketRequestDataObject = {
funcCallData: resultData,