Browse Source

[RN] Delay the generating of machineId

Because the implementation of LocalStorage in jitsi-meet at the time
of this writing is asynchronous with respect to loading and saving,
delay the generation of machineId in order to allow LocalStorage to
load any previously saved value.
dev1
Lyubo Marinov 7 years ago
parent
commit
203f9f7724

+ 1
- 1
JitsiMeetJS.js View File

462
      * @returns {string} the machine id
462
      * @returns {string} the machine id
463
      */
463
      */
464
     getMachineId() {
464
     getMachineId() {
465
-        return Settings.getMachineId();
465
+        return Settings.machineId;
466
     },
466
     },
467
 
467
 
468
     /**
468
     /**

+ 99
- 110
modules/settings/Settings.js View File

3
 
3
 
4
 import UsernameGenerator from '../util/UsernameGenerator';
4
 import UsernameGenerator from '../util/UsernameGenerator';
5
 
5
 
6
-/**
7
- * Gets the localStorage of the browser. (Technically, gets the localStorage of
8
- * the global object because there may be no browser but React Native for
9
- * example).
10
- * @returns {Storage} the local Storage object (if any)
11
- */
12
-function getLocalStorage() {
13
-
14
-    // eslint-disable-next-line no-invalid-this
15
-    const global = typeof window === 'undefined' ? this : window;
16
-    let storage;
6
+let _callStatsUserName;
17
 
7
 
18
-    try {
19
-        storage = global.localStorage;
20
-    } catch (error) {
21
-        logger.error(error);
22
-    }
23
-
24
-    return storage;
25
-}
26
-
27
-/**
28
- *
29
- */
30
-function _p8() {
31
-    return `${Math.random().toString(16)}000000000`.substr(2, 8);
32
-}
8
+let _machineId;
33
 
9
 
34
 /**
10
 /**
35
  *
11
  *
36
  */
12
  */
37
-function generateUniqueId() {
38
-    return _p8() + _p8() + _p8() + _p8();
39
-}
40
-
41
-/**
42
- * Generate unique id.
43
- * @returns {string} random unique id
44
- */
45
-function generateJitsiMeetId() {
46
-    const jitsiMeetId = generateUniqueId();
47
-
48
-    logger.log('generated id', jitsiMeetId);
49
-
50
-    return jitsiMeetId;
51
-}
52
-
53
-/**
54
- * Generate fake username for callstats.
55
- * @returns {string} fake random username
56
- */
57
-function generateCallStatsUsername() {
58
-    const username = UsernameGenerator.generateUsername();
59
-
60
-    logger.log('generated callstats uid', username);
61
-
62
-    return username;
63
-}
64
-
65
-/**
66
- *
67
- */
68
-class Settings {
13
+export default {
69
     /**
14
     /**
70
-     *
15
+     * Returns fake username for callstats
16
+     * @returns {string} fake username for callstats
71
      */
17
      */
72
-    constructor() {
73
-        const localStorage = getLocalStorage();
18
+    get callStatsUserName() {
19
+        if (!_callStatsUserName) {
20
+            const localStorage = getLocalStorage();
74
 
21
 
75
-        if (localStorage) {
76
-            this.userId
77
-                = localStorage.getItem('jitsiMeetId') || generateJitsiMeetId();
78
-            this.callStatsUserName
79
-                = localStorage.getItem('callStatsUserName')
80
-                    || generateCallStatsUsername();
81
-
82
-            this.save();
83
-        } else {
84
-            logger.log('localStorage is not supported');
85
-            this.userId = generateJitsiMeetId();
86
-            this.callStatsUserName = generateCallStatsUsername();
22
+            if (localStorage) {
23
+                _callStatsUserName = localStorage.getItem('callStatsUserName');
24
+            }
25
+            if (!_callStatsUserName) {
26
+                _callStatsUserName = generateCallStatsUserName();
27
+                if (localStorage) {
28
+                    localStorage.setItem(
29
+                        'callStatsUserName',
30
+                        _callStatsUserName);
31
+                }
32
+            }
87
         }
33
         }
88
-    }
89
 
34
 
90
-    /**
91
-     * Save settings to localStorage if browser supports that.
92
-     */
93
-    save() {
94
-        const localStorage = getLocalStorage();
95
-
96
-        if (localStorage) {
97
-            localStorage.setItem('jitsiMeetId', this.userId);
98
-            localStorage.setItem('callStatsUserName', this.callStatsUserName);
99
-        }
100
-    }
35
+        return _callStatsUserName;
36
+    },
101
 
37
 
102
     /**
38
     /**
103
      * Returns current machine id.
39
      * Returns current machine id.
104
      * @returns {string} machine id
40
      * @returns {string} machine id
105
      */
41
      */
106
-    getMachineId() {
107
-        return this.userId;
108
-    }
42
+    get machineId() {
43
+        if (!_machineId) {
44
+            const localStorage = getLocalStorage();
45
+
46
+            if (localStorage) {
47
+                _machineId = localStorage.getItem('jitsiMeetId');
48
+            }
49
+            if (!_machineId) {
50
+                _machineId = generateJitsiMeetId();
51
+                if (localStorage) {
52
+                    localStorage.setItem('jitsiMeetId', _machineId);
53
+                }
54
+            }
55
+        }
56
+
57
+        return _machineId;
58
+    },
109
 
59
 
110
     /**
60
     /**
111
-     * Returns fake username for callstats
112
-     * @returns {string} fake username for callstats
61
+     * Returns current session id.
62
+     * @returns {string} current session id
113
      */
63
      */
114
-    getCallStatsUserName() {
115
-        return this.callStatsUserName;
116
-    }
64
+    get sessionId() {
65
+        // We may update sessionId in localStorage from another JitsiConference
66
+        // instance and that's why we should always re-read it.
67
+        const localStorage = getLocalStorage();
68
+
69
+        return localStorage ? localStorage.getItem('sessionId') : undefined;
70
+    },
117
 
71
 
118
     /**
72
     /**
119
      * Save current session id.
73
      * Save current session id.
120
      * @param {string} sessionId session id
74
      * @param {string} sessionId session id
121
      */
75
      */
122
-    setSessionId(sessionId) {
76
+    set sessionId(sessionId) {
123
         const localStorage = getLocalStorage();
77
         const localStorage = getLocalStorage();
124
 
78
 
125
         if (localStorage) {
79
         if (localStorage) {
130
             }
84
             }
131
         }
85
         }
132
     }
86
     }
