0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -05:00

fix(node): make sure "setImmediate" is not clamped to 4ms (#19213)

This commit changes implementation of "setImmediate"
from "node:timers" module to 0ms timer that is never
clamped to 4ms no matter how many nested calls there are.

Closes https://github.com/denoland/deno/issues/19034
This commit is contained in:
Bartek Iwańczuk 2023-05-23 00:19:44 +02:00 committed by GitHub
parent 7b4c483aa1
commit 25232fa4e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 3 deletions

View file

@ -8,6 +8,7 @@ import * as timers from "ext:deno_web/02_timers.js";
const clearTimeout_ = timers.clearTimeout;
const clearInterval_ = timers.clearInterval;
const setTimeoutUnclamped = timers.setTimeoutUnclamped;
export function setTimeout(
callback: (...args: unknown[]) => void,
@ -46,10 +47,12 @@ export function clearInterval(timeout?: Timeout | number | string) {
}
// TODO(bartlomieju): implement the 'NodeJS.Immediate' versions of the timers.
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/1163ead296d84e7a3c80d71e7c81ecbd1a130e9a/types/node/v12/globals.d.ts#L1120-L1131
export const setImmediate = (
export function setImmediate(
cb: (...args: unknown[]) => void,
...args: unknown[]
): Timeout => setTimeout(cb, 0, ...args);
): Timeout {
return setTimeoutUnclamped(cb, 0, ...args);
}
export const clearImmediate = clearTimeout;
export default {

View file

@ -99,6 +99,9 @@ function initializeTimer(
args,
repeat,
prevId,
// TODO(bartlomieju): remove this option, once `nextTick` and `setImmediate`
// in Node compat are cleaned up
respectNesting = true,
) {
// 2. If previousId was given, let id be previousId; otherwise, let
// previousId be an implementation-defined integer than is greater than zero
@ -131,7 +134,7 @@ function initializeTimer(
// The nesting level of 5 and minimum of 4 ms are spec-mandated magic
// constants.
if (timeout < 0) timeout = 0;
if (timerNestingLevel > 5 && timeout < 4) timeout = 4;
if (timerNestingLevel > 5 && timeout < 4 && respectNesting) timeout = 4;
// 9. Let task be a task that runs the following steps:
const task = {
@ -343,6 +346,18 @@ function setInterval(callback, timeout = 0, ...args) {
return initializeTimer(callback, timeout, args, true);
}
// TODO(bartlomieju): remove this option, once `nextTick` and `setImmediate`
// in Node compat are cleaned up
function setTimeoutUnclamped(callback, timeout = 0, ...args) {
checkThis(this);
if (typeof callback !== "function") {
callback = webidl.converters.DOMString(callback);
}
timeout = webidl.converters.long(timeout);
return initializeTimer(callback, timeout, args, false, undefined, false);
}
function clearTimeout(id = 0) {
checkThis(this);
id = webidl.converters.long(id);
@ -384,5 +399,6 @@ export {
refTimer,
setInterval,
setTimeout,
setTimeoutUnclamped,
unrefTimer,
};