Optimize sort_narrow_list

The optimizations are:
* Sort over the list of subscriptions instead of the DOM li elements.
  This requires storing the li elements for each sub on the sub object.
* Do a bulk insert of the li elements instead of doing them one by one.

(imported from commit 1a987799930fc677e25f0bc2dcf66f83a4ac3163)
This commit is contained in:
Zev Benjamin 2013-05-06 17:36:22 -04:00 committed by Tim Abbott
parent 45ba7c41e7
commit 466beef6fe
1 changed files with 32 additions and 19 deletions

View File

@ -3,37 +3,43 @@ var stream_list = (function () {
var exports = {}; var exports = {};
exports.sort_narrow_list = function () { exports.sort_narrow_list = function () {
var sort_recent = (subs.subscribed_streams().length > 40); var streams = subs.subscribed_streams();
var items = $('#stream_filters > li').get(); if (streams.length === 0) {
var parent = $('#stream_filters'); return;
items.sort(function(a,b){ }
var a_stream_name = $(a).attr('data-name');
var b_stream_name = $(b).attr('data-name'); var sort_recent = (streams.length > 40);
streams.sort(function(a, b) {
if (sort_recent) { if (sort_recent) {
if (recent_subjects[b_stream_name] !== undefined && if (recent_subjects[b] !== undefined &&
recent_subjects[a_stream_name] === undefined) { recent_subjects[a] === undefined) {
return 1; return 1;
} else if (recent_subjects[b_stream_name] === undefined && } else if (recent_subjects[b] === undefined &&
recent_subjects[a_stream_name] !== undefined) { recent_subjects[a] !== undefined) {
return -1; return -1;
} }
} }
return util.strcmp(a_stream_name, b_stream_name); return util.strcmp(a, b);
}); });
var parent = $('#stream_filters');
parent.empty(); parent.empty();
$.each(items, function(i, li){ var elems = [];
var stream_name = $(li).attr('data-name'); $.each(streams, function(i, stream) {
// TODO: we should export the sub objects better
var li = $(subs.have(stream).sidebar_li);
if (sort_recent) { if (sort_recent) {
if (recent_subjects[stream_name] === undefined) { if (recent_subjects[stream] === undefined) {
$(li).addClass("inactive_stream"); li.addClass('inactive_stream');
} else { } else {
$(li).removeClass("inactive_stream"); li.removeClass('inactive_stream');
} }
} }
parent.append(li); elems.push(li.get(0));
}); });
$(elems).appendTo(parent);
}; };
function iterate_to_find(selector, data_name, context) { function iterate_to_find(selector, data_name, context) {
@ -88,6 +94,7 @@ function add_narrow_filter(name, type) {
list_item.append("<i class='icon-lock'/>"); list_item.append("<i class='icon-lock'/>");
} }
$("#" + type + "_filters").append(list_item); $("#" + type + "_filters").append(list_item);
return list_item;
} }
exports.get_count = function (type, name) { exports.get_count = function (type, name) {
@ -192,13 +199,19 @@ $(function () {
$(document).on('sub_obj_created.zephyr', function (event) { $(document).on('sub_obj_created.zephyr', function (event) {
if (event.sub.subscribed) { if (event.sub.subscribed) {
var stream_name = event.sub.name; var stream_name = event.sub.name;
add_narrow_filter(stream_name, "stream"); var li = add_narrow_filter(stream_name, "stream");
if (li) {
event.sub.sidebar_li = li;
}
} }
}); });
$(document).on('subscription_add_done.zephyr', function (event) { $(document).on('subscription_add_done.zephyr', function (event) {
var stream_name = event.sub.name; var stream_name = event.sub.name;
add_narrow_filter(stream_name, "stream"); var li = add_narrow_filter(stream_name, "stream");
if (li) {
event.sub.sidebar_li = li;
}
exports.sort_narrow_list(); exports.sort_narrow_list();
}); });