From 21b6bb98877549de0456f91a27f176b4b63bb24c Mon Sep 17 00:00:00 2001 From: Aman Agrawal Date: Sun, 18 Aug 2024 17:08:27 +0000 Subject: [PATCH] message_list_data_cache: Update when receiving new messages. If we receive a message that we can add to the data cache without talking to the server, we do that, otherwise we remove it from the cache to avoid us having wrong filter status cached. --- web/src/message_events.js | 13 ++++++++----- web/src/message_list_data_cache.ts | 9 +++++++++ web/src/message_util.ts | 3 ++- web/tests/example5.test.js | 3 +-- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/web/src/message_events.js b/web/src/message_events.js index c8f503e97d..d69ce07d6f 100644 --- a/web/src/message_events.js +++ b/web/src/message_events.js @@ -2,7 +2,6 @@ import $ from "jquery"; import assert from "minimalistic-assert"; import * as alert_words from "./alert_words"; -import {all_messages_data} from "./all_messages_data"; import * as compose_fade from "./compose_fade"; import * as compose_notifications from "./compose_notifications"; import * as compose_recipient from "./compose_recipient"; @@ -42,10 +41,6 @@ export function insert_new_messages(messages, sent_by_this_client, deliver_local const any_untracked_unread_messages = unread.process_loaded_messages(messages, false); direct_message_group_data.process_loaded_messages(messages); - // all_messages_data is the data that we use to populate - // other lists, so we always update this - message_util.add_new_messages_data(messages, all_messages_data); - let need_user_to_scroll = false; for (const list of message_lists.all_rendered_message_lists()) { if (!list.data.filter.can_apply_locally()) { @@ -87,6 +82,14 @@ export function insert_new_messages(messages, sent_by_this_client, deliver_local } } + for (const msg_list_data of message_lists.non_rendered_data()) { + if (!msg_list_data.filter.can_apply_locally()) { + message_list_data_cache.remove(msg_list_data.filter); + } else { + message_util.add_new_messages_data(messages, msg_list_data); + } + } + // sent_by_this_client will be true if ANY of the messages // were sent by this client; notifications.notify_local_mixes // will filter out any not sent by us. diff --git a/web/src/message_list_data_cache.ts b/web/src/message_list_data_cache.ts index f9b54c409e..c2d77a0684 100644 --- a/web/src/message_list_data_cache.ts +++ b/web/src/message_list_data_cache.ts @@ -88,3 +88,12 @@ export function get_superset_datasets(filter: Filter): MessageListData[] { return [...superset_datasets, all_messages_data.all_messages_data]; } + +export function remove(filter: Filter): void { + for (const [key, cached_data] of cache.entries()) { + if (cached_data.filter.equals(filter)) { + cache.delete(key); + return; + } + } +} diff --git a/web/src/message_util.ts b/web/src/message_util.ts index 6440661d02..719aa9c0ea 100644 --- a/web/src/message_util.ts +++ b/web/src/message_util.ts @@ -73,11 +73,12 @@ export function add_new_messages_data( } | undefined { if (!msg_list_data.fetch_status.has_found_newest()) { + const filtered_msgs = msg_list_data.valid_non_duplicated_messages(messages); // The reasoning in add_new_messages applies here as well; // we're trying to maintain a data structure that's a // contiguous range of message history, so we can't append a // new message that might not be adjacent to that range. - msg_list_data.fetch_status.update_expected_max_message_id(messages); + msg_list_data.fetch_status.update_expected_max_message_id(filtered_msgs); return undefined; } return msg_list_data.add_messages(messages); diff --git a/web/tests/example5.test.js b/web/tests/example5.test.js index dcce636733..a7e7fa1393 100644 --- a/web/tests/example5.test.js +++ b/web/tests/example5.test.js @@ -39,6 +39,7 @@ message_lists.current = { }, }; message_lists.all_rendered_message_lists = () => [message_lists.current]; +message_lists.non_rendered_data = () => []; // And we will also test some real code, of course. const message_events = zrequire("message_events"); @@ -98,7 +99,6 @@ run_test("insert_message", ({override}) => { helper.redirect(direct_message_group_data, "process_loaded_messages"); helper.redirect(message_notifications, "received_messages"); - helper.redirect(message_util, "add_new_messages_data"); helper.redirect(message_util, "add_new_messages"); helper.redirect(stream_list, "update_streams_sidebar"); helper.redirect(unread_ops, "process_visible"); @@ -112,7 +112,6 @@ run_test("insert_message", ({override}) => { // comes in: assert.deepEqual(helper.events, [ [direct_message_group_data, "process_loaded_messages"], - [message_util, "add_new_messages_data"], [message_util, "add_new_messages"], [unread_ui, "update_unread_counts"], [unread_ops, "process_visible"],