Parcourir la source

Reduce duplication

master
Lyubomir Marinov il y a 9 ans
Parent
révision
2fe4df5293
2 fichiers modifiés avec 66 ajouts et 75 suppressions
  1. 28
    30
      modules/xmpp/JingleSessionPC.js
  2. 38
    45
      modules/xmpp/moderator.js

+ 28
- 30
modules/xmpp/JingleSessionPC.js Voir le fichier

@@ -859,14 +859,30 @@ JingleSessionPC.prototype._modifySources = function (successCallback, queueCallb
859 859
     this.removessrc = [];
860 860
 
861 861
     sdp.raw = sdp.session + sdp.media.join('');
862
-    this.peerconnection.setRemoteDescription(new RTCSessionDescription({type: 'offer', sdp: sdp.raw}),
862
+    /**
863
+     * Implements a failure callback which reports an error message and an
864
+     * optional error through (1) logger, (2) GlobalOnErrorHandler, and (3)
865
+     * queueCallback.
866
+     *
867
+     * @param {string} errmsg the error messsage to report
868
+     * @param {*} error an optional error to report in addition to errmsg
869
+     */
870
+    function reportError(errmsg, err) {
871
+        if (err) {
872
+           errmsg = errmsg + ': ' + err; // for logger and GlobalOnErrorHandler
873
+           logger.error(errmsg, err);
874
+        } else {
875
+           logger.error(errmsg);
876
+           err = errmsg; // for queueCallback
877
+        }
878
+        GlobalOnErrorHandler.callErrorHandler(new Error(errmsg));
879
+        queueCallback(err);
880
+    };
881
+    this.peerconnection.setRemoteDescription(
882
+        new RTCSessionDescription({type: 'offer', sdp: sdp.raw}),
863 883
         function() {
864
-
865 884
             if(self.signalingState == 'closed') {
866
-                var errmsg = "createAnswer attempt on closed state";
867
-                logger.error(errmsg);
868
-                GlobalOnErrorHandler.callErrorHandler(new Error(errmsg));
869
-                queueCallback(errmsg);
885
+                reportError("createAnswer attempt on closed state");
870 886
                 return;
871 887
             }
872 888
 
@@ -898,36 +914,18 @@ JingleSessionPC.prototype._modifySources = function (successCallback, queueCallb
898 914
                     //modifiedAnswer.sdp = modifiedAnswer.sdp.replace(/a=setup:active/g, 'a=setup:actpass');
899 915
                     self.peerconnection.setLocalDescription(modifiedAnswer,
900 916
                         function() {
901
-                            if(successCallback){
902
-                                successCallback();
903
-                            }
917
+                            successCallback && successCallback();
904 918
                             queueCallback();
905 919
                         },
906
-                        function(error) {
907
-                            var errmsg = "modified setLocalDescription failed";
908
-                            GlobalOnErrorHandler.callErrorHandler(new Error(
909
-                                errmsg + ": " + error));
910
-                            logger.error(errmsg, error);
911
-                            queueCallback(error);
912
-                        }
920
+                        reportError.bind(
921
+                            undefined,
922
+                            "modified setLocalDescription failed")
913 923
                     );
914 924
                 },
915
-                function(error) {
916
-                    var errmsg = "modified answer failed";
917
-                    GlobalOnErrorHandler.callErrorHandler(new Error(
918
-                        errmsg + ": " + error));
919
-                    logger.error(errmsg, error);
920
-                    queueCallback(error);
921
-                }
925
+                reportError.bind(undefined, "modified answer failed")
922 926
             );
923 927
         },
924
-        function(error) {
925
-            var errmsg = 'modify failed';
926
-            GlobalOnErrorHandler.callErrorHandler(new Error(
927
-                errmsg + ": " + error));
928
-            logger.error(errmsg, error);
929
-            queueCallback(error);
930
-        }
928
+        reportError.bind(undefined, 'modify failed')
931 929
     );
932 930
 };
933 931
 

+ 38
- 45
modules/xmpp/moderator.js Voir le fichier

@@ -427,67 +427,60 @@ Moderator.prototype.authenticate = function () {
427 427
     });
428 428
 };
429 429
 
