26 lines
818 B
JavaScript
26 lines
818 B
JavaScript
// 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
|
|
|
|
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;
|
|
}
|
|
|
|
function clearTimeout(timer) {
|
|
if (timer === undefined || timer === null || !timer.active) return;
|
|
timer.active = false;
|
|
__native_io_queue_cancel(timer.handle);
|
|
}
|