Skip to content

Commit

Permalink
socket-events/msg: if/else chains are not a switch replacement
Browse files Browse the repository at this point in the history
If we switch on a field, use switch for god's sake.
If/elif chains are for cases where you have multiple selectors.
  • Loading branch information
brunnre8 committed Apr 8, 2024
1 parent 6f274c0 commit 4cb438c
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions client/js/socket-events/msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}
}

0 comments on commit 4cb438c

Please sign in to comment.