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.
This commit is contained in:
Steve Howell 2017-12-22 06:37:26 -05:00 committed by showell
parent f6c41a54b9
commit 5dfd7f030a

View File

@ -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;
}
},