-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.js
125 lines (123 loc) · 4.61 KB
/
storage.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
class EJS_STORAGE {
constructor(dbName, storeName) {
this.dbName = dbName;
this.storeName = storeName;
}
addFileToDB(key, add) {
(async () => {
if (key === "?EJS_KEYS!") return;
let keys = await this.get("?EJS_KEYS!");
if (!keys) keys = [];
if (add) {
if (!keys.includes(key)) keys.push(key);
} else {
const index = keys.indexOf(key);
if (index !== -1) keys.splice(index, 1);
}
this.put("?EJS_KEYS!", keys);
})();
}
get(key) {
return new Promise((resolve, reject) => {
if (!window.indexedDB) return resolve();
let openRequest = indexedDB.open(this.dbName, 1);
openRequest.onerror = () => resolve();
openRequest.onsuccess = () => {
let db = openRequest.result;
let transaction = db.transaction([this.storeName], "readwrite");
let objectStore = transaction.objectStore(this.storeName);
let request = objectStore.get(key);
request.onsuccess = (e) => {
resolve(request.result);
};
request.onerror = () => resolve();
};
openRequest.onupgradeneeded = () => {
let db = openRequest.result;
if (! db.objectStoreNames.contains(this.storeName)) {
db.createObjectStore(this.storeName);
};
};
});
}
put(key, data) {
return new Promise((resolve, reject) => {
if (!window.indexedDB) return resolve();
let openRequest = indexedDB.open(this.dbName, 1);
openRequest.onerror = () => {};
openRequest.onsuccess = () => {
let db = openRequest.result;
let transaction = db.transaction([this.storeName], "readwrite");
let objectStore = transaction.objectStore(this.storeName);
let request = objectStore.put(data, key);
request.onerror = () => resolve();
request.onsuccess = () => {
this.addFileToDB(key, true);
resolve();
}
};
openRequest.onupgradeneeded = () => {
let db = openRequest.result;
if (! db.objectStoreNames.contains(this.storeName)) {
db.createObjectStore(this.storeName);
};
};
})
}
remove(key) {
return new Promise((resolve, reject) => {
if (!window.indexedDB) return resolve();
let openRequest = indexedDB.open(this.dbName, 1);
openRequest.onerror = () => {};
openRequest.onsuccess = () => {
let db = openRequest.result;
let transaction = db.transaction([this.storeName], "readwrite");
let objectStore = transaction.objectStore(this.storeName);
let request2 = objectStore.delete(key);
this.addFileToDB(key, false);
request2.onsuccess = () => resolve();
request2.onerror = () => {};
};
openRequest.onupgradeneeded = () => {
let db = openRequest.result;
if (! db.objectStoreNames.contains(this.storeName)) {
db.createObjectStore(this.storeName);
};
};
});
}
getSizes() {
return new Promise(async (resolve, reject) => {
if (!window.indexedDB) resolve({});
const keys = await this.get("?EJS_KEYS!");
if (!keys) return resolve({});
let rv = {};
for (let i=0; i<keys.length; i++) {
const result = await this.get(keys[i]);
if (!result || !result.data || typeof result.data.byteLength !== "number") continue;
rv[keys[i]] = result.data.byteLength;
}
resolve(rv);
})
}
}
class EJS_DUMMYSTORAGE {
constructor() {}
addFileToDB() {
return new Promise(resolve => resolve());
}
get() {
return new Promise(resolve => resolve());
}
put() {
return new Promise(resolve => resolve());
}
remove() {
return new Promise(resolve => resolve());
}
getSizes() {
return new Promise(resolve => resolve({}));
}
}
window.EJS_STORAGE = EJS_STORAGE;
window.EJS_DUMMYSTORAGE = EJS_DUMMYSTORAGE;