Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

debounce watcher causing heavy load in ngUpgrade apps #258

Open
sime opened this issue Jan 30, 2017 · 4 comments
Open

debounce watcher causing heavy load in ngUpgrade apps #258

sime opened this issue Jan 30, 2017 · 4 comments

Comments

@sime
Copy link

sime commented Jan 30, 2017

The $rootScope.$watch() causes a high load in ngUpgraded apps (2.4.x).

When I comment out the following block, the DevTools timeline shows next to no activity.

ngStorage/ngStorage.js

Lines 206 to 208 in c217d5b

$rootScope.$watch(function() {
_debounce || (_debounce = $timeout($storage.$apply, 100, false));
});

@bluegaspode
Copy link

Personally I think this is related to: angular/angular#13540 (comment)

The upgrade-adapter runs an angularjs digest cycle, when microtasks are empty.
Unfortunately this lines of code above regularly start a new timeout - which in turn starts the digest cycle again.

@bluegaspode
Copy link

This describes the problem even better, but still unresolved: angular/angular#15157

@meDavid
Copy link

meDavid commented Apr 8, 2020

The fix would be to give the $watch function a watchExpression as a first argument and a listener as a second. this would remove the need of the timeout as well:

$rootScope.$watch(function () {return $storage}, function () {
                 $storage.$apply();
               });

@Eugeny
Copy link

Eugeny commented Dec 20, 2021

Here's my shitty workaround:

diff --git a/node_modules/ngstorage/ngStorage.js b/node_modules/ngstorage/ngStorage.js
index 0bbdb69..15d138e 100644
--- a/node_modules/ngstorage/ngStorage.js
+++ b/node_modules/ngstorage/ngStorage.js
@@ -202,8 +202,10 @@
 
                 _last$storage = angular.copy($storage);
 
+                var setTimeout = window[window.Zone.__symbol__('setTimeout')];
+
                 $rootScope.$watch(function() {
-                    _debounce || (_debounce = $timeout($storage.$apply, 100, false));
+                    _debounce || (_debounce = setTimeout($storage.$apply, 100));
                 });
 
                 // #6: Use `$window.addEventListener` instead of `angular.element` to avoid the jQuery-specific `event.originalEvent`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants