-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor-cache.js
More file actions
74 lines (68 loc) · 2.55 KB
/
Copy patheditor-cache.js
File metadata and controls
74 lines (68 loc) · 2.55 KB
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
// CZEditor Cache — IndexedDB-based storage for large data
// Replaces localStorage for file content cache to avoid 5MB quota limit
const CZCache = (() => {
'use strict';
const DB_NAME = 'czeditor_cache';
const DB_VERSION = 1;
const STORE_NAME = 'cache';
function openDB() {
return new Promise((resolve, reject) => {
const req = indexedDB.open(DB_NAME, DB_VERSION);
req.onupgradeneeded = (e) => {
const db = e.target.result;
if (!db.objectStoreNames.contains(STORE_NAME)) {
db.createObjectStore(STORE_NAME);
}
};
req.onsuccess = () => resolve(req.result);
req.onerror = () => reject(req.error);
});
}
async function set(key, value) {
try {
const db = await openDB();
return new Promise((resolve, reject) => {
const tx = db.transaction(STORE_NAME, 'readwrite');
const store = tx.objectStore(STORE_NAME);
const req = store.put(value, key);
req.onsuccess = () => resolve();
req.onerror = () => reject(req.error);
tx.oncomplete = () => db.close();
});
} catch (e) {
console.warn('[CZCache] set failed:', e.message);
}
}
async function get(key) {
try {
const db = await openDB();
return new Promise((resolve, reject) => {
const tx = db.transaction(STORE_NAME, 'readonly');
const store = tx.objectStore(STORE_NAME);
const req = store.get(key);
req.onsuccess = () => resolve(req.result);
req.onerror = () => reject(req.error);
tx.oncomplete = () => db.close();
});
} catch (e) {
console.warn('[CZCache] get failed:', e.message);
return undefined;
}
}
async function remove(key) {
try {
const db = await openDB();
return new Promise((resolve, reject) => {
const tx = db.transaction(STORE_NAME, 'readwrite');
const store = tx.objectStore(STORE_NAME);
const req = store.delete(key);
req.onsuccess = () => resolve();
req.onerror = () => reject(req.error);
tx.oncomplete = () => db.close();
});
} catch (e) {
console.warn('[CZCache] remove failed:', e.message);
}
}
return { set, get, remove };
})();