-
Notifications
You must be signed in to change notification settings - Fork 286
/
render-debug.js
124 lines (104 loc) · 3.04 KB
/
render-debug.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import PortMixin from "ember-debug/mixins/port-mixin";
import ProfileManager from "ember-debug/models/profile-manager";
var Ember = window.Ember;
var computed = Ember.computed;
var oneWay = computed.oneWay;
var later = Ember.run.later;
var profileManager = new ProfileManager();
var queue = [];
function push(info) {
var index = queue.push(info);
if (1 === index) {
later(flush, 50);
}
return index - 1;
}
function flush() {
var entry, i;
for (i = 0; i < queue.length; i++) {
entry = queue[i];
if (entry.type === 'began') {
// If there was an error during rendering `entry.endedIndex` never gets set.
if (entry.endedIndex) {
queue[entry.endedIndex].profileNode = profileManager.began(entry.timestamp, entry.payload, entry.now);
}
} else {
profileManager.ended(entry.timestamp, entry.payload, entry.profileNode);
}
}
queue.length = 0;
}
Ember.subscribe("render", {
before: function(name, timestamp, payload) {
var info = {
type: 'began',
timestamp: timestamp,
payload: payload,
now: Date.now()
};
return push(info);
},
after: function(name, timestamp, payload, beganIndex) {
var endedInfo = {
type: 'ended',
timestamp: timestamp,
payload: payload
};
var index = push(endedInfo);
queue[beganIndex].endedIndex = index;
}
});
export default Ember.Object.extend(PortMixin, {
namespace: null,
port: oneWay('namespace.port').readOnly(),
application: oneWay('namespace.application').readOnly(),
viewDebug: oneWay('namespace.viewDebug').readOnly(),
portNamespace: 'render',
profileManager: profileManager,
init: function() {
this._super();
this._subscribeForViewTrees();
},
willDestroy: function() {
this._super();
this.profileManager.offProfilesAdded(this, this.sendAdded);
this.profileManager.offProfilesAdded(this, this._updateViewTree);
},
_subscribeForViewTrees: function() {
this.profileManager.onProfilesAdded(this, this._updateViewTree);
},
_updateViewTree: function(profiles) {
var viewDurations = {};
this._flatten(profiles).forEach(function(node) {
if (node.viewGuid) {
viewDurations[node.viewGuid] = node.duration;
}
});
this.get('viewDebug').updateDurations(viewDurations);
},
_flatten: function(profiles, array) {
var self = this;
array = array || [];
profiles.forEach(function(profile) {
array.push(profile);
self._flatten(profile.children, array);
});
return array;
},
sendAdded: function(profiles) {
this.sendMessage('profilesAdded', { profiles: profiles });
},
messages: {
watchProfiles: function() {
this.sendMessage('profilesAdded', { profiles: this.profileManager.profiles });
this.profileManager.onProfilesAdded(this, this.sendAdded);
},
releaseProfiles: function() {
this.profileManager.offProfilesAdded(this, this.sendAdded);
},
clear: function() {
this.profileManager.clearProfiles();
this.sendMessage('profilesUpdated', {profiles: []});
}
}
});