Просмотр исходного кода

feat(JitsiConnection): disconnect->return Promise

dev1
Hristo Terezov 6 лет назад
Родитель
Сommit
0ee30bf12a
4 измененных файлов: 59 добавлений и 35 удалений
  1. 2
    1
      JitsiConnection.js
  2. 2
    2
      modules/statistics/AnalyticsAdapter.js
  3. 50
    32
      modules/xmpp/xmpp.js
  4. 5
    0
      service/xmpp/XMPPEvents.js

+ 2
- 1
JitsiConnection.js Просмотреть файл

74
 
74
 
75
 /**
75
 /**
76
  * Disconnect the client from the server.
76
  * Disconnect the client from the server.
77
+ * @returns {Promise} - Resolves when the disconnect process is finished or rejects with an error.
77
  */
78
  */
78
 JitsiConnection.prototype.disconnect = function(...args) {
79
 JitsiConnection.prototype.disconnect = function(...args) {
79
     // XXX Forward any arguments passed to JitsiConnection.disconnect to
80
     // XXX Forward any arguments passed to JitsiConnection.disconnect to
80
     // XMPP.disconnect. For example, the caller of JitsiConnection.disconnect
81
     // XMPP.disconnect. For example, the caller of JitsiConnection.disconnect
81
     // may optionally pass the event which triggered the disconnect in order to
82
     // may optionally pass the event which triggered the disconnect in order to
82
     // provide the implementation with finer-grained context.
83
     // provide the implementation with finer-grained context.
83
-    this.xmpp.disconnect(...args);
84
+    return this.xmpp.disconnect(...args);
84
 };
85
 };
85
 
86
 
86
 /**
87
 /**

+ 2
- 2
modules/statistics/AnalyticsAdapter.js Просмотреть файл

115
      */
115
      */
