Skip to content

Instantly share code, notes, and snippets.

@bertrandg
Last active February 28, 2018 10:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bertrandg/6516282749ea7116f610 to your computer and use it in GitHub Desktop.
Save bertrandg/6516282749ea7116f610 to your computer and use it in GitHub Desktop.
var debugWatchers = function(selector, showExp) {
var target, i, checkInsideFn, jq, items,
nb_watchers = 0,
nb_scopes = 0,
scopes_id = {};
if(typeof jQuery == 'undefined') {
jq = document.createElement('script');
jq.type = 'text/javascript';
jq.src = '//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js';
jq.onload = function() { debugWatchers(selector, showExp); }
document.getElementsByTagName('head')[0].appendChild(jq);
console.info('loading jQuery..');
} else {
switch(true) {
case (selector instanceof jQuery):
if(selector.length > 1) {
console.error('Your selector target', selector.length, ' elements instead of only one. Please work on your selector, nth-child() is your friend!');
console.log($(selector));
return;
} else if(selector.length == 0) {
console.error('Your selector target no element..');
return;
}
target = selector.get(0);
break;
case (typeof selector == 'object'):
if(selector.nodeName) {
target = selector;
} else {
console.error('Your selector is invalid..');
return;
}
break;
case (typeof selector == 'string'):
if($(selector).length > 1) {
console.error('Your selector target', $(selector).length, ' elements instead of only one. Please work on your selector, nth-child() is your friend!');
console.log($(selector));
return;
} else if($(selector).length == 0) {
console.error('Your selector target no element..');
return;
}
target = document.querySelector(selector);
break;
default:
selector = 'html';
target = document.querySelector(selector);
break;
}
checkInsideFn = function(elem) {
var data = elem.data();
if (data.hasOwnProperty('$scope') && data.$scope.$$watchers) {
var scope = data.$scope;
if(!scopes_id[scope.$id]) {
scopes_id[scope.$id] = true;
nb_watchers += scope.$$watchers.length;
nb_scopes++;
console.warn(nb_scopes, ' --> element: ', elem, ' - scope_id = ', scope.$id, ' - total watchers = ', scope.$$watchers.length);
if(showExp) {
angular.forEach(scope.$$watchers, function(elem) {
if(typeof elem.exp == 'function') console.info('expr = ', elem.exp.exp);
else console.info('expr = ', elem.exp);
});
}
}
}
};
console.info('Target blocked: ', target);
checkInsideFn( angular.element(target) );
items = target.getElementsByTagName("*");
for(i = items.length; i--;) checkInsideFn( angular.element(items[i]) );
console.info('Found', nb_scopes, ' scopes with ', nb_watchers, ' watchers');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment