summaryrefslogtreecommitdiff
path: root/includes/external/matrix/node_modules/matrix-js-sdk/lib/sliding-sync.js
blob: c94f166e5f4bb946f000b9bcab368d8d40cf446d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.SlidingSyncState = exports.SlidingSyncEvent = exports.SlidingSync = exports.MSC3575_WILDCARD = exports.MSC3575_STATE_KEY_ME = exports.MSC3575_STATE_KEY_LAZY = exports.ExtensionState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _logger = require("./logger");
var _typedEventEmitter = require("./models/typed-event-emitter");
var _utils = require("./utils");
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
// /sync requests allow you to set a timeout= but the request may continue
// beyond that and wedge forever, so we need to track how long we are willing
// to keep open the connection. This constant is *ADDED* to the timeout= value
// to determine the max time we're willing to wait.
const BUFFER_PERIOD_MS = 10 * 1000;
const MSC3575_WILDCARD = "*";
exports.MSC3575_WILDCARD = MSC3575_WILDCARD;
const MSC3575_STATE_KEY_ME = "$ME";
exports.MSC3575_STATE_KEY_ME = MSC3575_STATE_KEY_ME;
const MSC3575_STATE_KEY_LAZY = "$LAZY";

/**
 * Represents a subscription to a room or set of rooms. Controls which events are returned.
 */
exports.MSC3575_STATE_KEY_LAZY = MSC3575_STATE_KEY_LAZY;
let SlidingSyncState;
/**
 * Internal Class. SlidingList represents a single list in sliding sync. The list can have filters,
 * multiple sliding windows, and maintains the index-\>room_id mapping.
 */
exports.SlidingSyncState = SlidingSyncState;
(function (SlidingSyncState) {
  SlidingSyncState["RequestFinished"] = "FINISHED";
  SlidingSyncState["Complete"] = "COMPLETE";
})(SlidingSyncState || (exports.SlidingSyncState = SlidingSyncState = {}));
class SlidingList {
  // returned data

  /**
   * Construct a new sliding list.
   * @param list - The range, sort and filter values to use for this list.
   */
  constructor(list) {
    (0, _defineProperty2.default)(this, "list", void 0);
    (0, _defineProperty2.default)(this, "isModified", void 0);
    (0, _defineProperty2.default)(this, "roomIndexToRoomId", {});
    (0, _defineProperty2.default)(this, "joinedCount", 0);
    this.replaceList(list);
  }

  /**
   * Mark this list as modified or not. Modified lists will return sticky params with calls to getList.
   * This is useful for the first time the list is sent, or if the list has changed in some way.
   * @param modified - True to mark this list as modified so all sticky parameters will be re-sent.
   */
  setModified(modified) {
    this.isModified = modified;
  }

  /**
   * Update the list range for this list. Does not affect modified status as list ranges are non-sticky.
   * @param newRanges - The new ranges for the list
   */
  updateListRange(newRanges) {
    this.list.ranges = JSON.parse(JSON.stringify(newRanges));
  }

  /**
   * Replace list parameters. All fields will be replaced with the new list parameters.
   * @param list - The new list parameters
   */
  replaceList(list) {
    list.filters = list.filters || {};
    list.ranges = list.ranges || [];
    this.list = JSON.parse(JSON.stringify(list));
    this.isModified = true;

    // reset values as the join count may be very different (if filters changed) including the rooms
    // (e.g. sort orders or sliding window ranges changed)

    // the constantly changing sliding window ranges. Not an array for performance reasons
    // E.g. tracking ranges 0-99, 500-599, we don't want to have a 600 element array
    this.roomIndexToRoomId = {};
    // the total number of joined rooms according to the server, always >= len(roomIndexToRoomId)
    this.joinedCount = 0;
  }

  /**
   * Return a copy of the list suitable for a request body.
   * @param forceIncludeAllParams - True to forcibly include all params even if the list
   * hasn't been modified. Callers may want to do this if they are modifying the list prior to calling
   * updateList.
   */
  getList(forceIncludeAllParams) {
    let list = {
      ranges: JSON.parse(JSON.stringify(this.list.ranges))
    };
    if (this.isModified || forceIncludeAllParams) {
      list = JSON.parse(JSON.stringify(this.list));
    }
    return list;
  }

