Jelajahi Sumber

Optimize subIngredients so that they are calculated up front for each recipe.

Lee Morgan 5 tahun lalu
induk
melakukan
efafe8e1bc

+ 11 - 4
views/dashboardPage/js/classes/Merchant.js

@@ -131,14 +131,17 @@ class Merchant{
                 }
             }
 
-            this._recipes.push(new Recipe(
+            let newRecipe = new Recipe(
                 recipes[i]._id,
                 recipes[i].name,
                 recipes[i].price,
                 ingredients,
                 this,
                 recipes[i].hidden
-            ));
+            );
+
+            newRecipe.calculateIngredientTotals();
+            this._recipes.push(newRecipe);
         }
 
         //populate transactions
@@ -180,6 +183,7 @@ class Merchant{
                 }
             })
             .catch((err)=>{
+                console.log(err);
                 controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
             })
             .finally(()=>{
@@ -292,14 +296,17 @@ class Merchant{
     */
     addRecipes(recipes){
         for(let i = 0; i < recipes.length; i++){
-            this._recipes.push(new Recipe(
+            let newRecipe = new Recipe(
                 recipes[i]._id,
                 recipes[i].name,
                 recipes[i].price,
                 recipes[i].ingredients,
                 this,
                 recipes[i].hidden
-            ));
+            );
+
+            newRecipe.calculateIngredientTotals();
+            this._recipes.push(newRecipe);
         }
     }
 

+ 27 - 0
views/dashboardPage/js/classes/Recipe.js

@@ -63,6 +63,7 @@ class Recipe{
         this._parent = parent;
         this._hidden = hidden;
         this._ingredients = [];
+        this._ingredientTotals = {};
 
         for(let i = 0; i < ingredients.length; i++){
             const ingredient = parent.getIngredient(ingredients[i].ingredient);
@@ -111,6 +112,14 @@ class Recipe{
         return this._ingredients;
     }
 
+    get ingredientTotals(){
+        return this._ingredientTotals;
+    }
+
+    getIngredientTotal(id){
+        return (this._ingredientTotals[id] === undefined) ? 0 : this._ingredientTotals[id];
+    }
+
     addIngredient(ingredient, quantity){
         let recipeIngredient = new RecipeIngredient(ingredient, quantity);
         this._ingredients.push(recipeIngredient);
@@ -122,6 +131,24 @@ class Recipe{
     removeIngredients(){
         this._ingredients = [];
     }
+
+    calculateIngredientTotals(){
+        let traverseIngredient = (ingredient, multiplier)=>{
+            for(let i = 0; i < ingredient.subIngredients.length; i++){
+                traverseIngredient(ingredient.subIngredients[i].ingredient, multiplier * ingredient.subIngredients[i].quantity);                
+            }
+
+            if(this._ingredientTotals[ingredient.id] === undefined){
+                this._ingredientTotals[ingredient.id] = multiplier;
+            }else{
+                this._ingredientTotals[ingredient.id] += multiplier;
+            }
+        }
+
+        for(let i = 0; i < this._ingredients.length; i++){
+            traverseIngredient(this._ingredients[i]._ingredient, this._ingredients[i]._quantity);
+        }
+    }
 }
 
 module.exports = Recipe;

+ 3 - 18
views/dashboardPage/js/classes/Transaction.js

@@ -44,27 +44,12 @@ class Transaction{
     Gets the quantity for a given ingredient
     */
     getIngredientQuantity(ingredient){
-        let traverseIngredient = (transIngredient)=>{
-            let total = 0;
-            for(let i = 0; i < transIngredient.subIngredients.length; i++){
-                if(transIngredient.subIngredients[i].ingredient === ingredient) return transIngredient.subIngredients[i].quantity;
-                total += traverseIngredient(transIngredient.subIngredients[i].ingredient) * transIngredient.subIngredients[i].quantity;
-            }
-
-            return total;
-        }
-
-        let quantity = 0;
-
+        let total = 0;
         for(let i = 0; i < this._recipes.length; i++){
-            for(let j = 0; j < this._recipes[i].recipe.ingredients.length; j++){
-                let transIngredient = this._recipes[i].recipe.ingredients[j];
-                let multiplier = (ingredient === transIngredient.ingredient) ? 1 : traverseIngredient(transIngredient.ingredient);
-                quantity += multiplier * transIngredient.quantity * this._recipes[i].quantity;
-            }
+            total += this._recipes[i].recipe.getIngredientTotal(ingredient.id);
         }
 
-        return quantity;
+        return total;
     }
 }
 

+ 1 - 13
views/dashboardPage/js/strands/recipeBook.js

@@ -92,19 +92,7 @@ let recipeBook = {
                 if(typeof(response) === "string"){
                     controller.createBanner(response, "error");
                 }else{
-                    let newRecipes = [];
-                    for(let i = 0; i < response.new.length; i++){
-                        newRecipes.push(new Recipe(
-                            response.new[i]._id,
-                            response.new[i].name,
-                            response.new[i].price,
-                            merchant,
-                            [],
-                            response.new[i].hidden
-                        ));
-                    }
-
-                    merchant.addRecipes(newRecipes);
+                    merchant.addRecipes(response);
 
                     for(let i = 0; i < response.removed.length; i++){
                         for(let j = 0; j < merchant.recipes.length; j++){