2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-09-02 17:07:11 -04:00
|
|
|
import { assert } from "./util.ts";
|
|
|
|
import { window } from "./window.ts";
|
|
|
|
import * as dispatch from "./dispatch.ts";
|
|
|
|
import { sendSync, sendAsync } from "./dispatch_json.ts";
|
2019-10-26 18:29:32 -04:00
|
|
|
import { RBTree } from "./rbtree.ts";
|
2019-09-02 17:07:11 -04:00
|
|
|
|
|
|
|
const { console } = window;
|
2018-07-06 11:20:35 -04:00
|
|
|
|
|
|
|
interface Timer {
|
|
|
|
id: number;
|
2018-10-02 20:47:40 -04:00
|
|
|
callback: () => void;
|
|
|
|
delay: number;
|
|
|
|
due: number;
|
|
|
|
repeat: boolean;
|
|
|
|
scheduled: boolean;
|
|
|
|
}
|
|
|
|
|
2019-01-26 16:10:38 -05:00
|
|
|
// Timeout values > TIMEOUT_MAX are set to 1.
|
|
|
|
const TIMEOUT_MAX = 2 ** 31 - 1;
|
|
|
|
|
2018-10-02 20:47:40 -04:00
|
|
|
let globalTimeoutDue: number | null = null;
|
|
|
|
|
|
|
|
let nextTimerId = 1;
|
|
|
|
const idMap = new Map<number, Timer>();
|
2019-10-26 18:29:32 -04:00
|
|
|
type DueNode = { due: number; timers: Timer[] };
|
|
|
|
const dueTree = new RBTree<DueNode>((a, b) => a.due - b.due);
|
2018-07-06 11:20:35 -04:00
|
|
|
|
2019-03-10 15:37:05 -04:00
|
|
|
function clearGlobalTimeout(): void {
|
|
|
|
globalTimeoutDue = null;
|
2019-08-26 08:50:21 -04:00
|
|
|
sendSync(dispatch.OP_GLOBAL_TIMER_STOP);
|
2019-03-10 15:37:05 -04:00
|
|
|
}
|
|
|
|
|
2019-10-19 17:09:24 -04:00
|
|
|
let pendingEvents = 0;
|
2019-12-03 22:19:03 -05:00
|
|
|
const pendingFireTimers: Timer[] = [];
|
|
|
|
let hasPendingFireTimers = false;
|
|
|
|
let pendingScheduleTimers: Timer[] = [];
|
2019-10-19 17:09:24 -04:00
|
|
|
|
2019-03-10 15:37:05 -04:00
|
|
|
async function setGlobalTimeout(due: number, now: number): Promise<void> {
|
2018-10-02 20:47:40 -04:00
|
|
|
// Since JS and Rust don't use the same clock, pass the time to rust as a
|
|
|
|
// relative time value. On the Rust side we'll turn that into an absolute
|
|
|
|
// value again.
|
2019-09-07 12:27:18 -04:00
|
|
|
const timeout = due - now;
|
2019-03-10 15:37:05 -04:00
|
|
|
assert(timeout >= 0);
|
2018-10-02 20:47:40 -04:00
|
|
|
// Send message to the backend.
|
|
|
|
globalTimeoutDue = due;
|
2019-10-19 17:09:24 -04:00
|
|
|
pendingEvents++;
|
2019-08-26 08:50:21 -04:00
|
|
|
await sendAsync(dispatch.OP_GLOBAL_TIMER, { timeout });
|
2019-10-19 17:09:24 -04:00
|
|
|
pendingEvents--;
|
2019-03-10 15:37:05 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
|
|
fireTimers();
|
|
|
|
}
|
|
|
|
|
|
|
|
function setOrClearGlobalTimeout(due: number | null, now: number): void {
|
|
|
|
if (due == null) {
|
|
|
|
clearGlobalTimeout();
|
|
|
|
} else {
|
|
|
|
setGlobalTimeout(due, now);
|
|
|
|
}
|
2018-09-24 22:46:36 -04:00
|
|
|
}
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
function schedule(timer: Timer, now: number): void {
|
2018-10-02 20:47:40 -04:00
|
|
|
assert(!timer.scheduled);
|
|
|
|
assert(now <= timer.due);
|
2019-12-03 22:19:03 -05:00
|
|
|
// There are more timers pending firing.
|
|
|
|
// We must ensure new timer scheduled after them.
|
|
|
|
// Push them to a queue that would be depleted after last pending fire
|
|
|
|
// timer is fired.
|
|
|
|
// (This also implies behavior of setInterval)
|
|
|
|
if (hasPendingFireTimers) {
|
|
|
|
pendingScheduleTimers.push(timer);
|
|
|
|
return;
|
|
|
|
}
|
2018-10-02 20:47:40 -04:00
|
|
|
// Find or create the list of timers that will fire at point-in-time `due`.
|
2019-10-26 18:29:32 -04:00
|
|
|
const maybeNewDueNode = { due: timer.due, timers: [] };
|
|
|
|
let dueNode = dueTree.find(maybeNewDueNode);
|
|
|
|
if (dueNode === null) {
|
|
|
|
dueTree.insert(maybeNewDueNode);
|
|
|
|
dueNode = maybeNewDueNode;
|
2018-10-02 20:47:40 -04:00
|
|
|
}
|
|
|
|
// Append the newly scheduled timer to the list and mark it as scheduled.
|
2019-10-26 18:29:32 -04:00
|
|
|
dueNode!.timers.push(timer);
|
2018-10-02 20:47:40 -04:00
|
|
|
timer.scheduled = true;
|
|
|
|
// If the new timer is scheduled to fire before any timer that existed before,
|
|
|
|
// update the global timeout to reflect this.
|
|
|
|
if (globalTimeoutDue === null || globalTimeoutDue > timer.due) {
|
2019-03-10 15:37:05 -04:00
|
|
|
setOrClearGlobalTimeout(timer.due, now);
|
2018-10-02 20:47:40 -04:00
|
|
|
}
|
|
|
|
}
|
2018-09-05 22:13:36 -04:00
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
function unschedule(timer: Timer): void {
|
2019-12-03 22:19:03 -05:00
|
|
|
// Check if our timer is pending scheduling or pending firing.
|
|
|
|
// If either is true, they are not in tree, and their idMap entry
|
|
|
|
// will be deleted soon. Remove it from queue.
|
|
|
|
let index = -1;
|
|
|
|
if ((index = pendingScheduleTimers.indexOf(timer)) >= 0) {
|
|
|
|
pendingScheduleTimers.splice(index);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((index = pendingFireTimers.indexOf(timer)) >= 0) {
|
|
|
|
pendingFireTimers.splice(index);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// If timer is not in the 2 pending queues and is unscheduled,
|
|
|
|
// it is not in the tree.
|
2018-10-02 20:47:40 -04:00
|
|
|
if (!timer.scheduled) {
|
|
|
|
return;
|
|
|
|
}
|
2019-10-26 18:29:32 -04:00
|
|
|
const searchKey = { due: timer.due, timers: [] };
|
2018-10-02 20:47:40 -04:00
|
|
|
// Find the list of timers that will fire at point-in-time `due`.
|
2019-10-26 18:29:32 -04:00
|
|
|
const list = dueTree.find(searchKey)!.timers;
|
2018-10-02 20:47:40 -04:00
|
|
|
if (list.length === 1) {
|
|
|
|
// Time timer is the only one in the list. Remove the entire list.
|
|
|
|
assert(list[0] === timer);
|
2019-10-26 18:29:32 -04:00
|
|
|
dueTree.remove(searchKey);
|
2018-10-02 20:47:40 -04:00
|
|
|
// If the unscheduled timer was 'next up', find when the next timer that
|
|
|
|
// still exists is due, and update the global alarm accordingly.
|
|
|
|
if (timer.due === globalTimeoutDue) {
|
2019-10-26 18:29:32 -04:00
|
|
|
const nextDueNode: DueNode | null = dueTree.min();
|
|
|
|
setOrClearGlobalTimeout(nextDueNode && nextDueNode.due, Date.now());
|
2018-09-05 22:13:36 -04:00
|
|
|
}
|
2018-10-02 20:47:40 -04:00
|
|
|
} else {
|
|
|
|
// Multiple timers that are due at the same point in time.
|
|
|
|
// Remove this timer from the list.
|
|
|
|
const index = list.indexOf(timer);
|
2018-10-08 03:46:49 -04:00
|
|
|
assert(index > -1);
|
2018-10-02 20:47:40 -04:00
|
|
|
list.splice(index, 1);
|
|
|
|
}
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
function fire(timer: Timer): void {
|
2018-10-02 20:47:40 -04:00
|
|
|
// If the timer isn't found in the ID map, that means it has been cancelled
|
|
|
|
// between the timer firing and the promise callback (this function).
|
|
|
|
if (!idMap.has(timer.id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Reschedule the timer if it is a repeating one, otherwise drop it.
|
|
|
|
if (!timer.repeat) {
|
|
|
|
// One-shot timer: remove the timer from this id-to-timer map.
|
|
|
|
idMap.delete(timer.id);
|
|
|
|
} else {
|
|
|
|
// Interval timer: compute when timer was supposed to fire next.
|
|
|
|
// However make sure to never schedule the next interval in the past.
|
2019-10-26 18:29:32 -04:00
|
|
|
const now = Date.now();
|
2018-10-02 20:47:40 -04:00
|
|
|
timer.due = Math.max(now, timer.due + timer.delay);
|
|
|
|
schedule(timer, now);
|
|
|
|
}
|
|
|
|
// Call the user callback. Intermediate assignment is to avoid leaking `this`
|
|
|
|
// to it, while also keeping the stack trace neat when it shows up in there.
|
|
|
|
const callback = timer.callback;
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
function fireTimers(): void {
|
2019-10-26 18:29:32 -04:00
|
|
|
const now = Date.now();
|
2019-09-19 18:09:43 -04:00
|
|
|
// Bail out if we're not expecting the global timer to fire.
|
2019-10-19 17:09:24 -04:00
|
|
|
if (globalTimeoutDue === null || pendingEvents > 0) {
|
2018-10-02 20:47:40 -04:00
|
|
|
return;
|
|
|
|
}
|
2019-10-26 18:29:32 -04:00
|
|
|
// After firing the timers that are due now, this will hold the first timer
|
|
|
|
// list that hasn't fired yet.
|
|
|
|
let nextDueNode: DueNode | null;
|
|
|
|
while ((nextDueNode = dueTree.min()) !== null && nextDueNode.due <= now) {
|
|
|
|
dueTree.remove(nextDueNode);
|
2018-10-02 20:47:40 -04:00
|
|
|
// Fire all the timers in the list.
|
2019-10-26 18:29:32 -04:00
|
|
|
for (const timer of nextDueNode.timers) {
|
2018-10-02 20:47:40 -04:00
|
|
|
// With the list dropped, the timer is no longer scheduled.
|
|
|
|
timer.scheduled = false;
|
2019-12-03 22:19:03 -05:00
|
|
|
// Place the callback to pending timers to fire.
|
|
|
|
pendingFireTimers.push(timer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pendingFireTimers.length > 0) {
|
|
|
|
hasPendingFireTimers = true;
|
|
|
|
// Fire the list of pending timers as a chain of microtasks.
|
|
|
|
window.queueMicrotask(firePendingTimers);
|
|
|
|
} else {
|
|
|
|
setOrClearGlobalTimeout(nextDueNode && nextDueNode.due, now);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function firePendingTimers(): void {
|
|
|
|
if (pendingFireTimers.length === 0) {
|
|
|
|
// All timer tasks are done.
|
|
|
|
hasPendingFireTimers = false;
|
|
|
|
// Schedule all new timers pushed during previous timer executions
|
|
|
|
const now = Date.now();
|
|
|
|
for (const newTimer of pendingScheduleTimers) {
|
|
|
|
newTimer.due = Math.max(newTimer.due, now);
|
|
|
|
schedule(newTimer, now);
|
2018-10-02 20:47:40 -04:00
|
|
|
}
|
2019-12-03 22:19:03 -05:00
|
|
|
pendingScheduleTimers = [];
|
|
|
|
// Reschedule for next round of timeout.
|
|
|
|
const nextDueNode = dueTree.min();
|
2019-12-10 06:21:30 -05:00
|
|
|
const due = nextDueNode && Math.max(nextDueNode.due, now);
|
2019-12-03 22:19:03 -05:00
|
|
|
setOrClearGlobalTimeout(due, now);
|
|
|
|
} else {
|
|
|
|
// Fire a single timer and allow its children microtasks scheduled first.
|
|
|
|
fire(pendingFireTimers.shift()!);
|
|
|
|
// ...and we schedule next timer after this.
|
|
|
|
window.queueMicrotask(firePendingTimers);
|
2018-10-02 20:47:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
export type Args = unknown[];
|
2018-11-29 22:25:07 -05:00
|
|
|
|
2019-06-13 11:08:27 -04:00
|
|
|
function checkThis(thisArg: unknown): void {
|
|
|
|
if (thisArg !== null && thisArg !== undefined && thisArg !== window) {
|
|
|
|
throw new TypeError("Illegal invocation");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 10:57:09 -04:00
|
|
|
function checkBigInt(n: unknown): void {
|
|
|
|
if (typeof n === "bigint") {
|
|
|
|
throw new TypeError("Cannot convert a BigInt value to a number");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-29 22:25:07 -05:00
|
|
|
function setTimer(
|
2018-10-02 20:47:40 -04:00
|
|
|
cb: (...args: Args) => void,
|
2018-07-06 11:20:35 -04:00
|
|
|
delay: number,
|
2018-10-02 20:47:40 -04:00
|
|
|
args: Args,
|
|
|
|
repeat: boolean
|
2018-07-06 11:20:35 -04:00
|
|
|
): number {
|
2019-06-11 03:50:36 -04:00
|
|
|
// Bind `args` to the callback and bind `this` to window(global).
|
|
|
|
const callback: () => void = cb.bind(window, ...args);
|
2018-10-24 11:54:34 -04:00
|
|
|
// In the browser, the delay value must be coercible to an integer between 0
|
2018-10-02 20:47:40 -04:00
|
|
|
// and INT32_MAX. Any other value will cause the timer to fire immediately.
|
|
|
|
// We emulate this behavior.
|
2019-10-26 18:29:32 -04:00
|
|
|
const now = Date.now();
|
2019-01-26 16:10:38 -05:00
|
|
|
if (delay > TIMEOUT_MAX) {
|
|
|
|
console.warn(
|
|
|
|
`${delay} does not fit into` +
|
|
|
|
" a 32-bit signed integer." +
|
|
|
|
"\nTimeout duration was set to 1."
|
|
|
|
);
|
|
|
|
delay = 1;
|
|
|
|
}
|
2018-10-02 20:47:40 -04:00
|
|
|
delay = Math.max(0, delay | 0);
|
2019-01-26 16:10:38 -05:00
|
|
|
|
2018-10-02 20:47:40 -04:00
|
|
|
// Create a new, unscheduled timer object.
|
|
|
|
const timer = {
|
|
|
|
id: nextTimerId++,
|
|
|
|
callback,
|
|
|
|
args,
|
|
|
|
delay,
|
|
|
|
due: now + delay,
|
|
|
|
repeat,
|
|
|
|
scheduled: false
|
|
|
|
};
|
|
|
|
// Register the timer's existence in the id-to-timer map.
|
|
|
|
idMap.set(timer.id, timer);
|
|
|
|
// Schedule the timer in the due table.
|
|
|
|
schedule(timer, now);
|
|
|
|
return timer.id;
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Sets a timer which executes a function once after the timer expires. */
|
2018-11-29 22:25:07 -05:00
|
|
|
export function setTimeout(
|
2018-10-02 20:47:40 -04:00
|
|
|
cb: (...args: Args) => void,
|
2019-09-07 12:27:18 -04:00
|
|
|
delay = 0,
|
2018-10-02 20:47:40 -04:00
|
|
|
...args: Args
|
2018-07-06 11:20:35 -04:00
|
|
|
): number {
|
2019-08-29 10:57:09 -04:00
|
|
|
checkBigInt(delay);
|
2019-06-13 11:08:27 -04:00
|
|
|
// @ts-ignore
|
|
|
|
checkThis(this);
|
2018-10-02 20:47:40 -04:00
|
|
|
return setTimer(cb, delay, args, false);
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
|
2019-12-03 22:19:03 -05:00
|
|
|
/** Repeatedly calls a function, with a fixed time delay between each call. */
|
2018-11-29 22:25:07 -05:00
|
|
|
export function setInterval(
|
2018-10-02 20:47:40 -04:00
|
|
|
cb: (...args: Args) => void,
|
2019-09-07 12:27:18 -04:00
|
|
|
delay = 0,
|
2018-10-02 20:47:40 -04:00
|
|
|
...args: Args
|
|
|
|
): number {
|
2019-08-29 10:57:09 -04:00
|
|
|
checkBigInt(delay);
|
2019-06-13 11:08:27 -04:00
|
|
|
// @ts-ignore
|
|
|
|
checkThis(this);
|
2018-10-02 20:47:40 -04:00
|
|
|
return setTimer(cb, delay, args, true);
|
|
|
|
}
|
|
|
|
|
2019-03-10 15:37:05 -04:00
|
|
|
/** Clears a previously set timer by id. AKA clearTimeout and clearInterval. */
|
2019-06-18 09:24:20 -04:00
|
|
|
function clearTimer(id: number): void {
|
2019-06-17 13:42:20 -04:00
|
|
|
id = Number(id);
|
2018-10-02 20:47:40 -04:00
|
|
|
const timer = idMap.get(id);
|
|
|
|
if (timer === undefined) {
|
|
|
|
// Timer doesn't exist any more or never existed. This is not an error.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Unschedule the timer if it is currently scheduled, and forget about it.
|
|
|
|
unschedule(timer);
|
|
|
|
idMap.delete(timer.id);
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
2019-06-18 09:24:20 -04:00
|
|
|
|
2019-09-07 12:27:18 -04:00
|
|
|
export function clearTimeout(id = 0): void {
|
2019-08-30 11:51:53 -04:00
|
|
|
checkBigInt(id);
|
2019-07-18 06:09:32 -04:00
|
|
|
if (id === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2019-06-18 09:24:20 -04:00
|
|
|
clearTimer(id);
|
|
|
|
}
|
|
|
|
|
2019-09-07 12:27:18 -04:00
|
|
|
export function clearInterval(id = 0): void {
|
2019-08-30 11:51:53 -04:00
|
|
|
checkBigInt(id);
|
2019-07-18 06:09:32 -04:00
|
|
|
if (id === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2019-06-18 09:24:20 -04:00
|
|
|
clearTimer(id);
|
|
|
|
}
|