diff --git a/package.json b/package.json index 78e5fc1..d38cdf8 100644 --- a/package.json +++ b/package.json @@ -42,4 +42,4 @@ "tslint-config-prettier": "^1.18.0" }, "private": false -} +} \ No newline at end of file diff --git a/test/test.ts b/test/test.ts index 807e55e..90e780f 100644 --- a/test/test.ts +++ b/test/test.ts @@ -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' diff --git a/ts/smartsocket.classes.smartsocket.ts b/ts/smartsocket.classes.smartsocket.ts index 4677467..d4f2178 100644 --- a/ts/smartsocket.classes.smartsocket.ts +++ b/ts/smartsocket.classes.smartsocket.ts @@ -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; diff --git a/ts/smartsocket.classes.socketfunction.ts b/ts/smartsocket.classes.socketfunction.ts index 7510fbb..615c320 100644 --- a/ts/smartsocket.classes.socketfunction.ts +++ b/ts/smartsocket.classes.socketfunction.ts @@ -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; -} +export type TFuncDef = (dataArg: any, connectionArg: SocketConnection) => PromiseLike; // export objects export let allSocketFunctions = new Objectmap(); @@ -40,7 +39,7 @@ export let allSocketFunctions = new Objectmap(); */ 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 { + invoke(dataArg: ISocketFunctionCall, socketConnectionArg: SocketConnection): Promise { 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 diff --git a/ts/smartsocket.classes.socketrequest.ts b/ts/smartsocket.classes.socketrequest.ts index e3c6891..67b0a07 100644 --- a/ts/smartsocket.classes.socketrequest.ts +++ b/ts/smartsocket.classes.socketrequest.ts @@ -38,12 +38,12 @@ export let allSocketRequests = new Objectmap(); // 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,