Skip to content

Commit 2596999

Browse files
committed
Review comments
1 parent a3248c0 commit 2596999

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/sliding-sync.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { IAbortablePromise } from "./@types/partials";
1919
import { MatrixClient } from "./client";
2020
import { IRoomEvent, IStateEvent } from "./sync-accumulator";
2121
import { TypedEventEmitter } from "./models//typed-event-emitter";
22-
import { sleep } from "./utils";
22+
import { sleep, IDeferred, defer } from "./utils";
2323

2424
// /sync requests allow you to set a timeout= but the request may continue
2525
// beyond that and wedge forever, so we need to track how long we are willing
@@ -340,7 +340,7 @@ export class SlidingSync extends TypedEventEmitter<SlidingSyncEvent, SlidingSync
340340
private txnId?: string = null;
341341
// a list (in chronological order of when they were sent) of objects containing the txn ID and
342342
// a defer to resolve/reject depending on whether they were successfully sent or not.
343-
private txnIdDefers: {txnId: string, resolve: Function, reject: Function}[] = [];
343+
private txnIdDefers: (IDeferred<string> & { txnId: string})[] = [];
344344
// map of extension name to req/resp handler
345345
private extensions: Record<string, Extension> = {};
346346

@@ -410,6 +410,9 @@ export class SlidingSync extends TypedEventEmitter<SlidingSyncEvent, SlidingSync
410410
* whereas setList always will.
411411
* @param index The list index to modify
412412
* @param ranges The new ranges to apply.
413+
* @return A promise which resolves to the transaction ID when it has been received down sync
414+
* (or rejects with the transaction ID if the action was not applied e.g the request was cancelled
415+
* immediately after sending, in which case the action will be applied in the subsequent request)
413416
*/
414417
public setListRanges(index: number, ranges: number[][]): Promise<string> {
415418
this.lists[index].updateListRange(ranges);
@@ -421,6 +424,9 @@ export class SlidingSync extends TypedEventEmitter<SlidingSyncEvent, SlidingSync
421424
* lists.
422425
* @param index The index to modify
423426
* @param list The new list parameters.
427+
* @return A promise which resolves to the transaction ID when it has been received down sync
428+
* (or rejects with the transaction ID if the action was not applied e.g the request was cancelled
429+
* immediately after sending, in which case the action will be applied in the subsequent request)
424430
*/
425431
public setList(index: number, list: MSC3575List): Promise<string> {
426432
if (this.lists[index]) {
@@ -445,6 +451,9 @@ export class SlidingSync extends TypedEventEmitter<SlidingSyncEvent, SlidingSync
445451
* /sync request to resend new subscriptions. If the /sync stream has not started, this will
446452
* prepare the room subscriptions for when start() is called.
447453
* @param s The new desired room subscriptions.
454+
* @return A promise which resolves to the transaction ID when it has been received down sync
455+
* (or rejects with the transaction ID if the action was not applied e.g the request was cancelled
456+
* immediately after sending, in which case the action will be applied in the subsequent request)
448457
*/
449458
public modifyRoomSubscriptions(s: Set<string>): Promise<string> {
450459
this.desiredRoomSubscriptions = s;
@@ -455,6 +464,9 @@ export class SlidingSync extends TypedEventEmitter<SlidingSyncEvent, SlidingSync
455464
* Modify which events to retrieve for room subscriptions. Invalidates all room subscriptions
456465
* such that they will be sent up afresh.
457466
* @param rs The new room subscription fields to fetch.
467+
* @return A promise which resolves to the transaction ID when it has been received down sync
468+
* (or rejects with the transaction ID if the action was not applied e.g the request was cancelled
469+
* immediately after sending, in which case the action will be applied in the subsequent request)
458470
*/
459471
public modifyRoomSubscriptionInfo(rs: MSC3575RoomSubscription): Promise<string> {
460472
this.roomSubscriptionInfo = rs;
@@ -628,16 +640,14 @@ export class SlidingSync extends TypedEventEmitter<SlidingSyncEvent, SlidingSync
628640
*/
629641
public resend(): Promise<string> {
630642
this.needsResend = true;
631-
this.txnId = ""+Math.random();
632-
const p: Promise<string> = new Promise((resolve, reject) => {
633-
this.txnIdDefers.push({
634-
txnId: this.txnId,
635-
resolve: resolve,
636-
reject: reject,
637-
});
643+
this.txnId = this.client.makeTxnId();
644+
const d = defer<string>();
645+
this.txnIdDefers.push({
646+
...d,
647+
txnId: this.txnId,
638648
});
639649
this.pendingReq?.abort();
640-
return p;
650+
return d.promise;
641651
}
642652

643653
private resolveTransactionDefers(txnId?: string) {

0 commit comments

Comments
 (0)