From b054050ee8f0ebee3085abce9c6fabefb666b2b8 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Sat, 4 May 2024 10:31:19 -0700 Subject: [PATCH] unread: Fix implicit use of any. Signed-off-by: Anders Kaseorg --- web/src/inbox_ui.js | 12 ++---- web/src/unread.ts | 95 ++++++++++++++++++++++++++------------------- 2 files changed, 59 insertions(+), 48 deletions(-) diff --git a/web/src/inbox_ui.js b/web/src/inbox_ui.js index daba7dbde1..616ad5c325 100644 --- a/web/src/inbox_ui.js +++ b/web/src/inbox_ui.js @@ -448,13 +448,11 @@ function reset_data() { topics_dict = new Map(); streams_dict = new Map(); - const include_per_bucket_max_msg_id = true; - const unread_dms = unread.get_unread_pm(include_per_bucket_max_msg_id); + const unread_dms = unread.get_unread_pm(); const unread_dms_count = unread_dms.total_count; const unread_dms_dict = unread_dms.pm_dict; - const include_per_topic_max_msg_id = true; - const unread_stream_message = unread.get_unread_topics(include_per_topic_max_msg_id); + const unread_stream_message = unread.get_unread_topics(); const unread_stream_msg_count = unread_stream_message.stream_unread_messages; const unread_streams_dict = unread_stream_message.topic_counts; @@ -1023,13 +1021,11 @@ export function update() { return; } - const include_per_bucket_max_msg_id = true; - const unread_dms = unread.get_unread_pm(include_per_bucket_max_msg_id); + const unread_dms = unread.get_unread_pm(); const unread_dms_count = unread_dms.total_count; const unread_dms_dict = unread_dms.pm_dict; - const include_per_topic_max_msg_id = true; - const unread_stream_message = unread.get_unread_topics(include_per_topic_max_msg_id); + const unread_stream_message = unread.get_unread_topics(); const unread_streams_dict = unread_stream_message.topic_counts; let has_dms_post_filter = false; diff --git a/web/src/unread.ts b/web/src/unread.ts index eec7cb39df..c4d8b5ff04 100644 --- a/web/src/unread.ts +++ b/web/src/unread.ts @@ -42,7 +42,7 @@ const unread_messages = new Set(); // // Functionally a cache; see clear_and_populate_unread_mention_topics // for how we can refresh it efficiently. -export const unread_mention_topics = new Map(); +export const unread_mention_topics = new Map>(); export type StreamCountInfo = { unmuted_count: number; @@ -56,6 +56,11 @@ type DirectMessageCountInfo = { pm_dict: Map; }; +type DirectMessageCountInfoWithLatestMsgId = { + total_count: number; + pm_dict: Map; +}; + class UnreadDirectMessageCounter { bucketer = new Map>(); // Maps direct message id to the user id string that's the key for bucketer @@ -107,20 +112,30 @@ class UnreadDirectMessageCounter { this.reverse_lookup.delete(message_id); } - get_counts(include_latest_msg_id = false): DirectMessageCountInfo { - const pm_dict = new Map(); // Hash by user_ids_string -> count Optional[, max_id] + get_counts(): DirectMessageCountInfo { + const pm_dict = new Map(); // Hash by user_ids_string -> count Optional[, max_id] let total_count = 0; for (const [user_ids_string, id_set] of this.bucketer) { const count = id_set.size; - if (include_latest_msg_id) { - const latest_msg_id = Math.max(...id_set); - pm_dict.set(user_ids_string, { - count, - latest_msg_id, - }); - } else { - pm_dict.set(user_ids_string, count); - } + pm_dict.set(user_ids_string, count); + total_count += count; + } + return { + total_count, + pm_dict, + }; + } + + get_counts_with_latest_msg_id(): DirectMessageCountInfoWithLatestMsgId { + const pm_dict = new Map(); // Hash by user_ids_string -> count Optional[, max_id] + let total_count = 0; + for (const [user_ids_string, id_set] of this.bucketer) { + const count = id_set.size; + const latest_msg_id = Math.max(...id_set); + pm_dict.set(user_ids_string, { + count, + latest_msg_id, + }); total_count += count; } return { @@ -247,9 +262,12 @@ class UnreadTopicCounter { this.reverse_lookup.delete(message_id); } - get_counts_per_topic(include_per_topic_latest_msg_id = false): UnreadTopicCounts { + get_counts_per_topic(): UnreadTopicCounts { let stream_unread_messages = 0; - const topic_counts_by_stream_id = new Map(); // hash by stream_id -> count + const topic_counts_by_stream_id = new Map< + number, + Map + >(); // hash by stream_id -> count for (const [stream_id, per_stream_bucketer] of this.bucketer) { // We track unread counts for streams that may be currently // unsubscribed. Since users may re-subscribe, we don't @@ -260,19 +278,15 @@ class UnreadTopicCounter { continue; } - const topic_unread = new Map(); + const topic_unread = new Map(); let stream_count = 0; for (const [topic, msgs] of per_stream_bucketer) { const topic_count = msgs.size; - if (include_per_topic_latest_msg_id) { - const latest_msg_id = Math.max(...msgs); - topic_unread.set(topic, { - topic_count, - latest_msg_id, - }); - } else { - topic_unread.set(topic, topic_count); - } + const latest_msg_id = Math.max(...msgs); + topic_unread.set(topic, { + topic_count, + latest_msg_id, + }); stream_count += topic_count; } @@ -289,7 +303,7 @@ class UnreadTopicCounter { get_counts(): UnreadStreamCounts { let stream_unread_messages = 0; let followed_topic_unread_messages = 0; - const stream_counts_by_id = new Map(); // hash by stream_id -> count + const stream_counts_by_id = new Map(); // hash by stream_id -> count for (const stream_id of this.bucketer.keys()) { // We track unread counts for streams that may be currently // unsubscribed. Since users may re-subscribe, we don't @@ -303,9 +317,10 @@ class UnreadTopicCounter { // get_stream_count_info calculates both the number of // unmuted unread as well as the number of muted // unreads. - stream_counts_by_id.set(stream_id, this.get_stream_count_info(stream_id)); - stream_unread_messages += stream_counts_by_id.get(stream_id).unmuted_count; - followed_topic_unread_messages += stream_counts_by_id.get(stream_id).followed_count; + const stream_count_info = this.get_stream_count_info(stream_id); + stream_counts_by_id.set(stream_id, stream_count_info); + stream_unread_messages += stream_count_info.unmuted_count; + followed_topic_unread_messages += stream_count_info.followed_count; } return { @@ -561,8 +576,9 @@ function add_message_to_unread_mention_topics(message_id: number): void { return; } const topic_key = recent_view_util.get_topic_key(message.stream_id, message.topic); - if (unread_mention_topics.has(topic_key)) { - unread_mention_topics.get(topic_key).add(message_id); + const topic_message_ids = unread_mention_topics.get(topic_key); + if (topic_message_ids !== undefined) { + topic_message_ids.add(message_id); } else { unread_mention_topics.set(topic_key, new Set([message_id])); } @@ -577,9 +593,7 @@ function remove_message_from_unread_mention_topics(message_id: number): void { } const {stream_id, topic} = stream_topic; const topic_key = recent_view_util.get_topic_key(stream_id, topic); - if (unread_mention_topics.has(topic_key)) { - unread_mention_topics.get(topic_key).delete(message_id); - } + unread_mention_topics.get(topic_key)?.delete(message_id); } export function clear_and_populate_unread_mention_topics(): void { @@ -607,8 +621,9 @@ export function clear_and_populate_unread_mention_topics(): void { const {stream_id, topic} = stream_topic; const topic_key = recent_view_util.get_topic_key(stream_id, topic); - if (unread_mention_topics.has(topic_key)) { - unread_mention_topics.get(topic_key).add(message_id); + const topic_message_ids = unread_mention_topics.get(topic_key); + if (topic_message_ids !== undefined) { + topic_message_ids.add(message_id); } else { unread_mention_topics.set(topic_key, new Set([message_id])); } @@ -836,12 +851,12 @@ export function declare_bankruptcy(): void { unread_mention_topics.clear(); } -export function get_unread_pm(include_per_bucket_latest_msg_id = false): DirectMessageCountInfo { - return unread_direct_message_counter.get_counts(include_per_bucket_latest_msg_id); +export function get_unread_pm(): DirectMessageCountInfoWithLatestMsgId { + return unread_direct_message_counter.get_counts_with_latest_msg_id(); } -export function get_unread_topics(include_per_topic_latest_msg_id = false): UnreadTopicCounts { - return unread_topic_counter.get_counts_per_topic(include_per_topic_latest_msg_id); +export function get_unread_topics(): UnreadTopicCounts { + return unread_topic_counter.get_counts_per_topic(); } export type FullUnreadCountsData = { @@ -955,7 +970,7 @@ export function topic_has_any_unread_mentions(stream_id: number, topic: string): // Because this function is called in a loop for every displayed // Recent Conversations row, it's important for it to run in O(1) time. const topic_key = stream_id + ":" + topic.toLowerCase(); - return unread_mention_topics.get(topic_key) && unread_mention_topics.get(topic_key).size > 0; + return (unread_mention_topics.get(topic_key)?.size ?? 0) > 0; } export function topic_has_any_unread(stream_id: number, topic: string): boolean {