Index: lib/ioIndexedDB.js |
diff --git a/lib/ioIndexedDB.js b/lib/ioIndexedDB.js |
index 5ffb55e4d2186207e760ccb35ba3ec8dfb8a0d21..59a5df57b21714101b7db1f6f3c604de7d2b0420 100644 |
--- a/lib/ioIndexedDB.js |
+++ b/lib/ioIndexedDB.js |
@@ -157,23 +157,61 @@ function getObjectStore(dbInstance, storeName) |
.objectStore(storeName); |
} |
+function reestablishConnection(dbInstance, retries) |
+{ |
+ dbInstance.close(); |
+ retries = retries || 1; |
Sebastian Noack
2018/08/31 18:07:05
You can use default arguments. Also I would rather
geo
2018/09/04 16:12:08
Done.
|
+ db = openDB(dbConfig); |
+ |
+ return db.catch(err => |
+ { |
+ if (retries == 10) |
+ 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"); |
Sebastian Noack
2018/08/31 18:07:05
Nit: Can this import be moved to the top of the mo
geo
2018/09/04 16:12:08
Yes, we end up with a circular dependency, as inde
|
+ |
+ return IndexedDBBackup.getBackupData() |
+ .then(backupData => |
+ saveFile( |
+ { |
+ fileName: fileToKey(fileName), |
+ content: backupData.content, |
+ lastModified: backupData.lastModified |
+ }, |
+ dbInstance, |
+ storeName) |
+ .then(() => backupData)); |
Sebastian Noack
2018/08/31 18:07:05
Nit: The indentation is a little off here:
.the
geo
2018/09/04 16:12:08
I've changed a bit the indentation, hopefully it's
|
+ } |
+ 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(() => Promise.resolve()); |
}); |
} |
@@ -185,7 +223,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)); |
Sebastian Noack
2018/08/31 18:07:05
Nit: For consistent with other code, can you inden
geo
2018/09/04 16:12:08
Done.
|
+ } |
}); |
} |
@@ -197,7 +244,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); |
}); |
} |