@@ -954,7 +954,7 @@ export function makeInterruptibleAsyncInterval(
954
954
) : InterruptibleAsyncInterval {
955
955
let timerId : NodeJS . Timeout | undefined ;
956
956
let lastCallTime : number ;
957
- let lastWakeTime : number ;
957
+ let cannotBeExpedited = false ;
958
958
let stopped = false ;
959
959
960
960
options = options ?? { } ;
@@ -965,10 +965,8 @@ export function makeInterruptibleAsyncInterval(
965
965
966
966
function wake ( ) {
967
967
const currentTime = clock ( ) ;
968
- const timeSinceLastWake = currentTime - lastWakeTime ;
969
- const timeSinceLastCall = currentTime - lastCallTime ;
970
- const timeUntilNextCall = interval - timeSinceLastCall ;
971
- lastWakeTime = currentTime ;
968
+ const nextScheduledCallTime = lastCallTime + interval ;
969
+ const timeUntilNextCall = nextScheduledCallTime - currentTime ;
972
970
973
971
// For the streaming protocol: there is nothing obviously stopping this
974
972
// interval from being woken up again while we are waiting "infinitely"
@@ -986,14 +984,15 @@ export function makeInterruptibleAsyncInterval(
986
984
}
987
985
988
986
// debounce multiple calls to wake within the `minInterval`
989
- if ( timeSinceLastWake < minInterval ) {
987
+ if ( cannotBeExpedited ) {
990
988
return ;
991
989
}
992
990
993
991
// reschedule a call as soon as possible, ensuring the call never happens
994
992
// faster than the `minInterval`
995
993
if ( timeUntilNextCall > minInterval ) {
996
994
reschedule ( minInterval ) ;
995
+ cannotBeExpedited = true ;
997
996
}
998
997
}
999
998
@@ -1005,7 +1004,7 @@ export function makeInterruptibleAsyncInterval(
1005
1004
}
1006
1005
1007
1006
lastCallTime = 0 ;
1008
- lastWakeTime = 0 ;
1007
+ cannotBeExpedited = false ;
1009
1008
}
1010
1009
1011
1010
function reschedule ( ms ?: number ) {
@@ -1018,7 +1017,7 @@ export function makeInterruptibleAsyncInterval(
1018
1017
}
1019
1018
1020
1019
function executeAndReschedule ( ) {
1021
- lastWakeTime = 0 ;
1020
+ cannotBeExpedited = false ;
1022
1021
lastCallTime = clock ( ) ;
1023
1022
1024
1023
fn ( err => {
0 commit comments