From 043cbb15effbdf534d6afabaf9579b2d68cfabc5 Mon Sep 17 00:00:00 2001 From: David Lee Date: Fri, 20 Dec 2019 12:31:12 +0800 Subject: [PATCH] Ensure read storage only once --- lib/mojito.js | 52 +++++++++++++-------------------------------------- 1 file changed, 13 insertions(+), 39 deletions(-) diff --git a/lib/mojito.js b/lib/mojito.js index 6bb575e..13d5e75 100644 --- a/lib/mojito.js +++ b/lib/mojito.js @@ -405,40 +405,22 @@ Mojito = (function () }, setInTest: function (val) { - var cookie = this.getCookie(), - values; + var storedDecision = this.getStoredDecision(), + inTest = val == 1; - if (cookie != null) + if (storedDecision.inTest !== inTest) { - values = cookie.split('.'); - - // Return if inTest value is unchanged - if (values[0] == val) - { - return; - } - - if (values.length == 2) + if (storedDecision.recipe == null) { - this.setCookie('', val + '.' + values[1]); + this.setCookie('', val); } else { - this.setCookie('', val); + this.setCookie('', val + '.' + storedDecision.recipe); } - } - else - { - // Cookie doesn't exist yet - this.setCookie('', val); - } - if (!this.options.storedDecision) - { - this.options.storedDecision = {}; + storedDecision.inTest = inTest; } - - this.options.storedDecision.inTest = val == 1; }, getRecipe: function () { @@ -446,26 +428,18 @@ Mojito = (function () }, setRecipe: function (recipe) { - var cookie = this.getCookie(), - values; - if (Utils.arrayIndexOf(this.recipes, recipe) == -1) { return; } - else - { - values = cookie.split('.'); - // Return if recipe value is unchanged - if (values[1] == recipe) - { - return; - } - this.setCookie('', values[0] + '.' + recipe); - } + var storedDecision = this.getStoredDecision(); - this.options.storedDecision.recipe = recipe; + if (storedDecision.recipe !== recipe) + { + this.setCookie('', (storedDecision?'1':'0') + '.' + recipe); + storedDecision.recipe = recipe; + } }, setCookie: function (name, value) {