Clean up startup code for streams.

The startup code in subs.js used to intermingle data
stuff and UI stuff in a loop inside a called function,
which made the code hard to reason about.

Now there is a clear separation of concerns, with these methods
being called in succession:

    stream_data.initialize_from_page_params();
    stream_list.create_initial_sidebar_rows();

The first method was mostly extracted from subs.js, but I simplified
some things, like not needing to make a copy of the hashes
we were passed in, plus I now garbage collect email_dict.  Also,
the code path that initialize_from_page_params() mostly replaces
used to call create_sub(), which fired a trigger, but now it
just does data stuff.

Once the data structure is built up, it's a very simple matter
to build the initial sidebar rows, and that's what the second
method does.
This commit is contained in:
Steve Howell
2016-10-17 10:34:58 -07:00
committed by Tim Abbott
parent d26942e72d
commit 7fd74c45d7
3 changed files with 45 additions and 45 deletions

View File

@@ -263,6 +263,34 @@ exports.get_streams_for_settings_page = function (public_streams) {
return sub_rows;
};
exports.initialize_from_page_params = function () {
function populate_subscriptions(subs, subscribed) {
subs.forEach(function (sub) {
var stream_name = sub.name;
sub.subscribed = subscribed;
// When we get subscriber lists from the back end,
// they are sent as user ids to save bandwidth,
// but the legacy JS code wants emails.
if (sub.subscribers) {
sub.subscribers = _.map(sub.subscribers, function (subscription) {
return page_params.email_dict[subscription];
});
}
exports.create_sub_from_server_data(stream_name, sub);
});
}
populate_subscriptions(page_params.subbed_info, true);
populate_subscriptions(page_params.unsubbed_info, false);
// Garbage collect data structures that were only used for initialization.
delete page_params.subbed_info;
delete page_params.unsubbed_info;
delete page_params.email_dict;
};
return exports;
}());

View File

@@ -46,7 +46,22 @@ function filter_streams_by_search(streams) {
return filtered_streams;
}
exports.create_initial_sidebar_rows = function () {
// This code is slightly opaque, but it ends up building
// up list items and attaching them to the "sub" data
// structures that are kept in stream_data.js.
var subs = stream_data.subscribed_subs();
_.each(subs, function (sub) {
exports.create_sidebar_row(sub);
});
};
exports.build_stream_list = function () {
// This function assumes we have already created the individual
// sidebar rows. Our job here is to build the bigger widget,
// which largely is a matter of arranging the individual rows in
// the right order.
var streams = stream_data.subscribed_streams();
if (streams.length === 0) {
return;

View File

@@ -373,29 +373,6 @@ exports.pin_or_unpin_stream = function (stream_name) {
}
};
function populate_subscriptions(subs, subscribed) {
var sub_rows = [];
subs.sort(function (a, b) {
return util.strcmp(a.name, b.name);
});
subs.forEach(function (elem) {
var stream_name = elem.name;
var sub = create_sub(stream_name, {color: elem.color, in_home_view: elem.in_home_view,
invite_only: elem.invite_only,
desktop_notifications: elem.desktop_notifications,
audible_notifications: elem.audible_notifications,
pin_to_top: elem.pin_to_top,
subscribed: subscribed,
email_address: elem.email_address,
stream_id: elem.stream_id,
subscribers: elem.subscribers,
description: elem.description});
sub_rows.push(sub);
});
return sub_rows;
}
exports.filter_table = function (query) {
var sub_name_elements = $('#subscriptions_table .subscription_name');
@@ -663,31 +640,11 @@ exports.remove_user_from_stream = function (user_email, stream_name, success, fa
});
};
function inline_emails_into_subscriber_list(subs, email_dict) {
// When we get subscriber lists from the back end, they are sent as user ids to
// save bandwidth, but the legacy JS code wants emails.
_.each(subs, function (sub) {
if (sub.subscribers) {
sub.subscribers = _.map(sub.subscribers, function (subscription) {
return email_dict[subscription];
});
}
});
}
$(function () {
var i;
inline_emails_into_subscriber_list(page_params.subbed_info, page_params.email_dict);
inline_emails_into_subscriber_list(page_params.unsubbed_info, page_params.email_dict);
// Populate stream_info with data handed over to client-side template.
populate_subscriptions(page_params.subbed_info, true);
populate_subscriptions(page_params.unsubbed_info, false);
// Garbage collect data structures that were only used for initialization.
delete page_params.subbed_info;
delete page_params.unsubbed_info;
stream_data.initialize_from_page_params();
stream_list.create_initial_sidebar_rows();
// We build the stream_list now. It may get re-built again very shortly
// when new messages come in, but it's fairly quick.