  /**
   * Check if a given index is within the list range. This is required even though the /sync API
   * provides explicit updates with index positions because of the following situation:
   *   0 1 2 3 4 5 6 7 8   indexes
   *   a b c       d e f   COMMANDS: SYNC 0 2 a b c; SYNC 6 8 d e f;
   *   a b c       d _ f   COMMAND: DELETE 7;
   *   e a b c       d f   COMMAND: INSERT 0 e;
   *   c=3 is wrong as we are not tracking it, ergo we need to see if `i` is in range else drop it
   * @param i - The index to check
   * @returns True if the index is within a sliding window
   */
  isIndexInRange(i) {
    for (const r of this.list.ranges) {
      if (r[0] <= i && i <= r[1]) {
        return true;
      }
    }
    return false;
  }
}

/**
 * When onResponse extensions should be invoked: before or after processing the main response.
 */
let ExtensionState;
/**
 * An interface that must be satisfied to register extensions
 */
exports.ExtensionState = ExtensionState;
(function (ExtensionState) {
  ExtensionState["PreProcess"] = "ExtState.PreProcess";
  ExtensionState["PostProcess"] = "ExtState.PostProcess";
})(ExtensionState || (exports.ExtensionState = ExtensionState = {}));
/**
 * Events which can be fired by the SlidingSync class. These are designed to provide different levels
 * of information when processing sync responses.
 *  - RoomData: concerns rooms, useful for SlidingSyncSdk to update its knowledge of rooms.
 *  - Lifecycle: concerns callbacks at various well-defined points in the sync process.
 *  - List: concerns lists, useful for UI layers to re-render room lists.
 * Specifically, the order of event invocation is:
 *  - Lifecycle (state=RequestFinished)
 *  - RoomData (N times)
 *  - Lifecycle (state=Complete)
 *  - List (at most once per list)
 */
let SlidingSyncEvent;
exports.SlidingSyncEvent = SlidingSyncEvent;
(function (SlidingSyncEvent) {
  SlidingSyncEvent["RoomData"] = "SlidingSync.RoomData";
  SlidingSyncEvent["Lifecycle"] = "SlidingSync.Lifecycle";
  SlidingSyncEvent["List"] = "SlidingSync.List";
})(SlidingSyncEvent || (exports.SlidingSyncEvent = SlidingSyncEvent = {}));
/**
 * SlidingSync is a high-level data structure which controls the majority of sliding sync.
 * It has no hooks into JS SDK except for needing a MatrixClient to perform the HTTP request.
 * This means this class (and everything it uses) can be used in isolation from JS SDK if needed.
 * To hook this up with the JS SDK, you need to use SlidingSyncSdk.
 */
class SlidingSync extends _typedEventEmitter.TypedEventEmitter {
  // flag set when resend() is called because we cannot rely on detecting AbortError in JS SDK :(

  // the txn_id to send with the next request.

  // a list (in chronological order of when they were sent) of objects containing the txn ID and
  // a defer to resolve/reject depending on whether they were successfully sent or not.

  // map of extension name to req/resp handler

  // the *desired* room subscriptions

  // map of custom subscription name to the subscription

  // map of room ID to custom subscription name

  /**
   * Create a new sliding sync instance
   * @param proxyBaseUrl - The base URL of the sliding sync proxy
   * @param lists - The lists to use for sliding sync.
   * @param roomSubscriptionInfo - The params to use for room subscriptions.
   * @param client - The client to use for /sync calls.
   * @param timeoutMS - The number of milliseconds to wait for a response.
   */
  constructor(proxyBaseUrl, lists, roomSubscriptionInfo, client, timeoutMS) {
    super();
    this.proxyBaseUrl = proxyBaseUrl;
    this.roomSubscriptionInfo = roomSubscriptionInfo;
    this.client = client;
    this.timeoutMS = timeoutMS;
    (0, _defineProperty2.default)(this, "lists", void 0);
    (0, _defineProperty2.default)(this, "listModifiedCount", 0);
    (0, _defineProperty2.default)(this, "terminated", false);
    (0, _defineProperty2.default)(this, "needsResend", false);
    (0, _defineProperty2.default)(this, "txnId", null);
    (0, _defineProperty2.default)(this, "txnIdDefers", []);
    (0, _defineProperty2.default)(this, "extensions", {});
    (0, _defineProperty2.default)(this, "desiredRoomSubscriptions", new Set());
    (0, _defineProperty2.default)(this, "confirmedRoomSubscriptions", new Set());
    (0, _defineProperty2.default)(this, "customSubscriptions", new Map());
    (0, _defineProperty2.default)(this, "roomIdToCustomSubscription", new Map());
    (0, _defineProperty2.default)(this, "pendingReq", void 0);
    (0, _defineProperty2.default)(this, "abortController", void 0);
    this.lists = new Map();
    lists.forEach((list, key) => {
      this.lists.set(key, new SlidingList(list));
    });
  }

