Skip to content

Commit

Permalink
lib: added _get() and _set() to save values in localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
gheja committed Sep 22, 2017
1 parent 244bfcb commit b3a5e3f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const GAME_STATE_READY = 2;
const GAME_STATE_WON = 3;
const GAME_STATE_LOST = 4;

const LOCAL_STORAGE_PREFIX = "whereiswinston";

// [ [ h, s, l, type ], [ ... ], ... ]
let BODY_TYPE_DEFINITIONS =
[
Expand Down
27 changes: 27 additions & 0 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,33 @@ const PI2 = 2 * PI;

var _raf = window.requestAnimationFrame;

/*
"localStorage does not write or delete entries instantly. If you create
or delete a localStorage key, always have local variables updated at the
same time, because when you need them later, localStorage may not have
updated itself yet, and there are chances it won't contain what you think
it contains." - http://xem.github.io/articles/#js13k17
thx Maxime Euzière!
*/
let _localStorageCache = {};

function _get(key)
{
if (_localStorageCache[key] === undefined)
{
_localStorageCache[key] = window.localStorage.getItem(LOCAL_STORAGE_PREFIX + ":" + key);
}

return _localStorageCache[key];
}

function _set(key, value)
{
_localStorageCache[key] = value;

window.localStorage.setItem(LOCAL_STORAGE_PREFIX + ":" + key, value);
}

function _scale(x)
{
return x * _windowScale;
Expand Down

0 comments on commit b3a5e3f

Please sign in to comment.