Browse Source

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.
dev1
Lyubomir Marinov 9 years ago
parent
commit
24d71f6936
1 changed files with 29 additions and 31 deletions
  1. 29
    31
      modules/settings/Settings.js

+ 29
- 31
modules/settings/Settings.js View File

@@ -1,20 +1,17 @@
1 1
 var logger = require("jitsi-meet-logger").getLogger(__filename);
2
-
3 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 15
 function generateUniqueId() {
19 16
     function _p8() {
20 17
         return (Math.random().toString(16) + "000000000").substr(2, 8);
@@ -48,14 +45,13 @@ function Settings() {
48 45
     this.userId;
49 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 56
         this.save();
61 57
     } else {
@@ -69,12 +65,11 @@ function Settings() {
69 65
  * Save settings to localStorage if browser supports that.
70 66
  */
71 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,10 +93,13 @@ Settings.prototype.getCallStatsUserName = function () {
98 93
  * @param {string} sessionId session id
99 94
  */
100 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,10 +115,10 @@ Settings.prototype.clearSessionId = function () {
117 115
  * @returns {string} current session id
118 116
  */
119 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 124
 module.exports = Settings;

Loading…
Cancel
Save