fix(core): update

This commit is contained in:
2021-01-28 02:12:22 +00:00
parent c08acd076e
commit f4d820d37e
3 changed files with 86 additions and 25 deletions

View File

@ -4,6 +4,8 @@ import type * as smartexpress from '@pushrocks/smartexpress';
const publicRoleName = 'publicRoleName';
const publicRolePass = 'publicRolePass';
export type TTypedSocketSide = 'server' | 'client';
export class TypedSocket {
// STATIC
/**
@ -35,6 +37,7 @@ export class TypedSocket {
})
);
const typedsocket = new TypedSocket(
'server',
typedrouterArg,
async <T extends plugins.typedrequestInterfaces.ITypedRequest>(
dataArg: T,
@ -91,6 +94,7 @@ 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;
@ -105,6 +109,7 @@ export class TypedSocket {
}
// INSTANCE
public side: TTypedSocketSide;
public typedrouter: plugins.typedrequest.TypedRouter;
private postMethod: plugins.typedrequest.IPostMethod &
((
@ -113,15 +118,28 @@ export class TypedSocket {
) => Promise<plugins.typedrequestInterfaces.ITypedRequest>);
private socketServerOrClient: plugins.smartsocket.Smartsocket | plugins.smartsocket.SmartsocketClient;
constructor(
sideArg: TTypedSocketSide,
typedrouterArg: plugins.typedrequest.TypedRouter,
postMethodArg: plugins.typedrequest.IPostMethod,
socketServerOrClientArg: plugins.smartsocket.Smartsocket | plugins.smartsocket.SmartsocketClient
) {
this.side = sideArg;
this.typedrouter = typedrouterArg;
this.postMethod = postMethodArg;
this.socketServerOrClient = socketServerOrClientArg;
}
public addTag(keyArg: string, payloadArg: any) {
if (this.side === 'client' && this.socketServerOrClient instanceof plugins.smartsocket.SmartsocketClient) {
this.socketServerOrClient.socketConnection.addTag({
id: keyArg,
payload: payloadArg
})
} else {
throw new Error('tagging is only supported on clients');
}
}
public createTypedRequest<T extends plugins.typedrequestInterfaces.ITypedRequest>(
methodName: T['method'],
targetConnection?: plugins.smartsocket.SocketConnection
@ -148,10 +166,22 @@ export class TypedSocket {
}
}
} else {
console.warn('this method >>findTargetConnection<< is only available from the server');
throw new Error('this method >>findTargetConnection<< is only available from the server');
}
}
public async findTargetConnectionByTag(keyArg: string, payload?: any) {
this.findTargetConnection(socketConnectionArg => {
let result: boolean;
if (!payload) {
result = !!socketConnectionArg.getTagById('keyArg')
} else {
result = !!socketConnectionArg.getTagById('keyArg') === payload;
}
return result;
})
}
public async stop() {
await this.socketServerOrClient.stop()
}