  /**
   * Add a custom room subscription, referred to by an arbitrary name. If a subscription with this
   * name already exists, it is replaced. No requests are sent by calling this method.
   * @param name - The name of the subscription. Only used to reference this subscription in
   * useCustomSubscription.
   * @param sub - The subscription information.
   */
  addCustomSubscription(name, sub) {
    if (this.customSubscriptions.has(name)) {
      _logger.logger.warn(`addCustomSubscription: ${name} already exists as a custom subscription, ignoring.`);
      return;
    }
    this.customSubscriptions.set(name, sub);
  }

  /**
   * Use a custom subscription previously added via addCustomSubscription. No requests are sent
   * by calling this method. Use modifyRoomSubscriptions to resend subscription information.
   * @param roomId - The room to use the subscription in.
   * @param name - The name of the subscription. If this name is unknown, the default subscription
   * will be used.
   */
  useCustomSubscription(roomId, name) {
    // We already know about this custom subscription, as it is immutable,
    // we don't need to unconfirm the subscription.
    if (this.roomIdToCustomSubscription.get(roomId) === name) {
      return;
    }
    this.roomIdToCustomSubscription.set(roomId, name);
    // unconfirm this subscription so a resend() will send it up afresh.
    this.confirmedRoomSubscriptions.delete(roomId);
  }

  /**
   * Get the room index data for a list.
   * @param key - The list key
   * @returns The list data which contains the rooms in this list
   */
  getListData(key) {
    const data = this.lists.get(key);
    if (!data) {
      return null;
    }
    return {
      joinedCount: data.joinedCount,
      roomIndexToRoomId: Object.assign({}, data.roomIndexToRoomId)
    };
  }

  /**
   * Get the full request list parameters for a list index. This function is provided for callers to use
   * in conjunction with setList to update fields on an existing list.
   * @param key - The list key to get the params for.
   * @returns A copy of the list params or undefined.
   */
  getListParams(key) {
    const params = this.lists.get(key);
    if (!params) {
      return null;
    }
    return params.getList(true);
  }

  /**
   * Set new ranges for an existing list. Calling this function when _only_ the ranges have changed
   * is more efficient than calling setList(index,list) as this function won't resend sticky params,
   * whereas setList always will.
   * @param key - The list key to modify
   * @param ranges - The new ranges to apply.
   * @returns A promise which resolves to the transaction ID when it has been received down sync
   * (or rejects with the transaction ID if the action was not applied e.g the request was cancelled
   * immediately after sending, in which case the action will be applied in the subsequent request)
   */
  setListRanges(key, ranges) {
    const list = this.lists.get(key);
    if (!list) {
      return Promise.reject(new Error("no list with key " + key));
    }
    list.updateListRange(ranges);
    return this.resend();
  }

  /**
   * Add or replace a list. Calling this function will interrupt the /sync request to resend new
   * lists.
   * @param key - The key to modify
   * @param list - The new list parameters.
   * @returns A promise which resolves to the transaction ID when it has been received down sync
   * (or rejects with the transaction ID if the action was not applied e.g the request was cancelled
   * immediately after sending, in which case the action will be applied in the subsequent request)
   */
  setList(key, list) {
    const existingList = this.lists.get(key);
    if (existingList) {
      existingList.replaceList(list);
      this.lists.set(key, existingList);
    } else {
      this.lists.set(key, new SlidingList(list));
    }
    this.listModifiedCount += 1;
    return this.resend();
  }

