fetchConversation: Minimize scans across loaded messages

FREEBIE
This commit is contained in:
Scott Nonnenberg 2017-05-25 13:25:08 -07:00
parent d5d1d58cc2
commit 3beecce94e

View file

@ -564,7 +564,11 @@
if (typeof unreadCount !== 'number') { if (typeof unreadCount !== 'number') {
unreadCount = 0; unreadCount = 0;
} }
var startingLoadedUnread = this.getLoadedUnreadCount();
var startingLoadedUnread = 0;
if (unreadCount > 0) {
startingLoadedUnread = this.getLoadedUnreadCount();
}
return new Promise(function(resolve) { return new Promise(function(resolve) {
var upper; var upper;
if (this.length === 0) { if (this.length === 0) {
@ -586,12 +590,17 @@
}; };
this.fetch(options).then(resolve); this.fetch(options).then(resolve);
}.bind(this)).then(function() { }.bind(this)).then(function() {
if (unreadCount > 0) {
if (unreadCount <= startingLoadedUnread) {
return;
}
var loadedUnread = this.getLoadedUnreadCount(); var loadedUnread = this.getLoadedUnreadCount();
if (startingLoadedUnread === loadedUnread) { if (startingLoadedUnread === loadedUnread) {
// that fetch didn't get us any more unread. stop fetching more. // that fetch didn't get us any more unread. stop fetching more.
return; return;
} }
if (loadedUnread < unreadCount) {
return this.fetchConversation(conversationId, limit, unreadCount); return this.fetchConversation(conversationId, limit, unreadCount);
} }
}.bind(this)); }.bind(this));