Skip to content

Commit

Permalink
move message checksum calc to render()
Browse files Browse the repository at this point in the history
Signed-off-by: Gerard Hickey <[email protected]>
  • Loading branch information
hickey committed Feb 27, 2024
1 parent 74f5dee commit 91f7c35
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 deletions www/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Messages {
this.db_version = 0;
this.last_update_time = 0;
this._updating = false;
this._message_checksum = null;
this._message_checksum = null; // only messages in channel

this.__current_channel = "";
this.__channels = new Array();
Expand All @@ -34,6 +34,10 @@ class Messages {
return this.__instance;
}

/* check() retrieves the current message database version from the
MeshChat server and compares it with the last known version.
If the database version is different (i.e. database has new messages),
then an update cycle is kicked off by calling fetch() */
check() {
console.debug("Messages.check()");

Expand Down Expand Up @@ -63,6 +67,10 @@ class Messages {
});
}

/* fetch() is used to retrieve the messages from the message database.
It is told the new database version with the pending_version param.
All messages are then stored in the local message db (this.messages)
and update() is called to update all the internal counters */
fetch(pending_version) {
console.debug("Messages.fetch(pending_version = " + pending_version + ")");

Expand Down Expand Up @@ -91,8 +99,6 @@ class Messages {
update(msg_ids=null) {
console.debug("Messages.update(msg_ids=" + JSON.stringify(msg_ids) + " )");

let message_checksum = 0;

if (msg_ids === null) {
msg_ids = Array.from(this.messages.keys());
}
Expand All @@ -116,11 +122,7 @@ class Messages {
this.__channels.push(message.channel);
}

// add this message to the checksum
if (message.channel == this.__current_channel) {
message_checksum += parseInt(message.id, 16);
}

// TODO not sure this is actually needed, get should be returning a reference
this.messages.set(id, message);
}

Expand All @@ -131,28 +133,20 @@ class Messages {
let b_msg = this.messages.get(b);
return a_msg.epoch > b_msg.epoch ? -1 : 1;
});

// this._message_checksum == null is the first rendering of the
// message table. No need to sound an alert.
if (this._message_checksum != null && message_checksum != this._message_checksum) {
// reset internal message checksum and notify of new messages
this.notify(Messages.NEW_MSG);
this._message_checksum = message_checksum;
}
}

set_channel(chan) {
console.debug("Messages.set_channel(chan=" + chan + ")");
this.__current_channel = chan;

// need to recalculate the message checksum
let message_checksum = 0;
for (var id of this.message_order) {
let message = this.messages.get(id);
if (message.channel == chan) {
message_checksum += parseInt(message.id, 16);
}
}
this._message_checksum = null;
// // need to recalculate the message checksum
// let message_checksum = 0;
// for (var id of this.message_order) {
// let message = this.messages.get(id);
// if (message.channel == chan) {
// message_checksum += parseInt(message.id, 16);
// }
// }
}

current_channel() {
Expand Down Expand Up @@ -227,6 +221,7 @@ class Messages {
console.debug("Messages.render(channel=" + channel + ", search_filter=" + search_filter + ")");
let html = '';
let search = search_filter.toLowerCase();
let message_checksum = 0;

for (var id of this.message_order) {
var message = this.messages.get(id);
Expand All @@ -249,13 +244,24 @@ class Messages {

if (channel == message.channel || this.__current_channel == '') {
html += this.render_row(message);

// add this message to the checksum
message_checksum += parseInt(message.id, 16);
}
}

// provide a message if no messages were found
if (html == "") {
html = "<tr><td>No messages found</td></tr>";
}

// this._message_checksum == null is the first rendering of the
// message table. No need to sound an alert.
if (this._message_checksum != null && message_checksum != this._message_checksum) {
this.notify(Messages.NEW_MSG);
}
this._message_checksum = message_checksum;

return html;
}

Expand Down

0 comments on commit 91f7c35

Please sign in to comment.