Browse Source

Simplify the source code

Simplify the source code by using some ES6 features such as arrow
functions to avoid the use of 'var self = this', the shorthand for
method definitions in object initializers.
tags/v0.0.2
Lyubomir Marinov 8 years ago
parent
commit
b10a1fcc44
1 changed files with 46 additions and 55 deletions
  1. 46
    55
      modules/RTC/ScreenObtainer.js

+ 46
- 55
modules/RTC/ScreenObtainer.js View File

1
 /* global chrome, $, alert */
1
 /* global chrome, $, alert */
2
 
2
 
3
+var AdapterJS = require("./adapter.screenshare");
4
+var GlobalOnErrorHandler = require("../util/GlobalOnErrorHandler");
3
 var logger = require("jitsi-meet-logger").getLogger(__filename);
5
 var logger = require("jitsi-meet-logger").getLogger(__filename);
4
 var RTCBrowserType = require("./RTCBrowserType");
6
 var RTCBrowserType = require("./RTCBrowserType");
5
-var AdapterJS = require("./adapter.screenshare");
6
 import JitsiTrackError from "../../JitsiTrackError";
7
 import JitsiTrackError from "../../JitsiTrackError";
7
 import * as JitsiTrackErrors from "../../JitsiTrackErrors";
8
 import * as JitsiTrackErrors from "../../JitsiTrackErrors";
8
-var GlobalOnErrorHandler = require("../util/GlobalOnErrorHandler");
9
 
9
 
10
 /**
10
 /**
11
  * Indicates whether the Chrome desktop sharing extension is installed.
11
  * Indicates whether the Chrome desktop sharing extension is installed.
54
  */
54
  */
