Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
New Twiddle
import Ember from 'ember';
var ComputedProto = Object.getPrototypeOf(Ember.computed(function(){}));
var isComputed = function(obj){
return typeof(obj) === 'object' && ComputedProto.isPrototypeOf(obj);
}
var ObserverProto = Object.getPrototypeOf(Ember.observer(function(){}));
var isObserver = function(obj){
return typeof(obj) === 'function' && ObserverProto.isPrototypeOf(obj);
}
var filterFn = function(model, k){
return ((typeof model[k] !== 'function') || isObserver(model[k])) && k[0] !== '_';
};
var difference = function(a, b){
return a.filter((item) => {
return !b.includes(item);
});
};
var EmberObjectKeysToRemove = [];
var emptyObject = Ember.Object.create();
for (k in emptyObject) {
if (typeof emptyObject[k] !== 'function') {
EmberObjectKeysToRemove.push(k);
}
}
var EmberArrayKeysToRemove = [];
var emptyArray = Ember.A();
for (k in emptyObject) {
if (typeof emptyObject[k] !== 'function') {
EmberObjectKeysToRemove.push(k);
}
}
var getEmberObjectProperties = function(model) {
var modelKeys = Object.getOwnPropertyNames(model).filter(filterFn);
var k;
for (k in model) {
if (filterFn(model, k)) {
modelKeys.push(k);
}
}
return difference(modelKeys, EmberObjectKeysToRemove);
};
var getObjectProperties = function(obj){
if (Ember.Object.detectInstance(obj)){
return getEmberObjectProperties(obj);
} else {
return Object.keys(obj);
}
};
var instrument = function(item_, seenSet){
seenSet = seenSet || new WeakSet();
var items = [{item: item_, prefix: ''}];
while (items.length){
var itemDef = items.shift();
var item = itemDef.item;
var prefix = itemDef.prefix;
var i;
if (!item){
} else if (Array.isArray(item)){
if (seenSet.has(item)){
continue;
}
seenSet.add(item);
items = items.concat(item.map(function(sub){ return {item:sub, prefix: prefix + '[]'};}));
} else if (typeof(item) === 'object') {
if (seenSet.has(item)){
return;
}
seenSet.add(item);
if (isObserver(item) || prefix.indexOf('boaz') !== -1){
console.log('observer!');
window.bb = item;
} else if (isComputed(item)){
prefix = prefix.substring(1);
var fn = item._getter;
item._getter = (function(fn, prefix){
return function(){
console.time(prefix);
var result = fn.apply(this, arguments);
instrument(result, seenSet);
console.timeEnd(prefix);
return result;
};
})(fn, prefix);
} else {
var keys = getObjectProperties(item);
for (i = 0; i < keys.length; i++) {
var key = keys[i];
items.push({item: item[key], prefix: prefix + '.' + key});
}
}
}
}
};
var foo = Ember.Object.extend({
bar: 1,
baz: Ember.computed('bar', function(){
return this.get('bar') + 1;
}),
bat: Ember.computed('baz', function(){
return this.get('baz') + 1;
}),
bay: Ember.computed('bat', function(){
return this.get('bat') + 1;
}),
baq: Ember.computed('bay', function(){
return this.get('bay') + 1;
}),
bam: Ember.computed('baq', function(){
return this.get('baq') + 1;
}),
boaz: Ember.observer('bam', function(){
console.log('boaz');
}),
}).create();
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
foo: foo,
actions: {
foo: function(){
foo.set('bar', this.get('foo.bar') + 1);
return false;
},
instrument: function(){
window.ff = foo;
console.log(getEmberObjectProperties(foo));
instrument(this);
}
}
});
<h1>Welcome to {{appName}}</h1>
<br>
{{foo.bam}}
<br>
<button {{action 'foo'}}>click</button>
<button {{action 'instrument'}}>instrument</button>
<br>
<br>
{
"version": "0.11.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.10.2",
"ember-data": "2.11.0",
"ember-template-compiler": "2.10.2",
"ember-testing": "2.10.2",
"lodash": "https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment