-
Notifications
You must be signed in to change notification settings - Fork 199
/
ng-profile-scope-method.js
44 lines (38 loc) · 1.38 KB
/
ng-profile-scope-method.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
Wraps a method on a scope (found from selector + method name)
with profiling. After method stops, restores the original non-profiled method
Method can return a promise.
Typical use: profile button click.
$scope.myMethod = function () ...
where $scope could be determined from element 'my-selector'
*/
(function profileScopeMethod() {
var selector = '#find';
var methodName = 'find';
var name = selector + ':' + methodName;
/* global angular */
var el = angular.element(document.querySelector(selector));
var scope = el.scope() || el.isolateScope();
console.assert(scope, 'cannot find scope from ' + name);
var fn = scope[methodName];
console.assert(typeof fn === 'function', 'missing ' + methodName);
var $timeout = el.injector().get('$timeout');
var $q = el.injector().get('$q');
scope[methodName] = function () {
console.profile(name);
console.time(name);
// method can return a value or a promise
var returned = fn();
$q.when(returned).finally(function finishedMethod() {
console.timeStamp('finished', methodName);
$timeout(function afterDOMUpdate() {
console.timeStamp('dom updated after', methodName);
console.timeEnd(name);
console.profileEnd();
scope[methodName] = fn;
console.log('restored', name);
}, 0);
});
};
console.log('wrapped', name, 'for measurements');
}());