55
 var ScreenObtainer = {
55
 var ScreenObtainer = {
56
     obtainStream: null,
56
     obtainStream: null,
57
+
57
     /**
58
     /**
58
      * Initializes the function used to obtain a screen capture
59
      * Initializes the function used to obtain a screen capture
59
      * (this.obtainStream).
60
      * (this.obtainStream).
68
      * @param options {object}
69
      * @param options {object}
69
      * @param gum {Function} GUM method
70
      * @param gum {Function} GUM method
70
      */
71
      */
71
-    init: function(options, gum) {
72
+    init(options, gum) {
72
         var obtainDesktopStream = null;
73
         var obtainDesktopStream = null;
73
         this.options = options = options || {};
74
         this.options = options = options || {};
74
         GUM = gum;
75
         GUM = gum;
81
             (options.desktopSharingChromeMethod || options.desktopSharing);
82
             (options.desktopSharingChromeMethod || options.desktopSharing);
82
 
83
 
83
         if (RTCBrowserType.isNWJS()) {
84
         if (RTCBrowserType.isNWJS()) {
84
-            obtainDesktopStream = function (options, onSuccess, onFailure) {
85
+            obtainDesktopStream = (options, onSuccess, onFailure) => {
85
                 window.JitsiMeetNW.obtainDesktopStream (
86
                 window.JitsiMeetNW.obtainDesktopStream (
86
-                    onSuccess, function (error, constraints) {
87
+                    onSuccess,
88
+                    (error, constraints) => {
87
                         var jitsiError;
89
                         var jitsiError;
88
                         // FIXME:
90
                         // FIXME:
89
                         // This is very very durty fix for recognising that the
91
                         // This is very very durty fix for recognising that the
148
             } else {
150
             } else {
149
                 obtainDesktopStream = this.obtainScreenOnFirefox;
151
                 obtainDesktopStream = this.obtainScreenOnFirefox;
150
             }
152
             }
151
-
152
         }
153
         }
153
 
154
 
154
         if (!obtainDesktopStream) {
155
         if (!obtainDesktopStream) {
163
      * environment.
164
      * environment.
164
      * @returns {boolean}
165
      * @returns {boolean}
165
      */
166
      */
166
-    isSupported: function() {
167
+    isSupported() {
167
         return !!this.obtainStream;
168
         return !!this.obtainStream;
168
     },
169
     },
170
+
169
     /**
171
     /**
170
      * Obtains a screen capture stream on Firefox.
172
      * Obtains a screen capture stream on Firefox.
171
      * @param callback
173
      * @param callback
172
      * @param errorCallback
174
      * @param errorCallback
173
      */
175
      */
174
-    obtainScreenOnFirefox:
175
-           function (options, callback, errorCallback) {
176
-        var self = this;
176
+    obtainScreenOnFirefox(options, callback, errorCallback) {
177
         var extensionRequired = false;
177
         var extensionRequired = false;
178
         if (this.options.desktopSharingFirefoxMaxVersionExtRequired === -1 ||
178
         if (this.options.desktopSharingFirefoxMaxVersionExtRequired === -1 ||
179
             (this.options.desktopSharingFirefoxMaxVersionExtRequired >= 0 &&
179
             (this.options.desktopSharingFirefoxMaxVersionExtRequired >= 0 &&
198
         // extension if it hasn't.
198
         // extension if it hasn't.
199
         if (firefoxExtInstalled === null) {
199
         if (firefoxExtInstalled === null) {
200
             window.setTimeout(
200
             window.setTimeout(
201
-                function() {
201
+                () => {
202
                     if (firefoxExtInstalled === null)
202
                     if (firefoxExtInstalled === null)
203
                         firefoxExtInstalled = false;
203
                         firefoxExtInstalled = false;
204
-                    self.obtainScreenOnFirefox(callback, errorCallback);
204
+                    this.obtainScreenOnFirefox(callback, errorCallback);
205
                 },
205
                 },
206
-                300
207
-            );
206
+                300);
208
             logger.log("Waiting for detection of jidesha on firefox to " +
207
             logger.log("Waiting for detection of jidesha on firefox to " +
209
                 "finish.");
208
                 "finish.");
210
             return;
209
             return;
221
         errorCallback(
220
         errorCallback(
222
             new JitsiTrackError(JitsiTrackErrors.FIREFOX_EXTENSION_NEEDED));
221
             new JitsiTrackError(JitsiTrackErrors.FIREFOX_EXTENSION_NEEDED));
223
     },
222
     },
223
+
224
     /**
224
     /**
225
      * Asks Chrome extension to call chooseDesktopMedia and gets chrome
225
      * Asks Chrome extension to call chooseDesktopMedia and gets chrome
226
      * 'desktop' stream for returned stream token.
226
      * 'desktop' stream for returned stream token.
227
      */
227
      */
228
-    obtainScreenFromExtension: function(options, streamCallback, failCallback) {
228
+    obtainScreenFromExtension(options, streamCallback, failCallback) {
229
         if (chromeExtInstalled) {
229
         if (chromeExtInstalled) {
230
             doGetStreamFromExtension(this.options, streamCallback,
230
             doGetStreamFromExtension(this.options, streamCallback,
231
                 failCallback);
231
                 failCallback);
239
             try {
239
             try {
240
                 chrome.webstore.install(
240
                 chrome.webstore.install(
241
                     getWebStoreInstallUrl(this.options),
241
                     getWebStoreInstallUrl(this.options),
242
-                    (arg) => {
242
+                    arg => {
243
                         logger.log("Extension installed successfully", arg);
243
                         logger.log("Extension installed successfully", arg);
244
                         chromeExtInstalled = true;
244
                         chromeExtInstalled = true;
245
-                        // We need to give a moment for the endpoint to become
246
-                        // available
245
+                        // We need to give a moment to the endpoint to become
246
+                        // available.
247
                         waitForExtensionAfterInstall(this.options, 200, 10)
247
                         waitForExtensionAfterInstall(this.options, 200, 10)
248
                             .then(() => {
248
                             .then(() => {
249
                                 doGetStreamFromExtension(this.options,
249
                                 doGetStreamFromExtension(this.options,
250
                                     streamCallback, failCallback);
250
                                     streamCallback, failCallback);
251
-                            }).catch(() => {
252
-                                // options param is {} because we won't do
253
-                                // external install in this case.
254
-                                this.handleExtensionInstallationError({},
255
-                                    streamCallback, failCallback);
251
+                            }).catch(reason => {
252
+                                this.handleExtensionInstallationError(options,
253
+                                    streamCallback, failCallback, reason);
256
                             });
254
                             });
257
                     },
255
                     },
258
                     this.handleExtensionInstallationError.bind(this,
256
                     this.handleExtensionInstallationError.bind(this,
264
             }
262
             }
265
         }
263
         }
266
     },
264
     },
267
-    handleExtensionInstallationError: function (options, streamCallback,
268
-        failCallback, e) {
269
-        if( CHROME_EXTENSION_POPUP_ERROR === e && options.interval > 0 &&
270
-            typeof(options.checkAgain) === "function" &&
271
-            typeof(options.listener) === "function") {
272
-            options.listener("waitingForExtension",
273
-                getWebStoreInstallUrl(this.options));
274
-            this.checkForChromeExtensionOnInterval(options,
275
-                streamCallback, failCallback, e);
265
+
266
+    handleExtensionInstallationError(options, streamCallback, failCallback, e) {
267
+        const webStoreInstallUrl = getWebStoreInstallUrl(this.options);
268
+
269
+        if (CHROME_EXTENSION_POPUP_ERROR === e
270
+                && options.interval > 0
271
+                && typeof(options.checkAgain) === "function"
272
+                && typeof(options.listener) === "function") {
273
+            options.listener("waitingForExtension", webStoreInstallUrl);
274
+            this.checkForChromeExtensionOnInterval(options, streamCallback,
275
+                failCallback, e);
276
             return;
276
             return;
277
         }
277
         }
278
-        var msg = "Failed to install the extension from "
279
-            + getWebStoreInstallUrl(this.options);
280
 
278
 
281
-        logger.log(msg, e);
279
+        const msg
280
+            = "Failed to install the extension from " + webStoreInstallUrl;
282
 
281
 
282
+        logger.log(msg, e);
283
         failCallback(new JitsiTrackError(
283
         failCallback(new JitsiTrackError(
284
             JitsiTrackErrors.CHROME_EXTENSION_INSTALLATION_ERROR,
284
             JitsiTrackErrors.CHROME_EXTENSION_INSTALLATION_ERROR,
285
-            msg
286
-        ));
285
+            msg));
287
     },
286
     },
288
-    checkForChromeExtensionOnInterval: function (options,
289
-        streamCallback, failCallback) {
287
+
288
+    checkForChromeExtensionOnInterval(options, streamCallback, failCallback) {
290
         if (options.checkAgain() === false) {
289
         if (options.checkAgain() === false) {
291
             failCallback(new JitsiTrackError(
290
             failCallback(new JitsiTrackError(
292
                 JitsiTrackErrors.CHROME_EXTENSION_INSTALLATION_ERROR));
291
                 JitsiTrackErrors.CHROME_EXTENSION_INSTALLATION_ERROR));
305
     }
304
     }
306
 };
305
 };
307
 
306
 
308
-
309
-
310
 /**
307
 /**
311
  * Obtains a desktop stream using getUserMedia.
308
  * Obtains a desktop stream using getUserMedia.
312
  * For this to work on Chrome, the
309
  * For this to work on Chrome, the
317
  * 'about:config'.
314
  * 'about:config'.
318
  */
315
  */
319
 function obtainWebRTCScreen(options, streamCallback, failCallback) {
316
 function obtainWebRTCScreen(options, streamCallback, failCallback) {
320
-    GUM(
321
-        ['screen'],
322
-        streamCallback,
323
-        failCallback
324
-    );
317
+    GUM(['screen'], streamCallback, failCallback);
325
 }
318
 }
326
 
319
 
327
 /**
320
 /**
386
         //TODO: remove chromeExtensionId (deprecated)
379
         //TODO: remove chromeExtensionId (deprecated)
387
         (options.desktopSharingChromeExtId || options.chromeExtensionId),
380
         (options.desktopSharingChromeExtId || options.chromeExtensionId),
388
         { getVersion: true },
381
         { getVersion: true },
389
-        function (response) {
382
+        response => {
390
             if (!response || !response.version) {
383
             if (!response || !response.version) {
391
                 // Communication failure - assume that no endpoint exists
384
                 // Communication failure - assume that no endpoint exists
392
                 logger.warn(
385
                 logger.warn(
420
             sources: (options.desktopSharingChromeSources ||
413
             sources: (options.desktopSharingChromeSources ||
421
                 options.desktopSharingSources)
414
                 options.desktopSharingSources)
422
         },
415
         },
423
-        function (response) {
416
+        response => {
424
             if (!response) {
417
             if (!response) {
425
                 // possibly re-wraping error message to make code consistent
418
                 // possibly re-wraping error message to make code consistent
426
                 var lastError = chrome.runtime.lastError;
419
                 var lastError = chrome.runtime.lastError;
435
             if (response.streamId) {
428
             if (response.streamId) {
436
                 GUM(
429
                 GUM(
437
                     ['desktop'],
430
                     ['desktop'],
438
-                    function (stream) {
439
-                        streamCallback(stream);
440
-                    },
431
+                    stream => streamCallback(stream),
441
                     failCallback,
432
                     failCallback,
442
-                    {desktopStream: response.streamId});
433
+                    { desktopStream: response.streamId });
443
             } else {
434
             } else {
444
                 // As noted in Chrome Desktop Capture API:
435
                 // As noted in Chrome Desktop Capture API:
445
                 // If user didn't select any source (i.e. canceled the prompt)
436
                 // If user didn't select any source (i.e. canceled the prompt)
478
     // Initialize Chrome extension inline installs
469
     // Initialize Chrome extension inline installs
479
     initInlineInstalls(options);
470
     initInlineInstalls(options);
480
     // Check if extension is installed
471
     // Check if extension is installed
481
-    checkChromeExtInstalled(function (installed, updateRequired) {
472
+    checkChromeExtInstalled((installed, updateRequired) => {
482
         chromeExtInstalled = installed;
473
         chromeExtInstalled = installed;
483
         chromeExtUpdateRequired = updateRequired;
474
         chromeExtUpdateRequired = updateRequired;
484
         logger.info(
475
         logger.info(
536
     }
527
     }
537
 
528
 
538
     var img = document.createElement('img');
529
     var img = document.createElement('img');
539
-    img.onload = function(){
530
+    img.onload = () => {
540
         logger.log("Detected firefox screen sharing extension.");
531
         logger.log("Detected firefox screen sharing extension.");
541
         firefoxExtInstalled = true;
532
         firefoxExtInstalled = true;
542
     };
533
     };
543
-    img.onerror = function(){
534
+    img.onerror = () => {
544
         logger.log("Detected lack of firefox screen sharing extension.");
535
         logger.log("Detected lack of firefox screen sharing extension.");
545
         firefoxExtInstalled = false;
536
         firefoxExtInstalled = false;
546
     };
537
     };

Loading…
Cancel
Save