@@ -70,41 +70,54 @@ 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- 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- }
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+ }
8385
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- }
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+ }
9698
97- if ( this . path == 'unread' ) {
98- $ ( '.total_unread_count' ) . html ( ' ' + this . rows . length + ' ' ) ;
99- }
99+ if ( this . path == 'unread' ) {
100+ $ ( '.total_unread_count' ) . html ( ' ' + this . rows . length + ' ' ) ;
101+ }
100102
101- this . sort ( ) ;
102- this . saveToLocalStorage ( ) ;
103+ this . sort ( ) ;
104+ this . saveToLocalStorage ( ) ;
105+
106+ if ( messagesReadyCB ) {
107+ messagesReadyCB ( this ) ;
108+ }
103109
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' ] ) ) ) ;
116+ this . saveToLocalStorage ( ) ;
104117 if ( messagesReadyCB ) {
105118 messagesReadyCB ( this ) ;
106119 }
107- } , this ) ;
120+ } ) ;
108121
109122 return this ;
110123 }
@@ -235,12 +248,13 @@ class Hm_MessagesStore {
235248
236249 let store = this ;
237250 return this . getRequestConfigs ( ) . map ( ( config ) => {
251+ const initialConfig = Object . assign ( [ ] , config ) ;
238252 return new Promise ( ( resolve , reject ) => {
239253 Hm_Ajax . request (
240254 config ,
241255 ( response ) => {
242256 if ( response ) {
243- response . sourceId = store . hashObject ( config ) ;
257+ response . sourceId = store . hashObject ( initialConfig ) ; // Do not use this config object because the request appends a "hm_page_key" entry, which would change the hash
244258 resolve ( response ) ;
245259 }
246260 } ,
0 commit comments