From fa60eb28e9de404750407129eb09bdb643eb2af7 Mon Sep 17 00:00:00 2001 From: Robert Long Date: Tue, 27 Jul 2021 16:27:04 -0700 Subject: [PATCH] Add pending call connection logic --- src/ConferenceCallManager.js | 48 +++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/src/ConferenceCallManager.js b/src/ConferenceCallManager.js index 50d54e3..74a05dc 100644 --- a/src/ConferenceCallManager.js +++ b/src/ConferenceCallManager.js @@ -143,6 +143,8 @@ export class ConferenceCallManager extends EventEmitter { }; this.participants = [this.localParticipant]; + this.pendingCalls = []; + this.client.on("RoomState.members", this._onMemberChanged); this.client.on("Call.incoming", this._onIncomingCall); } @@ -170,6 +172,16 @@ export class ConferenceCallManager extends EventEmitter { .getMembers() .forEach((member) => this._processMember(member.userId)); + for (const { call, onHangup, onReplaced } of this.pendingCalls) { + call.removeListener("hangup", onHangup); + call.removeListener("replaced", onReplaced); + const userId = call.opponentMember.userId; + this._addCall(call, userId); + call.answer(); + } + + this.pendingCalls = []; + this._updateParticipantState(); } @@ -262,10 +274,34 @@ export class ConferenceCallManager extends EventEmitter { _onIncomingCall = (call) => { if (!this.joined) { - console.debug( - "_onIncomingCall", - "Local user hasn't joined yet. Not answering." - ); + const onHangup = (call) => { + const index = this.pendingCalls.findIndex((p) => p.call === call); + + if (index !== -1) { + this.pendingCalls.splice(index, 1); + } + }; + + const onReplaced = (call, newCall) => { + const index = this.pendingCalls.findIndex((p) => p.call === call); + + if (index !== -1) { + this.pendingCalls.splice(index, 1, { + call: newCall, + onHangup: () => onHangup(newCall), + onReplaced: (nextCall) => onReplaced(newCall, nextCall), + }); + } + }; + + this.pendingCalls.push({ + call, + onHangup: () => onHangup(call), + onReplaced: (newCall) => onReplaced(call, newCall), + }); + call.on("hangup", onHangup); + call.on("replaced", onReplaced); + return; } @@ -282,6 +318,10 @@ export class ConferenceCallManager extends EventEmitter { }; _addCall(call, userId) { + if (call.state === "ended") { + return; + } + const existingCall = this.participants.find( (p) => !p.local && p.call && p.call.callId === call.callId );