-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLocalStorage.js
44 lines (38 loc) · 1.2 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
function str2ab(str) {
const buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char
const bufView = new Uint16Array(buf);
for (let i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
export default class LocalStorage {
constructor(fileName = "") {
this.fileName = fileName;
this.contentObj = {};
}
set(obj) {
const file = hmFS.open(this.fileName, hmFS.O_RDWR | hmFS.O_TRUNC);
const contentBuffer = str2ab(JSON.stringify(obj));
hmFS.write(file, contentBuffer, 0, contentBuffer.byteLength);
hmFS.close(file);
}
get() {
const [fsStat, err] = hmFS.stat(this.fileName);
if (err === 0) {
const { size } = fsStat;
const fileContentUnit = new Uint16Array(new ArrayBuffer(size));
const file = hmFS.open(this.fileName, hmFS.O_RDONLY | hmFS.O_CREAT);
hmFS.seek(file, 0, hmFS.SEEK_SET);
hmFS.read(file, fileContentUnit.buffer, 0, size);
hmFS.close(file);
try {
const val = String.fromCharCode.apply(null, fileContentUnit);
this.contentObj = val ? JSON.parse(val) : {};
} catch (error) {
this.contentObj = {};
}
}
return this.contentObj;
}
}