server/client: refactor command input

Keep happy path on the left and try to return as early
as we can to help the reader understand the logic better

The function is too large to be able to quickly scan an if / else
chain and see the function return at the end
This commit is contained in:
Reto Brunner 2023-03-04 17:01:58 +01:00
parent 402332340b
commit 0c01e54a61

View file

@ -465,38 +465,9 @@ class Client {
const cmd = args?.shift()?.toLowerCase() || "";
const irc = target.network.irc;
let connected = irc && irc.connection && irc.connection.connected;
const connected = irc?.connected;
if (inputs.userInputs.has(cmd)) {
const plugin = inputs.userInputs.get(cmd);
if (!plugin) {
// should be a no-op
throw new Error(`Plugin ${cmd} not found`);
}
if (typeof plugin.input === "function" && (connected || plugin.allowDisconnected)) {
connected = true;
plugin.input.apply(client, [target.network, target.chan, cmd, args]);
}
} else if (inputs.pluginCommands.has(cmd)) {
const plugin = inputs.pluginCommands.get(cmd);
if (typeof plugin.input === "function" && (connected || plugin.allowDisconnected)) {
connected = true;
plugin.input(
new PublicClient(client, plugin.packageInfo),
{network: target.network, chan: target.chan},
cmd,
args
);
}
} else if (connected) {
// TODO: fix
irc!.raw(text);
}
if (!connected) {
const emitFailureDisconnected = () => {
target.chan.pushMessage(
this,
new Msg({
@ -504,7 +475,44 @@ class Client {
text: "You are not connected to the IRC network, unable to send your command.",
})
);
};
if (inputs.userInputs.has(cmd)) {
const plugin = inputs.userInputs.get(cmd)!; // this is only save because we just checked it
if (!connected && !plugin.allowDisconnected) {
emitFailureDisconnected();
return;
}
plugin.input.apply(client, [target.network, target.chan, cmd, args]);
return;
}
if (inputs.pluginCommands.has(cmd)) {
const plugin = inputs.pluginCommands.get(cmd)!; // this is only save because we just checked it
if (!connected && !plugin.allowDisconnected) {
emitFailureDisconnected();
return;
}
plugin.input(
new PublicClient(client, plugin.packageInfo),
{network: target.network, chan: target.chan},
cmd,
args
);
return;
}
if (!connected) {
emitFailureDisconnected();
return;
}
// TODO: fix
irc!.raw(text);
}
compileCustomHighlights() {