From 5617435a5fddecdf0fe0d66ef9dce2cb60f77e7e Mon Sep 17 00:00:00 2001 From: bekzod Date: Tue, 13 Jun 2017 01:50:59 +0500 Subject: [PATCH] add error stack to `.later` --- lib/backburner/binary-search.ts | 10 +++--- lib/index.ts | 54 ++++++++++++++------------------- 2 files changed, 27 insertions(+), 37 deletions(-) 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 aff865f8..22dc961b 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -16,6 +16,8 @@ import Queue, { QUEUE_STATE } from './backburner/queue'; const noop = function() {}; const SET_TIMEOUT = setTimeout; +let UUID = 0; + export default class Backburner { public static Queue = Queue; @@ -437,25 +439,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); @@ -565,9 +549,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); @@ -587,31 +571,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(); } @@ -667,11 +653,15 @@ export default class Backburner { let i = 0; 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; }