smartsocket/ts/smartsocket.classes.socketfunction.ts
2022-01-19 15:34:52 +01:00

94 lines
2.7 KiB
TypeScript

import * as plugins from './smartsocket.plugins';
// import classes
import { SocketConnection } from './smartsocket.classes.socketconnection';
import { Smartsocket } from './smartsocket.classes.smartsocket';
import { SmartsocketClient } from './smartsocket.classes.smartsocketclient';
// export interfaces
/**
* interface of the contructor options of class SocketFunction
*/
export interface ISocketFunctionConstructorOptions<
T extends plugins.typedrequestInterfaces.ITypedRequest
> {
funcName: T['method'];
funcDef: TFuncDef<T>;
}
/**
* interface of the Socket Function call, in other words the object that routes a call to a function
*/
export interface ISocketFunctionCallDataRequest<
T extends plugins.typedrequestInterfaces.ITypedRequest
> {
funcName: T['method'];
funcDataArg: T['request'];
}
/**
* interface of the Socket Function call, in other words the object that routes a call to a function
*/
export interface ISocketFunctionCallDataResponse<
T extends plugins.typedrequestInterfaces.ITypedRequest
> {
funcName: T['method'];
funcDataArg: T['response'];
}
/**
* interface for function definition of SocketFunction
*/
export type TFuncDef<T extends plugins.typedrequestInterfaces.ITypedRequest> = (
dataArg: T['request'],
connectionArg: SocketConnection
) => PromiseLike<T['response']>;
// export classes
/**
* class that respresents a function that can be transparently called using a SocketConnection
*/
export class SocketFunction<T extends plugins.typedrequestInterfaces.ITypedRequest> {
// STATIC
public static getSocketFunctionByName<Q extends plugins.typedrequestInterfaces.ITypedRequest>(
smartsocketRefArg: Smartsocket | SmartsocketClient,
functionNameArg: string
): SocketFunction<Q> {
return smartsocketRefArg.socketFunctions.findSync((socketFunctionArg) => {
return socketFunctionArg.name === functionNameArg;
});
}
// INSTANCE
public name: string;
public funcDef: TFuncDef<T>;
/**
* the constructor for SocketFunction
*/
constructor(optionsArg: ISocketFunctionConstructorOptions<T>) {
this.name = optionsArg.funcName;
this.funcDef = optionsArg.funcDef;
}
/**
* invokes the function of this SocketFunction
*/
public async invoke(
dataArg: ISocketFunctionCallDataRequest<T>,
socketConnectionArg: SocketConnection
): Promise<ISocketFunctionCallDataResponse<T>> {
if (dataArg.funcName === this.name) {
const funcResponseData: ISocketFunctionCallDataResponse<T> = {
funcName: this.name,
funcDataArg: await this.funcDef(dataArg.funcDataArg, socketConnectionArg),
};
return funcResponseData;
} else {
throw new Error("SocketFunction.name does not match the data argument's .name!");
}
}
}