const { op_sleep } = Deno.core.ops;
const timers = new Map();
let nextId = 1;
globalThis.setInterval = function(callback, delay, ...args) {
const id = nextId++;
const controller = { cancelled: false };
timers.set(id, controller);
(async () => {
while (true) {
await op_sleep(delay);
const state = timers.get(id);
if (!state || state.cancelled) break;
try {
callback(...args);
} catch (e) {
console.error("Timer error:", e);
}
}
timers.delete(id);
})();
return id;
};
globalThis.clearInterval = function(id) {
const state = timers.get(id);
if (state) {
state.cancelled = true;
timers.delete(id);
}
};