From 5dfd7f030aa22ba5acda2fde2c8e2f6584574200 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Fri, 22 Dec 2017 06:37:26 -0500 Subject: [PATCH] refactor: Clean up add_subscription_marker(). This change has a few cleanups: * We early-return on last_msg_container === undefined to make the function flatter. * We avoid comparing two boolean values for equality, which can be a landmine if one of the values is `undefined`, which is falsy but not actually `false`. * We extract some local variables for readability. * We make the conditions for subscribe/unsubscribe more explicit. --- static/js/message_list_view.js | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/static/js/message_list_view.js b/static/js/message_list_view.js index da2c349c4f..c25fbb4da6 100644 --- a/static/js/message_list_view.js +++ b/static/js/message_list_view.js @@ -159,18 +159,26 @@ MessageListView.prototype = { add_subscription_marker: function MessageListView__add_subscription_marker( group, last_msg_container, first_msg_container) { - if (last_msg_container !== undefined && - first_msg_container.msg.historical !== last_msg_container.msg.historical) { + if (last_msg_container === undefined) { + return; + } + + var last_subscribed = !last_msg_container.msg.historical; + var first_subscribed = !first_msg_container.msg.historical; + var stream = first_msg_container.msg.stream; + + if (!last_subscribed && first_subscribed) { group.bookend_top = true; - if (first_msg_container.msg.historical) { - group.unsubscribed = first_msg_container.msg.stream; - group.bookend_content = - this.list.unsubscribed_bookend_content(first_msg_container.msg.stream); - } else { - group.subscribed = first_msg_container.msg.stream; - group.bookend_content = - this.list.subscribed_bookend_content(first_msg_container.msg.stream); - } + group.subscribed = stream; + group.bookend_content = this.list.subscribed_bookend_content(stream); + return; + } + + if (last_subscribed && !first_subscribed) { + group.bookend_top = true; + group.unsubscribed = stream; + group.bookend_content = this.list.unsubscribed_bookend_content(stream); + return; } },