codecombat/app/core/storage.coffee

27 lines
898 B
CoffeeScript
Raw Normal View History

# Pass false for fromCache to fetch keys that have been stored outside of lscache.
module.exports.load = (key, fromCache=true) ->
return lscache.get key if fromCache
2014-01-03 13:32:13 -05:00
s = localStorage.getItem(key)
return null unless s
try
value = JSON.parse(s)
return value
catch SyntaxError
console.warn('error loading from storage', key)
2014-01-03 13:32:13 -05:00
return null
# Pass 0 for expirationInMinutes to persist it as long as possible outside of lscache expiration.
module.exports.save = (key, value, expirationInMinutes) ->
expirationInMinutes ?= 7 * 24 * 60
if expirationInMinutes
lscache.set key, value, expirationInMinutes
else
localStorage.setItem key, JSON.stringify(value)
# Pass false for fromCache to remove keys that have been stored outside of lscache.
module.exports.remove = (key, fromCache=true) ->
if fromCache
lscache.remove key
else
localStorage.removeItem key