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

Merge pull request #1083 from matrix-org/dbkr/advanced_anti_chirp

Always process call events in batches
This commit is contained in:
David Baker
2019-11-21 19:22:24 +00:00
committed by GitHub

View File

@@ -4635,17 +4635,17 @@ function setupCallEventHandler(client) {
// callId: [Candidate] // callId: [Candidate]
}; };
// Maintain a buffer of events before the client has synced for the first time. // The sync code always emits one event at a time, so it will patiently
// This buffer will be inspected to see if we should send incoming call // wait for us to finish processing a call invite before delivering the
// notifications. It needs to be buffered to correctly determine if an // next event, even if that next event is a hangup. We therefore accumulate
// incoming call has had a matching answer/hangup. // all our call events and then process them on the 'sync' event, ie.
// each time a sync has completed. This way, we can avoid emitting incoming
// call events if we get both the invite and answer/hangup in the same sync.
// This happens quite often, eg. replaying sync from storage, catchup sync
// after loading and after we've been offline for a bit.
let callEventBuffer = []; let callEventBuffer = [];
let isClientSyncing = false; function onSync(state) {
const onSync = function(state) {
if (state === "SYNCING") { if (state === "SYNCING") {
isClientSyncing = true;
client.removeListener("sync", onSync);
const ignoreCallIds = {}; // Set<String> const ignoreCallIds = {}; // Set<String>
// inspect the buffer and mark all calls which have been answered // inspect the buffer and mark all calls which have been answered
// or hung up before passing them to the call event handler. // or hung up before passing them to the call event handler.
@@ -4658,19 +4658,20 @@ function setupCallEventHandler(client) {
} }
// now loop through the buffer chronologically and inject them // now loop through the buffer chronologically and inject them
callEventBuffer.forEach(function(e) { callEventBuffer.forEach(function(e) {
if (ignoreCallIds[e.getContent().call_id]) { if (
// This call has previously been ansered or hung up: ignore it e.getType() === "m.call.invite" &&
ignoreCallIds[e.getContent().call_id]
) {
// This call has previously been answered or hung up: ignore it
return; return;
} }
callEventHandler(e); callEventHandler(e);
}); });
callEventBuffer = []; callEventBuffer = [];
} }
}; }
client.on("sync", onSync); client.on("sync", onSync);
client.on("event", onEvent);
function onEvent(event) { function onEvent(event) {
if (event.getType().indexOf("m.call.") !== 0) { if (event.getType().indexOf("m.call.") !== 0) {
// not a call event // not a call event
@@ -4680,12 +4681,11 @@ function setupCallEventHandler(client) {
} }
return; return;
} }
if (!isClientSyncing) { // queue up for processing once all events from this sync have been
callEventBuffer.push(event); // processed (see above).
return; callEventBuffer.push(event);
}
callEventHandler(event);
} }
client.on("event", onEvent);
function callEventHandler(event) { function callEventHandler(event) {
const content = event.getContent(); const content = event.getContent();