@@ -23,6 +23,9 @@ type Timer = string | number;
23
23
24
24
const noop = function ( ) { } ;
25
25
26
+ const SET_TIMEOUT = setTimeout ;
27
+ const DISABLE_SCHEDULE = { } ;
28
+
26
29
function parseArgs ( ...args : any [ ] ) ;
27
30
function parseArgs ( ) {
28
31
let length = arguments . length ;
@@ -448,21 +451,22 @@ export default class Backburner {
448
451
let [ target , method , args , wait , isImmediate = true ] = parseDebounceArgs ( ...arguments ) ;
449
452
450
453
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 ) ;
460
458
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
+ }
463
467
}
464
468
465
- return timer ;
469
+ return timerId ;
466
470
}
467
471
468
472
// with target, with method name, with optional immediate
@@ -486,22 +490,28 @@ export default class Backburner {
486
490
debounceCount ++ ;
487
491
let [ target , method , args , wait , isImmediate = false ] = parseDebounceArgs ( ...arguments ) ;
488
492
489
- // Remove debouncee
490
493
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 ) ;
495
499
}
496
- }
500
+ } else {
501
+ let executeAt = this . _platform . now ( ) + wait || this . _timers [ index ] ;
502
+ this . _timers [ index ] = executeAt ;
497
503
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
+ }
499
507
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
+ }
502
512
}
503
513
504
- return timer ;
514
+ return timerId ;
505
515
}
506
516
507
517
public cancelTimers ( ) {
@@ -614,12 +624,12 @@ export default class Backburner {
614
624
let id = UUID ++ ;
615
625
616
626
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 ) ;
618
628
this . _installTimerTimeout ( ) ;
619
629
} else {
620
630
// find position to insert
621
631
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 ) ;
623
633
624
634
// we should be the new earliest timer if i == 0
625
635
if ( i === 0 ) {
@@ -630,10 +640,10 @@ export default class Backburner {
630
640
}
631
641
632
642
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 ) {
634
644
if ( this . _timers [ i ] === timer ) {
635
645
i = i - 1 ;
636
- this . _timers . splice ( i , 7 ) ;
646
+ this . _timers . splice ( i , 6 ) ;
637
647
if ( i === 0 ) {
638
648
this . _reinstallTimerTimeout ( ) ;
639
649
}
@@ -680,15 +690,14 @@ export default class Backburner {
680
690
let defaultQueue = this . _defaultQueue ;
681
691
let n = this . _platform . now ( ) ;
682
692
683
- for ( ; i < l ; i += 7 ) {
693
+ for ( ; i < l ; i += 6 ) {
684
694
let executeAt = timers [ i ] ;
685
695
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 ) {
688
698
let target = timers [ i + 2 ] ;
689
699
let method = timers [ i + 3 ] ;
690
- let args = timers [ i + 4 ] ;
691
- let stack = timers [ i + 6 ] ;
700
+ let stack = timers [ i + 5 ] ;
692
701
this . currentInstance ! . schedule ( defaultQueue , target , method , args , false , stack ) ;
693
702
}
694
703
}
0 commit comments