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

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

View File

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

View File

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

View File

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