Index: lib/ioIndexedDB.js |
diff --git a/lib/ioIndexedDB.js b/lib/ioIndexedDB.js |
index 5ffb55e4d2186207e760ccb35ba3ec8dfb8a0d21..6490e81b8b7a02782c336685e0afa69d46447a09 100644 |
--- a/lib/ioIndexedDB.js |
+++ b/lib/ioIndexedDB.js |
@@ -157,23 +157,60 @@ 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); |
Sebastian Noack
2018/09/04 21:00:01
Nit: Wrapping here seems unnecessary. It appears t
geo
2018/09/05 14:04:34
Done.
|
}); |
} |
@@ -185,7 +222,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 +243,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); |
}); |
} |