Skip to content

Instantly share code, notes, and snippets.

@GavinJoyce
Last active August 29, 2015 14:14
Show Gist options
  • Save GavinJoyce/0caa08b3cea7af044cf8 to your computer and use it in GitHub Desktop.
Save GavinJoyce/0caa08b3cea7af044cf8 to your computer and use it in GitHub Desktop.
Ember listener debugging
if (!String.prototype.startsWith) {
Object.defineProperty(String.prototype, 'startsWith', {
enumerable: false,
configurable: false,
writable: false,
value: function(searchString, position) {
position = position || 0;
return this.lastIndexOf(searchString, position) === position;
}
});
}
var container = Embercom.__container__;
var containerCache = container.resolveCache;
var containerKeys = Object.keys(containerCache);
function findAll(prefix) {
return containerKeys.filter(function(key) { return key.startsWith(prefix); })
};
function find(key) {
try {
return container.lookup(key);
} catch(e) {
console.log('warning: could not lookup ' + key, e);
}
};
function debugListeners(keys) {
keys.forEach(function(key) {
debugObjectListeners(key);
})
};
function debugObjectListeners(key) {
var object = find(key);
if(object && object.__ember_meta__ && object.__ember_meta__.listeners) {
var listeners = object.__ember_meta__.listeners;
console.log(key + ' listeners:');
var listenerKeys = Object.keys(listeners);
console.log(' -> ' + key, '[' + listenerKeys.length + ']');
listenerKeys.forEach(function(listenerKey) {
var subscriptionCount = listeners[listenerKey].length / 3;
if(subscriptionCount > 2) {
console.log(' > POSSIBLE MEMORY LEAK', key, listenerKey, subscriptionCount);
} else {
console.log(' > ' + listenerKey, subscriptionCount);
}
});
return object.__ember_meta__.listeners;
} else {
console.log('skipping ' + key);
}
};
function debug() {
var components = findAll('component:');
var controllers = findAll('controller:');
var views = findAll('view:');
console.log('There are ' + containerKeys.length + ' container keys');
console.log('There are ' + components.length + ' components');
console.log('There are ' + controllers.length + ' controllers');
debugListeners(controllers);
debugListeners(components);
debugListeners(views);
console.log('----------')
};
debug();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment