@@ -27,6 +27,48 @@ function parseRoomsResponse(response) {
27
27
return response . chunk . map ( ( apiRoom ) => groupRoomFromApiObject ( apiRoom ) ) ;
28
28
}
29
29
30
+ // The number of ongoing group requests
31
+ let ongoingRequestCount = 0 ;
32
+
33
+ // This has arbitrarily been set to a small number to lower the priority
34
+ // of doing group-related requests because we care about other important
35
+ // requests like hitting /sync.
36
+ const LIMIT = 3 ; // Maximum number of ongoing group requests
37
+
38
+ // FIFO queue of functions to call in the backlog
39
+ const backlogQueue = [
40
+ // () => {...}
41
+ ] ;
42
+
43
+ // Pull from the FIFO queue
44
+ function checkBacklog ( ) {
45
+ const item = backlogQueue . shift ( ) ;
46
+ if ( typeof item === 'function' ) item ( ) ;
47
+ }
48
+
49
+ // Limit the maximum number of ongoing promises returned by fn to LIMIT and
50
+ // use a FIFO queue to handle the backlog.
51
+ function limitConcurrency ( fn ) {
52
+ return new Promise ( ( resolve , reject ) => {
53
+ const item = ( ) => {
54
+ ongoingRequestCount ++ ;
55
+ resolve ( ) ;
56
+ } ;
57
+ if ( ongoingRequestCount >= LIMIT ) {
58
+ // Enqueue this request for later execution
59
+ backlogQueue . push ( item ) ;
60
+ } else {
61
+ item ( ) ;
62
+ }
63
+ } )
64
+ . then ( fn )
65
+ . then ( ( result ) => {
66
+ ongoingRequestCount -- ;
67
+ checkBacklog ( ) ;
68
+ return result ;
69
+ } ) ;
70
+ }
71
+
30
72
/**
31
73
* Stores the group summary for a room and provides an API to change it and
32
74
* other useful group APIs that may have an effect on the group summary.
@@ -56,23 +98,24 @@ export default class GroupStore extends EventEmitter {
56
98
this . _fetchResourcePromise = { } ;
57
99
this . _resourceFetcher = {
58
100
[ GroupStore . STATE_KEY . Summary ] : ( ) => {
59
- return MatrixClientPeg . get ( )
60
- . getGroupSummary ( this . groupId ) ;
101
+ return limitConcurrency (
102
+ ( ) => MatrixClientPeg . get ( ) . getGroupSummary ( this . groupId ) ,
103
+ ) ;
61
104
} ,
62
105
[ GroupStore . STATE_KEY . GroupRooms ] : ( ) => {
63
- return MatrixClientPeg . get ( )
64
- . getGroupRooms ( this . groupId )
65
- . then ( parseRoomsResponse ) ;
106
+ return limitConcurrency (
107
+ ( ) => MatrixClientPeg . get ( ) . getGroupRooms ( this . groupId ) . then ( parseRoomsResponse ) ,
108
+ ) ;
66
109
} ,
67
110
[ GroupStore . STATE_KEY . GroupMembers ] : ( ) => {
68
- return MatrixClientPeg . get ( )
69
- . getGroupUsers ( this . groupId )
70
- . then ( parseMembersResponse ) ;
111
+ return limitConcurrency (
112
+ ( ) => MatrixClientPeg . get ( ) . getGroupUsers ( this . groupId ) . then ( parseMembersResponse ) ,
113
+ ) ;
71
114
} ,
72
115
[ GroupStore . STATE_KEY . GroupInvitedMembers ] : ( ) => {
73
- return MatrixClientPeg . get ( )
74
- . getGroupInvitedUsers ( this . groupId )
75
- . then ( parseMembersResponse ) ;
116
+ return limitConcurrency (
117
+ ( ) => MatrixClientPeg . get ( ) . getGroupInvitedUsers ( this . groupId ) . then ( parseMembersResponse ) ,
118
+ ) ;
76
119
} ,
77
120
} ;
78
121
0 commit comments