unread: Refactor to move DOM element updates into UI layer.

This commit is contained in:
Elliott Jin 2017-02-11 00:55:18 -08:00 committed by showell
parent ba449d7c23
commit 4092aab620
14 changed files with 110 additions and 110 deletions

View File

@ -717,7 +717,7 @@ run(function (override, capture, args) {
// update_message_flags__read
var event = event_fixtures.update_message_flags__read;
override('message_store', 'get', capture(['message_id']));
override('unread', 'mark_messages_as_read', noop);
override('unread_ui', 'mark_messages_as_read', noop);
dispatch(event);
assert_same(args.message_id, 999);
});

View File

@ -72,7 +72,7 @@ $(function () {
// lists.
var message = ui.find_message(message_id);
unread.mark_message_as_read(message);
unread_ui.mark_message_as_read(message);
ui.update_starred(message.id, message.starred !== true);
message_flags.send_starred([message], message.starred);
}
@ -429,7 +429,7 @@ $(function () {
// BANKRUPTCY
$(".bankruptcy_button").click(function () {
unread.enable();
unread_ui.enable();
});
(function () {

View File

@ -626,7 +626,7 @@ exports.respond_to_message = function (opts) {
return;
}
unread.mark_message_as_read(message);
unread_ui.mark_message_as_read(message);
var stream = '';
var subject = '';

View File

@ -224,7 +224,7 @@ function maybe_add_narrowed_messages(messages, msg_list, messages_are_new, local
new_messages = _.map(new_messages, add_message_metadata);
exports.add_messages(new_messages, msg_list, {messages_are_new: messages_are_new});
unread.process_visible();
unread_ui.process_visible();
notifications.possibly_notify_new_messages_outside_viewport(new_messages, local_id);
notifications.notify_messages_outside_current_search(elsewhere_messages);
},
@ -352,7 +352,7 @@ exports.update_messages = function update_messages(events) {
message_list.narrowed.view.rerender_messages(msgs_to_rerender);
}
}
unread.update_unread_counts();
unread_ui.update_unread_counts();
stream_list.update_streams_sidebar();
pm_list.update_private_messages();
};
@ -361,7 +361,7 @@ exports.update_messages = function update_messages(events) {
// This function could probably benefit from some refactoring
exports.do_unread_count_updates = function do_unread_count_updates(messages) {
unread.process_loaded_messages(messages);
unread.update_unread_counts();
unread_ui.update_unread_counts();
resize.resize_page_components();
};
@ -411,7 +411,7 @@ exports.insert_new_messages = function insert_new_messages(messages, local_id) {
}
}
unread.process_visible();
unread_ui.process_visible();
notifications.received_messages(messages);
stream_list.update_streams_sidebar();
pm_list.update_private_messages();

View File

@ -116,17 +116,17 @@ exports.handle_updates = function (muted_topics) {
exports.mute_topic = function (stream, topic) {
muting.add_muted_topic(stream, topic);
unread.update_unread_counts();
unread_ui.update_unread_counts();
};
exports.unmute_topic = function (stream, topic) {
muting.remove_muted_topic(stream, topic);
unread.update_unread_counts();
unread_ui.update_unread_counts();
};
exports.update_muted_topics = function (muted_topics) {
muting.set_muted_topics(muted_topics);
unread.update_unread_counts();
unread_ui.update_unread_counts();
};
$(function () {

View File

@ -381,7 +381,7 @@ exports.by_subject = function (target_id, opts) {
exports.by_recipient(target_id, opts);
return;
}
unread.mark_message_as_read(original);
unread_ui.mark_message_as_read(original);
var search_terms = [
{operator: 'stream', operand: original.stream},
{operator: 'topic', operand: original.subject},
@ -395,7 +395,7 @@ exports.by_recipient = function (target_id, opts) {
opts = _.defaults({}, opts, {then_select_id: target_id});
// don't use current_msg_list as it won't work for muted messages or for out-of-narrow links
var message = message_store.get(target_id);
unread.mark_message_as_read(message);
unread_ui.mark_message_as_read(message);
switch (message.type) {
case 'private':
exports.by('pm-with', message.reply_to, opts);

View File

@ -31,7 +31,7 @@ exports.down = function (with_centering) {
// always returns a message.
var current_msg_table = rows.get_table(current_msg_list.table_name);
viewport.scrollTop(current_msg_table.outerHeight(true) - viewport.height() * 0.1);
unread.mark_current_list_as_read();
unread_ui.mark_current_list_as_read();
}
};
@ -48,7 +48,7 @@ exports.to_end = function () {
viewport.last_movement_direction = 1;
current_msg_list.select_id(next_id, {then_scroll: true,
from_scroll: true});
unread.mark_current_list_as_read();
unread_ui.mark_current_list_as_read();
};
exports.page_up = function () {
@ -62,7 +62,7 @@ exports.page_up = function () {
exports.page_down = function () {
if (viewport.at_bottom() && !current_msg_list.empty()) {
current_msg_list.select_id(current_msg_list.last().id, {then_scroll: false});
unread.mark_current_list_as_read();
unread_ui.mark_current_list_as_read();
} else {
ui.page_down_the_right_amount();
}

View File

@ -69,7 +69,7 @@ exports.initialize = function () {
// Update many places on the DOM to reflect unread
// counts.
unread.process_visible();
unread_ui.process_visible();
}).blur(function () {
window_has_focus = false;

View File

@ -183,7 +183,7 @@ exports.initialize = function initialize() {
} else {
messages = event.msg_list.message_range(event.previously_selected, event.id);
}
unread.mark_messages_as_read(messages, {from: 'pointer'});
unread_ui.mark_messages_as_read(messages, {from: 'pointer'});
}
});
};

View File

@ -231,7 +231,7 @@ function dispatch_normal_event(event) {
var msgs_to_update = _.map(event.messages, function (message_id) {
return message_store.get(message_id);
});
unread.mark_messages_as_read(msgs_to_update, {from: "server"});
unread_ui.mark_messages_as_read(msgs_to_update, {from: "server"});
break;
}
break;

View File

@ -439,7 +439,7 @@ $(function () {
stream_li.addClass('active-filter');
}
rebuild_recent_topics(op_stream[0]);
unread.process_visible();
unread_ui.process_visible();
}
});

View File

@ -215,7 +215,7 @@ exports.update_starred = function (message_id, starred) {
return;
}
unread.mark_message_as_read(message);
unread_ui.mark_message_as_read(message);
message.starred = starred;
@ -614,9 +614,9 @@ function scroll_finished() {
}
// When the window scrolls, it may cause some messages to
// enter the screen and become read. Calling
// unread.process_visible will update necessary
// unread_ui.process_visible will update necessary
// data structures and DOM elements.
setTimeout(unread.process_visible, 0);
setTimeout(unread_ui.process_visible, 0);
}
}
@ -633,7 +633,7 @@ var saved_compose_cursor = 0;
$(function () {
viewport.message_pane.scroll($.throttle(50, function () {
unread.process_visible();
unread_ui.process_visible();
scroll_finish();
}));

View File

@ -264,89 +264,6 @@ exports.num_unread_for_person = function (user_ids_string) {
return unread_privates.get(user_ids_string).num_items();
};
exports.update_unread_counts = function () {
if (exports.suppress_unread_counts) {
return;
}
// Pure computation:
var res = unread.get_counts();
// Side effects from here down:
// This updates some DOM elements directly, so try to
// avoid excessive calls to this.
activity.update_dom_with_unread_counts(res);
stream_list.update_dom_with_unread_counts(res);
pm_list.update_dom_with_unread_counts(res);
notifications.update_title_count(res.home_unread_messages);
notifications.update_pm_count(res.private_message_count);
};
exports.enable = function enable() {
exports.suppress_unread_counts = false;
exports.update_unread_counts();
};
// Takes a list of messages and marks them as read
exports.mark_messages_as_read = function mark_messages_as_read(messages, options) {
options = options || {};
var processed = false;
_.each(messages, function (message) {
if (!unread.message_unread(message)) {
// Don't do anything if the message is already read.
return;
}
if (current_msg_list === message_list.narrowed) {
unread.messages_read_in_narrow = true;
}
if (options.from !== "server") {
message_flags.send_read(message);
}
message.flags = message.flags || [];
message.flags.push('read');
message.unread = false;
unread.process_read_message(message, options);
home_msg_list.show_message_as_read(message, options);
message_list.all.show_message_as_read(message, options);
if (message_list.narrowed) {
message_list.narrowed.show_message_as_read(message, options);
}
notifications.close_notification(message);
processed = true;
});
if (processed) {
exports.update_unread_counts();
}
};
exports.mark_message_as_read = function mark_message_as_read(message, options) {
exports.mark_messages_as_read([message], options);
};
// If we ever materially change the algorithm for this function, we
// may need to update notifications.received_messages as well.
exports.process_visible = function process_visible() {
if (! notifications.window_has_focus()) {
return;
}
if (feature_flags.mark_read_at_bottom) {
if (viewport.bottom_message_visible()) {
exports.mark_current_list_as_read();
}
} else {
exports.mark_messages_as_read(viewport.visible_messages(true));
}
};
exports.mark_current_list_as_read = function mark_current_list_as_read(options) {
exports.mark_messages_as_read(current_msg_list.all_messages(), options);
};
return exports;
}());

View File

@ -46,13 +46,36 @@ exports.set_count_toggle_button = function (elem, count) {
return elem.text("1k+");
};
exports.update_unread_counts = function () {
if (unread.suppress_unread_counts) {
return;
}
// Pure computation:
var res = unread.get_counts();
// Side effects from here down:
// This updates some DOM elements directly, so try to
// avoid excessive calls to this.
activity.update_dom_with_unread_counts(res);
stream_list.update_dom_with_unread_counts(res);
pm_list.update_dom_with_unread_counts(res);
notifications.update_title_count(res.home_unread_messages);
notifications.update_pm_count(res.private_message_count);
};
exports.enable = function enable() {
unread.suppress_unread_counts = false;
exports.update_unread_counts();
};
exports.mark_all_as_read = function mark_all_as_read(cont) {
_.each(message_list.all.all_messages(), function (msg) {
msg.flags = msg.flags || [];
msg.flags.push('read');
});
unread.declare_bankruptcy();
unread.update_unread_counts();
exports.update_unread_counts();
channel.post({
url: '/json/messages/flags',
@ -64,6 +87,66 @@ exports.mark_all_as_read = function mark_all_as_read(cont) {
success: cont});
};
// Takes a list of messages and marks them as read
exports.mark_messages_as_read = function mark_messages_as_read(messages, options) {
options = options || {};
var processed = false;
_.each(messages, function (message) {
if (!unread.message_unread(message)) {
// Don't do anything if the message is already read.
return;
}
if (current_msg_list === message_list.narrowed) {
unread.messages_read_in_narrow = true;
}
if (options.from !== "server") {
message_flags.send_read(message);
}
message.flags = message.flags || [];
message.flags.push('read');
message.unread = false;
unread.process_read_message(message, options);
home_msg_list.show_message_as_read(message, options);
message_list.all.show_message_as_read(message, options);
if (message_list.narrowed) {
message_list.narrowed.show_message_as_read(message, options);
}
notifications.close_notification(message);
processed = true;
});
if (processed) {
exports.update_unread_counts();
}
};
exports.mark_message_as_read = function mark_message_as_read(message, options) {
exports.mark_messages_as_read([message], options);
};
// If we ever materially change the algorithm for this function, we
// may need to update notifications.received_messages as well.
exports.process_visible = function process_visible() {
if (! notifications.window_has_focus()) {
return;
}
if (feature_flags.mark_read_at_bottom) {
if (viewport.bottom_message_visible()) {
exports.mark_current_list_as_read();
}
} else {
exports.mark_messages_as_read(viewport.visible_messages(true));
}
};
exports.mark_current_list_as_read = function mark_current_list_as_read(options) {
exports.mark_messages_as_read(current_msg_list.all_messages(), options);
};
exports.mark_stream_as_read = function mark_stream_as_read(stream, cont) {
channel.post({
url: '/json/messages/flags',
@ -99,7 +182,7 @@ function consider_bankruptcy() {
if (!page_params.furthest_read_time) {
// We've never read a message.
unread.enable();
exports.enable();
return;
}
@ -111,7 +194,7 @@ function consider_bankruptcy() {
$('#bankruptcy-unread-count').html(unread_info);
$('#bankruptcy').modal('show');
} else {
unread.enable();
exports.enable();
}
}