From 6670e838b55c2bf13f6b138265f7b00245d3f45e Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Tue, 1 Oct 2024 17:57:22 +0200 Subject: [PATCH 1/3] fix setImmediate not triggering the timer if idle --- index.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/index.js b/index.js index efd5107..ccbf1f3 100644 --- a/index.js +++ b/index.js @@ -251,6 +251,7 @@ function ontimer () { function onimmediate () { const now = Date.now() + const timerRunning = queue.length > 0 let uncaughtError = null @@ -267,6 +268,11 @@ function onimmediate () { tick() + // some reentry wants the timers to start but they are not, lets start them + if (!timerRunning && queue.length > 0) { + updateTimer(queue.peek().ms) + } + if (uncaughtError !== null) { throw uncaughtError } From c4048c83f88b62b99fb376eb3267550edd2687d6 Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Tue, 1 Oct 2024 18:04:00 +0200 Subject: [PATCH 2/3] also set nextExpiry --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index ccbf1f3..57d8261 100644 --- a/index.js +++ b/index.js @@ -270,7 +270,9 @@ function onimmediate () { // some reentry wants the timers to start but they are not, lets start them if (!timerRunning && queue.length > 0) { - updateTimer(queue.peek().ms) + const l = queue.peek() + nextExpiry = l.expiry + updateTimer(l.ms) } if (uncaughtError !== null) { From 9859a6f2f9d7782a91ca54a0074069be90f1b2a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Tue, 1 Oct 2024 20:05:09 +0200 Subject: [PATCH 3/3] Add test case --- test/immediate.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/immediate.js b/test/immediate.js index 8a2e7d3..abed1bf 100644 --- a/test/immediate.js +++ b/test/immediate.js @@ -125,3 +125,15 @@ test('setImmediate following setTimeout', async function (t) { clearTimeout(timer) }) + +test('setImmediate that triggers setTimeout', async function (t) { + t.plan(2) + + timers.setImmediate(() => { + t.pass('immediate triggered') + + timers.setTimeout(() => { + t.pass('timeout triggered') + }, 20) + }) +})