Skip to content

Instantly share code, notes, and snippets.

@kentcdodds
Last active December 4, 2023 22:34
Show Gist options
  • Save kentcdodds/31c90402750572107922 to your computer and use it in GitHub Desktop.
Save kentcdodds/31c90402750572107922 to your computer and use it in GitHub Desktop.
Get Watchers of element and its children
function getWatchers(root) {
root = angular.element(root || document.documentElement);
var watcherCount = 0;
function getElemWatchers(element) {
var isolateWatchers = getWatchersFromScope(element.data().$isolateScope);
var scopeWatchers = getWatchersFromScope(element.data().$scope);
var watchers = scopeWatchers.concat(isolateWatchers);
angular.forEach(element.children(), function (childElement) {
watchers = watchers.concat(getElemWatchers(angular.element(childElement)));
});
return watchers;
}
function getWatchersFromScope(scope) {
if (scope) {
return scope.$$watchers || [];
} else {
return [];
}
}
return getElemWatchers(root);
}
getWatchers().length
@niklasbuschmann
Copy link

Lists the watchers sorted by the expression:

function getWatchers(root) {
  root = angular.element(root || document.documentElement);
  var watcherCount = 0;

  function getElemWatchers(element) {
    var isolateWatchers = getWatchersFromScope(element.data().$isolateScope);
    var scopeWatchers = getWatchersFromScope(element.data().$scope);
    var watchers = scopeWatchers.concat(isolateWatchers);
    angular.forEach(element.children(), function (childElement) {
      watchers = watchers.concat(getElemWatchers(angular.element(childElement)));
    });
    return watchers;
  }

  function getWatchersFromScope(scope) {
    if (scope) {
      return scope.$$watchers || [];
    } else {
      return [];
    }
  }

  function getWatcherExpressions (element) {
    var watchers = getElemWatchers(element);
    var watchlist = {
      '_total': watchers.length
    };
    angular.forEach(watchers, function (watcher) {
      watchlist[watcher.exp] = watchlist[watcher.exp] || [];
      watchlist[watcher.exp].push(watcher);
    });
    return watchlist;
  }

  return getWatcherExpressions(root);
}

getWatchers()

@xErik
Copy link

xErik commented Jul 1, 2015

@b-dur
Copy link

b-dur commented Aug 26, 2019

Lists the watchers sorted by expression, and only picks them once per scope Id.

function getWatchers(root) {
  var scopeIds = new Set();
  root = angular.element(root || document.documentElement);
  var watcherCount = 0;

  function getElemWatchers(element) {
    var isolateWatchers = getWatchersFromScope(element.data().$isolateScope);
    var scopeWatchers = getWatchersFromScope(element.data().$scope);
    var watchers = scopeWatchers.concat(isolateWatchers);
    angular.forEach(element.children(), function(childElement) {
      watchers = watchers.concat(
        getElemWatchers(angular.element(childElement))
      );
    });
    return watchers;
  }

  function getWatchersFromScope(scope) {
    if (scope) {
      if (!scopeIds.has(scope.$id)) {
        scopeIds.add(scope.$id);
        return scope.$$watchers || [];
      } else {
        return [];
      }
    } else {
      return [];
    }
  }

  function getWatcherExpressions(element) {
    var watchers = getElemWatchers(element);
    var watchlist = {
      _total: watchers.length
    };
    angular.forEach(watchers, function(watcher) {
      watchlist[watcher.exp] = watchlist[watcher.exp] || [];
      watchlist[watcher.exp].push(watcher);
    });
    return watchlist;
  }

  return getWatcherExpressions(root);
}

getWatchers();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment