diff --git a/lib/backburner/binary-search.ts b/lib/backburner/binary-search.ts index 4807f439..754c677d 100644 --- a/lib/backburner/binary-search.ts +++ b/lib/backburner/binary-search.ts @@ -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; } diff --git a/lib/index.ts b/lib/index.ts index 3c45ade8..52a1e41b 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -43,6 +43,8 @@ function parseArgs() { return [target, method, args]; } +let UUID = 0; + export default class Backburner { public static Queue = Queue; @@ -351,25 +353,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(...args); @@ -485,9 +469,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); @@ -541,31 +525,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(); } @@ -617,15 +603,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; + this.currentInstance.schedule(defaultQueue, target, method, args, false, stack); } else { break; }