Browse Source

fix(JitsiConference.leave): Promise and logic

dev1
hristoterezov 9 years ago
parent
commit
9075fcfc43

+ 20
- 34
JitsiConference.js View File

@@ -140,52 +140,38 @@ JitsiConference.prototype.isJoined = function () {
140 140
     return this.room && this.room.joined;
141 141
 };
142 142
 
143
-/**
144
- * Leaves the conference and calls onMemberLeft for every participant.
145
- */
146
-JitsiConference.prototype._leaveRoomAndRemoveParticipants = function () {
147
-    // remove all participants
148
-    this.getParticipants().forEach(function (participant) {
149
-        this.onMemberLeft(participant.getJid());
150
-    }.bind(this));
151
-
152
-    // leave the conference
153
-    if (this.room) {
154
-        this.room.leave();
155
-    }
156
-
157
-    this.room = null;
158
-};
159
-
160 143
 /**
161 144
  * Leaves the conference.
162 145
  * @returns {Promise}
163 146
  */
164 147
 JitsiConference.prototype.leave = function () {
165
-    var conference = this;
166
-
167 148
     if (this.participantConnectionStatus) {
168 149
         this.participantConnectionStatus.dispose();
169 150
         this.participantConnectionStatus = null;
170 151
     }
171 152
 
172
-    this.statistics.stopCallStats();
153
+    this.getLocalTracks().forEach(track => this.onTrackRemoved(track));
154
+
173 155
     this.rtc.closeAllDataChannels();
156
+    if(this.statistics)
157
+        this.statistics.dispose();
174 158
 
175
-    return Promise.all(
176
-        conference.getLocalTracks().map(function (track) {
177
-            return conference.removeTrack(track);
178
-        })
179
-    ).then(this._leaveRoomAndRemoveParticipants.bind(this))
180
-    .catch(function (error) {
181
-        logger.error(error);
182
-        GlobalOnErrorHandler.callUnhandledRejectionHandler(
183
-            {promise: this, reason: error});
184
-        // We are proceeding with leaving the conference because room.leave may
185
-        // succeed.
186
-        this._leaveRoomAndRemoveParticipants();
187
-        return Promise.resolve();
188
-    }.bind(this));
159
+    // leave the conference
160
+    if (this.room) {
161
+        let room = this.room;
162
+        this.room = null;
163
+        return room.leave().catch(() => {
164
+            // remove all participants because currently the conference won't
165
+            // be usable anyway. This is done on success automatically by the
166
+            // ChatRoom instance.
167
+            this.getParticipants().forEach(
168
+                participant => this.onMemberLeft(participant.getJid()));
169
+        });
170
+    }
171
+
172
+    // If this.room == null we are calling second time leave().
173
+    return Promise.reject(
174
+        new Error("The conference is has been already left"));
189 175
 };
190 176
 
191 177
 /**

+ 0
- 5
JitsiConferenceEventManager.js View File

@@ -406,11 +406,6 @@ JitsiConferenceEventManager.prototype.setupChatRoomListeners = function () {
406 406
     });
407 407
 
408 408
     if(conference.statistics) {
409
-        chatRoom.addListener(XMPPEvents.DISPOSE_CONFERENCE,
410
-            function () {
411
-                conference.statistics.dispose();
412
-            });
413
-
414 409
         chatRoom.addListener(XMPPEvents.CONNECTION_ICE_FAILED,
415 410
             function (pc) {
416 411
                 conference.statistics.sendIceConnectionFailedEvent(pc);

+ 1
- 0
modules/statistics/statistics.js View File

@@ -216,6 +216,7 @@ Statistics.prototype.removeByteSentStatsListener = function (listener) {
216 216
 };
217 217
 
218 218
 Statistics.prototype.dispose = function () {
219
+    this.stopCallStats();
219 220
     this.stopRemoteStats();
220 221
     if(this.eventEmitter)
221 222
         this.eventEmitter.removeAllListeners();

+ 20
- 2
modules/xmpp/ChatRoom.js View File

@@ -958,10 +958,29 @@ ChatRoom.prototype.onMute = function (iq) {
958 958
 
959 959
 /**
960 960
  * Leaves the room. Closes the jingle session.
961
+ * @returns {Promise} which is resolved if XMPPEvents.MUC_LEFT is received less
962
+ * than 5s after sending presence unavailable. Otherwise the promise is
963
+ * rejected.
961 964
  */
962 965
 ChatRoom.prototype.leave = function () {
963 966
     this._dispose();
964
-    this.doLeave();
967
+    return new Promise((resolve, reject) => {
968
+        let timeout = setTimeout(() => onMucLeft(true), 5000);
969
+        let eventEmitter = this.eventEmitter;
970
+        function onMucLeft(doReject = false) {
971
+            eventEmitter.removeListener(XMPPEvents.MUC_LEFT, onMucLeft);
972
+            clearTimeout(timeout);
973
+            if(doReject) {
974
+                // the timeout expired
975
+                reject(new Error("The timeout for the confirmation about " +
976
+                    "leaving the room expired."));
977
+            } else {
978
+                resolve();
979
+            }
980
+        }
981
+        eventEmitter.on(XMPPEvents.MUC_LEFT, onMucLeft);
982
+        this.doLeave();
983
+    });
965 984
 };
966 985
 
967 986
 /**
@@ -971,7 +990,6 @@ ChatRoom.prototype._dispose = function () {
971 990
     if (this.session) {
972 991
         this.session.close();
973 992
     }
974
-    this.eventEmitter.emit(XMPPEvents.DISPOSE_CONFERENCE);
975 993
 };
976 994
 
977 995
 module.exports = ChatRoom;

+ 0
- 1
service/xmpp/XMPPEvents.js View File

@@ -43,7 +43,6 @@ var XMPPEvents = {
43 43
     // Designates an event indicating that the display name of a participant
44 44
     // has changed.
45 45
     DISPLAY_NAME_CHANGED: "xmpp.display_name_changed",
46
-    DISPOSE_CONFERENCE: "xmpp.dispose_conference",
47 46
     ETHERPAD: "xmpp.etherpad",
48 47
     FOCUS_DISCONNECTED: 'xmpp.focus_disconnected',
49 48
     FOCUS_LEFT: "xmpp.focus_left",

Loading…
Cancel
Save