Skip to content

Commit

Permalink
refactor(lst): convert to a CJS module
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb committed Jun 1, 2015
1 parent 1639ead commit 91d0a8f
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 3 deletions.
7 changes: 6 additions & 1 deletion gulpfile.js
Expand Up @@ -41,10 +41,15 @@ gulp.task('build/jasmine-patch.js', function() {
return generateBrowserScript('./lib/browser/jasmine-patch.js', 'jasmine-patch.js');
});

gulp.task('build/long-stack-trace-zone.js', function() {
return generateBrowserScript('./lib/browser/long-stack-trace-zone.js', 'long-stack-trace-zone.js');
});

gulp.task('build', [
'build/zone.js',
'build/zone-microtask.js',
'build/jasmine-patch.js'
'build/jasmine-patch.js',
'build/long-stack-trace-zone.js'
]);


Expand Down
3 changes: 2 additions & 1 deletion karma-microtasks.conf.js
Expand Up @@ -6,7 +6,8 @@ module.exports = function (config) {
files: [
'test/util.js',
'test/setup-microtask.js',
'dist/*-zone.js',
'dist/counting-zone.js',
'dist/except-zone.js',
'test/**/*.spec.js',
{pattern: 'test/assets/**/*.*', watched: true, served: true, included: false},
{pattern: 'lib/**/*.js', watched: true, served: false, included: false}
Expand Down
3 changes: 2 additions & 1 deletion karma.conf.js
Expand Up @@ -6,7 +6,8 @@ module.exports = function (config) {
files: [
'test/util.js',
'test/setup.js',
'dist/*-zone.js',
'dist/counting-zone.js',
'dist/except-zone.js',
//'test/lib/brick.js',
'test/**/*.spec.js',
{pattern: 'test/assets/**/*.*', watched: true, served: true, included: false},
Expand Down
7 changes: 7 additions & 0 deletions lib/browser/long-stack-trace-zone.js
@@ -0,0 +1,7 @@
'use strict';

if (!global.Zone) {
throw new Error('zone.js should be installed before loading the long stack trace zone');
}

global.Zone.longStackTraceZone = require('../zones/long-stack-trace.js');
90 changes: 90 additions & 0 deletions lib/zones/long-stack-trace.js
@@ -0,0 +1,90 @@
/*
* Wrapped stacktrace
*
* We need this because in some implementations, constructing a trace is slow
* and so we want to defer accessing the trace for as long as possible
*/

'use strict';

function _Stacktrace(e) {
this._e = e;
};

_Stacktrace.prototype.get = function () {
if (global.zone.stackFramesFilter && this._e.stack) {
return this._e.stack
.split('\n')
.filter(global.zone.stackFramesFilter)
.join('\n');
}

return this._e.stack;
}

function _getStacktraceWithUncaughtError () {
return new _Stacktrace(new Error());
}

function _getStacktraceWithCaughtError () {
try {
throw new Error();
} catch (e) {
return new _Stacktrace(e);
}
}

// Some implementations of exception handling don't create a stack trace if the exception
// isn't thrown, however it's faster not to actually throw the exception.
var stack = _getStacktraceWithUncaughtError();

var _getStacktrace = stack && stack._e.stack
? _getStacktraceWithUncaughtError
: _getStacktraceWithCaughtError;

module.exports = {
getLongStacktrace: function (exception) {
var traces = [];
var currentZone = this;
if (exception) {
if (currentZone.stackFramesFilter && exception.stack) {
traces.push(exception.stack.split('\n')
.filter(currentZone.stackFramesFilter)
.join('\n'));
} else {
traces.push(exception.stack);
}
}
var now = Date.now();

while (currentZone && currentZone.constructedAtException) {
traces.push(
'--- ' + (Date(currentZone.constructedAtTime)).toString() +
' - ' + (now - currentZone.constructedAtTime) + 'ms ago',
currentZone.constructedAtException.get());
currentZone = currentZone.parent;
}

return traces.join('\n');
},

stackFramesFilter: function (line) {
return /zone(-microtask)?(\.min)?\.js/.test(line);
},

onError: function (exception) {
var reporter = this.reporter || console.log.bind(console);
reporter(exception.toString());
reporter(this.getLongStacktrace(exception));
},

'$fork': function (parentFork) {
return function() {
var newZone = parentFork.apply(this, arguments);
newZone.constructedAtException = _getStacktrace();
newZone.constructedAtTime = Date.now();
return newZone;
}
}
};

1 change: 1 addition & 0 deletions test/setup-microtask.js
@@ -1,5 +1,6 @@
// Setup tests for Zone with microtask support
require('../lib/browser/zone-microtask.js');
require('../lib/browser/long-stack-trace-zone.js');

// Patch jasmine
require('../lib/browser/jasmine-patch.js');
Expand Down
1 change: 1 addition & 0 deletions test/setup.js
@@ -1,5 +1,6 @@
// Setup tests for Zone without microtask support
require('../lib/browser/zone.js');
require('../lib/browser/long-stack-trace-zone.js');

// Patch jasmine
require('../lib/browser/jasmine-patch.js');
Expand Down

0 comments on commit 91d0a8f

Please sign in to comment.