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' ;
@@ -193,8 +192,6 @@ export default class Backburner {
193
192
private _onEnd : ( currentInstance : DeferredActionQueues , nextInstance : DeferredActionQueues | null ) => void ;
194
193
private queueNames : string [ ] ;
195
194
private instanceStack : DeferredActionQueues [ ] = [ ] ;
196
- private _debouncees : any [ ] = [ ] ;
197
- private _throttlers : any [ ] = [ ] ;
198
195
private _eventCallbacks : {
199
196
end : Function [ ] ;
200
197
begin : Function [ ] ;
@@ -450,26 +447,21 @@ export default class Backburner {
450
447
throttleCount ++ ;
451
448
let [ target , method , args , wait , isImmediate = true ] = parseDebounceArgs ( ...arguments ) ;
452
449
453
- let index = findItem ( target , method , this . _throttlers ) ;
450
+ let index = findTimerItem ( target , method , this . _timers ) ;
454
451
if ( index > - 1 ) {
455
- this . _throttlers [ index + 2 ] = args ;
456
- return this . _throttlers [ index + 3 ] ;
457
- } // throttled
452
+ let timerId = this . _timers [ index + 1 ] ;
453
+ this . _timers [ index + 4 ] = args ;
454
+ return timerId ;
455
+ }
458
456
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 ) ;
457
+ wait = parseInt ( wait , 10 ) ;
458
+
459
+ let timer = this . _later ( target , method , args , wait , isImmediate ) ;
466
460
467
461
if ( isImmediate ) {
468
462
this . _join ( target , method , args ) ;
469
463
}
470
464
471
- this . _throttlers . push ( target , method , args , timer ) ;
472
-
473
465
return timer ;
474
466
}
475
467
@@ -495,64 +487,40 @@ export default class Backburner {
495
487
let [ target , method , args , wait , isImmediate = false ] = parseDebounceArgs ( ...arguments ) ;
496
488
497
489
// Remove debouncee
498
- let index = findItem ( target , method , this . _debouncees ) ;
490
+ let index = findTimerItem ( target , method , this . _timers ) ;
499
491
if ( index > - 1 ) {
500
- let timerId = this . _debouncees [ index + 3 ] ;
501
- this . _platform . clearTimeout ( timerId ) ;
502
- this . _debouncees . splice ( index , 4 ) ;
492
+ this . _timers . splice ( index , 7 ) ;
493
+ if ( index === 0 ) {
494
+ this . _reinstallTimerTimeout ( ) ;
495
+ }
503
496
}
504
497
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 ) ;
510
- }
511
- } , wait ) ;
498
+ let timer = this . _later ( target , method , args , wait , isImmediate ) ;
512
499
513
500
if ( isImmediate && index === - 1 ) {
514
501
this . _join ( target , method , args ) ;
515
502
}
516
503
517
- this . _debouncees . push ( target , method , args , timer ) ;
518
-
519
504
return timer ;
520
505
}
521
506
522
507
public cancelTimers ( ) {
523
508
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
509
this . _clearTimerTimeout ( ) ;
535
510
this . _timers = [ ] ;
536
-
537
511
this . _cancelAutorun ( ) ;
538
512
}
539
513
540
514
public hasTimers ( ) {
541
- return this . _timers . length > 0 ||
542
- this . _debouncees . length > 0 ||
543
- this . _throttlers . length > 0 ||
544
- this . _autorun !== null ;
515
+ return this . _timers . length > 0 || this . _autorun !== null ;
545
516
}
546
517
547
518
public cancel ( timer ?) {
548
519
cancelCount ++ ;
549
-
550
- if ( timer === undefined || timer === null ) { return false ; }
551
-
520
+ if ( timer === null || timer === undefined ) { return false ; }
552
521
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
522
+
523
+ if ( timerType === 'number' ) { // we're cancelling a setTimeout or throttle or debounce
556
524
return this . _cancelLaterTimer ( timer ) ;
557
525
} else if ( timerType === 'object' && timer . queue && timer . method ) { // we're cancelling a deferOnce
558
526
return timer . queue . cancel ( timer ) ;
@@ -640,18 +608,18 @@ export default class Backburner {
640
608
}
641
609
}
642
610
643
- private _later ( target , method , args , wait ) {
611
+ private _later ( target , method , args , wait , isImmediate = false ) {
644
612
let stack = this . DEBUG ? new Error ( ) : undefined ;
645
613
let executeAt = this . _platform . now ( ) + wait ;
646
- let id = ( UUID ++ ) + '' ;
614
+ let id = UUID ++ ;
647
615
648
616
if ( this . _timers . length === 0 ) {
649
- this . _timers . push ( executeAt , id , target , method , args , stack ) ;
617
+ this . _timers . push ( executeAt , id , target , method , args , isImmediate , stack ) ;
650
618
this . _installTimerTimeout ( ) ;
651
619
} else {
652
620
// find position to insert
653
621
let i = searchTimer ( executeAt , this . _timers ) ;
654
- this . _timers . splice ( i , 0 , executeAt , id , target , method , args , stack ) ;
622
+ this . _timers . splice ( i , 0 , executeAt , id , target , method , args , isImmediate , stack ) ;
655
623
656
624
// we should be the new earliest timer if i == 0
657
625
if ( i === 0 ) {
@@ -662,10 +630,10 @@ export default class Backburner {
662
630
}
663
631
664
632
private _cancelLaterTimer ( timer ) {
665
- for ( let i = 1 ; i < this . _timers . length ; i += 6 ) {
633
+ for ( let i = 1 ; i < this . _timers . length ; i += 7 ) {
666
634
if ( this . _timers [ i ] === timer ) {
667
635
i = i - 1 ;
668
- this . _timers . splice ( i , 6 ) ;
636
+ this . _timers . splice ( i , 7 ) ;
669
637
if ( i === 0 ) {
670
638
this . _reinstallTimerTimeout ( ) ;
671
639
}
@@ -675,17 +643,6 @@ export default class Backburner {
675
643
return false ;
676
644
}
677
645
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
646
/**
690
647
Trigger an event. Supports up to two arguments. Designed around
691
648
triggering transition events from one run loop instance to the
@@ -723,15 +680,17 @@ export default class Backburner {
723
680
let defaultQueue = this . _defaultQueue ;
724
681
let n = this . _platform . now ( ) ;
725
682
726
- for ( ; i < l ; i += 6 ) {
683
+ for ( ; i < l ; i += 7 ) {
727
684
let executeAt = timers [ i ] ;
728
685
if ( executeAt > n ) { break ; }
729
-
730
- let target = timers [ i + 2 ] ;
731
- let method = timers [ i + 3 ] ;
732
- let args = timers [ i + 4 ] ;
733
- let stack = timers [ i + 5 ] ;
734
- this . currentInstance ! . schedule ( defaultQueue , target , method , args , false , stack ) ;
686
+ let isImmediate = timers [ i + 5 ] ;
687
+ if ( isImmediate === false ) {
688
+ let target = timers [ i + 2 ] ;
689
+ let method = timers [ i + 3 ] ;
690
+ let args = timers [ i + 4 ] ;
691
+ let stack = timers [ i + 6 ] ;
692
+ this . currentInstance ! . schedule ( defaultQueue , target , method , args , false , stack ) ;
693
+ }
735
694
}
736
695
737
696
timers . splice ( 0 , i ) ;
0 commit comments