87
+};
133
 
88
 
134
-    /**
135
-     * Clear current session id.
136
-     */
137
-    clearSessionId() {
138
-        this.setSessionId(undefined);
139
-    }
89
+/**
90
+ * Generate fake username for callstats.
91
+ * @returns {string} fake random username
92
+ */
93
+function generateCallStatsUserName() {
94
+    const username = UsernameGenerator.generateUsername();
140
 
95
 
141
-    /**
142
-     * Returns current session id.
143
-     * @returns {string} current session id
144
-     */
145
-    getSessionId() {
146
-        // We may update sessionId in localStorage from another JitsiConference
147
-        // instance and that's why we should always re-read it.
148
-        const localStorage = getLocalStorage();
96
+    logger.log('generated callstats uid', username);
149
 
97
 
98
+    return username;
99
+}
150
 
100
 
151
-        return localStorage ? localStorage.getItem('sessionId') : undefined;
101
+/**
102
+ * Generate unique id.
103
+ * @returns {string} random unique id
104
+ */
105
+function generateJitsiMeetId() {
106
+    const jitsiMeetId = generateUniqueId();
107
+
108
+    logger.log('generated id', jitsiMeetId);
109
+
110
+    return jitsiMeetId;
111
+}
112
+
113
+/**
114
+ * Gets the localStorage of the browser. (Technically, gets the localStorage of
115
+ * the global object because there may be no browser but React Native for
116
+ * example).
117
+ * @returns {Storage} the local Storage object (if any)
118
+ */
119
+function getLocalStorage() {
120
+    let storage;
121
+
122
+    try {
123
+        // eslint-disable-next-line no-invalid-this
124
+        storage = (window || this).localStorage;
125
+    } catch (error) {
126
+        logger.error(error);
152
     }
127
     }
128
+
129
+    return storage;
153
 }
130
 }
154
 
131
 
155
-export default new Settings();
132
+/**
133
+ *
134
+ */
135
+function generateUniqueId() {
136
+    return _p8() + _p8() + _p8() + _p8();
137
+}
138
+
139
+/**
140
+ *
141
+ */
142
+function _p8() {
143
+    return `${Math.random().toString(16)}000000000`.substr(2, 8);
144
+}

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

70
         this.permanentProperties = Object.create(null);
