-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathinstrumentation.js
235 lines (190 loc) · 5.27 KB
/
instrumentation.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import Ember from 'ember-metal/core';
/**
The purpose of the Ember Instrumentation module is
to provide efficient, general-purpose instrumentation
for Ember.
Subscribe to a listener by using `Ember.subscribe`:
```javascript
Ember.subscribe("render", {
before: function(name, timestamp, payload) {
},
after: function(name, timestamp, payload) {
}
});
```
If you return a value from the `before` callback, that same
value will be passed as a fourth parameter to the `after`
callback.
Instrument a block of code by using `Ember.instrument`:
```javascript
Ember.instrument("render.handlebars", payload, function() {
// rendering logic
}, binding);
```
Event names passed to `Ember.instrument` are namespaced
by periods, from more general to more specific. Subscribers
can listen for events by whatever level of granularity they
are interested in.
In the above example, the event is `render.handlebars`,
and the subscriber listened for all events beginning with
`render`. It would receive callbacks for events named
`render`, `render.handlebars`, `render.container`, or
even `render.handlebars.layout`.
@class Instrumentation
@namespace Ember
@static
@private
*/
export var subscribers = [];
var cache = {};
var populateListeners = function(name) {
var listeners = [];
var subscriber;
for (var i = 0; i < subscribers.length; i++) {
subscriber = subscribers[i];
if (subscriber.regex.test(name)) {
listeners.push(subscriber.object);
}
}
cache[name] = listeners;
return listeners;
};
var time = (function() {
var perf = 'undefined' !== typeof window ? window.performance || {} : {};
var fn = perf.now || perf.mozNow || perf.webkitNow || perf.msNow || perf.oNow;
// fn.bind will be available in all the browsers that support the advanced window.performance... ;-)
return fn ? fn.bind(perf) : () => {
return +new Date();
};
})();
/**
Notifies event's subscribers, calls `before` and `after` hooks.
@method instrument
@namespace Ember.Instrumentation
@param {String} [name] Namespaced event name.
@param {Object} _payload
@param {Function} callback Function that you're instrumenting.
@param {Object} binding Context that instrument function is called with.
@private
*/
export function instrument(name, _payload, callback, binding) {
if (arguments.length <= 3 && typeof _payload === 'function') {
binding = callback;
callback = _payload;
_payload = undefined;
}
if (subscribers.length === 0) {
return callback.call(binding);
}
var payload = _payload || {};
var finalizer = _instrumentStart(name, () => payload);
if (finalizer) {
return withFinalizer(callback, finalizer, payload, binding);
} else {
return callback.call(binding);
}
}
function withFinalizer(callback, finalizer, payload, binding) {
try {
return callback.call(binding);
} catch(e) {
payload.exception = e;
return payload;
} finally {
return finalizer();
}
}
// private for now
export function _instrumentStart(name, _payload) {
var listeners = cache[name];
if (!listeners) {
listeners = populateListeners(name);
}
if (listeners.length === 0) {
return;
}
var payload = _payload();
var STRUCTURED_PROFILE = Ember.STRUCTURED_PROFILE;
var timeName;
if (STRUCTURED_PROFILE) {
timeName = name + ': ' + payload.object;
console.time(timeName);
}
var beforeValues = new Array(listeners.length);
var i, listener;
var timestamp = time();
for (i = 0; i < listeners.length; i++) {
listener = listeners[i];
beforeValues[i] = listener.before(name, timestamp, payload);
}
return function _instrumentEnd() {
var i, listener;
var timestamp = time();
for (i = 0; i < listeners.length; i++) {
listener = listeners[i];
listener.after(name, timestamp, payload, beforeValues[i]);
}
if (STRUCTURED_PROFILE) {
console.timeEnd(timeName);
}
};
}
/**
Subscribes to a particular event or instrumented block of code.
@method subscribe
@namespace Ember.Instrumentation
@param {String} [pattern] Namespaced event name.
@param {Object} [object] Before and After hooks.
@return {Subscriber}
@private
*/
export function subscribe(pattern, object) {
var paths = pattern.split('.');
var path;
var regex = [];
for (var i = 0; i < paths.length; i++) {
path = paths[i];
if (path === '*') {
regex.push('[^\\.]*');
} else {
regex.push(path);
}
}
regex = regex.join('\\.');
regex = regex + '(\\..*)?';
var subscriber = {
pattern: pattern,
regex: new RegExp('^' + regex + '$'),
object: object
};
subscribers.push(subscriber);
cache = {};
return subscriber;
}
/**
Unsubscribes from a particular event or instrumented block of code.
@method unsubscribe
@namespace Ember.Instrumentation
@param {Object} [subscriber]
@private
*/
export function unsubscribe(subscriber) {
var index;
for (var i = 0; i < subscribers.length; i++) {
if (subscribers[i] === subscriber) {
index = i;
}
}
subscribers.splice(index, 1);
cache = {};
}
/**
Resets `Ember.Instrumentation` by flushing list of subscribers.
@method reset
@namespace Ember.Instrumentation
@private
*/
export function reset() {
subscribers.length = 0;
cache = {};
}