// Timers. `setTimeout` gives a record, as node does, not an int: an IO handle // is used again after its op completes, so `clearTimeout` of an old int could // cancel another op. A record stays the timer's own // // Not as the spec says: `setTimeout` takes no args for the callback import { __native_io_queue_cancel, __native_io_queue_timeout } from "jsrt:native"; import { __pending } from "jsrt:loop"; export function setTimeout(f, ms) { // As in node, a delay below 1 ms is 1 ms const delay = ms === undefined || !(ms >= 1) ? 1 : ms; const timer = { handle: __native_io_queue_timeout(delay), active: true }; __pending[timer.handle] = (h) => { if (!timer.active) return; timer.active = false; f(); }; return timer; } export function clearTimeout(timer) { if (timer === undefined || timer === null || !timer.active) return; timer.active = false; __native_io_queue_cancel(timer.handle); }