From 529f79505d5924ee461593840c9383c5d8f6ed65 Mon Sep 17 00:00:00 2001 From: Nathan Whitaker <17734409+nathanwhit@users.noreply.github.com> Date: Fri, 8 Mar 2024 15:58:43 -0800 Subject: 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. --- ext/node/polyfills/internal/timers.mjs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'ext/node/polyfills/internal/timers.mjs') diff --git a/ext/node/polyfills/internal/timers.mjs b/ext/node/polyfills/internal/timers.mjs index 92fb51d57..bdaf95d91 100644 --- a/ext/node/polyfills/internal/timers.mjs +++ b/ext/node/polyfills/internal/timers.mjs @@ -20,6 +20,7 @@ import { ERR_OUT_OF_RANGE } from "ext:deno_node/internal/errors.ts"; import { emitWarning } from "node:process"; import { clearTimeout as clearTimeout_, + setImmediate as setImmediate_, setInterval as setInterval_, setTimeout as setTimeout_, } from "ext:deno_web/02_timers.js"; @@ -115,6 +116,35 @@ Timeout.prototype[Symbol.toPrimitive] = function () { return this[kTimerId]; }; +// Immediate constructor function. +export function Immediate(callback, args) { + this._immediateId = setImmediate_(callback, args); +} + +// Make sure the linked list only shows the minimal necessary information. +Immediate.prototype[inspect.custom] = function (_, options) { + return inspect(this, { + ...options, + // Only inspect one level. + depth: 0, + // It should not recurse. + customInspect: false, + }); +}; + +// FIXME(nathanwhit): actually implement {ref,unref,hasRef} once deno_core supports it +Immediate.prototype.unref = function () { + return this; +}; + +Immediate.prototype.ref = function () { + return this; +}; + +Immediate.prototype.hasRef = function () { + return true; +}; + /** * @param {number} msecs * @param {string} name -- cgit v1.2.3