filter: Cache value of can_mark_messages_read.

Given that can_mark_messages_read is called whenever the blue box
cursor stops on a message and that it is calculated purely on the
basis of sorted_term_types, it makes sense to cache the result.
This commit is contained in:
YashRE42 2020-03-17 00:45:14 +05:30 committed by Tim Abbott
parent 4d972e1d10
commit 96cd8d3677
2 changed files with 27 additions and 1 deletions

View File

@ -357,6 +357,25 @@ run_test('can_mark_messages_read', () => {
assert(!filter.can_mark_messages_read());
filter = new Filter(in_random_negated);
assert(!filter.can_mark_messages_read());
// test caching of term types
// init and stub
filter = new Filter(pm_with);
filter.stub = filter.calc_can_mark_messages_read;
filter.calc_can_mark_messages_read = function () {
this.calc_can_mark_messages_read_called = true;
return this.stub();
};
// uncached trial
filter.calc_can_mark_messages_read_called = false;
assert(filter.can_mark_messages_read());
assert(filter.calc_can_mark_messages_read_called);
// cached trial
filter.calc_can_mark_messages_read_called = false;
assert(filter.can_mark_messages_read());
assert(!filter.calc_can_mark_messages_read_called);
});
run_test('show_first_unread', () => {

View File

@ -387,7 +387,7 @@ Filter.prototype = {
return this.has_operator('search');
},
can_mark_messages_read: function () {
calc_can_mark_messages_read: function () {
const term_types = this.sorted_term_types();
if (_.isEqual(term_types, ['stream', 'topic'])) {
@ -426,6 +426,13 @@ Filter.prototype = {
return false;
},
can_mark_messages_read: function () {
if (this._can_mark_messages_read === undefined) {
this._can_mark_messages_read = this.calc_can_mark_messages_read();
}
return this._can_mark_messages_read;
},
allow_use_first_unread_when_narrowing: function () {
return this.can_mark_messages_read() || this.has_operator('is');
},