Skip to content

Commit

Permalink
Async stack traces using native Promise asyncronicity
Browse files Browse the repository at this point in the history
  • Loading branch information
adriancooney committed Apr 4, 2018
1 parent 07195a3 commit 47e3944
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
38 changes: 34 additions & 4 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,12 @@ export default class Backburner {
public run() {
runCount++;
let [target, method, args] = parseArgs(...arguments);
return this._run(target, method, args);

if(this.DEBUG) {
return this._run(null, this._asyncExecute(target, method, args, undefined), null);
} else {
return this._run(target, method, args);
}
}

/*
Expand Down Expand Up @@ -359,8 +364,13 @@ export default class Backburner {
public schedule(queueName, ..._args) {
scheduleCount++;
let [target, method, args] = parseArgs(..._args);
let stack = this.DEBUG ? new Error() : undefined;
return this._ensureInstance().schedule(queueName, target, method, args, false, stack);

if (this.DEBUG) {
let stackError = new Error();
return this._ensureInstance().schedule(queueName, null, this._asyncExecute(target, method, args, stackError), null, false, stackError);
} else {
return this._ensureInstance().schedule(queueName, target, method, args, false, undefined);
}
}

/*
Expand Down Expand Up @@ -427,7 +437,11 @@ export default class Backburner {
public later() {
laterCount++;
let [target, method, args, wait] = parseTimerArgs(...arguments);
return this._later(target, method, args, wait);
if(this.DEBUG) {
return this._later(null, this._asyncExecute(target, method, args, new Error()), null, wait);
} else {
return this._later(target, method, args, wait);
}
}

public throttle<T>(target: T, methodName: keyof T, wait?: number | string, immediate?: boolean): Timer;
Expand Down Expand Up @@ -757,6 +771,22 @@ export default class Backburner {
this._timerTimeoutId = this._platform.setTimeout(this._boundRunExpiredTimers, wait);
}

private _asyncExecute(target, method, args, stackError) {
let resolve;

new Promise(_resolve => resolve = _resolve)
.then(method.bind(target, ...args))
.catch(err => {
let onError = getOnError(this.options);

if(onError) {
onError.call(null, err, stackError);
}
});

return resolve;
}

private _ensureInstance(): DeferredActionQueues {
let currentInstance = this.currentInstance;
if (currentInstance === null) {
Expand Down
2 changes: 2 additions & 0 deletions tests/debug-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ QUnit.test('schedule - DEBUG flag enables stack tagging', function(assert) {

if (new Error().stack) { // workaround for CLI runner :(
assert.expect(4);
let done = assert.async();
let stack = bb.currentInstance && bb.currentInstance.queues.one.stackFor(1);
assert.ok(typeof stack === 'string', 'A stack is recorded');

let onError = function(error, errorRecordedForStack) {
assert.ok(errorRecordedForStack, 'errorRecordedForStack passed to error function');
assert.ok(errorRecordedForStack.stack, 'stack is recorded');
done();
};

bb = new Backburner(['errors'], { onError });
Expand Down

0 comments on commit 47e3944

Please sign in to comment.