ngStorage
ngStorage copied to clipboard
Investigation
After facing some mystical problems with the localStorage I looked into ngStorage
plugin's code and... ugh, well:
-
ngStorage copies whole local and session storage data into the
$storage
variable — well, I keep my localization JSON file into the local storage, I keep SVG icon sprite into the local storage, I keep all my XHR request responses into the session storage (because I useETag
) — and all this is stored in the$storage
variable... this is not good -
$storage
data synchronises with the real local/session storage every time $watcher executed — there is a debounce and there isangular.equals
check but... c'mon... all this runs at every digest cycle -
$storage
data synchronises with the real local/session storage every time data changed — so, every time I do something like$localStorage.foo = 'bar'
whole$storage
object writes itself to the local storage (remember, I keep there very big JSON file and SVG sprite) -
$storage
data synchronizes with the real local/session storage when browser's tab is closed or reloaded — so, if I completely clear the local storage with some other plugin which does not use$localStorage
or with vanilla JS (e.g.localStorage.clear()
) and perform page refresh (e.g.location.reload()
)... whole local storage data mystically appears back in my local storage :) -
there is a
storage
event listener which listens for storage changes (e.g. if I write something to the local storage with vanilla JS), but there is an early return if the event has nokey
... so, if I clear the local storage (e.g.localStorage.clear()
) event listener does not perform_last$storage = angular.copy($storage)
and does not synchronize local storage with the$storage
variable — so,ngStorage
just does not know that local storage is empty already, no wonder why I got all my local storage data synchronized back after I cleared everything few moments ago :)
Well, I don't know what actually I want to say down here with all I've written above — maybe only that this plugin is only useful at very basic level, be careful to use it with other plugins or with vanilla JS.