Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/ioIndexedDB.js

Issue 29860578: Issue 6775 - Work around filter data stored in IndexedDB getting lost on Microsoft Edge (Closed)
Patch Set: Created Sept. 5, 2018, 3:53 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
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);
});
}
« no previous file with comments | « lib/indexedDBBackup.js ('k') | metadata.edge » ('j') | qunit/tests/indexedDBBackup.js » ('J')

Powered by Google App Engine
This is Rietveld