1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-23 17:02:25 +03:00

listen to call event handlers when sync is prepared

This commit is contained in:
Germain Souquet
2021-05-19 17:05:20 +01:00
parent 61fd62ff81
commit ea1ef3dbec
2 changed files with 35 additions and 6 deletions

View File

@@ -356,7 +356,13 @@ export function MatrixClient(opts) {
// Start listening for calls after the initial sync is done // Start listening for calls after the initial sync is done
// We do not need to backfill the call event buffer // We do not need to backfill the call event buffer
// with encrypted events that might never get decrypted // with encrypted events that might never get decrypted
this.once("sync", () => this._callEventHandler.start()); function startCallEventHandler() {
if (this.isInitialSyncComplete()) {
this._callEventHandler.start();
this.off("sync", startCallEventHandler);
}
}
this.on("sync", startCallEventHandler);
} else { } else {
this._callEventHandler = null; this._callEventHandler = null;
} }

View File

@@ -55,10 +55,11 @@ export class CallEventHandler {
this.client.removeListener("event", this.onEvent); this.client.removeListener("event", this.onEvent);
} }
private evaluateEventBuffer = () => { private evaluateEventBuffer = async () => {
if (this.client.getSyncState() === "SYNCING") { if (this.client.getSyncState() === "SYNCING") {
// don't process any events until they are all decrypted await Promise.all(this.callEventBuffer.map(event => {
if (this.callEventBuffer.some((e) => e.isBeingDecrypted())) return; this.client.decryptEventIfNeeded(event);
}));
const ignoreCallIds = new Set<String>(); const ignoreCallIds = new Set<String>();
// inspect the buffer and mark all calls which have been answered // inspect the buffer and mark all calls which have been answered
@@ -88,16 +89,38 @@ export class CallEventHandler {
} }
} }
private onEvent = async (event: MatrixEvent) => { private onEvent = (event: MatrixEvent) => {
await this.client.decryptEventIfNeeded(event); this.client.decryptEventIfNeeded(event);
// any call events or ones that might be once they're decrypted
if ( if (
event.getType().indexOf("m.call.") === 0 || event.getType().indexOf("m.call.") === 0 ||
event.getType().indexOf("org.matrix.call.") === 0 event.getType().indexOf("org.matrix.call.") === 0
|| event.isBeingDecrypted()
) { ) {
// queue up for processing once all events from this sync have been // queue up for processing once all events from this sync have been
// processed (see above). // processed (see above).
this.callEventBuffer.push(event); this.callEventBuffer.push(event);
} }
if (event.isBeingDecrypted() || event.isDecryptionFailure()) {
// add an event listener for once the event is decrypted.
event.once("Event.decrypted", () => {
if (event.getType().indexOf("m.call.") === -1) return;
if (this.callEventBuffer.includes(event)) {
// we were waiting for that event to decrypt, so recheck the buffer
this.evaluateEventBuffer();
} else {
// This one wasn't buffered so just run the event handler for it
// straight away
try {
this.handleCallEvent(event);
} catch (e) {
logger.error("Caught exception handling call event", e);
}
}
});
}
} }
private handleCallEvent(event: MatrixEvent) { private handleCallEvent(event: MatrixEvent) {