unread: Convert unread_topic_counter to an ES6 class UnreadTopicCounter.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2020-07-22 19:01:19 -07:00 committed by Tim Abbott
parent e5ff78e893
commit 41778d81d2
1 changed files with 37 additions and 40 deletions

View File

@ -186,32 +186,30 @@ function make_per_stream_bucketer() {
}); });
} }
exports.unread_topic_counter = (function () { class UnreadTopicCounter {
const self = {}; bucketer = new Bucketer({
const bucketer = new Bucketer({
KeyDict: Map, // bucket keys are stream_ids KeyDict: Map, // bucket keys are stream_ids
make_bucket: make_per_stream_bucketer, make_bucket: make_per_stream_bucketer,
}); });
self.clear = function () { clear() {
bucketer.clear(); this.bucketer.clear();
}; }
self.set_streams = function (objs) { set_streams(objs) {
for (const obj of objs) { for (const obj of objs) {
const stream_id = obj.stream_id; const stream_id = obj.stream_id;
const topic = obj.topic; const topic = obj.topic;
const unread_message_ids = obj.unread_message_ids; const unread_message_ids = obj.unread_message_ids;
for (const msg_id of unread_message_ids) { for (const msg_id of unread_message_ids) {
self.add(stream_id, topic, msg_id); this.add(stream_id, topic, msg_id);
} }
} }
}; }
self.add = function (stream_id, topic, msg_id) { add(stream_id, topic, msg_id) {
bucketer.add({ this.bucketer.add({
bucket_key: stream_id, bucket_key: stream_id,
item_id: msg_id, item_id: msg_id,
add_callback(per_stream_bucketer) { add_callback(per_stream_bucketer) {
@ -221,17 +219,17 @@ exports.unread_topic_counter = (function () {
}); });
}, },
}); });
}; }
self.delete = function (msg_id) { delete(msg_id) {
bucketer.delete(msg_id); this.bucketer.delete(msg_id);
}; }
self.get_counts = function () { get_counts() {
const res = {}; const res = {};
res.stream_unread_messages = 0; res.stream_unread_messages = 0;
res.stream_count = new Map(); // hash by stream_id -> count res.stream_count = new Map(); // hash by stream_id -> count
for (const [stream_id, per_stream_bucketer] of bucketer) { for (const [stream_id, per_stream_bucketer] of this.bucketer) {
// We track unread counts for streams that may be currently // We track unread counts for streams that may be currently
// unsubscribed. Since users may re-subscribe, we don't // unsubscribed. Since users may re-subscribe, we don't
// completely throw away the data. But we do ignore it here, // completely throw away the data. But we do ignore it here,
@ -255,13 +253,13 @@ exports.unread_topic_counter = (function () {
} }
return res; return res;
}; }
self.get_missing_topics = function (opts) { get_missing_topics(opts) {
const stream_id = opts.stream_id; const stream_id = opts.stream_id;
const topic_dict = opts.topic_dict; const topic_dict = opts.topic_dict;
const per_stream_bucketer = bucketer.get_bucket(stream_id); const per_stream_bucketer = this.bucketer.get_bucket(stream_id);
if (!per_stream_bucketer) { if (!per_stream_bucketer) {
return []; return [];
} }
@ -280,12 +278,12 @@ exports.unread_topic_counter = (function () {
}); });
return result; return result;
}; }
self.get_stream_count = function (stream_id) { get_stream_count(stream_id) {
let stream_count = 0; let stream_count = 0;
const per_stream_bucketer = bucketer.get_bucket(stream_id); const per_stream_bucketer = this.bucketer.get_bucket(stream_id);
if (!per_stream_bucketer) { if (!per_stream_bucketer) {
return 0; return 0;
@ -299,10 +297,10 @@ exports.unread_topic_counter = (function () {
} }
return stream_count; return stream_count;
}; }
self.get = function (stream_id, topic) { get(stream_id, topic) {
const per_stream_bucketer = bucketer.get_bucket(stream_id); const per_stream_bucketer = this.bucketer.get_bucket(stream_id);
if (!per_stream_bucketer) { if (!per_stream_bucketer) {
return 0; return 0;
} }
@ -313,10 +311,10 @@ exports.unread_topic_counter = (function () {
} }
return topic_bucket.size; return topic_bucket.size;
}; }
self.get_msg_ids_for_stream = function (stream_id) { get_msg_ids_for_stream(stream_id) {
const per_stream_bucketer = bucketer.get_bucket(stream_id); const per_stream_bucketer = this.bucketer.get_bucket(stream_id);
if (!per_stream_bucketer) { if (!per_stream_bucketer) {
return []; return [];
@ -333,10 +331,10 @@ exports.unread_topic_counter = (function () {
} }
return util.sorted_ids(ids); return util.sorted_ids(ids);
}; }
self.get_msg_ids_for_topic = function (stream_id, topic) { get_msg_ids_for_topic(stream_id, topic) {
const per_stream_bucketer = bucketer.get_bucket(stream_id); const per_stream_bucketer = this.bucketer.get_bucket(stream_id);
if (!per_stream_bucketer) { if (!per_stream_bucketer) {
return []; return [];
} }
@ -348,10 +346,10 @@ exports.unread_topic_counter = (function () {
const ids = Array.from(topic_bucket); const ids = Array.from(topic_bucket);
return util.sorted_ids(ids); return util.sorted_ids(ids);
}; }
self.topic_has_any_unread = function (stream_id, topic) { topic_has_any_unread(stream_id, topic) {
const per_stream_bucketer = bucketer.get_bucket(stream_id); const per_stream_bucketer = this.bucketer.get_bucket(stream_id);
if (!per_stream_bucketer) { if (!per_stream_bucketer) {
return false; return false;
@ -363,10 +361,9 @@ exports.unread_topic_counter = (function () {
} }
return id_set.size !== 0; return id_set.size !== 0;
}; }
}
return self; exports.unread_topic_counter = new UnreadTopicCounter();
})();
exports.unread_mentions_counter = new Set(); exports.unread_mentions_counter = new Set();