@@ -40,6 +40,59 @@ describe('Scheduler.asap', () => {
40
40
sandbox . restore ( ) ;
41
41
} ) ;
42
42
43
+ it ( 'should reuse the interval for recursively scheduled actions with the same delay' , ( ) => {
44
+ const sandbox = sinon . sandbox . create ( ) ;
45
+ const fakeTimer = sandbox . useFakeTimers ( ) ;
46
+ // callThrough is missing from the declarations installed by the typings tool in stable
47
+ const stubSetInterval = ( < any > sinon . stub ( global , 'setInterval' ) ) . callThrough ( ) ;
48
+ function dispatch ( state : any ) : void {
49
+ state . index += 1 ;
50
+ if ( state . index < 3 ) {
51
+ ( < any > this ) . schedule ( state , state . period ) ;
52
+ }
53
+ }
54
+ const period = 50 ;
55
+ const state = { index : 0 , period } ;
56
+ asap . schedule ( dispatch , period , state ) ;
57
+ expect ( state ) . to . have . property ( 'index' , 0 ) ;
58
+ expect ( stubSetInterval ) . to . have . property ( 'callCount' , 1 ) ;
59
+ fakeTimer . tick ( period ) ;
60
+ expect ( state ) . to . have . property ( 'index' , 1 ) ;
61
+ expect ( stubSetInterval ) . to . have . property ( 'callCount' , 1 ) ;
62
+ fakeTimer . tick ( period ) ;
63
+ expect ( state ) . to . have . property ( 'index' , 2 ) ;
64
+ expect ( stubSetInterval ) . to . have . property ( 'callCount' , 1 ) ;
65
+ stubSetInterval . restore ( ) ;
66
+ sandbox . restore ( ) ;
67
+ } ) ;
68
+
69
+ it ( 'should not reuse the interval for recursively scheduled actions with a different delay' , ( ) => {
70
+ const sandbox = sinon . sandbox . create ( ) ;
71
+ const fakeTimer = sandbox . useFakeTimers ( ) ;
72
+ // callThrough is missing from the declarations installed by the typings tool in stable
73
+ const stubSetInterval = ( < any > sinon . stub ( global , 'setInterval' ) ) . callThrough ( ) ;
74
+ function dispatch ( state : any ) : void {
75
+ state . index += 1 ;
76
+ state . period -= 1 ;
77
+ if ( state . index < 3 ) {
78
+ ( < any > this ) . schedule ( state , state . period ) ;
79
+ }
80
+ }
81
+ const period = 50 ;
82
+ const state = { index : 0 , period } ;
83
+ asap . schedule ( dispatch , period , state ) ;
84
+ expect ( state ) . to . have . property ( 'index' , 0 ) ;
85
+ expect ( stubSetInterval ) . to . have . property ( 'callCount' , 1 ) ;
86
+ fakeTimer . tick ( period ) ;
87
+ expect ( state ) . to . have . property ( 'index' , 1 ) ;
88
+ expect ( stubSetInterval ) . to . have . property ( 'callCount' , 2 ) ;
89
+ fakeTimer . tick ( period ) ;
90
+ expect ( state ) . to . have . property ( 'index' , 2 ) ;
91
+ expect ( stubSetInterval ) . to . have . property ( 'callCount' , 3 ) ;
92
+ stubSetInterval . restore ( ) ;
93
+ sandbox . restore ( ) ;
94
+ } ) ;
95
+
43
96
it ( 'should schedule an action to happen later' , ( done : MochaDone ) => {
44
97
let actionHappened = false ;
45
98
asap . schedule ( ( ) => {
0 commit comments