message events: Refactor reify_message_id codepath.

The changes made in this commit are as follows:

* The `remove_messages` is moved to the `message_events.js`
  file from `ui.js`.

* We refactor `MessageListData.change_message_id` to no
  longer require an `opts` parameter as this function
  just returns whether we need to rerender or not.

  The blueslip error block can be removed since we made
  the change to no long defer the data updates in
  commit 3b5ba6b2c1,
  this case can no longer occur.
This commit is contained in:
ryanreh99
2020-11-13 03:13:04 +05:30
committed by Tim Abbott
parent 59e5f2d8fc
commit e5779c1ca0
8 changed files with 26 additions and 42 deletions

View File

@@ -27,6 +27,7 @@ set_global("home_msg_list", {});
set_global("hotspots", {});
set_global("markdown", {});
set_global("message_edit", {});
set_global("message_events", {});
set_global("message_list", {});
set_global("muting_ui", {});
set_global("narrow_state", {});
@@ -816,7 +817,7 @@ run_test("delete_message", (override) => {
override("stream_list.update_streams_sidebar", noop);
global.with_stub((stub) => {
override("unread_ops.process_read_messages_event", noop);
override("ui.remove_messages", stub.f);
override("message_events.remove_messages", stub.f);
dispatch(event);
const args = stub.get_args("message_ids");
assert_same(args.message_ids, [1337]);

View File

@@ -9,7 +9,6 @@ const noop = function () {};
set_global("Filter", noop);
global.stub_out_jquery();
set_global("document", null);
set_global("current_msg_list", {});
set_global("narrow_state", {});
set_global("stream_data", {});

View File

@@ -88,9 +88,7 @@ run_test("basics", () => {
assert.equal(mld.selected_id(), 125.01);
mld.get(125.01).id = 145;
mld.change_message_id(125.01, 145, {
rerender_view: () => {},
});
mld.change_message_id(125.01, 145);
assert_contents(mld, [120, 130, 140, 145]);
for (const msg of mld.all_messages()) {

View File

@@ -390,4 +390,15 @@ exports.update_messages = function update_messages(events) {
pm_list.update_private_messages();
};
exports.remove_messages = function (message_ids) {
for (const list of [message_list.all, home_msg_list, message_list.narrowed]) {
if (list === undefined) {
continue;
}
list.remove_and_rerender(message_ids);
}
recent_senders.update_topics_of_deleted_message_ids(message_ids);
recent_topics.update_topics_of_deleted_message_ids(message_ids);
};
window.message_events = exports;

View File

@@ -399,11 +399,10 @@ class MessageList {
}
change_message_id(old_id, new_id) {
const opts = {
is_current_list: () => current_msg_list === this,
rerender_view: () => this.rerender_view(),
};
this.data.change_message_id(old_id, new_id, opts);
const require_rerender = this.data.change_message_id(old_id, new_id);
if (require_rerender) {
this.rerender_view();
}
}
get_last_message_sent_by_me() {

View File

@@ -458,14 +458,14 @@ class MessageListData {
return item_list[cur_idx];
}
change_message_id(old_id, new_id, opts) {
change_message_id(old_id, new_id) {
// Update our local cache that uses the old id to the new id
if (this._hash.has(old_id)) {
const msg = this._hash.get(old_id);
this._hash.delete(old_id);
this._hash.set(new_id, msg);
} else {
return;
return false;
}
if (this._local_only.has(old_id)) {
@@ -479,24 +479,15 @@ class MessageListData {
this._selected_id = new_id;
}
this.reorder_messages(new_id, opts);
return this.reorder_messages(new_id);
}
reorder_messages(new_id, opts) {
reorder_messages(new_id) {
const message_sort_func = (a, b) => a.id - b.id;
// If this message is now out of order, re-order and re-render
const current_message = this._hash.get(new_id);
const index = this._items.indexOf(current_message);
if (index === -1) {
if (!this.muting_enabled && opts.is_current_list()) {
blueslip.error(
"Trying to re-order message but can't find message with new_id in _items!",
);
}
return;
}
const next = this._next_nonlocal_message(this._items, index, (idx) => idx + 1);
const prev = this._next_nonlocal_message(this._items, index, (idx) => idx - 1);
@@ -509,14 +500,10 @@ class MessageListData {
if (this.muting_enabled) {
this._all_items.sort(message_sort_func);
}
// The data updates above need to happen synchronously,
// but the rerendering can be deferred. It's unclear
// whether this deferral is necessary; it was present in
// the original 2013 local echo implementation but we
// don't have records for why we added it then.
setTimeout(opts.rerender_view, 0);
return true;
}
return false;
}
get_last_message_sent_by_me() {

View File

@@ -36,7 +36,7 @@ exports.dispatch_normal_event = function dispatch_normal_event(event) {
unread_ops.process_read_messages_event(msg_ids);
// This methods updates message_list too and since stream_topic_history relies on it
// this method should be called first.
ui.remove_messages(msg_ids);
message_events.remove_messages(msg_ids);
if (event.message_type === "stream") {
stream_topic_history.remove_messages({

View File

@@ -105,17 +105,6 @@ exports.show_message_failed = function (message_id, failed_msg) {
});
};
exports.remove_messages = function (message_ids) {
for (const list of [message_list.all, home_msg_list, message_list.narrowed]) {
if (list === undefined) {
continue;
}
list.remove_and_rerender(message_ids);
}
recent_senders.update_topics_of_deleted_message_ids(message_ids);
recent_topics.update_topics_of_deleted_message_ids(message_ids);
};
exports.show_failed_message_success = function (message_id) {
// Previously failed message succeeded
update_message_in_all_views(message_id, (row) => {