muting: Fix calling update_unread_counts in a loop.

Previously, set_muted_topics was calling update_unread_counts once for each
topic in the input; this results in poor performance when there is a large
number of muted topics.

Fixes: #3605
This commit is contained in:
Elliott Jin
2017-02-08 01:45:21 -08:00
committed by Tim Abbott
parent fa8045a484
commit 0e0584aeaa
2 changed files with 37 additions and 2 deletions

View File

@@ -55,6 +55,36 @@ var muting = require('js/muting.js');
]);
}());
(function test_muting_performance() {
// This test ensures that each call to mute_topic and set_muted_topics only
// results in one call to unread.update_unread_counts.
// We replace (for the duration of this test) the real update_unread_counts
// with a test version that just counts the number of calls.
var saved = unread.update_unread_counts;
var num_calls = 0;
unread.update_unread_counts = function () {
num_calls += 1;
};
muting.mute_topic('office', 'gossip');
assert.equal(num_calls, 1);
muting.set_muted_topics([
['social', 'breakfast'],
]);
assert.equal(num_calls, 2);
muting.set_muted_topics([
['social', 'breakfast'],
['design', 'typography'],
['devel', 'java'],
]);
assert.equal(num_calls, 3);
unread.update_unread_counts = saved;
}());
(function test_case_insensitivity() {
muting.set_muted_topics([]);
assert(!muting.is_topic_muted('SOCial', 'breakfast'));