Simplify unread_topic_count.update().

The prior implementation was needlessly complex.  Both del() and
add() are cheap and idempotent.

With this change we no longer bother to delete a topic from a
dictionary when its last message is mark as read, since it doesn't
really help performance.  We add a line to the tests to maintain
100% line coverage.
This commit is contained in:
Steve Howell
2017-08-02 15:08:19 -04:00
committed by Tim Abbott
parent fad4ed4e78
commit 1927a6ca45
2 changed files with 3 additions and 11 deletions

View File

@@ -104,6 +104,7 @@ var zero_counts = {
assert.equal(count, 2);
assert(unread.topic_has_any_unread(stream_id, 'lunch'));
assert(!unread.topic_has_any_unread(wrong_stream_id, 'lunch'));
assert(!unread.topic_has_any_unread(stream_id, 'NOT lunch'));
var event = {
subject: 'dinner',

View File

@@ -83,17 +83,8 @@ exports.unread_topic_counter = (function () {
};
self.update = function (stream_id, topic, new_topic, msg_id) {
if (unread_topics.has(stream_id) &&
unread_topics.get(stream_id).has(topic) &&
unread_topics.get(stream_id).get(topic).get(msg_id)) {
// Move the unread topic count to the new topic
unread_topics.get(stream_id).get(topic).del(msg_id);
if (unread_topics.get(stream_id).get(topic).num_items() === 0) {
unread_topics.get(stream_id).del(topic);
}
unread_topics.get(stream_id).setdefault(new_topic, num_dict());
unread_topics.get(stream_id).get(new_topic).set(msg_id, true);
}
self.del(stream_id, topic, msg_id);
self.add(stream_id, new_topic, msg_id);
};
self.add = function (stream_id, topic, msg_id) {