Skip to content

Commit

Permalink
Merge pull request #15 from rwjblue/allow-opt-out-of-view-events
Browse files Browse the repository at this point in the history
Allow opting out of view instrumentation hooks.
  • Loading branch information
rwjblue committed Sep 9, 2015
2 parents 90ac003 + d846937 commit 849cef4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
37 changes: 37 additions & 0 deletions addon/instance-initializers/ember-perf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Ember from 'ember';

// this lives in addon/ because when running on Ember < 1.12
// the ember-load-initializers project will attempt to call
// `Ember.Application#instanceInitializer` which does not exist.
//
// This was fixed upstream in https://github.com/ember-cli/ember-load-initializers/pull/21,
// but is not in widespread use yet.
export function initialize(instance) {
// in 2.1 the ApplicationInstance instance has a public `lookup` method
const container = instance.lookup ? instance : instance.container;

// in 2.1 the ApplicationInstance uses `_lookupFactory` to avoid making lookupFactory public
// along with `lookup`
const config = container._lookupFactory ? container._lookupFactory('config:environment') : container.lookupFactory('service:ember-perf');
const emberPerfConfig = config.emberPerfConfig || {};

let shouldSubscribeToViewEvents = 'logViewEvents' in emberPerfConfig ? emberPerfConfig.logViewEvents : true;

if (shouldSubscribeToViewEvents) {
const emberPerf = container.lookup('service:ember-perf');

Ember.subscribe('render', {
before(name, timestamp, payload) {
emberPerf.renderBefore(name, timestamp, payload);
},
after(name, timestamp, payload) {
emberPerf.renderAfter(name, timestamp, payload);
}
});
}
}

export default {
name: 'ember-perf-instance-setup',
initialize
};
30 changes: 16 additions & 14 deletions app/initializers/ember-perf.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import Ember from 'ember';
import EmberPerfService from 'ember-perf/services/ember-perf';
import RouterExt from 'ember-perf/ext/router';
import RouteExt from 'ember-perf/ext/route';
import config from '../config/environment';
import instanceInitializer from 'ember-perf/instance-initializers/ember-perf';

const {
Router, Route
} = Ember;

function injectServiceOntoFactories(emberPerf, container, application) {
if (Ember.Application.instanceInitializer) {
Ember.Application.instanceInitializer(instanceInitializer);
}

function injectServiceOntoFactories(emberPerf, application) {
const {
injectionFactories
} = emberPerf;
Expand All @@ -26,28 +32,24 @@ function injectServiceOntoFactories(emberPerf, container, application) {
export function initialize() {
const application = arguments[1] || arguments[0];
const container = application.__container__;

const {
emberPerf
} = config;
injectServiceOntoFactories(emberPerf, container, application);

injectServiceOntoFactories(emberPerf, application);

Route.reopen(RouteExt);
Router.reopen(RouterExt);

Ember.subscribe("render", {
before(name, timestamp, payload) {
container.lookup('service:ember-perf').renderBefore(name, timestamp, payload);
},
after(name, timestamp, payload) {
container.lookup('service:ember-perf').renderAfter(name, timestamp, payload);
}
});

// instance initializers were available as of Ember 1.12, this
// runs the instance initializer manually if needed
if (!application.instanceInitializer) {
instanceInitializer.initialize(container);
}
};

export default {
name: 'ember-perf-setup',
initialize: initialize
};
};
1 change: 1 addition & 0 deletions config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = function(/* environment, appConfig */) {
emberPerf: {
debugMode: false,
logRerenders: true,
logViewEvents: true,
injectionFactories: ['route', 'adapter']
}
};
Expand Down

0 comments on commit 849cef4

Please sign in to comment.