'use strict';

(function() {

    /**
     * @ngdoc overview
     * @name ngStorage
     */

    angular.module('ngStorage', []).

    /**
     * @ngdoc object
     * @name ngStorage.$localStorage
     * @requires $rootScope
     * @requires $browser
     * @requires $window
     */

    factory('$localStorage', _storageFactory('localStorage')).

    /**
     * @ngdoc object
     * @name ngStorage.$sessionStorage
     * @requires $rootScope
     * @requires $browser
     * @requires $window
     */

    factory('$sessionStorage', _storageFactory('sessionStorage'));

    function _storageFactory(storageType) {
        return [
            '$rootScope',
            '$browser',
            '$window',

            function(
                $rootScope,
                $browser,
                $window
            ){
                // #9: Assign a placeholder object if Web Storage is unavailable to prevent breaking the entire AngularJS app
                var webStorage = $window[storageType] || (console.warn('This browser does not support Web Storage!'), {}),

                    $storage = {
                        $default: function(items) {
                            for (var k in items) {
                                angular.isDefined($storage[k]) || ($storage[k] = items[k]);
                            }

                            return $storage;
                        },
                        $reset: function(items) {
                            for (var k in $storage) {
                                '$' === k[0] || delete $storage[k];
                            }

                            return $storage.$default(items);
                        }
                    },

                    _last$storage;

                for (var i = 0, k; i < webStorage.length; i++) {
                    // #8, #10: `webStorage.key(i)` may be an empty string (or throw an exception in IE9 if `webStorage` is empty)
                    (k = webStorage.key(i)) && 'ngStorage-' === k.slice(0, 10) && ($storage[k.slice(10)] = angular.fromJson(webStorage.getItem(k)));
                }

                _last$storage = angular.copy($storage);

                $browser.addPollFn(function() {
                    if (!angular.equals($storage, _last$storage)) {
                        angular.forEach($storage, function(v, k) {
                            angular.isDefined(v) && '$' !== k[0] && webStorage.setItem('ngStorage-' + k, angular.toJson(v));

                            delete _last$storage[k];
                        });

                        for (var k in _last$storage) {
                            webStorage.removeItem('ngStorage-' + k);
                        }

                        _last$storage = angular.copy($storage);
                    }
                });

                // #6: Use `$window.addEventListener` instead of `angular.element` to avoid the jQuery-specific `event.originalEvent`
                'localStorage' === storageType && $window.addEventListener('storage', function(event) {
                    if ('ngStorage-' === event.key.slice(0, 10)) {
                        event.newValue ? $storage[event.key.slice(10)] = angular.fromJson(event.newValue) : delete $storage[event.key.slice(10)];

                        _last$storage = angular.copy($storage);

                        $rootScope.$apply();
                    }
                });

                return $storage;
            }
        ];
    }

})();

@@ -222,34 +222,8 @@
 ope%0A
-     * @requires $browser%0A
     
@@ -416,34 +416,8 @@
 ope%0A
-     * @requires $browser%0A
     
@@ -605,32 +605,8 @@
 e',%0A
-            '$browser',%0A
     
@@ -679,34 +679,8 @@
 pe,%0A
-                $browser,%0A
     
@@ -1642,16 +1642,68 @@
 $storage
+,%0A                    %0A                    _debounce
 ;%0A%0A     
@@ -2139,39 +2139,155 @@
    $
-b
 ro
-wser.addPollFn(function() %7B%0A
+otScope.$watch(function() %7B%0A                    _debounce %7C%7C (_debounce = setTimeout(function() %7B%0A                        _debounce = null;%0A%0A    
     
@@ -2366,32 +2366,36 @@
                 
+    
 angular.forEach(
@@ -2441,32 +2441,36 @@
                 
+ 
+   
 angular.isDefine
@@ -2579,16 +2579,20 @@
         
+    
 delete _
@@ -2624,32 +2624,36 @@
                 
+    
 %7D);%0A%0A           
@@ -2657,32 +2657,36 @@
                 
+ 
+   
 for (var k in _l
@@ -2692,32 +2692,36 @@
 last$storage) %7B%0A
+    
                 
@@ -2789,32 +2789,36 @@
                 
+    
 %7D%0A%0A             
@@ -2820,32 +2820,36 @@
                 
+  
+  
 _last$storage = 
@@ -2884,33 +2884,67 @@
                 
-%7D
+    %7D%0A                    %7D, 100));
 %0A               

Resolve #2: Add dirty-check debouncing