Skip to content

Commit c9b5157

Browse files
committed
move message checksum code to update() and recalc when channel changed
Signed-off-by: Gerard Hickey <[email protected]>
1 parent c575379 commit c9b5157

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

www/messages.js

+24-14
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ class Messages {
9191
update(msg_ids=null) {
9292
console.debug("Messages.update(msg_ids=" + JSON.stringify(msg_ids) + " )");
9393

94+
let message_checksum = 0;
95+
9496
if (msg_ids === null) {
9597
msg_ids = Array.from(this.messages.keys());
9698
}
@@ -114,6 +116,11 @@ class Messages {
114116
this.__channels.push(message.channel);
115117
}
116118

119+
// add this message to the checksum
120+
if (message.channel == this.__current_channel) {
121+
message_checksum += parseInt(message.id, 16);
122+
}
123+
117124
this.messages.set(id, message);
118125
}
119126

@@ -124,11 +131,28 @@ class Messages {
124131
let b_msg = this.messages.get(b);
125132
return a_msg.epoch > b_msg.epoch ? -1 : 1;
126133
});
134+
135+
// this._message_checksum == null is the first rendering of the
136+
// message table. No need to sound an alert.
137+
if (this._message_checksum != null && message_checksum != this._message_checksum) {
138+
// reset internal message checksum and notify of new messages
139+
this.notify(Messages.NEW_MSG);
140+
this._message_checksum = message_checksum;
141+
}
127142
}
128143

129144
set_channel(chan) {
130145
console.debug("Messages.set_channel(chan=" + chan + ")");
131146
this.__current_channel = chan;
147+
148+
// need to recalculate the message checksum
149+
let message_checksum = 0;
150+
for (var id of this.message_order) {
151+
let message = this.messages.get(id);
152+
if (message.channel == chan) {
153+
message_checksum += parseInt(message.id, 16);
154+
}
155+
}
132156
}
133157

134158
current_channel() {
@@ -203,8 +227,6 @@ class Messages {
203227
console.debug("Messages.render(channel=" + channel + ", search_filter=" + search_filter + ")");
204228
let html = '';
205229
let search = search_filter.toLowerCase();
206-
// compare with last time render was called to detect new messages
207-
let message_checksum = 0;
208230

209231
for (var id of this.message_order) {
210232
var message = this.messages.get(id);
@@ -226,22 +248,10 @@ class Messages {
226248
}
227249

228250
if (channel == message.channel || this.__channel == '') {
229-
message_checksum += parseInt(id, 16);
230251
html += this.render_row(message);
231252
}
232253
}
233254

234-
// this._message_checksum == null is the first rendering of the
235-
// message table. No need to sound an alert.
236-
/* TODO checksum is not working as expected and the audio alert
237-
is played twice when a new message is entered locally
238-
and when changing channels */
239-
if (this._message_checksum != null && message_checksum != this._message_checksum) {
240-
// reset internal message checksum and notify of new messages
241-
this.notify(Messages.NEW_MSG);
242-
}
243-
this._message_checksum = message_checksum;
244-
245255
// provide a message if no messages were found
246256
if (html == "") {
247257
html = "<tr><td>No messages found</td></tr>";

0 commit comments

Comments
 (0)