Skip to content

Commit c5bc715

Browse files
committed
Resume to-device message queue after resumed sync
1 parent ad16b26 commit c5bc715

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/ToDeviceMessageQueue.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ limitations under the License.
1515
*/
1616

1717
import { logger } from "./logger";
18-
import { MatrixError, MatrixClient } from "./matrix";
18+
import { MatrixError, MatrixClient, ClientEvent } from "./matrix";
1919
import { IndexedToDeviceBatch, ToDeviceBatch, ToDeviceBatchWithTxnId, ToDevicePayload } from "./models/ToDeviceMessage";
2020
import { MatrixScheduler } from "./scheduler";
21+
import { SyncState } from "./sync";
2122

2223
const MAX_BATCH_SIZE = 20;
2324

@@ -37,12 +38,14 @@ export class ToDeviceMessageQueue {
3738
public start(): void {
3839
this.running = true;
3940
this.sendQueue();
41+
this.client.on(ClientEvent.Sync, this.onResumedSync);
4042
}
4143

4244
public stop(): void {
4345
this.running = false;
4446
if (this.retryTimeout !== null) clearTimeout(this.retryTimeout);
4547
this.retryTimeout = null;
48+
this.client.removeListener(ClientEvent.Sync, this.onResumedSync);
4649
}
4750

4851
public async queueBatch(batch: ToDeviceBatch): Promise<void> {
@@ -128,4 +131,15 @@ export class ToDeviceMessageQueue {
128131

129132
await this.client.sendToDevice(batch.eventType, contentMap, batch.txnId);
130133
}
134+
135+
/**
136+
* Listen to sync state changes and automatically resend any pending events
137+
* once syncing is resumed
138+
*/
139+
private onResumedSync = (state: SyncState | null, oldState: SyncState | null): void => {
140+
if (state === SyncState.Syncing && oldState !== SyncState.Syncing) {
141+
logger.info(`Resuming queue after resumed sync`);
142+
this.sendQueue();
143+
}
144+
};
131145
}

0 commit comments

Comments
 (0)