-
Notifications
You must be signed in to change notification settings - Fork 68
/
debug.js
561 lines (466 loc) · 15.1 KB
/
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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/** @license MIT License (c) copyright B Cavalier & J Hann */
/*jshint sub:true*/
/*global Node:true*/
/**
* debug
* wire plugin that logs timing and debug information about wiring context and object
* lifecycle events (e.g. creation, properties set, initialized, etc.).
*
* wire is part of the cujo.js family of libraries (http://cujojs.com/)
*
* Licensed under the MIT License at:
* http://www.opensource.org/licenses/mit-license.php
*
* Usage:
* {
* module: 'wire/debug',
*
* // verbose (Optional)
* // If set to true, even more (a LOT) info will be output.
* // Default is false if not specified.
* verbose: false,
*
* // timeout (Optional)
* // Milliseconds to wait for wiring to finish before reporting
* // failed components. There may be failures caused by 3rd party
* // wire plugins and components that wire.js cannot detect. This
* // provides a last ditch way to try to report those failures.
* // Default is 5000ms (5 seconds)
* timeout: 5000,
*
* // filter (Optional)
* // String or RegExp to match against a component's name. Only
* // components whose path matches will be reported in the debug
* // diagnostic output.
* // All components will still be tracked for failures.
* // This can be useful in reducing the amount of diagnostic output and
* // focusing it on specific components.
* // Defaults to matching all components
* // Examples:
* // filter: ".*View"
* // filter: /.*View/
* // filter: "[fF]oo[bB]ar"
* filter: ".*"
*
* // trace (Optional)
* // Enables application component tracing that will log information about component
* // method calls while your application runs. This provides a powerful way to watch
* // and debug your application as it runs.
* // To enable full tracing, which is a LOT of information:
* trace: true
* // Or, specify options to enable more focused tracing:
* trace: {
* // filter (Optional)
* // Similar to above, can be string pattern or RegExp
* // If not specified, the general debug filter is used (see above)
* filter: ".*View",
*
* // pointcut (Optional)
* // Matches *method names*. Can be used with or without specifying filter
* // When filter is not specified, this will match methods across all components.
* // For example, if all your components name their event emitters "on<Event>", e.g. "onClick"
* // you could trace all your event emitters:
* // Default: "^[^_]" (all methods not starting with '_')
* pointcut: "on.*",
*
* // step (Optional)
* // At what step in the wiring process should tracing start. This can be helpful
* // if you need to trace a component during wiring.
* // Values: 'create', 'configure', 'initialize', 'ready', 'destroy'
* // NOTE: This defines when tracing *begins*. For example, if this is set to
* // 'configure' (the default), anything that happens to components during and
* // after the configure step, until that component is destroyed, will be traced.
* // Default: 'configure'
* step: 'configure'
* }
* }
*/
(function(global, define) {
define(function(require) {
var meld, timer, defaultTimeout, logger, createTracer, ownProp;
meld = require('meld');
function noop() {}
ownProp = Object.prototype.hasOwnProperty.call.bind(Object.prototype.hasOwnProperty);
// Setup console for node, sane browsers, or IE
logger = typeof console != 'undefined'
? console
: global['console'] || { log:noop, error:noop };
// TODO: Consider using stacktrace.js
// https://github.com/eriwen/javascript-stacktrace
// For now, quick and dirty, based on how stacktrace.js chooses the appropriate field
// and log using console.error
function logStack(e) {
var stack = e.stack || e.stacktrace;
if(!stack) {
// If e.sourceURL and e.line are available, this is probably Safari, so
// we can build a clickable source:line
// Fallback to message if available
// If all else fails, just use e itself
stack = e.sourceURL && e.line
? e.sourceURL + ':' + e.line
: e.message || e;
}
logger.error(stack);
}
timer = createTimer();
// If we don't see any wiring progress in this amount of time
// since the last time we saw something happen, then we'll log
// an error.
defaultTimeout = 5000;
/**
* Builds a string with timing info and a message for debug output
*
* @param text {String} message
* @param contextTimer per-context timer information
*
* @returns A formatted string for output
*/
function time(text, contextTimer) {
var all, timing;
all = timer();
timing = "(total: " +
(contextTimer
? all.total + "ms, context: " + contextTimer()
: all)
+ "): ";
return "DEBUG " + timing + text;
}
/**
* Creates a timer function that, when called, returns an object containing
* the total elapsed time since the timer was created, and the split time
* since the last time the timer was called. All times in milliseconds
*
* @returns timer
*/
function createTimer() {
var start, split;
start = new Date().getTime();
split = start;
/**
* Returns the total elapsed time since this timer was created, and the
* split time since this getTime was last called.
*
* @returns Object containing total and split times in milliseconds, plus a
* toString() function that is useful in logging the time.
*/
return function getTime() {
var now, total, splitTime;
now = new Date().getTime();
total = now - start;
splitTime = now - split;
split = now;
return {
total:total,
split:splitTime,
toString:function () {
return '' + splitTime + 'ms / ' + total + 'ms';
}
};
};
}
function defaultFilter(path) {
return !!path;
}
function createPathFilter(filter) {
if (!filter) {
return defaultFilter;
}
var rx = filter.test ? filter : new RegExp(filter);
return function (path) {
return rx.test(path);
};
}
/**
* Returns true if it is a Node
* Adapted from: http://stackoverflow.com/questions/384286/javascript-isdom-how-do-you-check-if-a-javascript-object-is-a-dom-object
* @param it anything
* @return true iff it is a Node
*/
function isNode(it) {
return typeof Node === "object"
? it instanceof Node
: it && typeof it === "object" && typeof it.nodeType === "number" && typeof it.nodeName==="string";
}
function isObject(it) {
// In IE7 tos.call(null) is '[object Object]'
// so we need to check to see if 'it' is
// even set
return it && Object.prototype.toString.call(it) == '[object Object]';
}
/**
* Function that applies tracing AOP to components being wired
* @function
* @param options {Object} tracing options
* @param plugin {Object} debug plugin instance to which to add tracing functionality
*/
createTracer = (function() {
var depth, padding, defaultStep, defaultPointcut;
/** Current trace depth */
depth = 0;
/** Padding character for indenting traces */
padding = '.';
/** 2^8 padding = 128 */
for(var i=0; i<8; i++) {
padding += padding;
}
/** Default lifecycle step at which to begin tracing */
defaultStep = 'configure';
/** Default pointcut query to match methods that will be traced */
defaultPointcut = /^[^_]/;
function logAfter(context, tag, start, val) {
console.log(context + tag + (new Date().getTime() - start.getTime()) + 'ms): ', val);
}
/**
* Creates an aspect to be applied to components that are being traced
* @param path {String} component path
*/
function createTraceAspect(path) {
return {
around:function (joinpoint) {
var val, context, start, indent;
// Setup current indent level
indent = padding.substr(0, depth);
// Form full path to invoked method
context = indent + 'DEBUG: ' + path + '.' + joinpoint.method;
// Increase the depth before proceeding so that nested traces will be indented
++depth;
logger.log(context, joinpoint.args);
try {
start = new Date();
val = joinpoint.proceed();
logAfter(context, ' RETURN (', start, val);
// return result
return val;
} catch (e) {
// rethrow
logAfter(context, ' THROW (', start, e ? e.toString() : e);
throw e;
} finally {
// And now decrease the depth after
--depth;
}
}
};
}
/**
* Implementation of createTracer
*/
return function(options, plugin, filter) {
var trace, untrace, traceStep, traceFilter, tracePointcut, traceAspects;
traceFilter = options.trace.filter ? createPathFilter(options.trace.filter) : filter;
tracePointcut = options.trace.pointcut || defaultPointcut;
traceStep = options.trace.step || defaultStep;
function isTraceable(target, prop) {
return isObject(target) && typeof target[prop] === 'function'
&& prop !== 'wire$plugin'
&& tracePointcut.test(prop);
}
/**
* Trace pointcut query function that filters out wire plugins
* @param target {Object} target object to query for methods to advise
*/
function pointcut(target) {
var matches = [];
if(isNode(target)) {
return matches;
}
for (var p in target) {
// Only match functions, exclude wire plugins, and then apply
// the supplied tracePointcut regexp
if (isTraceable(target, p)) {
matches.push(p);
}
}
return matches;
}
traceAspects = [];
trace = function (path, target) {
if (traceFilter(path)) {
// Create the aspect, if the path matched
traceAspects.push(meld.add(target, pointcut, createTraceAspect(path)));
}
// trace intentionally does not resolve the promise
// trace relies on the existing plugin method to resolve it
};
untrace = function () {
for (var i = traceAspects.length-1; i >= 0; --i) {
traceAspects[i].remove();
}
};
// Defend against changes to the plugin in future revs
var orig = plugin[traceStep] || function (promise) { promise.resolve(); };
// Replace the plugin listener method with one that will call trace()
// and add traceAspect
plugin[traceStep] = function (promise, proxy, wire) {
trace(proxy.path, proxy.target);
orig(promise, proxy, wire);
};
return { trace: trace, untrace: untrace };
};
})();
function logSeparator() {
logger.log('---------------------------------------------------');
}
return function wireDebug(options) {
var contextTimer, timeout, paths, count, tag,
logCreated, logDestroyed, checkPathsTimeout,
verbose, filter, plugin, context, tracer;
verbose = options.verbose;
contextTimer = createTimer();
count = 0;
tag = "WIRING";
tracer = { trace: noop, untrace: noop };
filter = createPathFilter(options.filter);
function contextTime(msg) {
return time(msg, contextTimer);
}
logger.log(contextTime("Context debug"));
context = {
initialize: function(resolver) {
logger.log(contextTime("Context init"));
resolver.resolve();
},
ready: function(resolver) {
cancelPathsTimeout();
logger.log(contextTime("Context ready"));
resolver.resolve();
},
destroy: function(resolver) {
tracer.untrace();
logger.log(contextTime("Context destroyed"));
resolver.resolve();
},
error: function(resolver, api, err) {
cancelPathsTimeout();
console.error(contextTime("Context ERROR: ") + err, err);
logStack(err);
resolver.reject(err);
}
};
function makeListener(step, verbose) {
return function (promise, proxy /*, wire */) {
cancelPathsTimeout();
var path = proxy.path;
if (paths[path]) {
paths[path].status = step;
}
if (verbose && filter(path)) {
var message = time(step + ' ' + (path || proxy.id || ''), contextTimer);
if (proxy.target) {
logger.log(message, proxy.target, proxy.metadata);
} else {
logger.log(message, proxy);
}
}
if(count) {
checkPathsTimeout = setTimeout(checkPaths, timeout);
}
promise.resolve();
};
}
paths = {};
timeout = options.timeout || defaultTimeout;
logCreated = makeListener('created', verbose);
logDestroyed = makeListener('destroyed', true);
function cancelPathsTimeout() {
clearTimeout(checkPathsTimeout);
checkPathsTimeout = null;
}
function checkPaths() {
if (!checkPathsTimeout) {
return;
}
var p, component, msg, ready, notReady;
logSeparator();
if(count) {
ready = [];
notReady = [];
logger.error(tag + ': No progress in ' + timeout + 'ms, status:');
for (p in paths) {
component = paths[p];
msg = p + ': ' + component.status;
(component.status == 'ready' ? ready : notReady).push(
{ msg: msg, metadata: component.metadata }
);
}
if(notReady.length > 0) {
logSeparator();
logger.log('Components that DID NOT finish wiring');
for(p = notReady.length-1; p >= 0; --p) {
component = notReady[p];
logger.error(component.msg, component.metadata);
}
}
if(ready.length > 0) {
logSeparator();
logger.log('Components that finished wiring');
for(p = ready.length-1; p >= 0; --p) {
component = ready[p];
logger.log(component.msg, component.metadata);
}
}
} else {
logger.error(tag + ': No components created after ' + timeout + 'ms');
}
logSeparator();
}
/**
* Adds a named constructor function property to the supplied component
* so that the name shows up in debug inspectors. Squelches all errors.
*/
function makeConstructor(name, component) {
/*jshint evil:true*/
var ctor;
try {
// Generate a named function to use as the constructor
name = name.replace(/^[^a-zA-Z$_]|[^a-zA-Z0-9$_]+/g, '_');
eval('ctor = function ' + name + ' () {}');
// Be friendly and make configurable and writable just in case
// some other library or application code tries to set constructor.
Object.defineProperty(component, 'constructor', {
configurable: true,
writable: true,
value: ctor
});
} catch(e) {
// Oh well, ignore.
}
}
plugin = {
context: context,
create:function (promise, proxy) {
var path, component;
path = proxy.path;
component = proxy.target;
count++;
paths[path || ('(unnamed-' + count + ')')] = {
metadata: proxy.metadata
};
if(component && typeof component == 'object'
&& !ownProp(component, 'constructor')) {
makeConstructor(proxy.id, component);
}
logCreated(promise, proxy);
},
configure: makeListener('configured', verbose),
initialize: makeListener('initialized', verbose),
ready: makeListener('ready', true),
destroy: function(promise, proxy) {
// stop tracking destroyed components, since we don't
// care anymore
delete paths[proxy.path];
count--;
tag = "DESTROY";
logDestroyed(promise, proxy);
}
};
if (options.trace) {
tracer = createTracer(options, plugin, filter);
}
checkPathsTimeout = setTimeout(checkPaths, timeout);
return plugin;
};
});
})(this, typeof define == 'function' && define.amd
? define : function(factory) { module.exports = factory(require); }
);