diff --git a/bench/benches/later-cancel.js b/bench/benches/later-cancel.js index a968cb6f..06a05ab4 100644 --- a/bench/benches/later-cancel.js +++ b/bench/benches/later-cancel.js @@ -6,6 +6,8 @@ function sharedSetup() { var target = { someMethod: function() { } }; + + let t = 50; } module.exports = [ @@ -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', diff --git a/lib/backburner/binary-search.ts b/lib/backburner/binary-search.ts index 4807f439..2dbeeaab 100644 --- a/lib/backburner/binary-search.ts +++ b/lib/backburner/binary-search.ts @@ -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; @@ -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; } diff --git a/lib/index.ts b/lib/index.ts index 0b3c22aa..5d02a8d3 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -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; @@ -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); @@ -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; @@ -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) { @@ -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; }