unread: Replace buckets with Set.

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
This commit is contained in:
Anders Kaseorg 2020-01-31 18:28:35 -08:00 committed by Tim Abbott
parent 5262ff790e
commit 2f91f583fc
1 changed files with 17 additions and 66 deletions

View File

@ -14,55 +14,6 @@ exports.set_messages_read_in_narrow = function (value) {
exports.messages_read_in_narrow = value;
};
function make_id_set() {
/* This is just a basic set class where
elements should be numeric ids.
*/
const self = {};
const ids = new Dict();
self.clear = function () {
ids.clear();
};
self.add = function (id) {
ids.set(id, true);
};
self.has = function (id) {
return ids.has(id);
};
self.add_many = function (id_list) {
_.each(id_list, function (id) {
ids.set(id, true);
});
};
self.del = function (id) {
ids.del(id);
};
self.count = function () {
return ids.num_items();
};
self.members = function () {
return ids.keys();
};
self.max = function () {
return _.max(ids.keys());
};
self.is_empty = function () {
return ids.is_empty();
};
return self;
}
const unread_messages = new Set();
function make_bucketer(options) {
@ -93,10 +44,10 @@ function make_bucketer(options) {
reverse_lookup.set(item_id, bucket);
};
self.del = function (item_id) {
self.delete = function (item_id) {
const bucket = reverse_lookup.get(item_id);
if (bucket) {
bucket.del(item_id);
bucket.delete(item_id);
reverse_lookup.del(item_id);
}
};
@ -121,7 +72,7 @@ exports.unread_pm_counter = (function () {
const bucketer = make_bucketer({
fold_case: false,
make_bucket: make_id_set,
make_bucket: () => new Set(),
});
self.clear = function () {
@ -162,14 +113,14 @@ exports.unread_pm_counter = (function () {
};
self.del = function (message_id) {
bucketer.del(message_id);
bucketer.delete(message_id);
};
self.get_counts = function () {
const pm_dict = new Dict(); // Hash by user_ids_string -> count
let total_count = 0;
bucketer.each(function (id_set, user_ids_string) {
const count = id_set.count();
const count = id_set.size;
pm_dict.set(user_ids_string, count);
total_count += count;
});
@ -189,14 +140,14 @@ exports.unread_pm_counter = (function () {
if (!bucket) {
return 0;
}
return bucket.count();
return bucket.size;
};
self.get_msg_ids = function () {
const lists = [];
bucketer.each(function (id_set) {
const members = id_set.members();
const members = [...id_set];
lists.push(members);
});
@ -216,7 +167,7 @@ exports.unread_pm_counter = (function () {
return [];
}
const ids = bucket.members();
const ids = [...bucket];
return util.sorted_ids(ids);
};
@ -226,7 +177,7 @@ exports.unread_pm_counter = (function () {
function make_per_stream_bucketer() {
return make_bucketer({
fold_case: true, // bucket keys are topics
make_bucket: make_id_set,
make_bucket: () => new Set(),
});
}
@ -269,7 +220,7 @@ exports.unread_topic_counter = (function () {
};
self.del = function (msg_id) {
bucketer.del(msg_id);
bucketer.delete(msg_id);
};
function str_dict() {
@ -297,7 +248,7 @@ exports.unread_topic_counter = (function () {
res.topic_count.set(stream_id, str_dict());
let stream_count = 0;
per_stream_bucketer.each(function (msgs, topic) {
const topic_count = msgs.count();
const topic_count = msgs.size;
res.topic_count.get(stream_id).set(topic, topic_count);
if (!muting.is_topic_muted(stream_id, topic)) {
stream_count += topic_count;
@ -330,7 +281,7 @@ exports.unread_topic_counter = (function () {
const result = _.map(topic_names, function (topic_name) {
const msgs = per_stream_bucketer.get_bucket(topic_name);
const message_id = msgs.max();
const message_id = Math.max(...msgs);
return {
// retrieve the topic with its original case, since topic_name
@ -355,7 +306,7 @@ exports.unread_topic_counter = (function () {
const sub = stream_data.get_sub_by_id(stream_id);
per_stream_bucketer.each(function (msgs, topic) {
if (sub && !muting.is_topic_muted(stream_id, topic)) {
stream_count += msgs.count();
stream_count += msgs.size;
}
});
@ -373,7 +324,7 @@ exports.unread_topic_counter = (function () {
return 0;
}
return topic_bucket.count();
return topic_bucket.size;
};
self.get_msg_ids_for_stream = function (stream_id) {
@ -387,7 +338,7 @@ exports.unread_topic_counter = (function () {
const sub = stream_data.get_sub_by_id(stream_id);
per_stream_bucketer.each(function (msgs, topic) {
if (sub && !muting.is_topic_muted(stream_id, topic)) {
topic_lists.push(msgs.members());
topic_lists.push([...msgs]);
}
});
@ -407,7 +358,7 @@ exports.unread_topic_counter = (function () {
return [];
}
const ids = topic_bucket.members();
const ids = [...topic_bucket];
return util.sorted_ids(ids);
};
@ -424,7 +375,7 @@ exports.unread_topic_counter = (function () {
return false;
}
return !id_set.is_empty();
return id_set.size !== 0;
};
return self;