Skip to content

Commit

Permalink
Ensure read storage only once
Browse files Browse the repository at this point in the history
  • Loading branch information
allmywant committed Dec 20, 2019
1 parent efec23b commit 043cbb1
Showing 1 changed file with 13 additions and 39 deletions.
52 changes: 13 additions & 39 deletions lib/mojito.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,67 +405,41 @@ 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 ()
{
return this.getStoredDecision().recipe;
},
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)
{
Expand Down

0 comments on commit 043cbb1

Please sign in to comment.