Skip to content

Commit

Permalink
Make index more readable.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mitchdev committed Nov 18, 2024
1 parent 642d015 commit a1392f6
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions assets/chat/js/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,42 +85,41 @@ class ChatWindow extends EventEmitter {
const index = this.getMessageIndex(message);

/**
* If message index is < 0 then add message to the top of chat.
*
* If message index + 1 >= the length of messages array,
* it is a new message so add to bottom of chat.
*
* Otherwise the message is inbetween so insert in correct place.
* If the index of the message is 0 then prepend.
* If it's equal the length of the array then append.
* Otherwise insert at index.
*/
if (index < 0) {
if (index === 0) {
this.lines.prepend(message.ui);
this.messages.unshift(message);
} else if (index + 1 >= this.messages.length) {
} else if (index === this.messages.length) {
this.lines.append(message.ui);
this.messages.push(message);
this.lastmessage = message;
} else {
this.lines.insertBefore(message.ui, this.messages[index + 1].ui);
this.messages.splice(index + 1, 0, message);
this.lines.insertBefore(message.ui, this.messages[index].ui);
this.messages.splice(index, 0, message);
}

this.linecount += 1;
this.cleanupThrottle();
}

getMessageIndex(message) {
return this.messages.findLastIndex(
(m) => m.timestamp.valueOf() <= message.timestamp.valueOf(),
return (
this.messages.findLastIndex(
(m) => m.timestamp.valueOf() <= message.timestamp.valueOf(),
) + 1
);
}

getPreviousMessage(message) {
const index = this.getMessageIndex(message);
if (index < 0 || index > this.messages.length) {
if (index === 0) {
return null;
}

return this.messages[index];
return this.messages[index - 1];
}

containsMessage(message) {
Expand Down

0 comments on commit a1392f6

Please sign in to comment.