feature: more typings, refactor

This commit is contained in:
Kamil Myśliwiec
2018-11-28 23:08:27 +01:00
parent 0b23422ab1
commit 1cac0a99fd
108 changed files with 596 additions and 406 deletions

View File

@@ -39,14 +39,17 @@ export class ServerTCP extends Server implements CustomTransportStrategy {
this.server.close();
}
public bindHandler(socket) {
public bindHandler<T extends Record<string, any>>(socket: T) {
const readSocket = this.getSocketInstance(socket);
readSocket.on(MESSAGE_EVENT, async msg =>
readSocket.on(MESSAGE_EVENT, async (msg: ReadPacket & PacketId) =>
this.handleMessage(readSocket, msg),
);
}
public async handleMessage(socket, packet: ReadPacket & PacketId) {
public async handleMessage<T extends Record<string, any>>(
socket: T,
packet: ReadPacket & PacketId,
) {
const pattern = !isString(packet.pattern)
? JSON.stringify(packet.pattern)
: packet.pattern;
@@ -91,7 +94,7 @@ export class ServerTCP extends Server implements CustomTransportStrategy {
this.server.on(CLOSE_EVENT, this.handleClose.bind(this));
}
private getSocketInstance(socket): JsonSocket {
private getSocketInstance<T>(socket: T): JsonSocket {
return new JsonSocket(socket);
}
}