diff options
author | await-ovo <13152410380@163.com> | 2023-07-03 03:11:34 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-02 19:11:34 +0000 |
commit | 0f4051a37ad23377091043206e64126003caa480 (patch) | |
tree | 3fbe98a9c78ee716665dcadaa962ea132eb9bfea /ext/node/polyfills/timers.ts | |
parent | 01f0d03ae82c422c1f9551f3bfbb57daac769ddc (diff) |
fix(ext/node): ignore cancelled timer when node timer refresh (#19637)
For timers that have already executed clearTimeout, there is no need to recreate a new timer when refresh is executed again.
Diffstat (limited to 'ext/node/polyfills/timers.ts')
-rw-r--r-- | ext/node/polyfills/timers.ts | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/ext/node/polyfills/timers.ts b/ext/node/polyfills/timers.ts index e33f44f39..2bedbe65b 100644 --- a/ext/node/polyfills/timers.ts +++ b/ext/node/polyfills/timers.ts @@ -3,7 +3,17 @@ // TODO(petamoriken): enable prefer-primordials for node polyfills // deno-lint-ignore-file prefer-primordials -import { setUnrefTimeout, Timeout } from "ext:deno_node/internal/timers.mjs"; +const primordials = globalThis.__bootstrap.primordials; +const { + MapPrototypeGet, + MapPrototypeDelete, +} = primordials; + +import { + activeTimers, + setUnrefTimeout, + Timeout, +} from "ext:deno_node/internal/timers.mjs"; import { validateFunction } from "ext:deno_node/internal/validators.mjs"; import { promisify } from "ext:deno_node/internal/util.mjs"; export { setUnrefTimeout } from "ext:deno_node/internal/timers.mjs"; @@ -32,7 +42,13 @@ export function clearTimeout(timeout?: Timeout | number) { if (timeout == null) { return; } - clearTimeout_(+timeout); + const id = +timeout; + const timer = MapPrototypeGet(activeTimers, id); + if (timer) { + timeout._destroyed = true; + MapPrototypeDelete(activeTimers, id); + } + clearTimeout_(id); } export function setInterval( callback: (...args: unknown[]) => void, @@ -46,7 +62,13 @@ export function clearInterval(timeout?: Timeout | number | string) { if (timeout == null) { return; } - clearInterval_(+timeout); + const id = +timeout; + const timer = MapPrototypeGet(activeTimers, id); + if (timer) { + timeout._destroyed = true; + MapPrototypeDelete(activeTimers, id); + } + clearInterval_(id); } // 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 |