import { core, primordials } from "ext:core/mod.js";
const {
op_now,
op_sleep,
op_timer_handle,
op_void_async_deferred,
} = core.ensureFastOps();
const {
ArrayPrototypePush,
ArrayPrototypeShift,
FunctionPrototypeCall,
MapPrototypeDelete,
MapPrototypeGet,
MapPrototypeHas,
MapPrototypeSet,
Uint8Array,
Uint32Array,
PromisePrototypeThen,
SafeArrayIterator,
SafeMap,
TypedArrayPrototypeGetBuffer,
TypeError,
indirectEval,
} = primordials;
import * as webidl from "ext:deno_webidl/00_webidl.js";
import { reportException } from "ext:deno_web/02_event.js";
import { assert } from "ext:deno_web/00_infra.js";
const hrU8 = new Uint8Array(8);
const hr = new Uint32Array(TypedArrayPrototypeGetBuffer(hrU8));
function opNow() {
op_now(hrU8);
return (hr[0] * 1000 + hr[1] / 1e6);
}
const timerTasks = [];
let timerNestingLevel = 0;
function handleTimerMacrotask() {
if (timerTasks.length === 0) {
return undefined;
}
const task = ArrayPrototypeShift(timerTasks);
timerNestingLevel = task.nestingLevel;
try {
task.action();
} finally {
timerNestingLevel = 0;
}
return timerTasks.length === 0;
}
const activeTimers = new SafeMap();
let nextId = 1;
function initializeTimer(
callback,
timeout,
args,
repeat,
prevId,
respectNesting = true,
) {
let id;
let timerInfo;
if (prevId !== undefined) {
assert(repeat);
id = prevId;
timerInfo = MapPrototypeGet(activeTimers, id);
} else {
id = nextId++;
const cancelRid = op_timer_handle();
timerInfo = { cancelRid, isRef: true, promise: null };
MapPrototypeSet(activeTimers, id, timerInfo);
}
if (timeout < 0) timeout = 0;
if (timerNestingLevel > 5 && timeout < 4 && respectNesting) timeout = 4;
const task = {
action: () => {
if (!MapPrototypeHas(activeTimers, id)) {
return;
}
if (typeof callback === "function") {
try {
FunctionPrototypeCall(
callback,
globalThis,
...new SafeArrayIterator(args),
);
} catch (error) {
reportException(error);
}
} else {
indirectEval(callback);
}
if (repeat) {
if (MapPrototypeHas(activeTimers, id)) {
initializeTimer(callback, timeout, args, true, id);
}
} else {
core.tryClose(timerInfo.cancelRid);
MapPrototypeDelete(activeTimers, id);
}
},
nestingLevel: timerNestingLevel + 1,
};
runAfterTimeout(
task,
timeout,
timerInfo,
);
return id;
}
const scheduledTimers = { head: null, tail: null };
function runAfterTimeout(task, millis, timerInfo) {
const cancelRid = timerInfo.cancelRid;
let sleepPromise;
if (millis === 0) {
sleepPromise = op_void_async_deferred();
} else {
sleepPromise = op_sleep(millis, cancelRid);
}
timerInfo.promise = sleepPromise;
if (!timerInfo.isRef) {
core.unrefOpPromise(timerInfo.promise);
}
const timerObject = {
millis,
resolved: false,
prev: scheduledTimers.tail,
next: null,
task,
};
if (scheduledTimers.tail === null) {
assert(scheduledTimers.head === null);
scheduledTimers.head = scheduledTimers.tail = timerObject;
} else {
scheduledTimers.tail.next = timerObject;
scheduledTimers.tail = timerObject;
}
PromisePrototypeThen(
sleepPromise,
(cancelled) => {
if (timerObject.resolved) {
return;
}
if (cancelled !== null && !cancelled) {
removeFromScheduledTimers(timerObject);
return;
}
let currentEntry = scheduledTimers.head;
while (currentEntry !== null) {
if (currentEntry.millis <= timerObject.millis) {
currentEntry.resolved = true;
ArrayPrototypePush(timerTasks, currentEntry.task);
removeFromScheduledTimers(currentEntry);
if (currentEntry === timerObject) {
break;
}
}
currentEntry = currentEntry.next;
}
},
);
}
function removeFromScheduledTimers(timerObj) {
if (timerObj.prev !== null) {
timerObj.prev.next = timerObj.next;
} else {
assert(scheduledTimers.head === timerObj);
scheduledTimers.head = timerObj.next;
}
if (timerObj.next !== null) {
timerObj.next.prev = timerObj.prev;
} else {
assert(scheduledTimers.tail === timerObj);
scheduledTimers.tail = timerObj.prev;
}
}
function checkThis(thisArg) {
if (thisArg !== null && thisArg !== undefined && thisArg !== globalThis) {
throw new TypeError("Illegal invocation");
}
}
function setTimeout(callback, timeout = 0, ...args) {
checkThis(this);
if (typeof callback !== "function") {
callback = webidl.converters.DOMString(callback);
}
timeout = webidl.converters.long(timeout);
return initializeTimer(callback, timeout, args, false);
}
function setInterval(callback, timeout = 0, ...args) {
checkThis(this);
if (typeof callback !== "function") {
callback = webidl.converters.DOMString(callback);
}
timeout = webidl.converters.long(timeout);
return initializeTimer(callback, timeout, args, true);
}
function setTimeoutUnclamped(callback, timeout = 0, ...args) {
checkThis(this);
if (typeof callback !== "function") {
callback = webidl.converters.DOMString(callback);
}
timeout = webidl.converters.long(timeout);
return initializeTimer(callback, timeout, args, false, undefined, false);
}
function clearTimeout(id = 0) {
checkThis(this);
id = webidl.converters.long(id);
const timerInfo = MapPrototypeGet(activeTimers, id);
if (timerInfo !== undefined) {
core.tryClose(timerInfo.cancelRid);
MapPrototypeDelete(activeTimers, id);
}
}
function clearInterval(id = 0) {
checkThis(this);
clearTimeout(id);
}
function refTimer(id) {
const timerInfo = MapPrototypeGet(activeTimers, id);
if (timerInfo === undefined || timerInfo.isRef) {
return;
}
timerInfo.isRef = true;
core.refOpPromise(timerInfo.promise);
}
function unrefTimer(id) {
const timerInfo = MapPrototypeGet(activeTimers, id);
if (timerInfo === undefined || !timerInfo.isRef) {
return;
}
timerInfo.isRef = false;
core.unrefOpPromise(timerInfo.promise);
}
function defer(go) {
PromisePrototypeThen(op_void_async_deferred(), () => go());
}
export {
clearInterval,
clearTimeout,
defer,
handleTimerMacrotask,
opNow,
refTimer,
setInterval,
setTimeout,
setTimeoutUnclamped,
unrefTimer,
};