Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(timers): bind global functions #384

Merged
merged 7 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/runtime/node/timers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ import { setIntervalFallback } from "./internal/set-interval";
export * as promises from "./promises";

export const clearImmediate: typeof timers.clearImmediate =
globalThis.clearImmediate || clearImmediateFallback;
globalThis.clearImmediate?.bind(globalThis) || clearImmediateFallback;
export const clearInterval: typeof timers.clearInterval =
globalThis.clearInterval || noop;
globalThis.clearInterval?.bind(globalThis) || noop;
export const clearTimeout: typeof timers.clearTimeout =
globalThis.clearTimeout || noop;
globalThis.clearTimeout?.bind(globalThis) || noop;

export const setImmediate: typeof timers.setImmediate =
globalThis.setImmediate || setImmediateFallback;
globalThis.setImmediate?.bind(globalThis) || setImmediateFallback;
export const setTimeout: typeof timers.setTimeout =
globalThis.setTimeout || setTimeoutFallback;
globalThis.setTimeout?.bind(globalThis) || setTimeoutFallback;
export const setInterval: typeof timers.setInterval =
globalThis.setInterval || setIntervalFallback;
globalThis.setInterval?.bind(globalThis) || setIntervalFallback;

export const active = notImplemented("timers.active");
export const _unrefActive = notImplemented("timers._unrefActive");
Expand Down
12 changes: 12 additions & 0 deletions test/workerd/tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ export const workerd_dns = {
},
};

// --- node:timers

export const workerd_timers = {
async test() {
const timers = await import("unenv/runtime/node/timers");

timers.clearTimeout(timers.setTimeout(() => null, 1000));
timers.clearInterval(timers.setInterval(() => null, 1000));
timers.clearImmediate(timers.setImmediate(() => null));
},
};

// --- unenv:fetch

// https://github.com/unjs/unenv/issues/364
Expand Down
Loading