-
Notifications
You must be signed in to change notification settings - Fork 395
/
PromiseMonitor.js
154 lines (126 loc) · 3.98 KB
/
PromiseMonitor.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
/** @license MIT License (c) copyright 2010-2014 original author or authors */
/** @author Brian Cavalier */
/** @author John Hann */
(function(define) { 'use strict';
define(function(require) {
var defaultStackJumpSeparator = 'from execution context:';
var defaultStackFilter = /(node|module|timers)\.js:|when([\/\\]{1,2}(lib|monitor|es6-shim)[\/\\]{1,2}|\.js)|(new\sPromise)\b|(\b(PromiseMonitor|ConsoleReporter|Scheduler|RunHandlerTask|ProgressTask|Promise|.*Handler)\.[\w_]\w\w+\b)|\b(tryCatch\w+|getHandler\w*)\b/i;
var setTimer = require('../lib/timer').set;
var error = require('./error');
function PromiseMonitor(reporter) {
this._traces = {};
this.traceTask = 0;
this.logDelay = 100;
this.stackFilter = defaultStackFilter;
this.stackJumpSeparator = defaultStackJumpSeparator;
this.filterDuplicateFrames = true;
this._reporter = reporter;
if(typeof reporter.configurePromiseMonitor === 'function') {
reporter.configurePromiseMonitor(this);
}
var self = this;
this._doLogTraces = function() {
self._logTraces();
};
}
PromiseMonitor.prototype.captureStack = function(host, at) {
return error.captureStack(host, at);
};
PromiseMonitor.prototype.addTrace = function(handler, extraContext) {
this._traces[handler.id] = {
error: handler.value,
context: handler.context,
extraContext: extraContext
};
this.logTraces();
};
PromiseMonitor.prototype.removeTrace = function(handler) {
if(handler.id in this._traces) {
delete this._traces[handler.id];
this.logTraces();
}
};
PromiseMonitor.prototype.fatal = function(handler) {
var err = new Error();
err.stack = this._createLongTrace(handler.value, handler.context).join('\n');
setTimer(function() {
throw err;
}, 0);
};
PromiseMonitor.prototype.logTraces = function() {
if(!this.traceTask) {
this.traceTask = setTimer(this._doLogTraces, this.logDelay);
}
};
PromiseMonitor.prototype._logTraces = function() {
this.traceTask = void 0;
this._reporter.log(this.formatTraces(this._traces));
};
PromiseMonitor.prototype.formatTraces = function(traces) {
var keys = Object.keys(traces);
var formatted = [];
var longTrace, t, i;
for(i=0; i<keys.length; ++i) {
t = traces[keys[i]];
longTrace = this._createLongTrace(t.error, t.context, t.extraContext);
formatted.push(longTrace);
}
return formatted;
};
PromiseMonitor.prototype._createLongTrace = function(e, context, extraContext) {
var trace = error.parse(e) || [];
trace = filterFrames(this.stackFilter, trace, 0);
this._appendContext(trace, context);
this._appendContext(trace, extraContext);
return this.filterDuplicateFrames ? this._removeDuplicates(trace) : trace;
};
PromiseMonitor.prototype._removeDuplicates = function(trace) {
var seen = {};
var sep = this.stackJumpSeparator;
var count = 0;
return trace.reduceRight(function(deduped, line, i) {
if(i === 0) {
deduped.unshift(line);
} else if(line === sep) {
if(count > 0) {
deduped.unshift(line);
count = 0;
}
} else if(!seen[line]) {
seen[line] = true;
deduped.unshift(line);
++count;
}
return deduped;
}, []);
};
PromiseMonitor.prototype._appendContext = function(trace, context) {
trace.push.apply(trace, this._createTrace(context));
};
PromiseMonitor.prototype._createTrace = function(traceChain) {
var trace = [];
var stack;
while(traceChain) {
stack = error.parse(traceChain);
if (stack) {
stack = filterFrames(this.stackFilter, stack);
appendStack(trace, stack, this.stackJumpSeparator);
}
traceChain = traceChain.parent;
}
return trace;
};
function appendStack(trace, stack, separator) {
if (stack.length > 1) {
stack[0] = separator;
trace.push.apply(trace, stack);
}
}
function filterFrames(stackFilter, stack) {
return stack.filter(function(frame) {
return !stackFilter.test(frame);
});
}
return PromiseMonitor;
});
}(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(require); }));