@@ -119,12 +119,10 @@ export class SlidingSyncManager {
119
119
120
120
public slidingSync : SlidingSync ;
121
121
private client : MatrixClient ;
122
- private listIdToIndex : Record < string , number > ;
123
122
124
123
private configureDefer : IDeferred < void > ;
125
124
126
125
public constructor ( ) {
127
- this . listIdToIndex = { } ;
128
126
this . configureDefer = defer < void > ( ) ;
129
127
}
130
128
@@ -134,13 +132,18 @@ export class SlidingSyncManager {
134
132
135
133
public configure ( client : MatrixClient , proxyUrl : string ) : SlidingSync {
136
134
this . client = client ;
137
- this . listIdToIndex = { } ;
138
135
// by default use the encrypted subscription as that gets everything, which is a safer
139
136
// default than potentially missing member events.
140
- this . slidingSync = new SlidingSync ( proxyUrl , [ ] , ENCRYPTED_SUBSCRIPTION , client , SLIDING_SYNC_TIMEOUT_MS ) ;
137
+ this . slidingSync = new SlidingSync (
138
+ proxyUrl ,
139
+ new Map ( ) ,
140
+ ENCRYPTED_SUBSCRIPTION ,
141
+ client ,
142
+ SLIDING_SYNC_TIMEOUT_MS ,
143
+ ) ;
141
144
this . slidingSync . addCustomSubscription ( UNENCRYPTED_SUBSCRIPTION_NAME , UNENCRYPTED_SUBSCRIPTION ) ;
142
145
// set the space list
143
- this . slidingSync . setList ( this . getOrAllocateListIndex ( SlidingSyncManager . ListSpaces ) , {
146
+ this . slidingSync . setList ( SlidingSyncManager . ListSpaces , {
144
147
ranges : [ [ 0 , 20 ] ] ,
145
148
sort : [ "by_name" ] ,
146
149
slow_get_all_rooms : true ,
@@ -173,47 +176,16 @@ export class SlidingSyncManager {
173
176
return this . slidingSync ;
174
177
}
175
178
176
- public listIdForIndex ( index : number ) : string | null {
177
- for ( const listId in this . listIdToIndex ) {
178
- if ( this . listIdToIndex [ listId ] === index ) {
179
- return listId ;
180
- }
181
- }
182
- return null ;
183
- }
184
-
185
- /**
186
- * Allocate or retrieve the list index for an arbitrary list ID. For example SlidingSyncManager.ListSpaces
187
- * @param listId A string which represents the list.
188
- * @returns The index to use when registering lists or listening for callbacks.
189
- */
190
- public getOrAllocateListIndex ( listId : string ) : number {
191
- let index = this . listIdToIndex [ listId ] ;
192
- if ( index === undefined ) {
193
- // assign next highest index
194
- index = - 1 ;
195
- for ( const id in this . listIdToIndex ) {
196
- const listIndex = this . listIdToIndex [ id ] ;
197
- if ( listIndex > index ) {
198
- index = listIndex ;
199
- }
200
- }
201
- index ++ ;
202
- this . listIdToIndex [ listId ] = index ;
203
- }
204
- return index ;
205
- }
206
-
207
179
/**
208
180
* Ensure that this list is registered.
209
- * @param listIndex The list index to register
181
+ * @param listKey The list key to register
210
182
* @param updateArgs The fields to update on the list.
211
183
* @returns The complete list request params
212
184
*/
213
- public async ensureListRegistered ( listIndex : number , updateArgs : PartialSlidingSyncRequest ) : Promise < MSC3575List > {
214
- logger . debug ( "ensureListRegistered:::" , listIndex , updateArgs ) ;
185
+ public async ensureListRegistered ( listKey : string , updateArgs : PartialSlidingSyncRequest ) : Promise < MSC3575List > {
186
+ logger . debug ( "ensureListRegistered:::" , listKey , updateArgs ) ;
215
187
await this . configureDefer . promise ;
216
- let list = this . slidingSync . getList ( listIndex ) ;
188
+ let list = this . slidingSync . getListParams ( listKey ) ;
217
189
if ( ! list ) {
218
190
list = {
219
191
ranges : [ [ 0 , 20 ] ] ,
@@ -252,14 +224,14 @@ export class SlidingSyncManager {
252
224
try {
253
225
// if we only have range changes then call a different function so we don't nuke the list from before
254
226
if ( updateArgs . ranges && Object . keys ( updateArgs ) . length === 1 ) {
255
- await this . slidingSync . setListRanges ( listIndex , updateArgs . ranges ) ;
227
+ await this . slidingSync . setListRanges ( listKey , updateArgs . ranges ) ;
256
228
} else {
257
- await this . slidingSync . setList ( listIndex , list ) ;
229
+ await this . slidingSync . setList ( listKey , list ) ;
258
230
}
259
231
} catch ( err ) {
260
232
logger . debug ( "ensureListRegistered: update failed txn_id=" , err ) ;
261
233
}
262
- return this . slidingSync . getList ( listIndex ) ;
234
+ return this . slidingSync . getListParams ( listKey ) ! ;
263
235
}
264
236
265
237
public async setRoomVisible ( roomId : string , visible : boolean ) : Promise < string > {
@@ -304,7 +276,6 @@ export class SlidingSyncManager {
304
276
*/
305
277
public async startSpidering ( batchSize : number , gapBetweenRequestsMs : number ) : Promise < void > {
306
278
await sleep ( gapBetweenRequestsMs ) ; // wait a bit as this is called on first render so let's let things load
307
- const listIndex = this . getOrAllocateListIndex ( SlidingSyncManager . ListSearch ) ;
308
279
let startIndex = batchSize ;
309
280
let hasMore = true ;
310
281
let firstTime = true ;
@@ -316,7 +287,7 @@ export class SlidingSyncManager {
316
287
[ startIndex , endIndex ] ,
317
288
] ;
318
289
if ( firstTime ) {
319
- await this . slidingSync . setList ( listIndex , {
290
+ await this . slidingSync . setList ( SlidingSyncManager . ListSearch , {
320
291
// e.g [0,19] [20,39] then [0,19] [40,59]. We keep [0,20] constantly to ensure
321
292
// any changes to the list whilst spidering are caught.
322
293
ranges : ranges ,
@@ -342,15 +313,17 @@ export class SlidingSyncManager {
342
313
} ,
343
314
} ) ;
344
315
} else {
345
- await this . slidingSync . setListRanges ( listIndex , ranges ) ;
316
+ await this . slidingSync . setListRanges ( SlidingSyncManager . ListSearch , ranges ) ;
346
317
}
347
- // gradually request more over time
348
- await sleep ( gapBetweenRequestsMs ) ;
349
318
} catch ( err ) {
350
319
// do nothing, as we reject only when we get interrupted but that's fine as the next
351
320
// request will include our data
321
+ } finally {
322
+ // gradually request more over time, even on errors.
323
+ await sleep ( gapBetweenRequestsMs ) ;
352
324
}
353
- hasMore = endIndex + 1 < this . slidingSync . getListData ( listIndex ) ?. joinedCount ;
325
+ const listData = this . slidingSync . getListData ( SlidingSyncManager . ListSearch ) ! ;
326
+ hasMore = endIndex + 1 < listData . joinedCount ;
354
327
startIndex += batchSize ;
355
328
firstTime = false ;
356
329
}
0 commit comments