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.
This commit is contained in:
Reto Brunner 2024-04-08 06:56:12 +02:00
parent 29750a3e51
commit 1565eb8d05

View file

@ -3,8 +3,8 @@ import socket from "../socket";
import {cleanIrcMessage} from "../../../shared/irc"; import {cleanIrcMessage} from "../../../shared/irc";
import {store} from "../store"; import {store} from "../store";
import {switchToChannel} from "../router"; import {switchToChannel} from "../router";
import {ClientChan, NetChan} from "../types"; import {ClientChan, NetChan, ClientMessage} from "../types";
import {ClientMessage} from "../../../shared/types/msg"; import {SharedMsg} from "../../../shared/types/msg";
let pop; let pop;
@ -188,24 +188,40 @@ function notifyMessage(
} }
} }
function updateUserList(channel, msg) { function updateUserList(channel: ClientChan, msg: SharedMsg) {
if (msg.type === "message" || msg.type === "action") { switch (msg.type) {
const user = channel.users.find((u) => u.nick === msg.from.nick); case "message": // fallthrough
if (user) { case "action": {
user.lastMessage = new Date(msg.time).getTime() || Date.now(); 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) { case "quit": // fallthrough
channel.users.splice(idx, 1);
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) { case "kick": {
channel.users.splice(idx, 1); const idx = channel.users.findIndex((u) => u.nick === msg.target.nick);
if (idx > -1) {
channel.users.splice(idx, 1);
}
break;
} }
} }
} }