Improved error handling of temp browser store.

This commit is contained in:
Martin Edenhofer 2016-06-27 20:04:18 +02:00
parent b5a00eca14
commit f170a8a870
3 changed files with 44 additions and 6 deletions

View file

@ -26,6 +26,11 @@ class App.LocalStorage
_instance ?= new _storeSingleton _instance ?= new _storeSingleton
_instance.list() _instance.list()
@usage: ->
if _instance == undefined
_instance ?= new _storeSingleton
_instance.usage()
# The actual Singleton class # The actual Singleton class
class _storeSingleton class _storeSingleton
constructor: -> constructor: ->
@ -37,9 +42,7 @@ class _storeSingleton
key = "personal::#{user_id}::#{key}" key = "personal::#{user_id}::#{key}"
localStorage.setItem(key, JSON.stringify(value)) localStorage.setItem(key, JSON.stringify(value))
catch e catch e
if e is QUOTA_EXCEEDED_ERR App.Log.error 'App.LocalStorage', 'Local storage error!', e
# do something nice to notify your users
App.Log.error 'App.LocalStorage', 'Local storage quote exceeded!'
# get item # get item
get: (key, user_id) -> get: (key, user_id) ->
@ -62,3 +65,12 @@ class _storeSingleton
# return list of all keys # return list of all keys
list: -> list: ->
window.localStorage window.localStorage
# get usage
usage: ->
total = ''
for key of window.localStorage
value = localStorage.getItem(key)
if _.isString(value)
total += value
byteLength(total)

View file

@ -26,6 +26,11 @@ class App.SessionStorage
_instance ?= new _storeSingleton _instance ?= new _storeSingleton
_instance.list() _instance.list()
@usage: ->
if _instance == undefined
_instance ?= new _storeSingleton
_instance.usage()
# The actual Singleton class # The actual Singleton class
class _storeSingleton class _storeSingleton
constructor: -> constructor: ->
@ -38,9 +43,8 @@ class _storeSingleton
try try
sessionStorage.setItem(key, JSON.stringify(value)) sessionStorage.setItem(key, JSON.stringify(value))
catch e catch e
if e is QUOTA_EXCEEDED_ERR @clear()
# do something nice to notify your users App.Log.error 'App.SessionStorage', 'Session storage error!', e
App.Log.error 'App.SessionStorage', 'Session storage quote exceeded!'
# get item # get item
get: (key) -> get: (key) ->
@ -59,3 +63,12 @@ class _storeSingleton
# return list of all keys # return list of all keys
list: -> list: ->
window.sessionStorage window.sessionStorage
# get usage
usage: ->
total = ''
for key of window.sessionStorage
value = sessionStorage.getItem(key)
if _.isString(value)
total += value
byteLength(total)

View file

@ -80,6 +80,19 @@ function difference(object1, object2) {
return changes; return changes;
} }
// returns the byte length of an utf8 string
// taken from http://stackoverflow.com/questions/5515869/string-length-in-bytes-in-javascript
function byteLength(str) {
var s = str.length
for (var i=str.length-1; i>=0; i--) {
var code = str.charCodeAt(i)
if (code > 0x7f && code <= 0x7ff) s++
else if (code > 0x7ff && code <= 0xffff) s+=2
if (code >= 0xDC00 && code <= 0xDFFF) i-- //trail surrogate
}
return s
}
// clone, just data, no instances of objects // clone, just data, no instances of objects
function clone(item, full) { function clone(item, full) {