From 2f91f583fc0bff07b913ab918594b163f17287c0 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Fri, 31 Jan 2020 18:28:35 -0800 Subject: [PATCH] unread: Replace buckets with Set. Signed-off-by: Anders Kaseorg --- static/js/unread.js | 83 ++++++++++----------------------------------- 1 file changed, 17 insertions(+), 66 deletions(-) diff --git a/static/js/unread.js b/static/js/unread.js index 7e67e71902..8457a361ab 100644 --- a/static/js/unread.js +++ b/static/js/unread.js @@ -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;