Set initial user presences in O(n), not O(n²) time

Previous code iteratively called set_user_status() once for each user;
which in turn was rerendering the user sidebar, also once for each user.

I changed the API a bit by replacing activity.set_user_status() with
activity.set_user_statuses(), plural, that takes an object and updates
all the user statuses in one go before rendering.

(imported from commit 1111c9029264f892f25e76d2e5e5ff996dcbc7ca)
This commit is contained in:
Scott Feeney
2013-07-11 15:51:26 -04:00
parent 8fcf208815
commit 4313f2ff1e
2 changed files with 20 additions and 10 deletions

View File

@@ -143,11 +143,21 @@ exports.initialize = function () {
focus_ping();
};
exports.set_user_status = function (user_email, presence, server_time) {
if (user_email === page_params.email) {
// Set user statuses. `users` should be an object with user emails as keys
// and presence information (see `status_from_timestamp`) as values.
//
// The object does not need to include every user, only the ones
// whose presence you wish to update.
//
// This rerenders the user sidebar at the end, which can be slow if done too
// often, so try to avoid calling this repeatedly.
exports.set_user_statuses = function (users, server_time) {
$.each(users, function (email, presence) {
if (email === page_params.email) {
return;
}
user_info[user_email] = status_from_timestamp(server_time, presence);
user_info[email] = status_from_timestamp(server_time, presence);
});
update_users();
};

View File

@@ -72,10 +72,8 @@ $(function () {
"full_name": "Humbug Feedback Bot"});
}
$.each(page_params.initial_presences, function (email, presence) {
activity.set_user_status(email, presence, page_params.initial_servertime);
});
activity.set_user_statuses(page_params.initial_presences,
page_params.initial_servertime);
});
function within_viewport(row_offset, row_height) {
@@ -823,7 +821,9 @@ function get_updates_success(data) {
}
break;
case 'presence':
activity.set_user_status(event.email, event.presence, event.server_timestamp);
var users = {};
users[event.email] = event.presence;
activity.set_user_statuses(users, event.server_timestamp);
break;
}
});