gsklee / ngStorage

localStorage and sessionStorage done right for AngularJS.
MIT License
2.34k stars 461 forks source link

Investigation #259

Open artuska opened 7 years ago

artuska commented 7 years ago

After facing some mystical problems with the localStorage I looked into ngStorage plugin's code and... ugh, well:

  1. 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 use ETag) — and all this is stored in the $storage variable... this is not good

  2. $storage data synchronises with the real local/session storage every time $watcher executed — there is a debounce and there is angular.equals check but... c'mon... all this runs at every digest cycle

  3. $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)

  4. $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 :)

  5. 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 no key... 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.