@@ -107,6 +107,7 @@ export function createAtomicTimerGroup(delayMs = 0) {
107107 'createAtomicTimerGroup cannot be called in the edge runtime'
108108 )
109109 } else {
110+ let isFirstCallback = true
110111 let firstTimerIdleStart : number | null = null
111112 let didFirstTimerRun = false
112113
@@ -115,18 +116,22 @@ export function createAtomicTimerGroup(delayMs = 0) {
115116 let didImmediateRun = false
116117 function runFirstCallback ( callback : ( ) => void ) {
117118 didFirstTimerRun = true
118- setImmediate ( ( ) => {
119- didImmediateRun = true
120- } )
119+ if ( ! cannotGuaranteeAtomicTimers ) {
120+ setImmediate ( ( ) => {
121+ didImmediateRun = true
122+ } )
123+ }
121124 return callback ( )
122125 }
123126
124127 function runSubsequentCallback ( callback : ( ) => void ) {
125- if ( didImmediateRun ) {
126- // If the immediate managed to run between the timers, then we're not
127- // able to provide the guarantees that we're supposed to
128- cannotGuaranteeAtomicTimers = true
129- warnAboutTimers ( )
128+ if ( ! cannotGuaranteeAtomicTimers ) {
129+ if ( didImmediateRun ) {
130+ // If the immediate managed to run between the timers, then we're not
131+ // able to provide the guarantees that we're supposed to
132+ cannotGuaranteeAtomicTimers = true
133+ warnAboutTimers ( )
134+ }
130135 }
131136 return callback ( )
132137 }
@@ -138,17 +143,18 @@ export function createAtomicTimerGroup(delayMs = 0) {
138143 )
139144 }
140145
141- if ( cannotGuaranteeAtomicTimers ) {
142- // We already tried patching some timers, and it didn't work.
143- // No point trying again.
144- return setTimeout ( callback , delayMs )
145- }
146-
147146 const timer = setTimeout (
148- firstTimerIdleStart === null ? runFirstCallback : runSubsequentCallback ,
147+ isFirstCallback ? runFirstCallback : runSubsequentCallback ,
149148 delayMs ,
150149 callback
151150 )
151+ isFirstCallback = false
152+
153+ if ( cannotGuaranteeAtomicTimers ) {
154+ // We already tried patching some timers, and it didn't work.
155+ // No point trying again.
156+ return timer
157+ }
152158
153159 // NodeJS timers to have a `_idleStart` property, but it doesn't exist e.g. in Bun.
154160 // If it's not present, we'll warn and try to continue.
0 commit comments