diff --git a/load.js b/load.js index 87bdb77..e81a6b4 100644 --- a/load.js +++ b/load.js @@ -13,47 +13,40 @@ let itemLists = new Map(); * Load item set from local DB. Calls init() on success. */ async function load_local(init_func) { - if (!itemMap) { - let get_tx = db.transaction(['item_db', 'set_db'], 'readonly'); - let sets_store = get_tx.objectStore('set_db'); - let get_store = get_tx.objectStore('item_db'); - let request = get_store.getAll(); - request.onerror = function(event) { - console.log("Could not read local item db..."); - } - request.onsuccess = function(event) { - console.log("Successfully read local item db."); - items = request.result; - //console.log(items); - let request2 = sets_store.openCursor(); - - sets = {}; - request2.onerror = function(event) { - console.log("Could not read local set db..."); - } - - request2.onsuccess = function(event) { - let cursor = event.target.result; - if (cursor) { - sets[cursor.primaryKey] = cursor.value; - cursor.continue(); - } - else { - console.log("Successfully read local set db."); - //console.log(sets); - init_maps(); - } - } - } - await get_tx.complete; - db.close(); - } else { - console.log("Item and Set dbs already loaded!"); + let get_tx = db.transaction(['item_db', 'set_db'], 'readonly'); + let sets_store = get_tx.objectStore('set_db'); + let get_store = get_tx.objectStore('item_db'); + let request = get_store.getAll(); + request.onerror = function(event) { + console.log("Could not read local item db..."); } + request.onsuccess = function(event) { + console.log("Successfully read local item db."); + items = request.result; + //console.log(items); + let request2 = sets_store.openCursor(); - if (itemMap) { - init_func(); + sets = {}; + request2.onerror = function(event) { + console.log("Could not read local set db..."); + } + + request2.onsuccess = function(event) { + let cursor = event.target.result; + if (cursor) { + sets[cursor.primaryKey] = cursor.value; + cursor.continue(); + } + else { + console.log("Successfully read local set db."); + //console.log(sets); + init_maps(); + } + } } + await get_tx.complete; + db.close(); + init_func(); } /* diff --git a/load_ing.js b/load_ing.js index c69bb02..83cc13a 100644 --- a/load_ing.js +++ b/load_ing.js @@ -21,35 +21,29 @@ let recipeIDMap; */ async function ing_load_local(init_func) { console.log("IngMap is: \n " + ingMap); - if (!ingMap) { - let get_tx = idb.transaction(['ing_db', 'recipe_db'], 'readonly'); - let ings_store = get_tx.objectStore('ing_db'); - let recipes_store = get_tx.objectStore('recipe_db'); - let request3 = ings_store.getAll(); - request3.onerror = function(event) { - console.log("Could not read local ingredient db..."); - } - request3.onsuccess = function(event) { - console.log("Successfully read local ingredient db."); - ings = request3.result; - let request4 = recipes_store.getAll(); - request4.onerror = function(event) { - console.log("Could not read local recipe db..."); - } - request4.onsuccess = function(event) { - console.log("Successfully read local recipe db."); - recipes = request4.result; - init_ing_maps(); - } - } - await get_tx.complete; - idb.close(); - } else { - console.log("Ingredient and Recipe dbs already loaded!"); + let get_tx = idb.transaction(['ing_db', 'recipe_db'], 'readonly'); + let ings_store = get_tx.objectStore('ing_db'); + let recipes_store = get_tx.objectStore('recipe_db'); + let request3 = ings_store.getAll(); + request3.onerror = function(event) { + console.log("Could not read local ingredient db..."); } - if (ingMap) { - init_func(); + request3.onsuccess = function(event) { + console.log("Successfully read local ingredient db."); + ings = request3.result; + let request4 = recipes_store.getAll(); + request4.onerror = function(event) { + console.log("Could not read local recipe db..."); + } + request4.onsuccess = function(event) { + console.log("Successfully read local recipe db."); + recipes = request4.result; + init_ing_maps(); + } } + await get_tx.complete; + idb.close(); + init_func(); } function clean_ing(ing) {