  /**
   * Get the room subscriptions for the sync API.
   * @returns A copy of the desired room subscriptions.
   */
  getRoomSubscriptions() {
    return new Set(Array.from(this.desiredRoomSubscriptions));
  }

  /**
   * Modify the room subscriptions for the sync API. Calling this function will interrupt the
   * /sync request to resend new subscriptions. If the /sync stream has not started, this will
   * prepare the room subscriptions for when start() is called.
   * @param s - The new desired room subscriptions.
   * @returns A promise which resolves to the transaction ID when it has been received down sync
   * (or rejects with the transaction ID if the action was not applied e.g the request was cancelled
   * immediately after sending, in which case the action will be applied in the subsequent request)
   */
  modifyRoomSubscriptions(s) {
    this.desiredRoomSubscriptions = s;
    return this.resend();
  }

  /**
   * Modify which events to retrieve for room subscriptions. Invalidates all room subscriptions
   * such that they will be sent up afresh.
   * @param rs - The new room subscription fields to fetch.
   * @returns A promise which resolves to the transaction ID when it has been received down sync
   * (or rejects with the transaction ID if the action was not applied e.g the request was cancelled
   * immediately after sending, in which case the action will be applied in the subsequent request)
   */
  modifyRoomSubscriptionInfo(rs) {
    this.roomSubscriptionInfo = rs;
    this.confirmedRoomSubscriptions = new Set();
    return this.resend();
  }

  /**
   * Register an extension to send with the /sync request.
   * @param ext - The extension to register.
   */
  registerExtension(ext) {
    if (this.extensions[ext.name()]) {
      throw new Error(`registerExtension: ${ext.name()} already exists as an extension`);
    }
    this.extensions[ext.name()] = ext;
  }
  getExtensionRequest(isInitial) {
    const ext = {};
    Object.keys(this.extensions).forEach(extName => {
      ext[extName] = this.extensions[extName].onRequest(isInitial);
    });
    return ext;
  }
  onPreExtensionsResponse(ext) {
    Object.keys(ext).forEach(extName => {
      if (this.extensions[extName].when() == ExtensionState.PreProcess) {
        this.extensions[extName].onResponse(ext[extName]);
      }
    });
  }
  onPostExtensionsResponse(ext) {
    Object.keys(ext).forEach(extName => {
      if (this.extensions[extName].when() == ExtensionState.PostProcess) {
        this.extensions[extName].onResponse(ext[extName]);
      }
    });
  }

  /**
   * Invoke all attached room data listeners.
   * @param roomId - The room which received some data.
   * @param roomData - The raw sliding sync response JSON.
   */
  invokeRoomDataListeners(roomId, roomData) {
    if (!roomData.required_state) {
      roomData.required_state = [];
    }
    if (!roomData.timeline) {
      roomData.timeline = [];
    }
    this.emit(SlidingSyncEvent.RoomData, roomId, roomData);
  }

