From 5cdce82f7c7cbbb37589e721db6078dcaa69aa05 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Mon, 16 Jul 2018 15:42:20 +0000 Subject: [PATCH] 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. --- frontend_tests/node_tests/buddy_list.js | 1 + static/js/activity.js | 1 - static/js/buddy_list.js | 7 +++---- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/frontend_tests/node_tests/buddy_list.js b/frontend_tests/node_tests/buddy_list.js index f0da6158f0..6dfdcd513a 100644 --- a/frontend_tests/node_tests/buddy_list.js +++ b/frontend_tests/node_tests/buddy_list.js @@ -1,4 +1,5 @@ set_global('$', global.make_zjquery()); +zrequire('buddy_data'); zrequire('buddy_list'); run_test('get_items', () => { diff --git a/static/js/activity.js b/static/js/activity.js index 9e1da12c76..9b07d69828 100644 --- a/static/js/activity.js +++ b/static/js/activity.js @@ -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(); diff --git a/static/js/buddy_list.js b/static/js/buddy_list.js index a5b7ea5c56..42ab40092b 100644 --- a/static/js/buddy_list.js +++ b/static/js/buddy_list.js @@ -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