refactor: Add compare_function to buddy_list.

We were passing this in before, but having it as
a data member reinforces the idea that we'll want
this to be a first-class concept in the list, since
we depend on ordering for various things.
This commit is contained in:
Steve Howell 2018-07-16 15:42:20 +00:00 committed by Tim Abbott
parent 45ab5a2f61
commit 5cdce82f7c
3 changed files with 4 additions and 5 deletions

View File

@ -1,4 +1,5 @@
set_global('$', global.make_zjquery());
zrequire('buddy_data');
zrequire('buddy_list');
run_test('get_items', () => {

View File

@ -218,7 +218,6 @@ exports.insert_user_into_list = function (user_id) {
buddy_list.insert_or_move({
key: user_id,
item: info,
compare_function: buddy_data.compare_function,
});
exports.update_scrollbar.users();

View File

@ -34,6 +34,8 @@ var buddy_list = (function () {
return data;
};
self.compare_function = buddy_data.compare_function;
// Try to keep code below this line generic, so that we can
// extract a widget.
@ -100,13 +102,12 @@ var buddy_list = (function () {
self.find_position = function (opts) {
var key = opts.key;
var compare_function = opts.compare_function;
var i;
for (i = 0; i < self.keys.length; i += 1) {
var list_key = self.keys[i];
if (compare_function(key, list_key) < 0) {
if (self.compare_function(key, list_key) < 0) {
return i;
}
}
@ -130,13 +131,11 @@ var buddy_list = (function () {
self.insert_or_move = function (opts) {
var key = opts.key.toString();
var item = opts.item;
var compare_function = opts.compare_function;
self.maybe_remove_key({key: key});
var pos = self.find_position({
key: key,
compare_function: compare_function,
});
// Order is important here--get the other_key