Refactor IndexedDb boilerplate.
This commit is contained in:
parent
2e6cd66f56
commit
c3d93998d1
2 changed files with 12 additions and 29 deletions
39
index.html
39
index.html
|
|
@ -1077,48 +1077,31 @@ chmod 755 kbtd
|
|||
};
|
||||
});
|
||||
|
||||
async function getFileHandle(path) {
|
||||
async function dbOperation(mode, fn) {
|
||||
const db = await dbPromise;
|
||||
const tx = db.transaction(STORE_NAME, mode);
|
||||
const store = tx.objectStore(STORE_NAME);
|
||||
return new Promise((resolve, reject) => {
|
||||
const tx = db.transaction(STORE_NAME, 'readonly');
|
||||
const store = tx.objectStore(STORE_NAME);
|
||||
const request = store.get(path);
|
||||
const request = fn(store);
|
||||
request.onsuccess = () => resolve(request.result);
|
||||
request.onerror = () => reject(request.error);
|
||||
});
|
||||
}
|
||||
|
||||
async function getFileHandle(path) {
|
||||
return dbOperation('readonly', store => store.get(path));
|
||||
}
|
||||
|
||||
async function storeFileHandle(path, handle) {
|
||||
const db = await dbPromise;
|
||||
return new Promise((resolve, reject) => {
|
||||
const tx = db.transaction(STORE_NAME, 'readwrite');
|
||||
const store = tx.objectStore(STORE_NAME);
|
||||
const request = store.put(handle, path);
|
||||
request.onsuccess = () => resolve();
|
||||
request.onerror = () => reject(request.error);
|
||||
});
|
||||
return dbOperation('readwrite', store => store.put(handle, path));
|
||||
}
|
||||
|
||||
async function removeFileHandle(path) {
|
||||
const db = await dbPromise;
|
||||
return new Promise((resolve, reject) => {
|
||||
const tx = db.transaction(STORE_NAME, 'readwrite');
|
||||
const store = tx.objectStore(STORE_NAME);
|
||||
const request = store.delete(path);
|
||||
request.onsuccess = () => resolve();
|
||||
request.onerror = () => reject(request.error);
|
||||
});
|
||||
return dbOperation('readwrite', store => store.delete(path));
|
||||
}
|
||||
|
||||
async function getAllStoredProjects() {
|
||||
const db = await dbPromise;
|
||||
return new Promise((resolve, reject) => {
|
||||
const tx = db.transaction(STORE_NAME, 'readonly');
|
||||
const store = tx.objectStore(STORE_NAME);
|
||||
const request = store.getAllKeys();
|
||||
request.onsuccess = () => resolve(request.result);
|
||||
request.onerror = () => reject(request.error);
|
||||
});
|
||||
return dbOperation('readonly', store => store.getAllKeys());
|
||||
}
|
||||
|
||||
async function validateFileHandle(handle) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue