-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.js
executable file
·44 lines (36 loc) · 983 Bytes
/
cache.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
const AsyncStorage = require('./storage');
var cache = {
TOKEN: 'token',
get: function(name, opt_parser) {
// Then check local storage
return AsyncStorage.getItem(name).then((data) => {
// Then see if we can make an object, fallback to string
try {
data = JSON.parse(data);
} catch(e) {
// Debug here if you want, but this fallback is by design.
}
if (opt_parser) return opt_parser(data);
return data;
});
},
set: function(name, value) {
// Then try to force objects to strings
try {
if (typeof value === "string") {
value = JSON.stringify(value);
}
} catch(e) {
// TODO: when debugging, show an error
}
// Lastly, we send it to local storage
return AsyncStorage.setItem(name, value);
},
delete: function(name) {
return AsyncStorage.removeItem(name);
},
clearAll: function() {
return AsyncStorage.clear();
}
};
module.exports = cache;