From: Sven Schöling Date: Fri, 7 May 2021 13:49:16 +0000 (+0200) Subject: FileDB: async db.open fixes X-Git-Tag: kivitendo-mebil_0.1-0~9^2~77 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=152a83a6553986090c54962e055d3db68142ab5f;p=kivitendo-erp.git FileDB: async db.open fixes Dadurch, dass das komplette Interface asynchron ist, muss jeder Request an die Datenbank potentiell verzögert werden bis eine Verbindung steht. --- diff --git a/js/kivi.FileDB.js b/js/kivi.FileDB.js index b2109bda8..15e5d5a01 100644 --- a/js/kivi.FileDB.js +++ b/js/kivi.FileDB.js @@ -42,29 +42,37 @@ namespace("kivi.FileDB", function(ns) { }; ns.store_image = function (blob, filename, success) { - let put_request = ns.open_store("readwrite").add(blob, filename); + ns.open_rw_store((store) => { + let put_request = store.add(blob, filename); - put_request.onsuccess = success; - put_request.on_error = ns.onerror; + put_request.onsuccess = success; + put_request.on_error = ns.onerror; + }); }; ns.retrieve_image = function(key, success) { - let get_request = ns.open_store().objectStore(store).get(key); + ns.open_ro_store((store) => { + let get_request = store.get(key); - get_request.onsuccess = success; - get_request.onerror = request.onerror; + get_request.onsuccess = success; + get_request.onerror = request.onerror; + }); }; ns.retrieve_all = function(success) { - let request = ns.open_store().getAll(); - request.onsuccess = (event) => { success(event.target.result); }; - request.onerror = ns.error; + ns.open_ro_store((store) => { + let request = store.getAll(); + request.onsuccess = (event) => { success(event.target.result); }; + request.onerror = ns.error; + }); }; ns.retrieve_all_keys = function(success) { - let request = ns.open_store().getAllKeys(); - request.onsuccess = (event) => { success(event.target.result); }; - request.onerror = ns.error; + ns.open_ro_store((store) => { + let request = store.getAllKeys(); + request.onsuccess = (event) => { success(event.target.result); }; + request.onerror = ns.error; + }); }; ns.delete_all= function() { @@ -74,12 +82,30 @@ namespace("kivi.FileDB", function(ns) { }; ns.delete_key= function(key, success) { - let request = ns.open_store("readwrite").delete(key); - request.onsuccess = (event) => { if (success) success(event.target.result); }; - request.onerror = ns.error; + ns.open_rw_store((store) => { + let request = store.delete(key); + request.onsuccess = (event) => { if (success) success(event.target.result); }; + request.onerror = ns.error; + }); + }; + + ns.open_rw_store = function(callback) { + if (db && db_version == db.version) { + callback(ns.open_store("readwrite")); + } else { + request.aftersuccess.push(() => callback(ns.open_store("readwrite"))); + } + }; + + ns.open_ro_store = function(callback) { + if (db && db_version == db.version) { + callback(ns.open_store("readonly")); + } else { + request.aftersuccess.push(() => callback(ns.open_store("readonly"))); + } }; - ns.open_store = function(mode = "readonly") { + ns.open_store = function(mode = "readonly") { return db.transaction([store], mode).objectStore(store); };