Skip to content

Commit c60436b

Browse files
committed
optimizations
1 parent f1d11bb commit c60436b

File tree

3 files changed

+46
-37
lines changed

3 files changed

+46
-37
lines changed

lib/backburner/binary-search.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
export default function binarySearch(time, timers) {
22
let start = 0;
3-
let end = timers.length - 7;
3+
let end = timers.length - 6;
44
let middle;
55
let l;
66

77
while (start < end) {
88
// since timers is an array of pairs 'l' will always
99
// be an integer
10-
l = (end - start) / 7;
10+
l = (end - start) / 6;
1111

1212
// compensate for the index in case even number
1313
// of pairs inside timers
14-
middle = start + l - (l % 7);
14+
middle = start + l - (l % 6);
1515

1616
if (time >= timers[middle]) {
17-
start = middle + 7;
17+
start = middle + 6;
1818
} else {
1919
end = middle;
2020
}
2121
}
2222

23-
return (time >= timers[start]) ? start + 7 : start;
23+
return (time >= timers[start]) ? start + 6 : start;
2424
}

lib/backburner/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function findItem(target, method, collection) {
2525
export function findTimerItem(target, method, collection) {
2626
let index = -1;
2727

28-
for (let i = 2, l = collection.length; i < l; i += 7) {
28+
for (let i = 2, l = collection.length; i < l; i += 6) {
2929
if (collection[i] === target && collection[i + 1] === method) {
3030
index = i - 2;
3131
break;

lib/index.ts

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ type Timer = string | number;
2323

2424
const noop = function() {};
2525

26+
const SET_TIMEOUT = setTimeout;
27+
const DISABLE_SCHEDULE = {};
28+
2629
function parseArgs(...args: any[]);
2730
function parseArgs() {
2831
let length = arguments.length;
@@ -448,21 +451,22 @@ export default class Backburner {
448451
let [target, method, args, wait, isImmediate = true] = parseDebounceArgs(...arguments);
449452

450453
let index = findTimerItem(target, method, this._timers);
451-
if (index > -1) {
452-
let timerId = this._timers[index + 1];
453-
this._timers[index + 4] = args;
454-
return timerId;
455-
}
456-
457-
wait = parseInt(wait, 10);
458-
459-
let timer = this._later(target, method, args, wait, isImmediate);
454+
let timerId;
455+
if (index === -1) {
456+
wait = parseInt(wait, 10);
457+
timerId = this._later(target, method, isImmediate ? DISABLE_SCHEDULE : args, wait);
460458

461-
if (isImmediate) {
462-
this._join(target, method, args);
459+
if (isImmediate) {
460+
this._join(target, method, args);
461+
}
462+
} else {
463+
timerId = this._timers[index + 1];
464+
if (this._timers[index + 4] !== DISABLE_SCHEDULE) {
465+
this._timers[index + 4] = args;
466+
}
463467
}
464468

465-
return timer;
469+
return timerId;
466470
}
467471

468472
// with target, with method name, with optional immediate
@@ -486,22 +490,28 @@ export default class Backburner {
486490
debounceCount++;
487491
let [target, method, args, wait, isImmediate = false] = parseDebounceArgs(...arguments);
488492

489-
// Remove debouncee
490493
let index = findTimerItem(target, method, this._timers);
491-
if (index > -1) {
492-
this._timers.splice(index, 7);
493-
if (index === 0) {
494-
this._reinstallTimerTimeout();
494+
let timerId;
495+
if (index === -1) {
496+
timerId = this._later(target, method, isImmediate ? DISABLE_SCHEDULE : args, wait);
497+
if (isImmediate) {
498+
this._join(target, method, args);
495499
}
496-
}
500+
} else {
501+
let executeAt = this._platform.now() + wait || this._timers[index];
502+
this._timers[index] = executeAt;
497503

498-
let timer = this._later(target, method, args, wait, isImmediate);
504+
if (this._timers[index + 4] !== DISABLE_SCHEDULE) {
505+
this._timers[index + 4] = args;
506+
}
499507

500-
if (isImmediate && index === -1) {
501-
this._join(target, method, args);
508+
timerId = this._timers[index + 1];
509+
if (index === 0) {
510+
this._reinstallTimerTimeout();
511+
}
502512
}
503513

504-
return timer;
514+
return timerId;
505515
}
506516

507517
public cancelTimers() {
@@ -614,12 +624,12 @@ export default class Backburner {
614624
let id = UUID++;
615625

616626
if (this._timers.length === 0) {
617-
this._timers.push(executeAt, id, target, method, args, isImmediate, stack);
627+
this._timers.push(executeAt, id, target, method, args, stack);
618628
this._installTimerTimeout();
619629
} else {
620630
// find position to insert
621631
let i = searchTimer(executeAt, this._timers);
622-
this._timers.splice(i, 0, executeAt, id, target, method, args, isImmediate, stack);
632+
this._timers.splice(i, 0, executeAt, id, target, method, args, stack);
623633

624634
// we should be the new earliest timer if i == 0
625635
if (i === 0) {
@@ -630,10 +640,10 @@ export default class Backburner {
630640
}
631641

632642
private _cancelLaterTimer(timer) {
633-
for (let i = 1; i < this._timers.length; i += 7) {
643+
for (let i = 1; i < this._timers.length; i += 6) {
634644
if (this._timers[i] === timer) {
635645
i = i - 1;
636-
this._timers.splice(i, 7);
646+
this._timers.splice(i, 6);
637647
if (i === 0) {
638648
this._reinstallTimerTimeout();
639649
}
@@ -680,15 +690,14 @@ export default class Backburner {
680690
let defaultQueue = this._defaultQueue;
681691
let n = this._platform.now();
682692

683-
for (; i < l; i += 7) {
693+
for (; i < l; i += 6) {
684694
let executeAt = timers[i];
685695
if (executeAt > n) { break; }
686-
let isImmediate = timers[i + 5];
687-
if (isImmediate === false) {
696+
let args = timers[i + 4];
697+
if (args !== DISABLE_SCHEDULE) {
688698
let target = timers[i + 2];
689699
let method = timers[i + 3];
690-
let args = timers[i + 4];
691-
let stack = timers[i + 6];
700+
let stack = timers[i + 5];
692701
this.currentInstance!.schedule(defaultQueue, target, method, args, false, stack);
693702
}
694703
}

0 commit comments

Comments
 (0)