Skip to content

Commit

Permalink
Merge pull request #145 from egilkh/combine-unloads
Browse files Browse the repository at this point in the history
Combine unloads approches
  • Loading branch information
egilkh committed Aug 19, 2015
2 parents 757add0 + 7dc256c commit 660e9b7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
46 changes: 26 additions & 20 deletions ngStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,27 @@
// #8, #10: `webStorage.key(i)` may be an empty string (or throw an exception in IE9 if `webStorage` is empty)
(k = webStorage.key(i)) && storageKeyPrefix === k.slice(0, prefixLength) && ($storage[k.slice(prefixLength)] = deserializer(webStorage.getItem(k)));
}
}
},
$apply: function() {
var temp$storage;

_debounce = null;

if (!angular.equals($storage, _last$storage)) {
temp$storage = angular.copy(_last$storage);
angular.forEach($storage, function(v, k) {
angular.isDefined(v) && '$' !== k[0] && webStorage.setItem(storageKeyPrefix + k, serializer(v));

delete temp$storage[k];
});

for (var k in temp$storage) {
webStorage.removeItem(storageKeyPrefix + k);
}

_last$storage = angular.copy($storage);
}
},
},
_last$storage,
_debounce;
Expand All @@ -164,25 +184,7 @@
_last$storage = angular.copy($storage);

$rootScope.$watch(function() {
var temp$storage;
_debounce || (_debounce = $timeout(function() {
_debounce = null;

if (!angular.equals($storage, _last$storage)) {
temp$storage = angular.copy(_last$storage);
angular.forEach($storage, function(v, k) {
angular.isDefined(v) && '$' !== k[0] && webStorage.setItem(storageKeyPrefix + k, serializer(v));

delete temp$storage[k];
});

for (var k in temp$storage) {
webStorage.removeItem(storageKeyPrefix + k);
}

_last$storage = angular.copy($storage);
}
}, 100, false));
_debounce || (_debounce = $timeout($storage.$apply, 100, false));
});

// #6: Use `$window.addEventListener` instead of `angular.element` to avoid the jQuery-specific `event.originalEvent`
Expand All @@ -196,6 +198,10 @@
}
});

$window.addEventListener && $window.addEventListener('beforeunload', function() {
$storage.$sync();
});

return $storage;
}
];
Expand Down
3 changes: 3 additions & 0 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ describe('ngStorage', function() {
delete $storage.$default;
delete $storage.$reset;
delete $storage.$sync;
delete $storage.$apply;

expect($storage).to.deep.equal({
string: 'a string',
Expand Down Expand Up @@ -171,6 +172,7 @@ describe('ngStorage', function() {
delete $storage.$default;
delete $storage.$reset;
delete $storage.$sync;
delete $storage.$apply;

expect($storage).to.deep.equal({});

Expand Down Expand Up @@ -210,6 +212,7 @@ describe('ngStorage', function() {
delete $storage.$default;
delete $storage.$reset;
delete $storage.$sync;
delete $storage.$apply;

expect($storage).to.deep.equal({some: 'value'});

Expand Down

0 comments on commit 660e9b7

Please sign in to comment.