70
         this.permanentProperties = Object.create(null);
71
 
71
 
72
         this.addPermanentProperties(
72
         this.addPermanentProperties(
73
-            { callstatsname: Settings.getCallStatsUserName() });
73
+            { callstatsname: Settings.callStatsUserName });
74
     }
74
     }
75
 
75
 
76
     /**
76
     /**

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

310
     }
310
     }
311
 
311
 
312
     if (!CallStats.isBackendInitialized()) {
312
     if (!CallStats.isBackendInitialized()) {
313
-        const userName = Settings.getCallStatsUserName();
313
+        const userName = Settings.callStatsUserName;
314
 
314
 
315
         if (!CallStats.initBackend({
315
         if (!CallStats.initBackend({
316
             callStatsID: this.options.callStatsID,
316
             callStatsID: this.options.callStatsID,

+ 8
- 8
modules/xmpp/moderator.js View File

78
 
78
 
79
                 return;
79
                 return;
80
             }
80
             }
81
-            Settings.setSessionId(event.data.sessionId);
81
+            Settings.sessionId = event.data.sessionId;
82
 
82
 
83
             // After popup is closed we will authenticate
83
             // After popup is closed we will authenticate
84
         }
84
         }
143
         type: 'set' });
143
         type: 'set' });
144
 
144
 
145
     // Session Id used for authentication
145
     // Session Id used for authentication
146
-    const sessionId = Settings.getSessionId();
147
-    const machineUID = Settings.getMachineId();
146
+    const { sessionId } = Settings;
147
+    const machineUID = Settings.machineId;
148
 
148
 
149
     logger.info(`Session ID: ${sessionId} machine UID: ${machineUID}`);
149
     logger.info(`Session ID: ${sessionId} machine UID: ${machineUID}`);
150
 
150
 
276
 
276
 
277
     if (sessionId) {
277
     if (sessionId) {
278
         logger.info(`Received sessionId:  ${sessionId}`);
278
         logger.info(`Received sessionId:  ${sessionId}`);
279
-        Settings.setSessionId(sessionId);
279
+        Settings.sessionId = sessionId;
280
     }
280
     }
281
 };
281
 };
282
 
282
 
363
 
363
 
364
     if (invalidSession) {
364
     if (invalidSession) {
365
         logger.info('Session expired! - removing');
365
         logger.info('Session expired! - removing');
366
-        Settings.clearSessionId();
366
+        Settings.sessionId = undefined;
367
     }
367
     }
368
     if ($(error).find('>error>graceful-shutdown').length) {
368
     if ($(error).find('>error>graceful-shutdown').length) {
369
         this.eventEmitter.emit(XMPPEvents.GRACEFUL_SHUTDOWN);
369
         this.eventEmitter.emit(XMPPEvents.GRACEFUL_SHUTDOWN);
495
     const attrs = {
495
     const attrs = {
496
         xmlns: 'http://jitsi.org/protocol/focus',
496
         xmlns: 'http://jitsi.org/protocol/focus',
497
         room: this.roomName,
497
         room: this.roomName,
498
-        'machine-uid': Settings.getMachineId()
498
+        'machine-uid': Settings.machineId
499
     };
499
     };
500
     let str = 'auth url'; // for logger
500
     let str = 'auth url'; // for logger
501
 
501
 
542
 Moderator.prototype.logout = function(callback) {
542
 Moderator.prototype.logout = function(callback) {
543
     const iq = $iq({ to: this.getFocusComponent(),
543
     const iq = $iq({ to: this.getFocusComponent(),
544
         type: 'set' });
544
         type: 'set' });
545
-    const sessionId = Settings.getSessionId();
545
+    const { sessionId } = Settings;
546
 
546
 
547
     if (!sessionId) {
547
     if (!sessionId) {
548
         callback();
548
         callback();
563
                 logoutUrl = decodeURIComponent(logoutUrl);
563
                 logoutUrl = decodeURIComponent(logoutUrl);
564
             }
564
             }
565
             logger.info(`Log out OK, url: ${logoutUrl}`, result);
565
             logger.info(`Log out OK, url: ${logoutUrl}`, result);
566
-            Settings.clearSessionId();
566
+            Settings.sessionId = undefined;
567
             callback(logoutUrl);
567
             callback(logoutUrl);
568
         },
568
         },
569
         error => {
569
         error => {

Loading…
Cancel
Save