components/buffer: fold NICK change chains

This commit is contained in:
Simon Ser 2024-03-29 16:08:04 +01:00
parent 23ceda5523
commit ebcb731e2f

View file

@ -643,7 +643,38 @@ export default class Buffer extends Component {
`; `;
} }
function createFoldGroup(msgs) { function createFoldGroup(msgs) {
// Filter out PART → JOIN pairs // Merge NICK change chains
let nickChanges = new Map();
let mergedMsgs = [];
for (let msg of msgs) {
let keep = true;
switch (msg.command) {
case "PART":
case "QUIT":
nickChanges.delete(msg.prefix.name);
break;
case "NICK":
let prev = nickChanges.get(msg.prefix.name);
if (!prev) {
// Future NICK messages may mutate this one
msg = { ...msg };
nickChanges.set(msg.params[0], msg);
break;
}
prev.params = msg.params;
nickChanges.delete(msg.prefix.name);
nickChanges.set(msg.params[0], prev);
keep = false;
break;
}
if (keep) {
mergedMsgs.push(msg);
}
}
msgs = mergedMsgs;
// Filter out PART → JOIN pairs, as well as no-op NICKs from previous step
let partIndexes = new Map(); let partIndexes = new Map();
let keep = []; let keep = [];
msgs.forEach((msg, i) => { msgs.forEach((msg, i) => {
@ -654,6 +685,8 @@ export default class Buffer extends Component {
keep[partIndexes.get(msg.prefix.name)] = false; keep[partIndexes.get(msg.prefix.name)] = false;
partIndexes.delete(msg.prefix.name); partIndexes.delete(msg.prefix.name);
keep.push(false); keep.push(false);
} else if (msg.command === "NICK" && msg.prefix.name == msg.params[0]) {
keep.push(false);
} else { } else {
keep.push(true); keep.push(true);
} }