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

Make localStorage completely optional

Since half of the Settings functions check whether localStorage is
supported, adds the check to the other half to make localStorage
completely optional.
master
Lyubomir Marinov 9 лет назад
Родитель
Сommit
24d71f6936
1 измененных файлов: 29 добавлений и 31 удалений
  1. 29
    31
      modules/settings/Settings.js

+ 29
- 31
modules/settings/Settings.js Просмотреть файл

1
 var logger = require("jitsi-meet-logger").getLogger(__filename);
1
 var logger = require("jitsi-meet-logger").getLogger(__filename);
2
-
3
 var UsernameGenerator = require('../util/UsernameGenerator');
2
 var UsernameGenerator = require('../util/UsernameGenerator');
4
 
3
 
5
 /**
4
 /**
6
- * Check if browser supports localStorage.
7
- * @returns {boolean} true if supports, false otherwise
5
+ * Gets the localStorage of the browser. (Technically, gets the localStorage of
6
+ * the global object because there may be no browser but React Native for
7
+ * example).
8
+ * @returns {Storage} the local Storage object (if any)
8
  */
9
  */
9
-function supportsLocalStorage() {
10
-    try {
11
-        return 'localStorage' in window && window.localStorage !== null;
12
-    } catch (e) {
13
-        return false;
14
-    }
10
+function getLocalStorage() {
11
+    var global = typeof window == 'undefined' ? this : window;
12
+    return global.localStorage;
15
 }
13
 }
16
 
14
 
17
-
18
 function generateUniqueId() {
15
 function generateUniqueId() {
19
     function _p8() {
16
     function _p8() {
20
         return (Math.random().toString(16) + "000000000").substr(2, 8);
17
         return (Math.random().toString(16) + "000000000").substr(2, 8);
48
     this.userId;
45
     this.userId;
49
     this.callStatsUserName;
46
     this.callStatsUserName;
50
 
47
 
51
-    if (supportsLocalStorage()) {
52
-        this.userId = window.localStorage.getItem('jitsiMeetId')
53
-            || generateJitsiMeetId();
54
-
55
-
56
-        this.callStatsUserName = window.localStorage.getItem(
57
-            'callStatsUserName'
58
-        ) || generateCallStatsUsername();
48
+    var localStorage = getLocalStorage();
49
+    if (localStorage) {
50
+        this.userId
51
+            = localStorage.getItem('jitsiMeetId') || generateJitsiMeetId();
52
+        this.callStatsUserName
53
+            = localStorage.getItem('callStatsUserName')
54
+                || generateCallStatsUsername();
59
 
55
 
60
         this.save();
56
         this.save();
61
     } else {
57
     } else {
69
  * Save settings to localStorage if browser supports that.
65
  * Save settings to localStorage if browser supports that.
70
  */
66
  */
71
 Settings.prototype.save = function () {
67
 Settings.prototype.save = function () {
72
-    if (!supportsLocalStorage()) {
73
-        return;
68
+    var localStorage = getLocalStorage();
69
+    if (localStorage) {
70
+        localStorage.setItem('jitsiMeetId', this.userId);
71
+        localStorage.setItem('callStatsUserName', this.callStatsUserName);
74
     }
72
     }
75
-
76
-    window.localStorage.setItem('jitsiMeetId', this.userId);
77
-    window.localStorage.setItem('callStatsUserName', this.callStatsUserName);
78
 };
73
 };
79
 
74
 
80
 /**
75
 /**
98
  * @param {string} sessionId session id
93
  * @param {string} sessionId session id
99
  */
94
  */
100
 Settings.prototype.setSessionId = function (sessionId) {
95
 Settings.prototype.setSessionId = function (sessionId) {
101
-    if (sessionId) {
102
-        window.localStorage.setItem('sessionId', sessionId);
103
-    } else {
104
-        window.localStorage.removeItem('sessionId');
96
+    var localStorage = getLocalStorage();
97
+    if (localStorage) {
98
+        if (sessionId) {
99
+            localStorage.setItem('sessionId', sessionId);
100
+        } else {
101
+            localStorage.removeItem('sessionId');
102
+        }
105
     }
103
     }
106
 };
104
 };
107
 
105
 
117
  * @returns {string} current session id
115
  * @returns {string} current session id
118
  */
116
  */
119
 Settings.prototype.getSessionId = function () {
117
 Settings.prototype.getSessionId = function () {
120
-    // we can update session id in localStorage from
121
-    // another JitsiConference instance
122
-    // thats why we should always re-read it
123
-    return window.localStorage.getItem('sessionId');
118
+    // We may update sessionId in localStorage from another JitsiConference
119
+    // instance and that's why we should always re-read it.
120
+    var localStorage = getLocalStorage();
121
+    return localStorage ? localStorage.getItem('sessionId') : undefined;
124
 };
122
 };
125
 
123
 
126
 module.exports = Settings;
124
 module.exports = Settings;

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