From 43006142c909bf6074e7387c2a138e705abdf2d3 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Mon, 20 Jan 2025 09:26:20 +0100 Subject: [PATCH] fix(node:timers) calling refresh breaks the timers --- src/node/internal/internal_timers.ts | 12 +++++------- .../api/node/tests/timers-nodejs-test.js | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/node/internal/internal_timers.ts b/src/node/internal/internal_timers.ts index c1e1610709b..6ae00fab6fc 100644 --- a/src/node/internal/internal_timers.ts +++ b/src/node/internal/internal_timers.ts @@ -53,13 +53,11 @@ export class Timeout { } #constructTimer(): number { - if (this.#isRepeat) { - // @ts-expect-error TS2322 Due to difference between Node.js and globals - return globalThis.setInterval(this.#callback, this.#after, ...this.#args); - } else { - // @ts-expect-error TS2322 Due to difference between Node.js and globals - return globalThis.setTimeout(this.#callback, this.#after, ...this.#args); - } + // @ts-expect-error TS2322 Due to difference between Node.js and globals + this.#timer = this.#isRepeat + ? globalThis.setInterval(this.#callback, this.#after, ...this.#args) + : globalThis.setTimeout(this.#callback, this.#after, ...this.#args); + return this.#timer; } #clearTimeout(): void { diff --git a/src/workerd/api/node/tests/timers-nodejs-test.js b/src/workerd/api/node/tests/timers-nodejs-test.js index 79c7fe8a277..8db12f268d1 100644 --- a/src/workerd/api/node/tests/timers-nodejs-test.js +++ b/src/workerd/api/node/tests/timers-nodejs-test.js @@ -136,3 +136,22 @@ export const testSetInterval = { } }, }; + +export const testRefresh = { + async test() { + const { promise, resolve, reject } = Promise.withResolvers(); + timers.clearTimeout( + timers + .setTimeout(() => { + reject(); + }, 1) + .refresh() + ); + + timers.setTimeout(() => { + resolve(); + }, 2); + + await promise; + }, +};