8
8
IPlatform ,
9
9
} from './backburner/platform' ;
10
10
import {
11
- findItem ,
12
- findTimer ,
11
+ findTimerItem ,
13
12
getOnError ,
14
13
isCoercableNumber
15
14
} from './backburner/utils' ;
@@ -24,6 +23,9 @@ type Timer = string | number;
24
23
25
24
const noop = function ( ) { } ;
26
25
26
+ const SET_TIMEOUT = setTimeout ;
27
+ const DISABLE_SCHEDULE = Object . freeze ( [ ] ) ;
28
+
27
29
function parseArgs ( ...args : any [ ] ) ;
28
30
function parseArgs ( ) {
29
31
let length = arguments . length ;
@@ -193,8 +195,6 @@ export default class Backburner {
193
195
private _onEnd : ( currentInstance : DeferredActionQueues , nextInstance : DeferredActionQueues | null ) => void ;
194
196
private queueNames : string [ ] ;
195
197
private instanceStack : DeferredActionQueues [ ] = [ ] ;
196
- private _debouncees : any [ ] = [ ] ;
197
- private _throttlers : any [ ] = [ ] ;
198
198
private _eventCallbacks : {
199
199
end : Function [ ] ;
200
200
begin : Function [ ] ;
@@ -450,27 +450,23 @@ export default class Backburner {
450
450
throttleCount ++ ;
451
451
let [ target , method , args , wait , isImmediate = true ] = parseDebounceArgs ( ...arguments ) ;
452
452
453
- let index = findItem ( target , method , this . _throttlers ) ;
454
- if ( index > - 1 ) {
455
- this . _throttlers [ index + 2 ] = args ;
456
- return this . _throttlers [ index + 3 ] ;
457
- } // throttled
458
-
459
- let timer = this . _platform . setTimeout ( ( ) => {
460
- let i = findTimer ( timer , this . _throttlers ) ;
461
- let [ context , func , params ] = this . _throttlers . splice ( i , 4 ) ;
462
- if ( isImmediate === false ) {
463
- this . _run ( context , func , params ) ;
464
- }
465
- } , wait ) ;
453
+ let index = findTimerItem ( target , method , this . _timers ) ;
454
+ let timerId ;
455
+ if ( index === - 1 ) {
456
+ timerId = this . _later ( target , method , isImmediate ? DISABLE_SCHEDULE : args , wait ) ;
466
457
467
- if ( isImmediate ) {
468
- this . _join ( target , method , args ) ;
458
+ if ( isImmediate ) {
459
+ this . _join ( target , method , args ) ;
460
+ }
461
+ } else {
462
+ timerId = this . _timers [ index + 1 ] ;
463
+ let argIndex = index + 4 ;
464
+ if ( this . _timers [ argIndex ] !== DISABLE_SCHEDULE ) {
465
+ this . _timers [ argIndex ] = args ;
466
+ }
469
467
}
470
468
471
- this . _throttlers . push ( target , method , args , timer ) ;
472
-
473
- return timer ;
469
+ return timerId ;
474
470
}
475
471
476
472
// with target, with method name, with optional immediate
@@ -494,65 +490,49 @@ export default class Backburner {
494
490
debounceCount ++ ;
495
491
let [ target , method , args , wait , isImmediate = false ] = parseDebounceArgs ( ...arguments ) ;
496
492
497
- // Remove debouncee
498
- let index = findItem ( target , method , this . _debouncees ) ;
499
- if ( index > - 1 ) {
500
- let timerId = this . _debouncees [ index + 3 ] ;
501
- this . _platform . clearTimeout ( timerId ) ;
502
- this . _debouncees . splice ( index , 4 ) ;
503
- }
493
+ let index = findTimerItem ( target , method , this . _timers ) ;
504
494
505
- let timer = this . _platform . setTimeout ( ( ) => {
506
- let i = findTimer ( timer , this . _debouncees ) ;
507
- let [ context , func , params ] = this . _debouncees . splice ( i , 4 ) ;
508
- if ( isImmediate === false ) {
509
- this . _run ( context , func , params ) ;
495
+ let timerId ;
496
+ if ( index === - 1 ) {
497
+ timerId = this . _later ( target , method , isImmediate ? DISABLE_SCHEDULE : args , wait ) ;
498
+ if ( isImmediate ) {
499
+ this . _join ( target , method , args ) ;
510
500
}
511
- } , wait ) ;
501
+ } else {
502
+ let executeAt = this . _platform . now ( ) + wait || this . _timers [ index ] ;
503
+ this . _timers [ index ] = executeAt ;
512
504
513
- if ( isImmediate && index === - 1 ) {
514
- this . _join ( target , method , args ) ;
515
- }
505
+ let argIndex = index + 4 ;
506
+ if ( this . _timers [ argIndex ] !== DISABLE_SCHEDULE ) {
507
+ this . _timers [ argIndex ] = args ;
508
+ }
509
+ timerId = this . _timers [ index + 1 ] ;
516
510
517
- this . _debouncees . push ( target , method , args , timer ) ;
511
+ if ( index === 0 ) {
512
+ this . _reinstallTimerTimeout ( ) ;
513
+ }
514
+ }
518
515
519
- return timer ;
516
+ return timerId ;
520
517
}
521
518
522
519
public cancelTimers ( ) {
523
520
cancelTimersCount ++ ;
524
- for ( let i = 3 ; i < this . _throttlers . length ; i += 4 ) {
525
- this . _platform . clearTimeout ( this . _throttlers [ i ] ) ;
526
- }
527
- this . _throttlers = [ ] ;
528
-
529
- for ( let t = 3 ; t < this . _debouncees . length ; t += 4 ) {
530
- this . _platform . clearTimeout ( this . _debouncees [ t ] ) ;
531
- }
532
- this . _debouncees = [ ] ;
533
-
534
521
this . _clearTimerTimeout ( ) ;
535
522
this . _timers = [ ] ;
536
-
537
523
this . _cancelAutorun ( ) ;
538
524
}
539
525
540
526
public hasTimers ( ) {
541
- return this . _timers . length > 0 ||
542
- this . _debouncees . length > 0 ||
543
- this . _throttlers . length > 0 ||
544
- this . _autorun !== null ;
527
+ return this . _timers . length > 0 || this . _autorun !== null ;
545
528
}
546
529
547
530
public cancel ( timer ?) {
548
531
cancelCount ++ ;
549
-
550
- if ( timer === undefined || timer === null ) { return false ; }
551
-
532
+ if ( timer === null || timer === undefined ) { return false ; }
552
533
let timerType = typeof timer ;
553
- if ( timerType === 'number' ) { // we're cancelling a throttle or debounce
554
- return this . _cancelItem ( timer , this . _throttlers ) || this . _cancelItem ( timer , this . _debouncees ) ;
555
- } else if ( timerType === 'string' ) { // we're cancelling a setTimeout
534
+
535
+ if ( timerType === 'number' ) { // we're cancelling a setTimeout or throttle or debounce
556
536
return this . _cancelLaterTimer ( timer ) ;
557
537
} else if ( timerType === 'object' && timer . queue && timer . method ) { // we're cancelling a deferOnce
558
538
return timer . queue . cancel ( timer ) ;
@@ -643,7 +623,7 @@ export default class Backburner {
643
623
private _later ( target , method , args , wait ) {
644
624
let stack = this . DEBUG ? new Error ( ) : undefined ;
645
625
let executeAt = this . _platform . now ( ) + wait ;
646
- let id = ( UUID ++ ) + '' ;
626
+ let id = UUID ++ ;
647
627
648
628
if ( this . _timers . length === 0 ) {
649
629
this . _timers . push ( executeAt , id , target , method , args , stack ) ;
@@ -664,8 +644,7 @@ export default class Backburner {
664
644
private _cancelLaterTimer ( timer ) {
665
645
for ( let i = 1 ; i < this . _timers . length ; i += 6 ) {
666
646
if ( this . _timers [ i ] === timer ) {
667
- i = i - 1 ;
668
- this . _timers . splice ( i , 6 ) ;
647
+ this . _timers . splice ( i - 1 , 6 ) ;
669
648
if ( i === 0 ) {
670
649
this . _reinstallTimerTimeout ( ) ;
671
650
}
@@ -675,17 +654,6 @@ export default class Backburner {
675
654
return false ;
676
655
}
677
656
678
- private _cancelItem ( timer , array ) {
679
- let index = findTimer ( timer , array ) ;
680
-
681
- if ( index > - 1 ) {
682
- this . _platform . clearTimeout ( timer ) ;
683
- array . splice ( index , 4 ) ;
684
- return true ;
685
- }
686
- return false ;
687
- }
688
-
689
657
/**
690
658
Trigger an event. Supports up to two arguments. Designed around
691
659
triggering transition events from one run loop instance to the
@@ -726,12 +694,13 @@ export default class Backburner {
726
694
for ( ; i < l ; i += 6 ) {
727
695
let executeAt = timers [ i ] ;
728
696
if ( executeAt > n ) { break ; }
729
-
730
- let target = timers [ i + 2 ] ;
731
- let method = timers [ i + 3 ] ;
732
697
let args = timers [ i + 4 ] ;
733
- let stack = timers [ i + 5 ] ;
734
- this . currentInstance ! . schedule ( defaultQueue , target , method , args , false , stack ) ;
698
+ if ( args !== DISABLE_SCHEDULE ) {
699
+ let target = timers [ i + 2 ] ;
700
+ let method = timers [ i + 3 ] ;
701
+ let stack = timers [ i + 5 ] ;
702
+ this . currentInstance ! . schedule ( defaultQueue , target , method , args , false , stack ) ;
703
+ }
735
704
}
736
705
737
706
timers . splice ( 0 , i ) ;
0 commit comments