-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflight-monitor.js
334 lines (288 loc) · 9.49 KB
/
flight-monitor.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
define(
[
'flight/lib/compose',
'flight/lib/registry',
'flight/lib/debug',
'./debugger.js'
],
function(compose, registry, debug, debuggerTemplate) {
'use strict';
// flightMonitor object
var debugActions = [];
var flightMonitor = {
__trackingId : 1,
__stopFlow : false,
__stopPattern : null,
_stopOn : function(eventName) {
if(this.__stopFlow) {
return true;
}
else if(!eventName || !this.__stopPattern) {
return false;
}
else if(eventName.match(this.__stopPattern)) {
flightMonitor.config.log(
'%cStopped on event ' + eventName,
'color: red;'
);
return (this.__stopFlow = true);
}
},
_trackingId : function() {
return this.__trackingId++;
},
// public interface
eventTree: {},
componentNodes : {},
config: {
// default logging options
showElementInfo : true,
showMethodInfo : true,
// extra options - if all turned on logging is a little too much
showDataInfo : false,
showEventId : false,
showMixins : false,
showStopped : false,
log : function() { console.log.apply(console, arguments); }
},
lastData : null,
stop : function() {
this.__stopFlow = true;
},
breakOn : function(pattern) {
this.__stopPattern = pattern;
},
step : function() {
this.__stopFlow = true;
if(!debugActions.length) {
debuggerTemplate.hide();
console.log('No events captured');
return;
}
debugActions.shift().trigger();
if(debugActions.length) {
debuggerTemplate.attachTo(debugActions[0]);
} else {
debuggerTemplate.hide();
}
},
continue: function() {
debuggerTemplate.hide();
if(!debugActions.length) {
console.log('No events captured');
return;
}
this.__stopFlow = false;
while(debugActions.length && !this.__stopFlow) {
debugActions.shift().trigger();
}
},
run : function() {
this.__stopPattern = false;
this.continue();
}
};
debuggerTemplate.onClick = flightMonitor.step;
// turn on flight debugger in silent mode to force flight to use the withLogging mixin
debug.enabled = true;
debug.events.logNone();
// overwrite compose.mixin to capture withLogging mixin
var lastComponent = null;
var _mixin = compose.mixin;
compose.mixin = function(component, mixins) {
for(var i=0; i < mixins.length; i++) {
if(mixins[i] && mixins[i].name === 'withLogging') {
mixins[i] = function flightMonitor() {
this.before('trigger', function() {
lastComponent = this;
});
}
}
}
return _mixin.apply(this, arguments);
};
// debugger item for stepping the event flow
function debugAction(type, el) {
this.type = type;
this.element = $(el).get(0);
if(this.type === 'callback') {
var componentName = arguments[2],
fn = arguments[3],
ev = arguments[4],
data = arguments[5];
this.trigger = function() {
flightMonitor.config.log(
' >',
componentName,
'is listening for',
ev.type,
'and calling',
fn.target.name ? fn.target.name: fn.target.toString()
);
fn.call(fn, ev, data);
};
this.toString = function() {
return 'calling ' + componentName + '::' + (fn.target.name ? fn.target.name: fn.target.toString());
};
this.component = componentName;
} else {
var $element = arguments[1],
component = arguments[2],
event = arguments[3],
data = arguments[4],
componentName = getComponentName(component.componentIdentity || component.identity);
this.trigger = function() {
lastComponent = component;
$element.trigger(event,data,true);
};
this.toString = function() {
return 'trigger ' + event.type + ' by ' + componentName;
};
this.component = componentName;
}
if(!debugActions.length) {
debuggerTemplate.attachTo(this);
}
}
// node generator for the eventTree data structure
function createNode(type, name) {
var node = {
type: type,
name: name,
addChild : function(node) {
(this.children || (this.children=[])).push(node);
return node;
}
};
return node;
}
// click triggers a clean tracking
document.addEventListener('click', function(ev) {
if(debugActions.length) {
return;
}
var eventNode = createNode('event', ev.type);
flightMonitor.eventTree = createNode('component', 'User');
flightMonitor.eventTree.addChild(eventNode);
ev.trackingId = flightMonitor._trackingId();
ev.node = eventNode;
var eventId = flightMonitor.config.showEventId ? '(#' + ev.trackingId + ')' : '';
var elementInfo = flightMonitor.config.showElementInfo ? ev.target : '';
flightMonitor.config.log(
'%c'+ev.type + eventId + ' triggered by the User' + (elementInfo ? ' on' : ''),
'color: red;',
elementInfo
);
}, true);
// get component info and create a name
function getComponentName(componentIndex) {
var componentName = registry.components[componentIndex].component.toString(),
parts = componentName.split(', ');
// parts[0] will be always 'flightMonitor'
componentName = parts[1];
if(flightMonitor.config.showMixins) {
componentName += (parts.length > 2 ? '[' + parts.slice(2).join(',') + ']' : '');
}
if(registry.components[componentIndex].attachedTo[0] === document) {
componentName = 'data/' + componentName;
} else {
componentName = 'ui/' + componentName;
}
return componentName;
}
// monitor trigger calls
$.fn.trigger = function(event, data, force) {
event = (typeof event === 'string' ? $.Event(event) : event);
if(flightMonitor._stopOn(event.type) && !force) {
flightMonitor.config.showStopped && flightMonitor.config.log(
'%cstopped event ' + event.type,
'color: red;'
);
debugActions.push(new debugAction('trigger', this, lastComponent, event, data));
return ;
}
flightMonitor.lastData = data;
event.trackingId = flightMonitor._trackingId();
var eventId = flightMonitor.config.showEventId ? '(#' + event.trackingId + ')' : '';
var elementInfo = flightMonitor.config.showElementInfo ? this.get(0) : '';
var dataInfo = flightMonitor.config.showDataInfo ? data : '';
if(lastComponent) {
var eventNode = createNode('event', event.type);
var componentIndex = lastComponent.componentIdentity || lastComponent.identity;
var componentName = getComponentName(componentIndex);
var componentNode = flightMonitor.componentNodes[componentName];
componentNode && componentNode.addChild(eventNode);
event.node = eventNode;
flightMonitor.config.log(
'%c' + event.type + eventId + ' triggered by ' + componentName, 'color: blue;',
(elementInfo ? ' on' : ''),
elementInfo,
dataInfo
);
} else {
flightMonitor.config.log(
'%c' + event.type + eventId + ' triggered from outside', 'color: navy;',
(elementInfo ? ' on' : ''),
elementInfo,
dataInfo
);
}
lastComponent = false;
return this.each(function() {
$.event.trigger( event, data, this );
});
};
// monitor event listeners
var originalFnOn = $.fn.on;
$.fn.on = function(type, callback) {
var element = this;
// is it a flight event handler?
if(typeof callback === 'function' && callback.target && callback.context) {
var instanceIndex = callback.context.identity,
componentIndex = instanceIndex;
// as Flight is not kind enough to help us connect the instance to a component,
// we have to do it for ourselves...
if(instanceIndex >= registry.components.length) {
componentIndex = registry.components.length-1;
callback.context.componentIdentity = componentIndex;
}
return originalFnOn.call(this, type, function(ev, data) {
var fnName = callback.target.name;
var componentName = getComponentName(componentIndex);
var trackingId = ev.trackingId || ev.originalEvent && ev.originalEvent.trackingId;
var eventNode = ev.node || ev.originalEvent && ev.originalEvent.node;
if(flightMonitor._stopOn()) {
flightMonitor.config.showStopped && flightMonitor.config.log(
'%cstopped callback ' + (fnName || 'anonymus'),
'color: red;'
);
debugActions.push(new debugAction('callback', ev.target, componentName, callback, ev, data));
ev.preventDefault();
return ;
}
if(trackingId && eventNode) {
var componentNode = createNode('component', componentName);
eventNode && eventNode.addChild(componentNode);
flightMonitor.componentNodes[componentName] = componentNode;
componentNode.method = fnName;
}
var eventId = flightMonitor.config.showEventId ? '(#' + trackingId + ')' : '';
var methodInfo = flightMonitor.config.showMethodInfo && fnName ? 'and calls ' + fnName : '';
var elementInfo = flightMonitor.config.showElementInfo ? element.get(0) : '';
flightMonitor.config.log(
' >',
componentName,
'is listening for',
ev.type + eventId + (elementInfo ? ' on' : ''),
elementInfo,
methodInfo
);
return callback(ev, data);
});
}
return originalFnOn.apply(this, arguments);
};
$.flightMonitor = flightMonitor;
return flightMonitor;
}
);