@@ -23,6 +23,9 @@ type Timer = string | number;
2323
2424const noop = function ( ) { } ;
2525
26+ const SET_TIMEOUT = setTimeout ;
27+ const DISABLE_SCHEDULE = { } ;
28+
2629function parseArgs ( ...args : any [ ] ) ;
2730function 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