  /**
   * Invoke all attached lifecycle listeners.
   * @param state - The Lifecycle state
   * @param resp - The raw sync response JSON
   * @param err - Any error that occurred when making the request e.g. network errors.
   */
  invokeLifecycleListeners(state, resp, err) {
    this.emit(SlidingSyncEvent.Lifecycle, state, resp, err);
  }
  shiftRight(listKey, hi, low) {
    const list = this.lists.get(listKey);
    if (!list) {
      return;
    }
    //     l   h
    // 0,1,2,3,4 <- before
    // 0,1,2,2,3 <- after, hi is deleted and low is duplicated
    for (let i = hi; i > low; i--) {
      if (list.isIndexInRange(i)) {
        list.roomIndexToRoomId[i] = list.roomIndexToRoomId[i - 1];
      }
    }
  }
  shiftLeft(listKey, hi, low) {
    const list = this.lists.get(listKey);
    if (!list) {
      return;
    }
    //     l   h
    // 0,1,2,3,4 <- before
    // 0,1,3,4,4 <- after, low is deleted and hi is duplicated
    for (let i = low; i < hi; i++) {
      if (list.isIndexInRange(i)) {
        list.roomIndexToRoomId[i] = list.roomIndexToRoomId[i + 1];
      }
    }
  }
  removeEntry(listKey, index) {
    const list = this.lists.get(listKey);
    if (!list) {
      return;
    }
    // work out the max index
    let max = -1;
    for (const n in list.roomIndexToRoomId) {
      if (Number(n) > max) {
        max = Number(n);
      }
    }
    if (max < 0 || index > max) {
      return;
    }
    // Everything higher than the gap needs to be shifted left.
    this.shiftLeft(listKey, max, index);
    delete list.roomIndexToRoomId[max];
  }
  addEntry(listKey, index) {
    const list = this.lists.get(listKey);
    if (!list) {
      return;
    }
    // work out the max index
    let max = -1;
    for (const n in list.roomIndexToRoomId) {
      if (Number(n) > max) {
        max = Number(n);
      }
    }
    if (max < 0 || index > max) {
      return;
    }
    // Everything higher than the gap needs to be shifted right, +1 so we don't delete the highest element
    this.shiftRight(listKey, max + 1, index);
  }
  processListOps(list, listKey) {
    let gapIndex = -1;
    const listData = this.lists.get(listKey);
    if (!listData) {
      return;
    }
    list.ops.forEach(op => {
      if (!listData) {
        return;
      }
      switch (op.op) {
        case "DELETE":
          {
            _logger.logger.debug("DELETE", listKey, op.index, ";");
            delete listData.roomIndexToRoomId[op.index];
            if (gapIndex !== -1) {
              // we already have a DELETE operation to process, so process it.
              this.removeEntry(listKey, gapIndex);
            }
            gapIndex = op.index;
            break;
          }
        case "INSERT":
          {
            _logger.logger.debug("INSERT", listKey, op.index, op.room_id, ";");
            if (listData.roomIndexToRoomId[op.index]) {
              // something is in this space, shift items out of the way
              if (gapIndex < 0) {
                // we haven't been told where to shift from, so make way for a new room entry.
                this.addEntry(listKey, op.index);
              } else if (gapIndex > op.index) {
                // the gap is further down the list, shift every element to the right
                // starting at the gap so we can just shift each element in turn:
                // [A,B,C,_] gapIndex=3, op.index=0
                // [A,B,C,C] i=3
                // [A,B,B,C] i=2
                // [A,A,B,C] i=1
                // Terminate. We'll assign into op.index next.
                this.shiftRight(listKey, gapIndex, op.index);
              } else if (gapIndex < op.index) {
                // the gap is further up the list, shift every element to the left
                // starting at the gap so we can just shift each element in turn
                this.shiftLeft(listKey, op.index, gapIndex);
              }
            }
            // forget the gap, we don't need it anymore. This is outside the check for
            // a room being present in this index position because INSERTs always universally
            // forget the gap, not conditionally based on the presence of a room in the INSERT
            // position. Without this, DELETE 0; INSERT 0; would do the wrong thing.
            gapIndex = -1;
            listData.roomIndexToRoomId[op.index] = op.room_id;
            break;
          }
        case "INVALIDATE":
          {
            const startIndex = op.range[0];
            for (let i = startIndex; i <= op.range[1]; i++) {
              delete listData.roomIndexToRoomId[i];
            }
            _logger.logger.debug("INVALIDATE", listKey, op.range[0], op.range[1], ";");
            break;
          }
        case "SYNC":
          {
            const startIndex = op.range[0];
            for (let i = startIndex; i <= op.range[1]; i++) {
              const roomId = op.room_ids[i - startIndex];
              if (!roomId) {
                break; // we are at the end of list
              }

              listData.roomIndexToRoomId[i] = roomId;
            }
            _logger.logger.debug("SYNC", listKey, op.range[0], op.range[1], (op.room_ids || []).join(" "), ";");
            break;
          }
      }
    });
    if (gapIndex !== -1) {
      // we already have a DELETE operation to process, so process it
      // Everything higher than the gap needs to be shifted left.
      this.removeEntry(listKey, gapIndex);
    }
  }

