From 9db280bbe66c913541991f385c41f6b457bb70f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Tue, 1 Jun 2021 15:31:46 +0200 Subject: [PATCH] Listen for CallsChanged MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This should avoid delays and such Signed-off-by: Šimon Brandner --- src/components/structures/CallEventGrouper.ts | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/components/structures/CallEventGrouper.ts b/src/components/structures/CallEventGrouper.ts index 15de2dcaf7..339b9359c2 100644 --- a/src/components/structures/CallEventGrouper.ts +++ b/src/components/structures/CallEventGrouper.ts @@ -18,7 +18,7 @@ limitations under the License. import { EventType } from "matrix-js-sdk/src/@types/event"; import { MatrixEvent } from "matrix-js-sdk/src/models/event"; import { CallEvent, CallState, CallType, MatrixCall } from "matrix-js-sdk/src/webrtc/call"; -import CallHandler from '../../CallHandler'; +import CallHandler, { CallHandlerEvent } from '../../CallHandler'; import { EventEmitter } from 'events'; import { MatrixClientPeg } from "../../MatrixClientPeg"; import defaultDispatcher from "../../dispatcher/dispatcher"; @@ -43,6 +43,12 @@ export default class CallEventGrouper extends EventEmitter { call: MatrixCall; state: CallState | CustomCallState; + constructor() { + super(); + + CallHandler.sharedInstance().addListener(CallHandlerEvent.CallsChanged, this.setCall) + } + private get invite(): MatrixEvent { return this.events.find((event) => event.getType() === EventType.CallInvite); } @@ -95,10 +101,10 @@ export default class CallEventGrouper extends EventEmitter { private setCallListeners() { if (!this.call) return; - this.call.addListener(CallEvent.State, this.setCallState); + this.call.addListener(CallEvent.State, this.setState); } - private setCallState = () => { + private setState = () => { if (SUPPORTED_STATES.includes(this.call?.state)) { this.state = this.call.state; } else { @@ -113,13 +119,17 @@ export default class CallEventGrouper extends EventEmitter { this.emit(CallEventGrouperEvent.StateChanged, this.state); } - public add(event: MatrixEvent) { - const callId = event.getContent().call_id; - this.events.push(event); + private setCall = () => { + const callId = this.events[0].getContent().call_id; if (!this.call) { this.call = CallHandler.sharedInstance().getCallById(callId); this.setCallListeners(); } - this.setCallState(); + this.setState(); + } + + public add(event: MatrixEvent) { + this.events.push(event); + this.setState(); } }