Skip to content

Commit

Permalink
add error stack to .later
Browse files Browse the repository at this point in the history
  • Loading branch information
bekzod committed Jun 9, 2017
1 parent 4e4d7a3 commit 85a6281
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 38 deletions.
14 changes: 14 additions & 0 deletions bench/benches/later-cancel.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ function sharedSetup() {
var target = {
someMethod: function() { }
};

let t = 50;
}

module.exports = [
Expand All @@ -21,6 +23,18 @@ module.exports = [
backburner.cancel(timer);
}
},
{
name: 'Later - function',

Backburner: Backburner,

setup: sharedSetup,

fn: function() {
backburner.later(null, target.someMethod, t);
t += 5;
}
},
{
name: 'Later & Cancel - function, target',

Expand Down
8 changes: 4 additions & 4 deletions lib/backburner/binary-search.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default function binarySearch(time, timers) {
let start = 0;
let end = timers.length - 2;
let end = timers.length - 6;
let middle;
let l;

Expand All @@ -11,14 +11,14 @@ export default function binarySearch(time, timers) {

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

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

return (time >= timers[start]) ? start + 2 : start;
return (time >= timers[start]) ? start + 6 : start;
}
62 changes: 28 additions & 34 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import Queue, { QUEUE_STATE } from './backburner/queue';

const noop = function() {};

let TIMER_ID = 0;
function getId() {
return '' + (TIMER_ID++);
}

export default class Backburner {
public static Queue = Queue;

Expand Down Expand Up @@ -445,25 +450,8 @@ export default class Backburner {
}
}

let onError = getOnError(this.options);
let executeAt = 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);
let stack = this.DEBUG ? new Error() : undefined;
return this._setTimeout(target, method, args, wait, stack);
}

public throttle(...args);
Expand Down Expand Up @@ -590,21 +578,21 @@ export default class Backburner {
if (!timer) { return false; }
let timerType = typeof timer;

if (timerType === 'number' || timerType === 'string') {
if (timerType === 'number') {
// we're cancelling a throttle or debounce
return this._cancelItem(timer, this._throttlers) || this._cancelItem(timer, this._debouncees);
} else if (timerType === 'object' && timer.queue && timer.method) { // we're cancelling a deferOnce
return timer.queue.cancel(timer);
} else if (timerType === 'function') { // we're cancelling a setTimeout
for (let i = 0, l = this._timers.length; i < l; i += 2) {
if (this._timers[i + 1] === timer) {
this._timers.splice(i, 2); // remove the two elements
} else if (timerType === 'string') { // we're cancelling a setTimeout
for (let i = 0, l = this._timers.length; i < l; i += 6) {
if (this._timers[i + 5] === timer) {
this._timers.splice(i, 6);
if (i === 0) {
this._reinstallTimerTimeout();
}
return true;
}
}
} else if (timerType === 'object' && timer.queue && timer.method) { // we're cancelling a deferOnce
return timer.queue.cancel(timer);
}

return false;
Expand All @@ -617,24 +605,27 @@ export default class Backburner {
}
}

private _setTimeout(fn, executeAt) {
private _setTimeout(target, method, args, wait, stack) {
let executeAt = now() + wait;
let id = getId();

if (this._timers.length === 0) {
this._timers.push(executeAt, fn);
this._timers.push(executeAt, target, method, args, stack, id);
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, target, method, args, stack, id);

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

return fn;
return id;
}

private _cancelItem(timer, array) {
Expand Down Expand Up @@ -684,11 +675,14 @@ export default class Backburner {
let l = timers.length;
let defaultQueue = this.options.defaultQueue;
let n = now();
for (; i < l; i += 2) {
for (; i < l; i += 6) {
let executeAt = timers[i];
if (executeAt <= n) {
let fn = timers[i + 1];
this.schedule(defaultQueue, null, fn);
let target = timers[i + 1];
let method = timers[i + 2];
let args = timers[i + 3];
let stack = timers[i + 4];
this.currentInstance.schedule(defaultQueue, target, method, args, false, stack);
} else {
break;
}
Expand Down

0 comments on commit 85a6281

Please sign in to comment.