  /**
   * Resend a Sliding Sync request. Used when something has changed in the request. Resolves with
   * the transaction ID of this request on success. Rejects with the transaction ID of this request
   * on failure.
   */
  resend() {
    var _this$abortController;
    if (this.needsResend && this.txnIdDefers.length > 0) {
      // we already have a resend queued, so just return the same promise
      return this.txnIdDefers[this.txnIdDefers.length - 1].promise;
    }
    this.needsResend = true;
    this.txnId = this.client.makeTxnId();
    const d = (0, _utils.defer)();
    this.txnIdDefers.push(_objectSpread(_objectSpread({}, d), {}, {
      txnId: this.txnId
    }));
    (_this$abortController = this.abortController) === null || _this$abortController === void 0 ? void 0 : _this$abortController.abort();
    this.abortController = new AbortController();
    return d.promise;
  }
  resolveTransactionDefers(txnId) {
    if (!txnId) {
      return;
    }
    // find the matching index
    let txnIndex = -1;
    for (let i = 0; i < this.txnIdDefers.length; i++) {
      if (this.txnIdDefers[i].txnId === txnId) {
        txnIndex = i;
        break;
      }
    }
    if (txnIndex === -1) {
      // this shouldn't happen; we shouldn't be seeing txn_ids for things we don't know about,
      // whine about it.
      _logger.logger.warn(`resolveTransactionDefers: seen ${txnId} but it isn't a pending txn, ignoring.`);
      return;
    }
    // This list is sorted in time, so if the input txnId ACKs in the middle of this array,
    // then everything before it that hasn't been ACKed yet never will and we should reject them.
    for (let i = 0; i < txnIndex; i++) {
      this.txnIdDefers[i].reject(this.txnIdDefers[i].txnId);
    }
    this.txnIdDefers[txnIndex].resolve(txnId);
    // clear out settled promises, including the one we resolved.
    this.txnIdDefers = this.txnIdDefers.slice(txnIndex + 1);
  }

  /**
   * Stop syncing with the server.
   */
  stop() {
    var _this$abortController2;
    this.terminated = true;
    (_this$abortController2 = this.abortController) === null || _this$abortController2 === void 0 ? void 0 : _this$abortController2.abort();
    // remove all listeners so things can be GC'd
    this.removeAllListeners(SlidingSyncEvent.Lifecycle);
    this.removeAllListeners(SlidingSyncEvent.List);
    this.removeAllListeners(SlidingSyncEvent.RoomData);
  }

  /**
   * Re-setup this connection e.g in the event of an expired session.
   */
  resetup() {
    var _this$abortController3;
    _logger.logger.warn("SlidingSync: resetting connection info");
    // any pending txn ID defers will be forgotten already by the server, so clear them out
    this.txnIdDefers.forEach(d => {
      d.reject(d.txnId);
    });
    this.txnIdDefers = [];
    // resend sticky params and de-confirm all subscriptions
    this.lists.forEach(l => {
      l.setModified(true);
    });
    this.confirmedRoomSubscriptions = new Set(); // leave desired ones alone though!
    // reset the connection as we might be wedged
    this.needsResend = true;
    (_this$abortController3 = this.abortController) === null || _this$abortController3 === void 0 ? void 0 : _this$abortController3.abort();
    this.abortController = new AbortController();
  }

