From 1565eb8d05f7faae39087f9255d8b1cfc6738f0f Mon Sep 17 00:00:00 2001 From: Reto Brunner Date: Mon, 8 Apr 2024 06:56:12 +0200 Subject: [PATCH] socket-events/msg: if/else chains are not a switch replacement If we switch on a field, use switch for god's sake. If/elif chains are for cases where you have multiple selectors. --- client/js/socket-events/msg.ts | 46 +++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/client/js/socket-events/msg.ts b/client/js/socket-events/msg.ts index 0f0e1879..b07f5e36 100644 --- a/client/js/socket-events/msg.ts +++ b/client/js/socket-events/msg.ts @@ -3,8 +3,8 @@ import socket from "../socket"; import {cleanIrcMessage} from "../../../shared/irc"; import {store} from "../store"; import {switchToChannel} from "../router"; -import {ClientChan, NetChan} from "../types"; -import {ClientMessage} from "../../../shared/types/msg"; +import {ClientChan, NetChan, ClientMessage} from "../types"; +import {SharedMsg} from "../../../shared/types/msg"; let pop; @@ -188,24 +188,40 @@ function notifyMessage( } } -function updateUserList(channel, msg) { - if (msg.type === "message" || msg.type === "action") { - const user = channel.users.find((u) => u.nick === msg.from.nick); +function updateUserList(channel: ClientChan, msg: SharedMsg) { + switch (msg.type) { + case "message": // fallthrough - if (user) { - user.lastMessage = new Date(msg.time).getTime() || Date.now(); + case "action": { + const user = channel.users.find((u) => u.nick === msg.from.nick); + + if (user) { + user.lastMessage = new Date(msg.time).getTime() || Date.now(); + } + + break; } - } else if (msg.type === "quit" || msg.type === "part") { - const idx = channel.users.findIndex((u) => u.nick === msg.from.nick); - if (idx > -1) { - channel.users.splice(idx, 1); + case "quit": // fallthrough + + case "part": { + const idx = channel.users.findIndex((u) => u.nick === msg.from.nick); + + if (idx > -1) { + channel.users.splice(idx, 1); + } + + break; } - } else if (msg.type === "kick") { - const idx = channel.users.findIndex((u) => u.nick === msg.target.nick); - if (idx > -1) { - channel.users.splice(idx, 1); + case "kick": { + const idx = channel.users.findIndex((u) => u.nick === msg.target.nick); + + if (idx > -1) { + channel.users.splice(idx, 1); + } + + break; } } }