Skip to content

Commit

Permalink
refactor: Extract method to create and update message lists in narrow.
Browse files Browse the repository at this point in the history
This commit extracts the method to create and update message
lists in the narrow. This is particularly useful in cases when we
need to update the narrow and view with our own message lists.

This is a preparatory commit to #30114, since it involves
creating a new message list from the messages fetched,
which may be of a different narrow from that of the
initial filter, and updating the view and narrow with it.
  • Loading branch information
roanster007 authored and timabbott committed May 22, 2024
1 parent 0fbdd02 commit 3b73f19
Showing 1 changed file with 82 additions and 73 deletions.
155 changes: 82 additions & 73 deletions web/src/narrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,83 @@ export function update_hash_to_match_filter(filter, trigger) {
changehash(new_hash, trigger);
}

function create_and_update_message_list(filter, id_info, opts) {
const excludes_muted_topics = filter.excludes_muted_topics();

// Check if we already have a rendered message list for the `filter`.
// TODO: If we add a message list other than `is_in_home` to be save as rendered,
// we need to add a `is_equal` function to `Filter` to compare the filters.
let msg_list;
let restore_rendered_list = false;
for (const list of message_lists.all_rendered_message_lists()) {
if (filter.is_in_home() && list.preserve_rendered_state) {
assert(list.data.filter.is_in_home());
msg_list = list;
restore_rendered_list = true;
break;
}
}

if (!restore_rendered_list) {
let msg_data = new MessageListData({
filter,
excludes_muted_topics,
});

// Populate the message list if we can apply our filter locally (i.e.
// with no backend help) and we have the message we want to select.
// Also update id_info accordingly.
maybe_add_local_messages({
id_info,
msg_data,
});

if (!id_info.local_select_id) {
// If we're not actually ready to select an ID, we need to
// trash the `MessageListData` object that we just constructed
// and pass an empty one to MessageList, because the block of
// messages in the MessageListData built inside
// maybe_add_local_messages is likely not be contiguous with
// the block we're about to request from the server instead.
msg_data = new MessageListData({
filter,
excludes_muted_topics,
});
}

msg_list = new message_list.MessageList({
data: msg_data,
});
}
assert(msg_list !== undefined);

// Put the narrow terms in the URL fragment/hash.
//
// opts.change_hash will be false when the URL fragment was
// the source of this narrow, and the fragment was not a link to
// a specific target message ID that has been moved.
//
// This needs to be called at the same time as updating the
// current message list so that we don't need to think about
// bugs related to the URL fragment/hash being desynced from
// message_lists.current.
//
// It's fine for the hash change to happen anytime before updating
// the current message list as we are trying to emulate the `hashchange`
// workflow we have which calls `narrow.activate` after hash is updated.
if (opts.change_hash) {
update_hash_to_match_filter(filter, opts.trigger);
}

// Show the new set of messages. It is important to set message_lists.current to
// the view right as it's being shown, because we rely on message_lists.current
// being shown for deciding when to condense messages.
// From here on down, any calls to the narrow_state API will
// reflect the requested narrow.
message_lists.update_current_message_list(msg_list);
return {msg_list, restore_rendered_list};
}

export function activate(raw_terms, opts) {
/* Main entry point for switching to a new view / message list.
Expand Down Expand Up @@ -417,79 +494,11 @@ export function activate(raw_terms, opts) {
}
}

const excludes_muted_topics = filter.excludes_muted_topics();

// Check if we already have a rendered message list for the `filter`.
// TODO: If we add a message list other than `is_in_home` to be save as rendered,
// we need to add a `is_equal` function to `Filter` to compare the filters.
let msg_list;
let restore_rendered_list = false;
for (const list of message_lists.all_rendered_message_lists()) {
if (is_combined_feed_global_view && list.preserve_rendered_state) {
assert(list.data.filter.is_in_home());
msg_list = list;
restore_rendered_list = true;
break;
}
}

if (!restore_rendered_list) {
let msg_data = new MessageListData({
filter,
excludes_muted_topics,
});

// Populate the message list if we can apply our filter locally (i.e.
// with no backend help) and we have the message we want to select.
// Also update id_info accordingly.
maybe_add_local_messages({
id_info,
msg_data,
});

if (!id_info.local_select_id) {
// If we're not actually ready to select an ID, we need to
// trash the `MessageListData` object that we just constructed
// and pass an empty one to MessageList, because the block of
// messages in the MessageListData built inside
// maybe_add_local_messages is likely not be contiguous with
// the block we're about to request from the server instead.
msg_data = new MessageListData({
filter,
excludes_muted_topics,
});
}

msg_list = new message_list.MessageList({
data: msg_data,
});
}
assert(msg_list !== undefined);

// Put the narrow terms in the URL fragment/hash.
//
// opts.change_hash will be false when the URL fragment was
// the source of this narrow, and the fragment was not a link to
// a specific target message ID that has been moved.
//
// This needs to be called at the same time as updating the
// current message list so that we don't need to think about
// bugs related to the URL fragment/hash being desynced from
// message_lists.current.
//
// It's fine for the hash change to happen anytime before updating
// the current message list as we are trying to emulate the `hashchange`
// workflow we have which calls `narrow.activate` after hash is updated.
if (opts.change_hash) {
update_hash_to_match_filter(filter, opts.trigger);
}

// Show the new set of messages. It is important to set message_lists.current to
// the view right as it's being shown, because we rely on message_lists.current
// being shown for deciding when to condense messages.
// From here on down, any calls to the narrow_state API will
// reflect the requested narrow.
message_lists.update_current_message_list(msg_list);
const {msg_list, restore_rendered_list} = create_and_update_message_list(
filter,
id_info,
opts,
);

let select_immediately;
let select_opts;
Expand Down

0 comments on commit 3b73f19

Please sign in to comment.