  /**
   * Start syncing with the server. Blocks until stopped.
   */
  async start() {
    this.abortController = new AbortController();
    let currentPos;
    while (!this.terminated) {
      this.needsResend = false;
      let doNotUpdateList = false;
      let resp;
      try {
        const listModifiedCount = this.listModifiedCount;
        const reqLists = {};
        this.lists.forEach((l, key) => {
          reqLists[key] = l.getList(false);
        });
        const reqBody = {
          lists: reqLists,
          pos: currentPos,
          timeout: this.timeoutMS,
          clientTimeout: this.timeoutMS + BUFFER_PERIOD_MS,
          extensions: this.getExtensionRequest(currentPos === undefined)
        };
        // check if we are (un)subscribing to a room and modify request this one time for it
        const newSubscriptions = difference(this.desiredRoomSubscriptions, this.confirmedRoomSubscriptions);
        const unsubscriptions = difference(this.confirmedRoomSubscriptions, this.desiredRoomSubscriptions);
        if (unsubscriptions.size > 0) {
          reqBody.unsubscribe_rooms = Array.from(unsubscriptions);
        }
        if (newSubscriptions.size > 0) {
          reqBody.room_subscriptions = {};
          for (const roomId of newSubscriptions) {
            const customSubName = this.roomIdToCustomSubscription.get(roomId);
            let sub = this.roomSubscriptionInfo;
            if (customSubName && this.customSubscriptions.has(customSubName)) {
              sub = this.customSubscriptions.get(customSubName);
            }
            reqBody.room_subscriptions[roomId] = sub;
          }
        }
        if (this.txnId) {
          reqBody.txn_id = this.txnId;
          this.txnId = null;
        }
        this.pendingReq = this.client.slidingSync(reqBody, this.proxyBaseUrl, this.abortController.signal);
        resp = await this.pendingReq;
        currentPos = resp.pos;
        // update what we think we're subscribed to.
        for (const roomId of newSubscriptions) {
          this.confirmedRoomSubscriptions.add(roomId);
        }
        for (const roomId of unsubscriptions) {
          this.confirmedRoomSubscriptions.delete(roomId);
        }
        if (listModifiedCount !== this.listModifiedCount) {
          // the lists have been modified whilst we were waiting for 'await' to return, but the abort()
          // call did nothing. It is NOT SAFE to modify the list array now. We'll process the response but
          // not update list pointers.
          _logger.logger.debug("list modified during await call, not updating list");
          doNotUpdateList = true;
        }
        // mark all these lists as having been sent as sticky so we don't keep sending sticky params
        this.lists.forEach(l => {
          l.setModified(false);
        });
        // set default empty values so we don't need to null check
        resp.lists = resp.lists || {};
        resp.rooms = resp.rooms || {};
        resp.extensions = resp.extensions || {};
        Object.keys(resp.lists).forEach(key => {
          const list = this.lists.get(key);
          if (!list || !resp) {
            return;
          }
          list.joinedCount = resp.lists[key].count;
        });
        this.invokeLifecycleListeners(SlidingSyncState.RequestFinished, resp);
      } catch (err) {
        if (err.httpStatus) {
          this.invokeLifecycleListeners(SlidingSyncState.RequestFinished, null, err);
          if (err.httpStatus === 400) {
            // session probably expired TODO: assign an errcode
            // so drop state and re-request
            this.resetup();
            currentPos = undefined;
            await (0, _utils.sleep)(50); // in case the 400 was for something else; don't tightloop
            continue;
          } // else fallthrough to generic error handling
        } else if (this.needsResend || err.name === "AbortError") {
          continue; // don't sleep as we caused this error by abort()ing the request.
        }

        _logger.logger.error(err);
        await (0, _utils.sleep)(5000);
      }
      if (!resp) {
        continue;
      }
      this.onPreExtensionsResponse(resp.extensions);
      Object.keys(resp.rooms).forEach(roomId => {
        this.invokeRoomDataListeners(roomId, resp.rooms[roomId]);
      });
      const listKeysWithUpdates = new Set();
      if (!doNotUpdateList) {
        for (const [key, list] of Object.entries(resp.lists)) {
          list.ops = list.ops || [];
          if (list.ops.length > 0) {
            listKeysWithUpdates.add(key);
          }
          this.processListOps(list, key);
        }
      }
      this.invokeLifecycleListeners(SlidingSyncState.Complete, resp);
      this.onPostExtensionsResponse(resp.extensions);
      listKeysWithUpdates.forEach(listKey => {
        const list = this.lists.get(listKey);
        if (!list) {
          return;
        }
        this.emit(SlidingSyncEvent.List, listKey, list.joinedCount, Object.assign({}, list.roomIndexToRoomId));
      });
      this.resolveTransactionDefers(resp.txn_id);
    }
  }
}
exports.SlidingSync = SlidingSync;
const difference = (setA, setB) => {
  const diff = new Set(setA);
  for (const elem of setB) {
    diff.delete(elem);
  }
  return diff;
};
//# sourceMappingURL=sliding-sync.js.map