Skip to content

Commit

Permalink
Add missing timers.promises (#13834)
Browse files Browse the repository at this point in the history
Co-authored-by: Jarred-Sumner <[email protected]>
  • Loading branch information
Jarred-Sumner and Jarred-Sumner authored Sep 10, 2024
1 parent 282b92d commit c7b8744
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
32 changes: 31 additions & 1 deletion src/js/node/timers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Hardcoded module "node:timers"
const { throwNotImplemented } = require("internal/shared");
const { defineCustomPromisify } = require("internal/promisify");

// Lazily load node:timers/promises promisified functions onto the global timers.
Expand Down Expand Up @@ -26,6 +26,7 @@ const { defineCustomPromisify } = require("internal/promisify");
});
}
}
var timersPromisesValue;

export default {
setTimeout,
Expand All @@ -34,4 +35,33 @@ export default {
setImmediate,
clearInterval,
clearImmediate,
get promises() {
return (timersPromisesValue ??= require("node:timers/promises"));
},
set promises(value) {
timersPromisesValue = value;
},
active(timer) {
if ($isCallable(timer?.refresh)) {
timer.refresh();
} else {
throwNotImplemented("'timers.active'");
}
},
unenroll(timer) {
if ($isCallable(timer?.refresh)) {
clearTimeout(timer);
return;
}

throwNotImplemented("'timers.unenroll'");
},
enroll(timer, msecs) {
if ($isCallable(timer?.refresh)) {
timer.refresh();
return;
}

throwNotImplemented("'timers.enroll'");
},
};
7 changes: 6 additions & 1 deletion test/js/node/timers/node-timers.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it, test } from "bun:test";
import { clearInterval, clearTimeout, setInterval, setTimeout } from "node:timers";
import { clearInterval, clearTimeout, promises, setInterval, setTimeout } from "node:timers";
import { promisify } from "util";

for (const fn of [setTimeout, setInterval]) {
Expand Down Expand Up @@ -52,3 +52,8 @@ it("node.js util.promisify(setImmediate) works", async () => {
});
}).toThrow("TestPassed");
});

it("timers.promises === timers/promises", async () => {
const ns = await import("node:timers/promises");
expect(ns.default).toBe(promises);
});

0 comments on commit c7b8744

Please sign in to comment.