mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	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:
		@@ -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() {
 | 
					(function test_case_insensitivity() {
 | 
				
			||||||
    muting.set_muted_topics([]);
 | 
					    muting.set_muted_topics([]);
 | 
				
			||||||
    assert(!muting.is_topic_muted('SOCial', 'breakfast'));
 | 
					    assert(!muting.is_topic_muted('SOCial', 'breakfast'));
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -4,13 +4,17 @@ var exports = {};
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
var muted_topics = new Dict({fold_case: true});
 | 
					var muted_topics = new Dict({fold_case: true});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
exports.mute_topic = function (stream, topic) {
 | 
					function set_muted_topic(stream, topic) {
 | 
				
			||||||
    var sub_dict = muted_topics.get(stream);
 | 
					    var sub_dict = muted_topics.get(stream);
 | 
				
			||||||
    if (!sub_dict) {
 | 
					    if (!sub_dict) {
 | 
				
			||||||
        sub_dict = new Dict({fold_case: true});
 | 
					        sub_dict = new Dict({fold_case: true});
 | 
				
			||||||
        muted_topics.set(stream, sub_dict);
 | 
					        muted_topics.set(stream, sub_dict);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    sub_dict.set(topic, true);
 | 
					    sub_dict.set(topic, true);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					exports.mute_topic = function (stream, topic) {
 | 
				
			||||||
 | 
					    set_muted_topic(stream, topic);
 | 
				
			||||||
    unread.update_unread_counts();
 | 
					    unread.update_unread_counts();
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -47,8 +51,9 @@ exports.set_muted_topics = function (tuples) {
 | 
				
			|||||||
        var stream = tuple[0];
 | 
					        var stream = tuple[0];
 | 
				
			||||||
        var topic = tuple[1];
 | 
					        var topic = tuple[1];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        exports.mute_topic(stream, topic);
 | 
					        set_muted_topic(stream, topic);
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					    unread.update_unread_counts();
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
return exports;
 | 
					return exports;
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user