diff options
author | Nathan Whitaker <17734409+nathanwhit@users.noreply.github.com> | 2024-03-08 15:58:43 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-08 15:58:43 -0800 |
commit | 529f79505d5924ee461593840c9383c5d8f6ed65 (patch) | |
tree | ec39563aad53fd1e0dfc4b7eea18aa5c6e9acf64 /ext/node/polyfills/timers.ts | |
parent | 58c28d9879c79c3d6cf9568ce4ee141bdd9ca59c (diff) |
fix(ext/node): Add Immediate class to mirror NodeJS.Immediate (#22808)
Fixes #21660
Adds a basic `Immediate` class to mirror `NodeJS.Immediate`, and changes
`setImmediate` and `clearImmediate` to return and accept (respectively)
`Immediate` objects.
Note that for now {ref,unref,hasRef} are effectively stubs, as deno_core
doesn't really natively support immediates (they're currently modeled as
timers with delay of 0). Eventually we probably want to actually
implement these properly.
Diffstat (limited to 'ext/node/polyfills/timers.ts')
-rw-r--r-- | ext/node/polyfills/timers.ts | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/ext/node/polyfills/timers.ts b/ext/node/polyfills/timers.ts index 9a7d59ab2..033afd952 100644 --- a/ext/node/polyfills/timers.ts +++ b/ext/node/polyfills/timers.ts @@ -11,6 +11,7 @@ const { import { activeTimers, + Immediate, setUnrefTimeout, Timeout, } from "ext:deno_node/internal/timers.mjs"; @@ -21,7 +22,6 @@ import * as timers from "ext:deno_web/02_timers.js"; const clearTimeout_ = timers.clearTimeout; const clearInterval_ = timers.clearInterval; -const setImmediate_ = timers.setImmediate; export function setTimeout( callback: (...args: unknown[]) => void, @@ -70,15 +70,21 @@ export function clearInterval(timeout?: Timeout | number | string) { } 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 export function setImmediate( cb: (...args: unknown[]) => void, ...args: unknown[] ): Timeout { - return setImmediate_(cb, ...args); + return new Immediate(cb, ...args); +} +export function clearImmediate(immediate: Immediate) { + if (immediate == null) { + return; + } + + // FIXME(nathanwhit): will probably change once + // deno_core has proper support for immediates + clearTimeout_(immediate._immediateId); } -export const clearImmediate = clearTimeout; export default { setTimeout, |