Przeglądaj źródła

Loads CallStats backend only after the script has finished downloading. (#622)

* Loads CallStats backend only after the script has finished downloading.

* Updates jsdoc and log level.

* Updates jsdoc.
dev1
Дамян Минков 8 lat temu
rodzic
commit
f1f9dafaf6
2 zmienionych plików z 57 dodań i 31 usunięć
  1. 0
    4
      modules/statistics/CallStats.js
  2. 57
    27
      modules/statistics/statistics.js

+ 0
- 4
modules/statistics/CallStats.js Wyświetl plik

499
      * which given <tt>tpc</tt> is connected.
499
      * which given <tt>tpc</tt> is connected.
500
      */
500
      */
501
     constructor(tpc, options) {
501
     constructor(tpc, options) {
502
-        if (!CallStats.backend) {
503
-            throw new Error('CallStats backend not intiialized!');
504
-        }
505
-
506
         this.confID = options.confID;
502
         this.confID = options.confID;
507
         this.tpc = tpc;
503
         this.tpc = tpc;
508
         this.peerconnection = tpc.peerconnection;
504
         this.peerconnection = tpc.peerconnection;

+ 57
- 27
modules/statistics/statistics.js Wyświetl plik

33
  * downloading their API as soon as possible and (2) do the downloading
33
  * downloading their API as soon as possible and (2) do the downloading
34
  * asynchronously.
34
  * asynchronously.
35
  *
35
  *
36
- * @param customScriptUrl
36
+ * @param {StatisticsOptions} options - Options to use for downloading and
37
+ * initializing callstats backend.
37
  */
38
  */
38
-function loadCallStatsAPI(customScriptUrl) {
39
+function loadCallStatsAPI(options) {
39
     if (!isCallstatsLoaded) {
40
     if (!isCallstatsLoaded) {
40
         ScriptUtil.loadScript(
41
         ScriptUtil.loadScript(
41
-            customScriptUrl
42
+            options.customScriptUrl
42
                 || 'https://api.callstats.io/static/callstats-ws.min.js',
43
                 || 'https://api.callstats.io/static/callstats-ws.min.js',
43
             /* async */ true,
44
             /* async */ true,
44
-            /* prepend */ true);
45
+            /* prepend */ true,
46
+            /* relativeURL */ undefined,
47
+            /* loadCallback */ () => _initCallStatsBackend(options)
48
+        );
45
         isCallstatsLoaded = true;
49
         isCallstatsLoaded = true;
46
     }
50
     }
51
+}
47
 
52
 
48
-    // FIXME At the time of this writing, we hope that the callstats.io API will
49
-    // have loaded by the time we needed it (i.e. CallStats.init is invoked).
53
+/**
54
+ * Initializes Callstats backend.
55
+ *
56
+ * @param {StatisticsOptions} options - The options to use for initializing
57
+ * callstats backend.
58
+ * @private
59
+ */
60
+function _initCallStatsBackend(options) {
61
+    if (CallStats.isBackendInitialized()) {
62
+        return;
63
+    }
64
+
65
+    const userName = Settings.callStatsUserName;
66
+
67
+    if (!CallStats.initBackend({
68
+        callStatsID: options.callStatsID,
69
+        callStatsSecret: options.callStatsSecret,
70
+        userName: options.swapUserNameAndAlias
71
+            ? options.callStatsAliasName : userName,
72
+        aliasName: options.swapUserNameAndAlias
73
+            ? userName : options.callStatsAliasName,
74
+        applicationName: options.applicationName
75
+    })) {
76
+        logger.error('CallStats Backend initialization failed bad');
77
+    }
50
 }
78
 }
51
 
79
 
52
 /**
80
 /**
92
     Statistics.disableThirdPartyRequests = options.disableThirdPartyRequests;
120
     Statistics.disableThirdPartyRequests = options.disableThirdPartyRequests;
93
 };
121
 };
94
 
122
 
123
+/**
124
+ * The options to configure Statistics.
125
+ * @typedef {Object} StatisticsOptions
126
+ * @property {string} applicationName - The application name to pass to
127
+ * callstats.
128
+ * @property {string} callStatsAliasName - The alias name to use when
129
+ * initializing callstats.
130
+ * @property {string} callStatsConfIDNamespace - A namespace to prepend the
131
+ * callstats conference ID with.
132
+ * @property {string} callStatsID - Callstats credentials - the id.
133
+ * @property {string} callStatsSecret - Callstats credentials - the secret.
134
+ * @property {string} customScriptUrl - A custom lib url to use when downloading
135
+ * callstats library.
136
+ * @property {string} roomName - The room name we are currently in.
137
+ * @property {boolean} swapUserNameAndAlias - Whether to swap the places of
138
+ * username and alias when initiating callstats.
139
+ */
95
 /**
140
 /**
96
  *
141
  *
97
  * @param xmpp
142
  * @param xmpp
98
- * @param options
143
+ * @param {StatisticsOptions} options - The options to use creating the
144
+ * Statistics.
99
  */
145
  */
100
 export default function Statistics(xmpp, options) {
146
 export default function Statistics(xmpp, options) {
101
     /**
147
     /**
116
             // requests to any third parties.
162
             // requests to any third parties.
117
             && (Statistics.disableThirdPartyRequests !== true);
163
             && (Statistics.disableThirdPartyRequests !== true);
118
     if (this.callStatsIntegrationEnabled) {
164
     if (this.callStatsIntegrationEnabled) {
119
-        if (!RTCBrowserType.isReactNative()) {
120
-            loadCallStatsAPI(this.options.callStatsCustomScriptUrl);
165
+        if (RTCBrowserType.isReactNative()) {
166
+            _initCallStatsBackend(this.options);
167
+        } else {
168
+            loadCallStatsAPI(this.options);
121
         }
169
         }
122
 
170
 
123
         if (!this.options.callStatsConfIDNamespace) {
171
         if (!this.options.callStatsConfIDNamespace) {
312
         return;
360
         return;
313
     }
361
     }
314
 
362
 
315
-    if (!CallStats.isBackendInitialized()) {
316
-        const userName = Settings.callStatsUserName;
317
-
318
-        if (!CallStats.initBackend({
319
-            callStatsID: this.options.callStatsID,
320
-            callStatsSecret: this.options.callStatsSecret,
321
-            userName: this.options.swapUserNameAndAlias
322
-                ? this.options.callStatsAliasName : userName,
323
-            aliasName: this.options.swapUserNameAndAlias
324
-                ? userName : this.options.callStatsAliasName,
325
-            applicationName: this.options.applicationName
326
-        })) {
327
-
328
-            // Backend initialization failed bad
329
-            return;
330
-        }
331
-    }
332
-
333
     logger.info(`Starting CallStats for ${tpc}...`);
363
     logger.info(`Starting CallStats for ${tpc}...`);
334
 
364
 
335
     const newInstance
365
     const newInstance

Ładowanie…
Anuluj
Zapisz