X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Futils%2FcompgameStorage.js;h=e30a69fbeb853d68ca57eb02678bb7b27aa27e10;hb=2c5d7b20742b802d9c47916915c1114bcfc9a9c3;hp=05a2074900214b48515841ecbeb906164c25e3c9;hpb=98b94cc3504937ae0af29a265394dfe52ea1ee67;p=vchess.git diff --git a/client/src/utils/compgameStorage.js b/client/src/utils/compgameStorage.js index 05a20749..e30a69fb 100644 --- a/client/src/utils/compgameStorage.js +++ b/client/src/utils/compgameStorage.js @@ -15,29 +15,34 @@ function dbOperation(callback) { let DBOpenRequest = window.indexedDB.open("vchess_comp", 4); DBOpenRequest.onerror = function(event) { - alert(store.state.tr["Database error: stop private browsing, or update your browser"]); - callback("error",null); + alert(store.state.tr[ + "Database error: stop private browsing, or update your browser"]); + callback("error", null); }; DBOpenRequest.onsuccess = function(event) { db = DBOpenRequest.result; - callback(null,db); + callback(null, db); db.close(); }; DBOpenRequest.onupgradeneeded = function(event) { let db = event.target.result; - let objectStore = db.createObjectStore("compgames", { keyPath: "vname" }); + let upgradeTransaction = event.target.transaction; + if (!db.objectStoreNames.contains("compgames")) + db.createObjectStore("compgames", { keyPath: "vname" }); + else + upgradeTransaction.objectStore("compgames"); }; } export const CompgameStorage = { add: function(game) { dbOperation((err,db) => { - if (err) - return; - let transaction = db.transaction("compgames", "readwrite"); - let objectStore = transaction.objectStore("compgames"); + if (err) return; + let objectStore = db + .transaction("compgames", "readwrite") + .objectStore("compgames"); objectStore.add(game); }); }, @@ -66,7 +71,9 @@ export const CompgameStorage = { // NOTE: need callback because result is obtained asynchronously get: function(gameId, callback) { dbOperation((err,db) => { - let objectStore = db.transaction("compgames").objectStore("compgames"); + let objectStore = db + .transaction("compgames", "readonly") + .objectStore("compgames"); objectStore.get(gameId).onsuccess = function(event) { callback(event.target.result); }; @@ -77,8 +84,9 @@ export const CompgameStorage = { remove: function(gameId) { dbOperation((err,db) => { if (!err) { - let transaction = db.transaction(["compgames"], "readwrite"); - transaction.objectStore("compgames").delete(gameId); + db.transaction("compgames", "readwrite") + .objectStore("compgames") + .delete(gameId); } }); }