Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add error stack to .later #250

Merged
merged 3 commits into from
Jan 20, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/backburner/binary-search.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
export default function binarySearch(time, timers) {
let start = 0;
let end = timers.length - 2;
let end = timers.length - 5;
let middle;
let l;

while (start < end) {
// since timers is an array of pairs 'l' will always
// be an integer
l = (end - start) / 2;
l = (end - start) / 5;

// compensate for the index in case even number
// of pairs inside timers
middle = start + l - (l % 2);
middle = start + l - (l % 5);

if (time >= timers[middle]) {
start = middle + 2;
start = middle + 5;
} else {
end = middle;
}
}

return (time >= timers[start]) ? start + 2 : start;
return (time >= timers[start]) ? start + 5 : start;
}
56 changes: 23 additions & 33 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ function parseArgs() {
return [target, method, args];
}

let UUID = 0;

export default class Backburner {
public static Queue = Queue;

Expand Down Expand Up @@ -327,25 +329,7 @@ export default class Backburner {
}
}

let onError = getOnError(this.options);
let executeAt = this._platform.now() + wait;

let fn;
if (onError) {
fn = function() {
try {
method.apply(target, args);
} catch (e) {
onError(e);
}
};
} else {
fn = function() {
method.apply(target, args);
};
}

return this._setTimeout(fn, executeAt);
return this._setTimeout(target, method, args, wait);
}

public throttle<T>(target: T, methodName: keyof T, wait?: number | string, immediate?: boolean): Timer;
Expand Down Expand Up @@ -530,9 +514,9 @@ export default class Backburner {
if (!timer) { return false; }
let timerType = typeof timer;

if (timerType === 'number' || timerType === 'string') { // we're cancelling a throttle or debounce
if (timerType === 'number') { // we're cancelling a throttle or debounce
return this._cancelItem(timer, this._throttlers) || this._cancelItem(timer, this._debouncees);
} else if (timerType === 'function') { // we're cancelling a setTimeout
} else if (timerType === 'string') { // we're cancelling a setTimeout
return this._cancelLaterTimer(timer);
} else if (timerType === 'object' && timer.queue && timer.method) { // we're cancelling a deferOnce
return timer.queue.cancel(timer);
Expand Down Expand Up @@ -586,31 +570,33 @@ export default class Backburner {
}
}

private _setTimeout(fn, executeAt) {
private _setTimeout(target, method, args, wait) {
let executeAt = this._platform.now() + wait;
let id = (UUID++) + '';

if (this._timers.length === 0) {
this._timers.push(executeAt, fn);
this._timers.push(executeAt, id, target, method, args);
this._installTimerTimeout();
return fn;
return id;
}

// find position to insert
let i = searchTimer(executeAt, this._timers);

this._timers.splice(i, 0, executeAt, fn);
this._timers.splice(i, 0, executeAt, id, target, method, args);

// we should be the new earliest timer if i == 0
if (i === 0) {
this._reinstallTimerTimeout();
}

return fn;
return id;
}

private _cancelLaterTimer(timer) {
for (let i = 1; i < this._timers.length; i += 2) {
for (let i = 1; i < this._timers.length; i += 5) {
if (this._timers[i] === timer) {
i = i - 1;
this._timers.splice(i, 2); // remove the two elements
this._timers.splice(i, 5);
if (i === 0) {
this._reinstallTimerTimeout();
}
Expand Down Expand Up @@ -662,15 +648,19 @@ export default class Backburner {

private _scheduleExpiredTimers() {
let timers = this._timers;
let l = timers.length;
let i = 0;
let l = timers.length;
let defaultQueue = this.options.defaultQueue;
let n = this._platform.now();
for (; i < l; i += 2) {

for (; i < l; i += 5) {
let executeAt = timers[i];
if (executeAt <= n) {
let fn = timers[i + 1];
this.schedule(defaultQueue, null, fn);
let target = timers[i + 2];
let method = timers[i + 3];
let args = timers[i + 4];
let stack = this.DEBUG ? new Error() : undefined;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is being done in the wrong place. _scheduleExpiredTimers is after the real setTimeout has ran and we are scheduling the jobs into the actions queue. That will mean the debug stack we are grabbing here is now the stack from where run.later(...) was called.

This needs to be moved into _setTimeout where we are doing this._timers.splice(...) / this._timers.push so that the captured debug stack is the "right" place.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, pushed a fix for this issue to the branch along with a test...

this.currentInstance!.schedule(defaultQueue, target, method, args, false, stack);
} else {
break;
}
Expand Down