fix(core): update

This commit is contained in:
Philipp Kunz 2022-03-08 00:14:40 +01:00
parent 6e2e2768e2
commit 2f51e10fc4

View File

@ -3,7 +3,7 @@ import * as plugins from './typedsocket.plugins';
const publicRoleName = 'publicRoleName'; const publicRoleName = 'publicRoleName';
const publicRolePass = 'publicRolePass'; const publicRolePass = 'publicRolePass';
export type TTypedSocketSide = 'server' | 'client'; export type TTypedSocketSide = 'server' | 'client';
export class TypedSocket { export class TypedSocket {
// STATIC // STATIC
@ -11,7 +11,7 @@ export class TypedSocket {
* creates a typedsocket server * creates a typedsocket server
* note: this will fail in browser environments as server libs are not bundled. * note: this will fail in browser environments as server libs are not bundled.
*/ */
public static async createServer( public static async createServer (
typedrouterArg: plugins.typedrequest.TypedRouter, typedrouterArg: plugins.typedrequest.TypedRouter,
smartexpressServerArg?: any smartexpressServerArg?: any
): Promise<TypedSocket> { ): Promise<TypedSocket> {
@ -34,7 +34,7 @@ export class TypedSocket {
const typedsocket = new TypedSocket( const typedsocket = new TypedSocket(
'server', 'server',
typedrouterArg, typedrouterArg,
async <T extends plugins.typedrequestInterfaces.ITypedRequest>( async <T extends plugins.typedrequestInterfaces.ITypedRequest> (
dataArg: T, dataArg: T,
targetConnectionArg?: plugins.smartsocket.SocketConnection targetConnectionArg?: plugins.smartsocket.SocketConnection
): Promise<T> => { ): Promise<T> => {
@ -43,7 +43,7 @@ export class TypedSocket {
console.log( console.log(
'Since no targetConnection was supplied and there is only one active one present, choosing that one automatically' 'Since no targetConnection was supplied and there is only one active one present, choosing that one automatically'
); );
targetConnectionArg = smartsocketServer.socketConnections.getArray()[0]; targetConnectionArg = smartsocketServer.socketConnections.getArray()[ 0 ];
} else { } else {
throw new Error('you need to specify the wanted targetConnection. Currently no target is selectable automatically.'); throw new Error('you need to specify the wanted targetConnection. Currently no target is selectable automatically.');
} }
@ -62,7 +62,7 @@ export class TypedSocket {
return typedsocket; return typedsocket;
} }
public static async createClient( public static async createClient (
typedrouterArg: plugins.typedrequest.TypedRouter, typedrouterArg: plugins.typedrequest.TypedRouter,
serverUrlArg: string, serverUrlArg: string,
aliasArg = 'clientArg' aliasArg = 'clientArg'
@ -89,7 +89,7 @@ export class TypedSocket {
const typedsocket = new TypedSocket( const typedsocket = new TypedSocket(
'client', 'client',
typedrouterArg, typedrouterArg,
async <T extends plugins.typedrequestInterfaces.ITypedRequest>(dataArg: T): Promise<T> => { async <T extends plugins.typedrequestInterfaces.ITypedRequest> (dataArg: T): Promise<T> => {
const response: T = (smartsocketClient.serverCall('processMessage', dataArg) as any) as T; const response: T = (smartsocketClient.serverCall('processMessage', dataArg) as any) as T;
return response; return response;
}, },
@ -122,7 +122,7 @@ export class TypedSocket {
this.socketServerOrClient = socketServerOrClientArg; this.socketServerOrClient = socketServerOrClientArg;
} }
public addTag(keyArg: string, payloadArg: any) { public addTag (keyArg: string, payloadArg: any) {
if (this.side === 'client' && this.socketServerOrClient instanceof plugins.smartsocket.SmartsocketClient) { if (this.side === 'client' && this.socketServerOrClient instanceof plugins.smartsocket.SmartsocketClient) {
this.socketServerOrClient.socketConnection.addTag({ this.socketServerOrClient.socketConnection.addTag({
id: keyArg, id: keyArg,
@ -133,8 +133,8 @@ export class TypedSocket {
} }
} }
public createTypedRequest<T extends plugins.typedrequestInterfaces.ITypedRequest>( public createTypedRequest<T extends plugins.typedrequestInterfaces.ITypedRequest> (
methodName: T['method'], methodName: T[ 'method' ],
targetConnection?: plugins.smartsocket.SocketConnection targetConnection?: plugins.smartsocket.SocketConnection
): plugins.typedrequest.TypedRequest<T> { ): plugins.typedrequest.TypedRequest<T> {
const typedrequest = new plugins.typedrequest.TypedRequest<T>( const typedrequest = new plugins.typedrequest.TypedRequest<T>(
@ -154,7 +154,7 @@ export class TypedSocket {
* @param asyncFindFuncArg * @param asyncFindFuncArg
* @returns * @returns
*/ */
public async findAllTargetConnections( public async findAllTargetConnections (
asyncFindFuncArg: (connectionArg: plugins.smartsocket.SocketConnection) => Promise<boolean> asyncFindFuncArg: (connectionArg: plugins.smartsocket.SocketConnection) => Promise<boolean>
) { ) {
if (this.socketServerOrClient instanceof plugins.smartsocket.Smartsocket) { if (this.socketServerOrClient instanceof plugins.smartsocket.Smartsocket) {
@ -175,31 +175,31 @@ export class TypedSocket {
* @param asyncFindFuncArg * @param asyncFindFuncArg
* @returns * @returns
*/ */
public async findTargetConnection( public async findTargetConnection (
asyncFindFuncArg: (connectionArg: plugins.smartsocket.SocketConnection) => Promise<boolean> asyncFindFuncArg: (connectionArg: plugins.smartsocket.SocketConnection) => Promise<boolean>
) { ) {
const allMatching = await this.findAllTargetConnections(asyncFindFuncArg); const allMatching = await this.findAllTargetConnections(asyncFindFuncArg);
return allMatching[0]; return allMatching[ 0 ];
} }
public async findAllTargetConnectionsByTag(keyArg: string, payloadArg?: any) { public async findAllTargetConnectionsByTag (keyArg: string, payloadArg?: any) {
return this.findAllTargetConnections(async socketConnectionArg => { return this.findAllTargetConnections(async socketConnectionArg => {
let result: boolean; let result: boolean;
if (!payloadArg) { if (!payloadArg) {
result = !!(await socketConnectionArg.getTagById(keyArg)); result = !!(await socketConnectionArg.getTagById(keyArg));
} else { } else {
result = !!(await socketConnectionArg.getTagById(keyArg)) === payloadArg; result = !!((await socketConnectionArg.getTagById(keyArg)).payload === payloadArg);
} }
return result; return result;
}) })
} }
public async findTargetConnectionByTag(keyArg: string, payloadArg?: any) { public async findTargetConnectionByTag (keyArg: string, payloadArg?: any) {
const allResults = await this.findAllTargetConnectionsByTag(keyArg, payloadArg) const allResults = await this.findAllTargetConnectionsByTag(keyArg, payloadArg)
return allResults[0]; return allResults[ 0 ];
} }
public async stop() { public async stop () {
await this.socketServerOrClient.stop() await this.socketServerOrClient.stop()
} }
} }