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.
This commit is contained in:
Aman Agrawal 2024-08-18 17:08:27 +00:00 committed by Tim Abbott
parent 5b9a2584c5
commit 21b6bb9887
4 changed files with 20 additions and 8 deletions

View File

@ -2,7 +2,6 @@ import $ from "jquery";
import assert from "minimalistic-assert"; import assert from "minimalistic-assert";
import * as alert_words from "./alert_words"; 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_fade from "./compose_fade";
import * as compose_notifications from "./compose_notifications"; import * as compose_notifications from "./compose_notifications";
import * as compose_recipient from "./compose_recipient"; 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); const any_untracked_unread_messages = unread.process_loaded_messages(messages, false);
direct_message_group_data.process_loaded_messages(messages); 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; let need_user_to_scroll = false;
for (const list of message_lists.all_rendered_message_lists()) { for (const list of message_lists.all_rendered_message_lists()) {
if (!list.data.filter.can_apply_locally()) { 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 // sent_by_this_client will be true if ANY of the messages
// were sent by this client; notifications.notify_local_mixes // were sent by this client; notifications.notify_local_mixes
// will filter out any not sent by us. // will filter out any not sent by us.

View File

@ -88,3 +88,12 @@ export function get_superset_datasets(filter: Filter): MessageListData[] {
return [...superset_datasets, all_messages_data.all_messages_data]; 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;
}
}
}

View File

@ -73,11 +73,12 @@ export function add_new_messages_data(
} }
| undefined { | undefined {
if (!msg_list_data.fetch_status.has_found_newest()) { 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; // The reasoning in add_new_messages applies here as well;
// we're trying to maintain a data structure that's a // we're trying to maintain a data structure that's a
// contiguous range of message history, so we can't append a // contiguous range of message history, so we can't append a
// new message that might not be adjacent to that range. // 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 undefined;
} }
return msg_list_data.add_messages(messages); return msg_list_data.add_messages(messages);

View File

@ -39,6 +39,7 @@ message_lists.current = {
}, },
}; };
message_lists.all_rendered_message_lists = () => [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. // And we will also test some real code, of course.
const message_events = zrequire("message_events"); 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(direct_message_group_data, "process_loaded_messages");
helper.redirect(message_notifications, "received_messages"); helper.redirect(message_notifications, "received_messages");
helper.redirect(message_util, "add_new_messages_data");
helper.redirect(message_util, "add_new_messages"); helper.redirect(message_util, "add_new_messages");
helper.redirect(stream_list, "update_streams_sidebar"); helper.redirect(stream_list, "update_streams_sidebar");
helper.redirect(unread_ops, "process_visible"); helper.redirect(unread_ops, "process_visible");
@ -112,7 +112,6 @@ run_test("insert_message", ({override}) => {
// comes in: // comes in:
assert.deepEqual(helper.events, [ assert.deepEqual(helper.events, [
[direct_message_group_data, "process_loaded_messages"], [direct_message_group_data, "process_loaded_messages"],
[message_util, "add_new_messages_data"],
[message_util, "add_new_messages"], [message_util, "add_new_messages"],
[unread_ui, "update_unread_counts"], [unread_ui, "update_unread_counts"],
[unread_ops, "process_visible"], [unread_ops, "process_visible"],