강동현

타입 리팩토링

import { RoomDescription, RoomInfo } from "./dataType";
import { keys } from "ts-transformer-keys";
// 서버로 들어오는 메세지 타입을 정의합니다.
// 'result' 속성은 서버 요청 결과에만 포함되는 특별한 속성입니다.
export interface ServerInboundMessageMap {
interface ServerInboundMessageMap {
// 로그인을 시도합니다.
login: {
username: string;
......@@ -47,7 +48,7 @@ export interface ServerInboundMessageMap {
}
// 서버에서 나가는 메세지 타입을 정의합니다.
export interface ServerOutboundMessageMap {
interface ServerOutboundMessageMap {
// 방에 접속 중인 유저 목록이 업데이트 되었습니다.
updateRoomUser: {
state: "added" | "updated" | "removed";
......@@ -121,10 +122,16 @@ export interface ServerOutboundMessageMap {
};
}
export type ServerInboundMessage<Key extends keyof ServerInboundMessageMap> =
Omit<ServerInboundMessageMap[Key], "result">;
export type ServerInboundMessageKey = keyof ServerInboundMessageMap;
export interface ServerResponse<Key extends keyof ServerInboundMessageMap> {
export const ServerInboundMessageKeyArray = keys<ServerInboundMessageMap>();
export type ServerInboundMessage<Key extends ServerInboundMessageKey> = Omit<
ServerInboundMessageMap[Key],
"result"
>;
export interface ServerResponse<Key extends ServerInboundMessageKey> {
ok: boolean;
reason?: string;
result?: "result" extends keyof ServerInboundMessageMap[Key]
......@@ -134,3 +141,7 @@ export interface ServerResponse<Key extends keyof ServerInboundMessageMap> {
export type ServerOutboundMessage<Key extends keyof ServerOutboundMessageMap> =
ServerOutboundMessageMap[Key];
export type ServerOutboundMessageKey = keyof ServerOutboundMessageMap;
export const ServerOutboundMessageKeyArray = keys<ServerOutboundMessageMap>();
......
{
"name": "common",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"ts-transformer-keys": "^0.4.3"
}
}
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
ts-transformer-keys@^0.4.3:
version "0.4.3"
resolved "https://registry.yarnpkg.com/ts-transformer-keys/-/ts-transformer-keys-0.4.3.tgz#d62389a40f430c00ef98fb9575fb6778a196e3ed"
integrity sha512-pOTLlet1SnAvhKNr9tMAFwuv5283OkUNiq1fXTEK+vrSv+kxU3e2Ijr/UkqyX2vuMmvcNHdpXC31hob7ljH//g==
import { Socket } from "socket.io";
import { ServerOutboundMessage, ServerOutboundMessageMap } from "../../common";
import { ServerOutboundMessage, ServerOutboundMessageKey } from "../../common";
import { MessageHandlerChain } from "../message/MessageHandlerChain";
import { Room } from "../room/Room";
import { User } from "../user/User";
......@@ -16,7 +16,7 @@ export class Connection {
this.messageHandlerChain = new MessageHandlerChain(this);
}
public send<T extends keyof ServerOutboundMessageMap>(
public send<T extends ServerOutboundMessageKey>(
type: T,
message: ServerOutboundMessage<T>
) {
......
import { Connection } from "../connection/Connection";
import {
ServerInboundMessage,
ServerInboundMessageMap,
ServerInboundMessageKey,
ServerResponse,
} from "../../common/index";
import { User } from "../user/User";
type UserHandlerMap = {
[Key in keyof ServerInboundMessageMap]?: (
[Key in ServerInboundMessageKey]?: (
user: User,
message: ServerInboundMessage<Key>
) => ServerResponse<Key>;
......@@ -21,7 +21,7 @@ export class MessageHandler {
}
public handle(
type: keyof ServerInboundMessageMap,
type: ServerInboundMessageKey,
user: User,
message: any,
callback: Function
......
import { Connection } from "../connection/Connection";
import {
ServerInboundMessage,
ServerInboundMessageMap,
ServerInboundMessageKey,
ServerOutboundMessageKeyArray,
ServerResponse,
} from "../../common/index";
import { keys } from "ts-transformer-keys";
import { User } from "../user/User";
export class MessageHandlerChain {
......@@ -24,8 +24,8 @@ export class MessageHandlerChain {
}
);
for (const key in keys<ServerInboundMessageMap>()) {
const type = key as keyof ServerInboundMessageMap;
for (const key in ServerOutboundMessageKeyArray) {
const type = key as ServerInboundMessageKey;
this.connection.socket.on(key, (message: any, callback: Function) => {
// Game > Room > User 순으로 전달
if (
......
......@@ -16,7 +16,6 @@
"socket.io": "^4.1.2",
"socket.io-client": "^4.1.2",
"ts-node": "^9.1.1",
"ts-transformer-keys": "^0.4.3",
"typescript": "^4.2.4",
"uuid": "^8.3.2"
},
......
......@@ -6,7 +6,7 @@ import { MessageHandler } from "../message/MessageHandler";
import {
ServerInboundMessage,
ServerOutboundMessage,
ServerOutboundMessageMap,
ServerOutboundMessageKey,
} from "../../common";
import { RoomDescription, RoomInfo, UserData } from "../../common/dataType";
......@@ -86,7 +86,7 @@ export class Room {
};
}
public broadcast<T extends keyof ServerOutboundMessageMap>(
public broadcast<T extends ServerOutboundMessageKey>(
type: T,
message: ServerOutboundMessage<T>,
except?: User
......
......@@ -1657,11 +1657,6 @@ ts-node@^9.1.1:
source-map-support "^0.5.17"
yn "3.1.1"
ts-transformer-keys@^0.4.3:
version "0.4.3"
resolved "https://registry.yarnpkg.com/ts-transformer-keys/-/ts-transformer-keys-0.4.3.tgz#d62389a40f430c00ef98fb9575fb6778a196e3ed"
integrity sha512-pOTLlet1SnAvhKNr9tMAFwuv5283OkUNiq1fXTEK+vrSv+kxU3e2Ijr/UkqyX2vuMmvcNHdpXC31hob7ljH//g==
type-detect@^4.0.0, type-detect@^4.0.5:
version "4.0.8"
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c"
......