Skip to content

Commit 0805360

Browse files
committed
avoid unecessary filtering
1 parent 606e843 commit 0805360

File tree

1 file changed

+27
-40
lines changed

1 file changed

+27
-40
lines changed

modules/core/js_modules/Hm_MessagesStore.js

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -70,50 +70,37 @@ class Hm_MessagesStore {
7070
const sourcesToRemove = Object.keys(this.sources).filter(key => !this.currentlyAvailableSources().includes(key));
7171
sourcesToRemove.forEach(key => delete this.sources[key]);
7272

73-
Promise.all(
74-
this.fetch(hideLoadingState).map((req) => {
75-
return new Promise(async (resolve) => {
76-
const { formatted_message_list: updatedMessages, pages, folder_status, do_not_flag_as_read_on_open, sourceId } = await req;
77-
// count and pages only available in non-combined pages where there is only one ajax call, so it is safe to overwrite
78-
this.count = folder_status && Object.values(folder_status)[0]?.messages;
79-
this.pages = parseInt(pages);
80-
this.newMessages = this.getNewMessages(updatedMessages);
81-
82-
if (typeof do_not_flag_as_read_on_open == 'booelan') {
83-
this.flagAsReadOnOpen = !do_not_flag_as_read_on_open;
84-
}
85-
86-
if (this.sources[sourceId]) {
87-
this.rows = this.rows.filter(row => !this.sources[sourceId].includes(row['1']));
88-
}
89-
this.sources[sourceId] = Object.keys(updatedMessages);
90-
for (const id in updatedMessages) {
91-
if (this.rows.map(row => row['1']).indexOf(id) === -1) {
92-
this.rows.push(updatedMessages[id]);
93-
} else {
94-
const index = this.rows.map(row => row['1']).indexOf(id);
95-
this.rows[index] = updatedMessages[id];
96-
}
97-
}
98-
99-
if (this.path == 'unread') {
100-
$('.total_unread_count').html(' '+this.rows.length+' ');
101-
}
73+
this.fetch(hideLoadingState).forEach(async (req) => {
74+
const { formatted_message_list: updatedMessages, pages, folder_status, do_not_flag_as_read_on_open, sourceId } = await req;
75+
// count and pages only available in non-combined pages where there is only one ajax call, so it is safe to overwrite
76+
this.count = folder_status && Object.values(folder_status)[0]?.messages;
77+
this.pages = parseInt(pages);
78+
this.newMessages = this.getNewMessages(updatedMessages);
79+
80+
if (typeof do_not_flag_as_read_on_open == 'booelan') {
81+
this.flagAsReadOnOpen = !do_not_flag_as_read_on_open;
82+
}
10283

103-
this.sort();
104-
this.saveToLocalStorage();
84+
if (this.sources[sourceId]) {
85+
this.rows = this.rows.filter(row => !this.sources[sourceId].includes(row['1']));
86+
}
87+
this.sources[sourceId] = Object.keys(updatedMessages);
88+
for (const id in updatedMessages) {
89+
if (this.rows.map(row => row['1']).indexOf(id) === -1) {
90+
this.rows.push(updatedMessages[id]);
91+
} else {
92+
const index = this.rows.map(row => row['1']).indexOf(id);
93+
this.rows[index] = updatedMessages[id];
94+
}
95+
}
10596

106-
if (messagesReadyCB) {
107-
messagesReadyCB(this);
108-
}
97+
if (this.path == 'unread') {
98+
$('.total_unread_count').html(' '+this.rows.length+' ');
99+
}
109100

110-
resolve();
111-
});
112-
}, this)
113-
).then(() => {
114-
// remove all existing rows that are not present in any of the fetched data
115-
this.rows = this.rows.filter(row => Object.values(this.sources).some(sourceRows => sourceRows.includes(row['1'])));
101+
this.sort();
116102
this.saveToLocalStorage();
103+
117104
if (messagesReadyCB) {
118105
messagesReadyCB(this);
119106
}

0 commit comments

Comments
 (0)