streams: Store list of stream ids instead of names for sorting.

We will store list of stream ids to sort streams instead of names.
We have added a compare_function for sorting the list of stream_ids
by comparing stream names.

This change helps us to remove a couple of get_sub calls and using
stream ids instead of name also helps in avoiding bugs caused due
to live update on renaming of stream.
This commit is contained in:
sahil839
2020-07-09 18:38:01 +05:30
committed by Tim Abbott
parent ed96758614
commit dcc24992b9
3 changed files with 36 additions and 38 deletions

View File

@@ -8,9 +8,22 @@ let all_streams = [];
exports.get_streams = function () {
// Right now this is only used for testing, but we should
// use it for things like hotkeys that cycle through streams.
return all_streams;
const sorted_streams = all_streams.map((stream_id) =>
stream_data.maybe_get_stream_name(stream_id),
);
return sorted_streams;
};
function compare_function(a, b) {
const stream_a = stream_data.get_sub_by_id(a);
const stream_b = stream_data.get_sub_by_id(b);
const stream_name_a = stream_a ? stream_a.name : "";
const stream_name_b = stream_b ? stream_b.name : "";
return util.strcmp(stream_name_a, stream_name_b);
}
function filter_streams_by_search(streams, search_term) {
if (search_term === "") {
return streams;
@@ -21,7 +34,7 @@ function filter_streams_by_search(streams, search_term) {
const filtered_streams = streams.filter((stream) =>
search_terms.some((search_term) => {
const lower_stream_name = stream.toLowerCase();
const lower_stream_name = stream_data.get_sub_by_id(stream).name.toLowerCase();
const cands = lower_stream_name.split(" ");
cands.push(lower_stream_name);
return cands.some((name) => name.startsWith(search_term));
@@ -47,7 +60,7 @@ exports.sort_groups = function (streams, search_term) {
const dormant_streams = [];
for (const stream of streams) {
const sub = stream_data.get_sub(stream);
const sub = stream_data.get_sub_by_id(stream);
const pinned = sub.pin_to_top;
if (pinned) {
pinned_streams.push(stream);
@@ -58,9 +71,9 @@ exports.sort_groups = function (streams, search_term) {
}
}
pinned_streams.sort(util.strcmp);
normal_streams.sort(util.strcmp);
dormant_streams.sort(util.strcmp);
pinned_streams.sort(compare_function);
normal_streams.sort(compare_function);
dormant_streams.sort(compare_function);
const same_as_before =
previous_pinned !== undefined &&
@@ -84,26 +97,12 @@ exports.sort_groups = function (streams, search_term) {
};
};
function pos(stream_id) {
const sub = stream_data.get_sub_by_id(stream_id);
const name = sub.name;
const i = all_streams.indexOf(name);
if (i < 0) {
return;
}
return i;
}
function maybe_get_stream_id(i) {
if (i < 0 || i >= all_streams.length) {
return;
}
const name = all_streams[i];
const stream_id = stream_data.get_stream_id(name);
return stream_id;
return all_streams[i];
}
exports.first_stream_id = function () {
@@ -111,9 +110,9 @@ exports.first_stream_id = function () {
};
exports.prev_stream_id = function (stream_id) {
const i = pos(stream_id);
const i = all_streams.indexOf(stream_id);
if (i === undefined) {
if (i < 0) {
return;
}
@@ -121,9 +120,9 @@ exports.prev_stream_id = function (stream_id) {
};
exports.next_stream_id = function (stream_id) {
const i = pos(stream_id);
const i = all_streams.indexOf(stream_id);
if (i === undefined) {
if (i < 0) {
return;
}