forked from NathanaelA/nativescript-localstorage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalstorage.js
133 lines (118 loc) · 3.42 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**********************************************************************************
* (c) 2017, Nathanael Anderson.
* Licensed under the MIT license.
*
* Version 1.1.5 [email protected]
**********************************************************************************/
'use strict';
var FileSystemAccess = require('file-system/file-system-access').FileSystemAccess;
// So that code that is looking for the "Storage" object will pass its check
if (!global.Storage) {
global.Storage = function Storage() { }
}
if (!global.localStorage) {
var localStorageData = {};
var localStorageTimeout = null;
var internalSaveData = function() {
var fsa = new FileSystemAccess();
var fileName = fsa.getDocumentsFolderPath() + "/localStorage.db";
try {
fsa.writeText(fileName, JSON.stringify(localStorageData));
} catch (err) {
// This should never happen on normal data, but if they tried to put non JS stuff it won't serialize
console.log("localStorage: unable to write storage, error: ", err);
}
};
var saveData = function() {
if (localStorageTimeout !== null) {
clearTimeout(localStorageTimeout);
}
localStorageTimeout = setTimeout(internalSaveData, 250);
};
var loadData = function() {
var fsa = new FileSystemAccess();
var fileName = fsa.getDocumentsFolderPath() + "/localStorage.db";
if (!fsa.fileExists(fileName)) {
return;
}
var data;
try {
var textData = fsa.readText(fileName);
data = JSON.parse(textData);
localStorageData = data;
}
catch (err) {
console.log("localStorage: error reading storage, Error: ", err);
}
};
loadData();
global.localStorage = {
getItem: function (name) {
if (localStorageData.hasOwnProperty(name)) {
return localStorageData[name];
}
return null;
},
key: function(id) {
var keys = Object.keys(localStorageData);
if (id >= keys.length) { return null; }
return keys[id];
},
setItem: function (name, value) {
localStorageData[name] = value;
saveData();
},
removeItem: function (name) {
if (localStorageData[name]) {
delete localStorageData[name];
saveData();
}
},
clear: function () {
localStorageData = {};
saveData();
}
};
Object.defineProperty(global.localStorage, "length", {
get: function() {
return (Object.keys(localStorageData).length);
},
enumerable: true,
configurable: true
});
}
if (!global.sessionStorage) {
var sessionStorageData = {};
global.sessionStorage = {
getItem: function (name) {
if (sessionStorageData.hasOwnProperty(name)) {
return sessionStorageData[name];
}
return null;
},
key: function(id) {
var keys = Object.keys(sessionStorageData);
if (id >= keys.length) { return null; }
return keys[id];
},
setItem: function (name, value) {
sessionStorageData[name] = value;
},
removeItem: function (name) {
if (sessionStorageData[name]) {
delete sessionStorageData[name];
}
},
clear: function () {
sessionStorageData = {};
}
};
Object.defineProperty(global.sessionStorage, "length", {
get: function() {
return (Object.keys(sessionStorageData).length);
},
enumerable: true,
configurable: true
});
}
module.exports = global.localStorage;