From 86c934e8ac857c2de3ba6712baf2459a65a90d8c Mon Sep 17 00:00:00 2001 From: Stefan Zermatten Date: Thu, 18 Jun 2015 15:14:37 +0200 Subject: [PATCH] Began replacing calls to helpers with calls to memoized functions --- rpg-docs/Model/Character/Characters.js | 26 ++++++++++++++----- .../character/stats/hitDice/hitDice.html | 4 +-- .../stats/skillDialog/skillDialog.html | 10 +++---- .../stats/skillDialog/skillDialog.js | 25 ++++++------------ .../character/stats/skillRow/skillRow.html | 6 +++-- .../character/stats/skillRow/skillRow.js | 7 ++--- .../client/views/character/stats/stats.html | 4 +-- rpg-docs/lib/functions/preventLoop.js | 22 +++++++++------- rpg-docs/lib/memoize/memoize.js | 2 +- 9 files changed, 58 insertions(+), 48 deletions(-) diff --git a/rpg-docs/Model/Character/Characters.js b/rpg-docs/Model/Character/Characters.js index 7e83729b..eed8b734 100644 --- a/rpg-docs/Model/Character/Characters.js +++ b/rpg-docs/Model/Character/Characters.js @@ -257,8 +257,16 @@ var attributeBase = function(charId, statName){ }; if (Meteor.isClient) { - Template.registerHelper("charCalculate", function(func, charId, input) { - return Characters.calculate[func](charId, input); + Template.registerHelper("characterCalculate", function(func, charId, input) { + try { + return Characters.calculate[func](charId, input); + } catch (e){ + if (!Characters.calculate[func]){ + throw new Error(func + "is not a function name"); + } else { + throw e; + } + } }); } @@ -311,15 +319,15 @@ Characters.calculate = { attributeValue: memoize(function(charId, attributeName){ var attribute = Characters.calculate.getField(charId, attributeName); //base value - var value = Characters.calculate.attributeBase(attributeName); + var value = Characters.calculate.attributeBase(charId, attributeName); //plus adjustment value += attribute.adjustment; return value; }), - attributeBase: preventLoop(memoize(function(charId, attributeName){ + attributeBase: memoize(preventLoop(function(charId, attributeName){ return attributeBase(charId, attributeName); })), - skillMod: preventLoop(memoize(function(charId, skillName){ + skillMod: memoize(preventLoop(function(charId, skillName){ var skill = Characters.calculate.getField(charId, skillName); //get the final value of the ability score var ability = Characters.calculate.attributeValue(charId, skill.ability); @@ -369,7 +377,7 @@ Characters.calculate = { {charId: charId, name: skillName, enabled: true}, {sort: {value: -1}} ); - return prof && prof.value; + return prof && prof.value || 0; }), passiveSkill: memoize(function(charId, skillName){ var skill = Characters.calculate.getField(charId, skillName); @@ -435,7 +443,11 @@ Characters.calculate = { var depreciated = function() { var err = new Error(); - console.log("this function has been superceeded", {stacktrace: err.stack}); + var name = ""; + if(Template.instance()){ + name = Template.instance().view.name; + } + console.log("this function has been depreciated", {viewName: name, stacktrace: err.stack}); }; //functions and calculated values. diff --git a/rpg-docs/client/views/character/stats/hitDice/hitDice.html b/rpg-docs/client/views/character/stats/hitDice/hitDice.html index 1a1d0b83..3298bb32 100644 --- a/rpg-docs/client/views/character/stats/hitDice/hitDice.html +++ b/rpg-docs/client/views/character/stats/hitDice/hitDice.html @@ -18,10 +18,10 @@
- {{../attributeValue name}} + {{characterCalculate "attributeValue" ../_id name}}
- d{{diceNum}} {{../abilityMod "constitution"}} + d{{diceNum}} {{characterCalculate "abilityMod" ../_id "constitution"}}
diff --git a/rpg-docs/client/views/character/stats/skillDialog/skillDialog.html b/rpg-docs/client/views/character/stats/skillDialog/skillDialog.html index 954dcd61..10953f17 100644 --- a/rpg-docs/client/views/character/stats/skillDialog/skillDialog.html +++ b/rpg-docs/client/views/character/stats/skillDialog/skillDialog.html @@ -1,4 +1,4 @@ - +