@@ -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,34 +965,34 @@ 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"
975
973
// for `fn` to be called again`. Since the function effectively
976
974
// never completes, the `timeUntilNextCall` will continue to grow
977
975
// negatively unbounded, so it will never trigger a reschedule here.
978
976
977
+ // This is possible in virtualized environments like AWS Lambda where our
978
+ // clock is unreliable. In these cases the timer is "running" but never
979
+ // actually completes, so we want to execute immediately and then attempt
980
+ // to reschedule.
981
+ if ( timeUntilNextCall < 0 ) {
982
+ executeAndReschedule ( ) ;
983
+ return ;
984
+ }
985
+
979
986
// debounce multiple calls to wake within the `minInterval`
980
- if ( timeSinceLastWake < minInterval ) {
987
+ if ( cannotBeExpedited ) {
981
988
return ;
982
989
}
983
990
984
991
// reschedule a call as soon as possible, ensuring the call never happens
985
992
// faster than the `minInterval`
986
993
if ( timeUntilNextCall > minInterval ) {
987
994
reschedule ( minInterval ) ;
988
- }
989
-
990
- // This is possible in virtualized environments like AWS Lambda where our
991
- // clock is unreliable. In these cases the timer is "running" but never
992
- // actually completes, so we want to execute immediately and then attempt
993
- // to reschedule.
994
- if ( timeUntilNextCall < 0 ) {
995
- executeAndReschedule ( ) ;
995
+ cannotBeExpedited = true ;
996
996
}
997
997
}
998
998
@@ -1004,7 +1004,7 @@ export function makeInterruptibleAsyncInterval(
1004
1004
}
1005
1005
1006
1006
lastCallTime = 0 ;
1007
- lastWakeTime = 0 ;
1007
+ cannotBeExpedited = false ;
1008
1008
}
1009
1009
1010
1010
function reschedule ( ms ?: number ) {
@@ -1017,7 +1017,7 @@ export function makeInterruptibleAsyncInterval(
1017
1017
}
1018
1018
1019
1019
function executeAndReschedule ( ) {
1020
- lastWakeTime = 0 ;
1020
+ cannotBeExpedited = false ;
1021
1021
lastCallTime = clock ( ) ;
1022
1022
1023
1023
fn ( err => {
0 commit comments