Index: lib/ioIndexedDB.js |
diff --git a/lib/ioIndexedDB.js b/lib/ioIndexedDB.js |
index 5ffb55e4d2186207e760ccb35ba3ec8dfb8a0d21..8cfc5b9b2cdac6537bc74ef7ea09c26a02ca61f9 100644 |
--- a/lib/ioIndexedDB.js |
+++ b/lib/ioIndexedDB.js |
@@ -157,23 +157,59 @@ function getObjectStore(dbInstance, storeName) |
.objectStore(storeName); |
} |
+function reestablishConnection(dbInstance, retries = 10) |
+{ |
+ dbInstance.close(); |
+ db = openDB(dbConfig); |
+ |
+ return db.catch(err => |
+ { |
+ if (!retries) |
+ throw err; |
+ |
+ return reestablishConnection(dbInstance, --retries); |
+ }); |
+} |
+ |
function getFile(fileName, dbInstance, storeName) |
+{ |
+ return getFromIndexedDB(fileToKey(fileName), dbInstance, storeName) |
+ .then(indexedDBResult => |
+ { |
+ if (!indexedDBResult) |
+ { |
+ const {IndexedDBBackup} = require("./indexedDBBackup"); |
+ |
+ return IndexedDBBackup.getBackupData() |
+ .then(backupData => |
+ saveFile( |
+ { |
+ fileName: fileToKey(fileName), |
+ content: backupData.content, |
+ lastModified: backupData.lastModified |
+ }, |
+ dbInstance, |
+ storeName).then(() => backupData) |
+ ); |
+ } |
+ return indexedDBResult; |
+ }); |
+} |
+ |
+function getFromIndexedDB(fileName, dbInstance, storeName) |
{ |
return new Promise((resolve, reject) => |
{ |
let store = getObjectStore(dbInstance, storeName); |
- let req = store.get(fileToKey(fileName)); |
+ let req = store.get(fileName); |
- req.onsuccess = event => |
- { |
- let {result} = event.currentTarget; |
- |
- if (result) |
- resolve(result); |
- else |
- reject({type: "NoSuchFile"}); |
- }; |
- req.onerror = reject; |
+ req.onsuccess = event => resolve(event.currentTarget.result); |
+ req.onerror = event => reject(event.target.error); |
+ }) |
+ .catch(error => |
+ { |
+ if (error.name == "UnknownError") |
+ return reestablishConnection(dbInstance).then(() => undefined); |
}); |
} |
@@ -185,7 +221,16 @@ function saveFile(data, dbInstance, storeName) |
let req = store.put(data); |
req.onsuccess = resolve; |
- req.onerror = reject; |
+ req.onerror = event => reject(event.target.error); |
+ }) |
+ .catch(error => |
+ { |
+ if (error.name == "UnknownError") |
+ { |
+ return reestablishConnection(dbInstance).then(newDbInstance => |
+ saveFile(data, newDbInstance, storeName) |
+ ); |
+ } |
}); |
} |
@@ -197,7 +242,12 @@ function deleteFile(fileName, dbInstance, storeName) |
let req = store.delete(fileToKey(fileName)); |
req.onsuccess = resolve; |
- req.onerror = reject; |
+ req.onerror = event => reject(event.target.error); |
+ }) |
+ .catch(error => |
+ { |
+ if (error.name == "UnknownError") |
+ return reestablishConnection(dbInstance); |
}); |
} |