Index: lib/ioIndexedDB.js |
diff --git a/lib/ioIndexedDB.js b/lib/ioIndexedDB.js |
index 5ffb55e4d2186207e760ccb35ba3ec8dfb8a0d21..92e140c4d093cbb60e3e5c30b9d06ddd3784244b 100644 |
--- a/lib/ioIndexedDB.js |
+++ b/lib/ioIndexedDB.js |
@@ -157,23 +157,69 @@ function getObjectStore(dbInstance, storeName) |
.objectStore(storeName); |
} |
+function reestablishConnection(dbInstance, retries) |
+{ |
+ dbInstance.close(); |
+ retries = retries || 1; |
+ |
+ return new Promise((resolve) => |
+ { |
+ setTimeout(() => |
+ { |
+ db = openDB(dbConfig); |
+ return resolve(db); |
+ }, 0); |
Sebastian Noack
2018/08/21 20:26:25
Is this timeout necessary?
geo
2018/08/31 15:49:25
This part here is mostly from experimentation. If
Sebastian Noack
2018/08/31 18:07:04
Ok, let's stick with the delay then.
|
+ }) |
+ .catch(err => |
+ { |
+ if (retries == 10) |
+ return Promise.reject(err); |
Sebastian Noack
2018/08/21 20:26:25
Nit: You can just throw the error in here. This wi
geo
2018/08/31 15:49:24
Done.
|
+ |
+ 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() |
Sebastian Noack
2018/08/21 20:26:25
I wonder whether we also have to trigger the subsc
geo
2018/08/31 15:49:24
No need with the information we are currently savi
|
+ .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)); |
- |
- req.onsuccess = event => |
- { |
- let {result} = event.currentTarget; |
+ let req = store.get(fileName); |
- 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()); |
Sebastian Noack
2018/08/21 20:26:25
Nit: This seems redundant.
geo
2018/08/31 15:49:25
I want this to resolve with `undefined` so that `g
Sebastian Noack
2018/08/31 18:07:04
I see, then this should be:
.then(() => undefin
geo
2018/09/04 16:12:07
Done.
|
+ return Promise.reject(error); |
Sebastian Noack
2018/08/21 20:26:25
Nit: See above.
geo
2018/08/31 15:49:25
Done.
|
}); |
} |
@@ -185,7 +231,17 @@ 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)); |
+ } |
+ return Promise.reject(error); |
}); |
} |
@@ -197,7 +253,17 @@ 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) |
+ .then(() => Promise.resolve()); |
Sebastian Noack
2018/08/21 20:26:25
Nit: This seems redundant.
geo
2018/08/31 15:49:24
Done.
|
+ } |
+ |
+ return Promise.reject(error); |
Sebastian Noack
2018/08/21 20:26:25
Nit: See above.
geo
2018/08/31 15:49:25
Done.
|
}); |
} |