116
     dispose() {
116
     dispose() {
117
         logger.warn('Disposing of analytics adapter.');
117
         logger.warn('Disposing of analytics adapter.');
118
-        
118
+
119
         if (this.analyticsHandlers && this.analyticsHandlers.size > 0) {
119
         if (this.analyticsHandlers && this.analyticsHandlers.size > 0) {
120
             this.analyticsHandlers.forEach(handler => {
120
             this.analyticsHandlers.forEach(handler => {
121
-                if ( typeof handler.dispose === 'function' ) {
121
+                if (typeof handler.dispose === 'function') {
122
                     handler.dispose();
122
                     handler.dispose();
123
                 }
123
                 }
124
             });
124
             });

+ 50
- 32
modules/xmpp/xmpp.js Просмотреть файл

92
         // they wanted to utilize the connected connection in an unload handler
92
         // they wanted to utilize the connected connection in an unload handler
93
         // of their own. However, it should be fairly easy for them to do that
93
         // of their own. However, it should be fairly easy for them to do that
94
         // by registering their unload handler before us.
94
         // by registering their unload handler before us.
95
-        $(window).on('beforeunload unload', this.disconnect.bind(this));
95
+        $(window).on('beforeunload unload', ev => {
96
+            this.disconnect(ev).catch(() => {
97
+                // ignore errors in order to not brake the unload.
98
+            });
99
+        });
96
     }
100
     }
97
 
101
 
98
     /**
102
     /**
171
         logger.log(
175
         logger.log(
172
             `(TIME) Strophe ${statusStr}${msg ? `[${msg}]` : ''}:\t`,
176
             `(TIME) Strophe ${statusStr}${msg ? `[${msg}]` : ''}:\t`,
173
             now);
177
             now);
178
+
179
+        this.eventEmitter.emit(XMPPEvents.CONNECTION_STATUS_CHANGED, credentials, status, msg);
174
         if (status === Strophe.Status.CONNECTED
180
         if (status === Strophe.Status.CONNECTED
175
             || status === Strophe.Status.ATTACHED) {
181
             || status === Strophe.Status.ATTACHED) {
176
             if (this.options.useStunTurn
182
             if (this.options.useStunTurn
490
     /**
496
     /**
491
      * Disconnects this from the XMPP server (if this is connected).
497
      * Disconnects this from the XMPP server (if this is connected).
492
      *
498
      *
493
-     * @param ev optionally, the event which triggered the necessity to
499
+     * @param {Object} ev - Optionally, the event which triggered the necessity to
494
      * disconnect from the XMPP server (e.g. beforeunload, unload).
500
      * disconnect from the XMPP server (e.g. beforeunload, unload).
501
+     * @returns {Promise} - Resolves when the disconnect process is finished or rejects with an error.
495
      */
502
      */
496
     disconnect(ev) {
503
     disconnect(ev) {
497
         if (this.disconnectInProgress || !this.connection) {
504
         if (this.disconnectInProgress || !this.connection) {
498
             this.eventEmitter.emit(JitsiConnectionEvents.WRONG_STATE);
505
             this.eventEmitter.emit(JitsiConnectionEvents.WRONG_STATE);
499
 
506
 
500
-            return;
507
+            return Promise.reject(new Error('Wrong connection state!'));
501
         }
508
         }
502
 
509
 
503
         this.disconnectInProgress = true;
510
         this.disconnectInProgress = true;
504
 
511
 
505
-        // XXX Strophe is asynchronously sending by default. Unfortunately, that
506
-        // means that there may not be enough time to send an unavailable
507
-        // presence or disconnect at all. Switching Strophe to synchronous
508
-        // sending is not much of an option because it may lead to a noticeable
509
-        // delay in navigating away from the current location. As a compromise,
510
-        // we will try to increase the chances of sending an unavailable
511
-        // presence and/or disconecting within the short time span that we have
512
-        // upon unloading by invoking flush() on the connection. We flush() once
513
-        // before disconnect() in order to attemtp to have its unavailable
514
-        // presence at the top of the send queue. We flush() once more after
515
-        // disconnect() in order to attempt to have its unavailable presence
516
-        // sent as soon as possible.
517
-        this.connection.flush();
518
-
519
-        if (ev !== null && typeof ev !== 'undefined') {
520
-            const evType = ev.type;
521
-
522
-            if (evType === 'beforeunload' || evType === 'unload') {
523
-                // XXX Whatever we said above, synchronous sending is the best
524
-                // (known) way to properly disconnect from the XMPP server.
525
-                // Consequently, it may be fine to have the source code and
526
-                // comment it in or out depending on whether we want to run with
527
-                // it for some time.
528
-                this.connection.options.sync = true;
512
+        return new Promise(resolve => {
513
+            const disconnectListener = (credentials, status) => {
514
+                if (status === Strophe.Status.DISCONNECTED) {
515
+                    resolve();
516
+                    this.eventEmitter.removeListener(XMPPEvents.CONNECTION_STATUS_CHANGED, disconnectListener);
517
+                }
518
+            };
519
+
520
+            this.eventEmitter.on(XMPPEvents.CONNECTION_STATUS_CHANGED, disconnectListener);
521
+
522
+            // XXX Strophe is asynchronously sending by default. Unfortunately, that
523
+            // means that there may not be enough time to send an unavailable
524
+            // presence or disconnect at all. Switching Strophe to synchronous
525
+            // sending is not much of an option because it may lead to a noticeable
526
+            // delay in navigating away from the current location. As a compromise,
527
+            // we will try to increase the chances of sending an unavailable
528
+            // presence and/or disconecting within the short time span that we have
529
+            // upon unloading by invoking flush() on the connection. We flush() once
530
+            // before disconnect() in order to attemtp to have its unavailable
531
+            // presence at the top of the send queue. We flush() once more after
532
+            // disconnect() in order to attempt to have its unavailable presence
533
+            // sent as soon as possible.
534
+            this.connection.flush();
535
+
536
+            if (ev !== null && typeof ev !== 'undefined') {
537
+                const evType = ev.type;
538
+
539
+                if (evType === 'beforeunload' || evType === 'unload') {
540
+                    // XXX Whatever we said above, synchronous sending is the best
541
+                    // (known) way to properly disconnect from the XMPP server.
542
+                    // Consequently, it may be fine to have the source code and
543
+                    // comment it in or out depending on whether we want to run with
544
+                    // it for some time.
545
+                    this.connection.options.sync = true;
546
+                }
529
             }
547
             }
530
-        }
531
 
548
 
532
-        this.connection.disconnect();
549
+            this.connection.disconnect();
533
 
550
 
534
-        if (this.connection.options.sync !== true) {
535
-            this.connection.flush();
536
-        }
551
+            if (this.connection.options.sync !== true) {
552
+                this.connection.flush();
553
+            }
554
+        });
537
     }
555
     }
538
 
556
 
539
     /**
557
     /**

+ 5
- 0
service/xmpp/XMPPEvents.js Просмотреть файл

52
     // This should go to the RTC module.
52
     // This should go to the RTC module.
53
     CONNECTION_ICE_FAILED: 'xmpp.connection.ice.failed',
53
     CONNECTION_ICE_FAILED: 'xmpp.connection.ice.failed',
54
 
54
 
55
+    /**
56
+     * Designates an event indicating connection status changes.
57
+     */
58
+    CONNECTION_STATUS_CHANGED: 'xmpp.connection.status.changed',
59
+
55
     // Designates an event indicating that the display name of a participant
60
     // Designates an event indicating that the display name of a participant
56
     // has changed.
61
     // has changed.
57
     DISPLAY_NAME_CHANGED: 'xmpp.display_name_changed',
62
     DISPLAY_NAME_CHANGED: 'xmpp.display_name_changed',

Загрузка…
Отмена
Сохранить