From 3e59a171b71a94b0b14a617f4f4a64c2053a28c6 Mon Sep 17 00:00:00 2001 From: bekzod Date: Sun, 28 May 2017 01:08:42 +0500 Subject: [PATCH] wip --- lib/backburner/binary-search.ts | 8 ++++---- lib/index.ts | 30 ++++++++++++++---------------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/lib/backburner/binary-search.ts b/lib/backburner/binary-search.ts index 1a1e5a5d..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 - 5; + 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 % 5); + middle = start + l - (l % 6); if (time >= timers[middle]) { - start = middle + 5; + start = middle + 6; } else { end = middle; } } - return (time >= timers[start]) ? start + 5 : start; + return (time >= timers[start]) ? start + 6 : start; } diff --git a/lib/index.ts b/lib/index.ts index 4bb92790..d62cee98 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -15,10 +15,7 @@ import iteratorDrain from './backburner/iterator-drain'; import Queue, { QUEUE_STATE } from './backburner/queue'; -let id = 0; -function getId() { - return (id++) + ''; -} +let UUID = 0; export default class Backburner { public static Queue = Queue; @@ -449,7 +446,8 @@ export default class Backburner { } } - return this._setTimeout(target, method, args, wait); + let stack = this.DEBUG ? new Error() : undefined; + return this._setTimeout(target, method, args, wait, stack); } public throttle(...args); @@ -579,18 +577,18 @@ export default class Backburner { 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 === 'string') { // we're cancelling a setTimeout - for (let i = 0, l = this._timers.length; i < l; i += 5) { - if (this._timers[i + 4] === timer) { - this._timers.splice(i, 5); + 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; @@ -603,12 +601,12 @@ export default class Backburner { } } - private _setTimeout(target, method, args, wait) { + private _setTimeout(target, method, args, wait, stack) { let executeAt = now() + wait; - let id = getId() + let id = UUID++; if (this._timers.length === 0) { - this._timers.push(executeAt, target, method, args, id); + this._timers.push(executeAt, target, method, args, stack, id); this._installTimerTimeout(); return id; } @@ -616,7 +614,7 @@ export default class Backburner { // find position to insert let i = searchTimer(executeAt, this._timers); - this._timers.splice(i, 0, executeAt, target, method, args, id); + this._timers.splice(i, 0, executeAt, target, method, args, stack, id); // we should be the new earliest timer if i == 0 if (i === 0) { @@ -671,13 +669,13 @@ export default class Backburner { let l = timers.length; let defaultQueue = this.options.defaultQueue; let n = now(); - for (; i < l; i += 5) { + for (; i < l; i += 6) { let executeAt = timers[i]; if (executeAt <= n) { let target = timers[i + 1]; let method = timers[i + 2]; let args = timers[i + 3]; - let stack = this.DEBUG ? new Error() : undefined; + let stack = timers[i + 4]; this.currentInstance.schedule(defaultQueue, target, method, args, false, stack); } else { break;