Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
bekzod committed May 29, 2017
1 parent 580bf27 commit 3e59a17
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
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 - 5;
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 % 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;
}
30 changes: 14 additions & 16 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -603,20 +601,20 @@ 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;
}

// 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) {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 3e59a17

Please sign in to comment.