fix(core): update

This commit is contained in:
Philipp Kunz 2022-10-26 10:54:36 +02:00
parent fc1124fb16
commit 9d7f433685
2 changed files with 41 additions and 33 deletions

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@apiglobal/typedsocket',
version: '2.0.6',
version: '2.0.7',
description: 'a typedrequest extension supporting websockets'
}

View File

@ -11,7 +11,7 @@ export class TypedSocket {
* creates a typedsocket server
* 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,
smartexpressServerArg?: any
): Promise<TypedSocket> {
@ -34,7 +34,7 @@ export class TypedSocket {
const typedsocket = new TypedSocket(
'server',
typedrouterArg,
async <T extends plugins.typedrequestInterfaces.ITypedRequest> (
async <T extends plugins.typedrequestInterfaces.ITypedRequest>(
dataArg: T,
targetConnectionArg?: plugins.smartsocket.SocketConnection
): Promise<T> => {
@ -43,16 +43,18 @@ export class TypedSocket {
console.log(
'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 {
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.'
);
}
}
const response: T = await smartsocketServer.clientCall(
const response: T = (await smartsocketServer.clientCall(
'processMessage',
dataArg,
targetConnectionArg
) as any;
)) as any;
return response;
},
smartsocketServer
@ -62,7 +64,7 @@ export class TypedSocket {
return typedsocket;
}
public static async createClient (
public static async createClient(
typedrouterArg: plugins.typedrequest.TypedRouter,
serverUrlArg: string,
aliasArg = 'clientArg'
@ -74,8 +76,8 @@ export class TypedSocket {
port: domain.port || 3000,
url: `${domain.nodeParsedUrl.protocol}//${domain.nodeParsedUrl.hostname}`,
autoReconnect: true,
}
console.log(`starting typedsocket with the following settings:`)
};
console.log(`starting typedsocket with the following settings:`);
console.log(socketOptions);
const smartsocketClient = new plugins.smartsocket.SmartsocketClient(socketOptions);
smartsocketClient.addSocketFunction(
@ -89,8 +91,8 @@ export class TypedSocket {
const typedsocket = new TypedSocket(
'client',
typedrouterArg,
async <T extends plugins.typedrequestInterfaces.ITypedRequest> (dataArg: T): Promise<T> => {
const response: T = (smartsocketClient.serverCall('processMessage', dataArg) as any) as T;
async <T extends plugins.typedrequestInterfaces.ITypedRequest>(dataArg: T): Promise<T> => {
const response: T = smartsocketClient.serverCall('processMessage', dataArg) as any as T;
return response;
},
smartsocketClient
@ -109,7 +111,9 @@ export class TypedSocket {
typedRequestPostObject: plugins.typedrequestInterfaces.ITypedRequest,
socketConnectionArg?: plugins.smartsocket.SocketConnection
) => Promise<plugins.typedrequestInterfaces.ITypedRequest>);
private socketServerOrClient: plugins.smartsocket.Smartsocket | plugins.smartsocket.SmartsocketClient;
private socketServerOrClient:
| plugins.smartsocket.Smartsocket
| plugins.smartsocket.SmartsocketClient;
constructor(
sideArg: TTypedSocketSide,
typedrouterArg: plugins.typedrequest.TypedRouter,
@ -122,19 +126,22 @@ export class TypedSocket {
this.socketServerOrClient = socketServerOrClientArg;
}
public addTag (keyArg: string, payloadArg: any) {
if (this.side === 'client' && this.socketServerOrClient instanceof plugins.smartsocket.SmartsocketClient) {
public addTag(keyArg: string, payloadArg: any) {
if (
this.side === 'client' &&
this.socketServerOrClient instanceof plugins.smartsocket.SmartsocketClient
) {
this.socketServerOrClient.socketConnection.addTag({
id: keyArg,
payload: payloadArg
})
payload: payloadArg,
});
} else {
throw new Error('tagging is only supported on clients');
}
}
public createTypedRequest<T extends plugins.typedrequestInterfaces.ITypedRequest> (
methodName: T[ 'method' ],
public createTypedRequest<T extends plugins.typedrequestInterfaces.ITypedRequest>(
methodName: T['method'],
targetConnection?: plugins.smartsocket.SocketConnection
): plugins.typedrequest.TypedRequest<T> {
const typedrequest = new plugins.typedrequest.TypedRequest<T>(
@ -152,9 +159,9 @@ export class TypedSocket {
/**
* returns all matching target connection
* @param asyncFindFuncArg
* @returns
* @returns
*/
public async findAllTargetConnections (
public async findAllTargetConnections(
asyncFindFuncArg: (connectionArg: plugins.smartsocket.SocketConnection) => Promise<boolean>
) {
if (this.socketServerOrClient instanceof plugins.smartsocket.Smartsocket) {
@ -173,35 +180,36 @@ export class TypedSocket {
/**
* returns a single target connection by returning the first one of all matching ones
* @param asyncFindFuncArg
* @returns
* @returns
*/
public async findTargetConnection (
public async findTargetConnection(
asyncFindFuncArg: (connectionArg: plugins.smartsocket.SocketConnection) => Promise<boolean>
) {
const allMatching = await this.findAllTargetConnections(asyncFindFuncArg);
return allMatching[ 0 ];
return allMatching[0];
}
public async findAllTargetConnectionsByTag (keyArg: string, payloadArg?: any) {
return this.findAllTargetConnections(async socketConnectionArg => {
public async findAllTargetConnectionsByTag(keyArg: string, payloadArg?: any) {
return this.findAllTargetConnections(async (socketConnectionArg) => {
let result: boolean;
if (!payloadArg) {
result = !!(await socketConnectionArg.getTagById(keyArg));
} else {
result = !!(
plugins.smartjson.stringify((await socketConnectionArg.getTagById(keyArg))?.payload) === plugins.smartjson.stringify(payloadArg)
plugins.smartjson.stringify((await socketConnectionArg.getTagById(keyArg))?.payload) ===
plugins.smartjson.stringify(payloadArg)
);
}
return result;
})
});
}
public async findTargetConnectionByTag (keyArg: string, payloadArg?: any) {
const allResults = await this.findAllTargetConnectionsByTag(keyArg, payloadArg)
return allResults[ 0 ];
public async findTargetConnectionByTag(keyArg: string, payloadArg?: any) {
const allResults = await this.findAllTargetConnectionsByTag(keyArg, payloadArg);
return allResults[0];
}
public async stop () {
await this.socketServerOrClient.stop()
public async stop() {
await this.socketServerOrClient.stop();
}
}