430
-Moderator.prototype.getLoginUrl =  function (urlCallback, failureCallback) {
430
+Moderator.prototype.getLoginUrl = function (urlCallback, failureCallback) {
431
+    this._getLoginUrl(/* popup */ false, urlCallback, failureCallback);
432
+};
433
+
434
+/**
435
+ *
436
+ * @param {boolean} popup false for {@link Moderator#getLoginUrl} or true for
437
+ * {@link Moderator#getPopupLoginUrl}
438
+ * @param urlCb
439
+ * @param failureCb
440
+ */
441
+Moderator.prototype._getLoginUrl = function (popup, urlCb, failureCb) {
431 442
     var iq = $iq({to: this.getFocusComponent(), type: 'get'});
432
-    iq.c('login-url', {
443
+    var attrs = {
433 444
         xmlns: 'http://jitsi.org/protocol/focus',
434 445
         room: this.roomName,
435 446
         'machine-uid': this.settings.getUserId()
436
-    });
447
+    };
448
+    var str = 'auth url'; // for logger
449
+    if (popup) {
450
+       attrs.popup = true;
451
+       str = 'POPUP ' + str;
452
+    }
453
+    iq.c('login-url', attrs);
454
+    /**
455
+     * Implements a failure callback which reports an error message and an error
456
+     * through (1) GlobalOnErrorHandler, (2) logger, and (3) failureCb.
457
+     *
458
+     * @param {string} errmsg the error messsage to report
459
+     * @param {*} error the error to report (in addition to errmsg)
460
+     */
461
+    function reportError(errmsg, err) {
462
+        GlobalOnErrorHandler.callErrorHandler(new Error(errmsg));
463
+        logger.error(errmsg, err);
464
+        failureCb(err);
465
+    }
437 466
     this.connection.sendIQ(
438 467
         iq,
439 468
         function (result) {
440 469
             var url = $(result).find('login-url').attr('url');
441
-            url = url = decodeURIComponent(url);
470
+            url = decodeURIComponent(url);
442 471
             if (url) {
443
-                logger.info("Got auth url: " + url);
444
-                urlCallback(url);
472
+                logger.info('Got ' + str + ': ' + url);
473
+                urlCb(url);
445 474
             } else {
446
-                var errmsg = "Failed to get auth url from the focus";
447
-                GlobalOnErrorHandler.callErrorHandler(new Error(errmsg));
448
-                logger.error(errmsg, result);
449
-                failureCallback(result);
475
+                reportError('Failed to get ' + str + ' from the focus', result);
450 476
             }
451 477
         },
452
-        function (error) {
453
-            var errmsg = "Get auth url error";
454
-            GlobalOnErrorHandler.callErrorHandler(new Error(errmsg));
455
-            logger.error(errmsg, error);
456
-            failureCallback(error);
457
-        }
478
+        reportError.bind(undefined, 'Get ' + str + ' error')
458 479
     );
459 480
 };
460 481
 
461 482
 Moderator.prototype.getPopupLoginUrl = function (urlCallback, failureCallback) {
462
-    var iq = $iq({to: this.getFocusComponent(), type: 'get'});
463
-    iq.c('login-url', {
464
-        xmlns: 'http://jitsi.org/protocol/focus',
465
-        room: this.roomName,
466
-        'machine-uid': this.settings.getUserId(),
467
-        popup: true
468
-    });
469
-    this.connection.sendIQ(
470
-        iq,
471
-        function (result) {
472
-            var url = $(result).find('login-url').attr('url');
473
-            url = url = decodeURIComponent(url);
474
-            if (url) {
475
-                logger.info("Got POPUP auth url:  " + url);
476
-                urlCallback(url);
477
-            } else {
478
-                var errmsg = "Failed to get POPUP auth url from the focus";
479
-                GlobalOnErrorHandler.callErrorHandler(new Error(errmsg));
480
-                logger.error(errmsg, result);
481
-                failureCallback(result);
482
-            }
483
-        },
484
-        function (error) {
485
-            var errmsg = "Get POPUP auth url error";
486
-            GlobalOnErrorHandler.callErrorHandler(new Error(errmsg));
487
-            logger.error(errmsg, error);
488
-            failureCallback(error);
489
-        }
490
-    );
483
+    this._getLoginUrl(/* popup */ true, urlCallback, failureCallback);
491 484
 };
492 485
 
493 486
 Moderator.prototype.logout =  function (callback) {

Chargement…
Annuler
Enregistrer