From 67a052e46ae9e40a175d84f9a702db8bd18e491c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Tue, 1 Jun 2021 10:55:03 +0200 Subject: [PATCH] Reorganize things and do some fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- src/components/structures/CallEventGrouper.ts | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/components/structures/CallEventGrouper.ts b/src/components/structures/CallEventGrouper.ts index 84f178b75f..5bf7d45f59 100644 --- a/src/components/structures/CallEventGrouper.ts +++ b/src/components/structures/CallEventGrouper.ts @@ -73,26 +73,30 @@ export default class CallEventGrouper extends EventEmitter { } private setCallListeners() { + if (!this.call) return; this.call.addListener(CallEvent.State, this.setCallState); } private setCallState = () => { - if (SUPPORTED_STATES.includes(this.call.state)) { + if (SUPPORTED_STATES.includes(this.call?.state)) { this.state = this.call.state; - this.emit(CallEventGrouperEvent.StateChanged, this.state); + } else { + const lastEvent = this.events[this.events.length - 1]; + const lastEventType = lastEvent.getType(); + + if (lastEventType === EventType.CallHangup) this.state = CallState.Ended; + else if (lastEventType === EventType.CallReject) this.state = CallState.Ended; } + this.emit(CallEventGrouperEvent.StateChanged, this.state); } public add(event: MatrixEvent) { - this.events.push(event); - const type = event.getType(); - - if (type === EventType.CallHangup) this.state = CallState.Ended; - else if (type === EventType.CallReject) this.state = CallState.Ended; const callId = event.getContent().call_id; - this.call = CallHandler.sharedInstance().getCallById(callId); - if (!this.call) return; - this.setCallListeners(); + this.events.push(event); + if (!this.call) { + this.call = CallHandler.sharedInstance().getCallById(callId); + this.setCallListeners(); + } this.setCallState(); } }