forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalstorage.js
56 lines (51 loc) · 1.45 KB
/
localstorage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Meteor._localStorage is not an ideal name, but we can change it later.
// Let's test to make sure that localStorage actually works. For example, in
// Safari with private browsing on, window.localStorage exists but actually
// trying to use it throws.
// Accessing window.localStorage can also immediately throw an error in IE (#1291).
var key = '_localstorage_test_' + Random.id();
var retrieved;
try {
if (window.localStorage) {
window.localStorage.setItem(key, key);
retrieved = window.localStorage.getItem(key);
window.localStorage.removeItem(key);
}
} catch (e) {
// ... ignore
}
if (key === retrieved) {
Meteor._localStorage = {
getItem: function (key) {
return window.localStorage.getItem(key);
},
setItem: function (key, value) {
window.localStorage.setItem(key, value);
},
removeItem: function (key) {
window.localStorage.removeItem(key);
}
};
}
if (!Meteor._localStorage) {
Meteor._debug(
"You are running a browser with no localStorage or userData "
+ "support. Logging in from one tab will not cause another "
+ "tab to be logged in.");
Meteor._localStorage = {
_data: {},
setItem: function (key, val) {
this._data[key] = val;
},
removeItem: function (key) {
delete this._data[key];
},
getItem: function (key) {
var value = this._data[key];
if (value === undefined)
return null;
else
return value;
}
};
}