Skip to content

Commit

Permalink
fix(node:timers) calling refresh breaks the timers (#3361)
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb authored Jan 20, 2025
1 parent 1f6ed36 commit d171c7f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/node/internal/internal_timers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions src/workerd/api/node/tests/timers-nodejs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
};

0 comments on commit d171c7f

Please sign in to comment.