Przeglądaj źródła

fix(JitsiConference) properly handle leave() errors

Rewrite the function using async / await and reverse the logic to make i easier
to read / understand.
dev1
Saúl Ibarra Corretgé 3 lat temu
rodzic
commit
9e5d83f4ac
1 zmienionych plików z 43 dodań i 39 usunięć
  1. 43
    39
      JitsiConference.js

+ 43
- 39
JitsiConference.js Wyświetl plik

@@ -618,7 +618,7 @@ JitsiConference.prototype.isP2PTestModeEnabled = function() {
618 618
  * Leaves the conference.
619 619
  * @returns {Promise}
620 620
  */
621
-JitsiConference.prototype.leave = function() {
621
+JitsiConference.prototype.leave = async function() {
622 622
     if (this.participantConnectionStatus) {
623 623
         this.participantConnectionStatus.dispose();
624 624
         this.participantConnectionStatus = null;
@@ -662,53 +662,57 @@ JitsiConference.prototype.leave = function() {
662 662
         this.p2pJingleSession = null;
663 663
     }
664 664
 
665
-    // leave the conference
666
-    if (this.room) {
667
-        const room = this.room;
665
+    // Leave the conference. If this.room == null we are calling second time leave().
666
+    if (!this.room) {
667
+        throw new Error('The conference is has been already left');
668
+    }
669
+
670
+    const room = this.room;
668 671
 
669
-        // Unregister connection state listeners
670
-        room.removeListener(
671
-            XMPPEvents.CONNECTION_INTERRUPTED,
672
-            this._onIceConnectionInterrupted);
673
-        room.removeListener(
674
-            XMPPEvents.CONNECTION_RESTORED,
675
-            this._onIceConnectionRestored);
676
-        room.removeListener(
677
-            XMPPEvents.CONNECTION_ESTABLISHED,
678
-            this._onIceConnectionEstablished);
672
+    // Unregister connection state listeners
673
+    room.removeListener(
674
+        XMPPEvents.CONNECTION_INTERRUPTED,
675
+        this._onIceConnectionInterrupted);
676
+    room.removeListener(
677
+        XMPPEvents.CONNECTION_RESTORED,
678
+        this._onIceConnectionRestored);
679
+    room.removeListener(
680
+        XMPPEvents.CONNECTION_ESTABLISHED,
681
+        this._onIceConnectionEstablished);
679 682
 
680
-        room.removeListener(
681
-            XMPPEvents.CONFERENCE_PROPERTIES_CHANGED,
682
-            this._updateProperties);
683
+    room.removeListener(
684
+        XMPPEvents.CONFERENCE_PROPERTIES_CHANGED,
685
+        this._updateProperties);
683 686
 
684
-        room.removeListener(XMPPEvents.MEETING_ID_SET, this._sendConferenceJoinAnalyticsEvent);
687
+    room.removeListener(XMPPEvents.MEETING_ID_SET, this._sendConferenceJoinAnalyticsEvent);
685 688
 
686
-        this.eventManager.removeXMPPListeners();
689
+    this.eventManager.removeXMPPListeners();
687 690
 
688
-        this._signalingLayer.setChatRoom(null);
691
+    this._signalingLayer.setChatRoom(null);
689 692
 
690
-        this.room = null;
693
+    this.room = null;
691 694
 
692
-        return room.leave()
693
-            .catch(error => {
694
-                // remove all participants because currently the conference
695
-                // won't be usable anyway. This is done on success automatically
696
-                // by the ChatRoom instance.
697
-                this.getParticipants().forEach(
698
-                    participant => this.onMemberLeft(participant.getJid()));
699
-
700
-                throw error;
701
-            })
702
-            .then(() => {
703
-                if (this.rtc) {
704
-                    this.rtc.destroy();
705
-                }
706
-            });
695
+    let leaveError;
696
+
697
+    try {
698
+        await room.leave();
699
+    } catch (err) {
700
+        leaveError = err;
701
+
702
+        // Remove all participants because currently the conference
703
+        // won't be usable anyway. This is done on success automatically
704
+        // by the ChatRoom instance.
705
+        this.getParticipants().forEach(
706
+            participant => this.onMemberLeft(participant.getJid()));
707 707
     }
708 708
 
709
-    // If this.room == null we are calling second time leave().
710
-    return Promise.reject(
711
-        new Error('The conference is has been already left'));
709
+    if (this.rtc) {
710
+        this.rtc.destroy();
711
+    }
712
+
713
+    if (leaveError) {
714
+        throw leaveError;
715
+    }
712 716
 };
713 717
 
714 718
 /**

Ładowanie…
Anuluj
Zapisz