2018-05-28 21:50:44 -04:00
|
|
|
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
|
|
|
// All rights reserved. MIT License.
|
2018-06-13 13:37:24 -04:00
|
|
|
import { deno as pb } from "./msg.pb";
|
2018-06-05 04:10:27 -04:00
|
|
|
import { pubInternal, sub } from "./dispatch";
|
2018-05-25 15:36:13 -04:00
|
|
|
import { assert } from "./util";
|
2018-05-18 11:49:28 -04:00
|
|
|
|
|
|
|
let nextTimerId = 1;
|
|
|
|
|
|
|
|
// tslint:disable-next-line:no-any
|
2018-05-23 14:53:41 -04:00
|
|
|
export type TimerCallback = (...args: any[]) => void;
|
2018-05-18 11:49:28 -04:00
|
|
|
|
|
|
|
interface Timer {
|
|
|
|
id: number;
|
|
|
|
cb: TimerCallback;
|
|
|
|
interval: boolean;
|
2018-05-23 14:53:41 -04:00
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
args: any[];
|
2018-05-31 14:26:43 -04:00
|
|
|
delay: number; // milliseconds
|
2018-05-18 11:49:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const timers = new Map<number, Timer>();
|
|
|
|
|
2018-05-21 22:07:40 -04:00
|
|
|
export function initTimers() {
|
2018-06-05 04:10:27 -04:00
|
|
|
sub("timers", onMessage);
|
2018-05-21 22:07:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function onMessage(payload: Uint8Array) {
|
|
|
|
const msg = pb.Msg.decode(payload);
|
2018-05-25 15:36:13 -04:00
|
|
|
assert(msg.command === pb.Msg.Command.TIMER_READY);
|
2018-06-05 04:43:00 -04:00
|
|
|
const { timerReadyId, timerReadyDone } = msg;
|
|
|
|
const timer = timers.get(timerReadyId);
|
2018-05-23 14:53:41 -04:00
|
|
|
if (!timer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
timer.cb(...timer.args);
|
2018-06-05 04:43:00 -04:00
|
|
|
if (timerReadyDone) {
|
|
|
|
timers.delete(timerReadyId);
|
2018-05-21 22:07:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 11:28:14 -04:00
|
|
|
function setTimer(
|
2018-05-23 14:53:41 -04:00
|
|
|
cb: TimerCallback,
|
2018-05-31 14:26:43 -04:00
|
|
|
delay: number,
|
2018-05-31 11:28:14 -04:00
|
|
|
interval: boolean,
|
2018-05-23 14:53:41 -04:00
|
|
|
// tslint:disable-next-line:no-any
|
2018-05-31 11:28:14 -04:00
|
|
|
args: any[]
|
2018-05-23 14:53:41 -04:00
|
|
|
): number {
|
2018-05-18 11:49:28 -04:00
|
|
|
const timer = {
|
|
|
|
id: nextTimerId++,
|
2018-05-31 11:28:14 -04:00
|
|
|
interval,
|
2018-05-31 14:26:43 -04:00
|
|
|
delay,
|
2018-05-23 14:53:41 -04:00
|
|
|
args,
|
2018-05-18 11:49:28 -04:00
|
|
|
cb
|
|
|
|
};
|
|
|
|
timers.set(timer.id, timer);
|
2018-06-05 04:10:27 -04:00
|
|
|
pubInternal("timers", {
|
2018-05-25 15:36:13 -04:00
|
|
|
command: pb.Msg.Command.TIMER_START,
|
|
|
|
timerStartId: timer.id,
|
2018-05-31 14:26:43 -04:00
|
|
|
timerStartInterval: timer.interval,
|
|
|
|
timerStartDelay: timer.delay
|
2018-05-18 11:49:28 -04:00
|
|
|
});
|
|
|
|
return timer.id;
|
|
|
|
}
|
2018-05-23 14:53:41 -04:00
|
|
|
|
2018-05-31 11:28:14 -04:00
|
|
|
export function setTimeout(
|
|
|
|
cb: TimerCallback,
|
2018-05-31 14:26:43 -04:00
|
|
|
delay: number,
|
2018-05-31 11:28:14 -04:00
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
...args: any[]
|
|
|
|
): number {
|
2018-05-31 14:26:43 -04:00
|
|
|
return setTimer(cb, delay, false, args);
|
2018-05-31 11:28:14 -04:00
|
|
|
}
|
|
|
|
|
2018-05-23 14:53:41 -04:00
|
|
|
export function setInterval(
|
|
|
|
cb: TimerCallback,
|
2018-05-31 14:26:43 -04:00
|
|
|
delay: number,
|
2018-05-23 14:53:41 -04:00
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
...args: any[]
|
|
|
|
): number {
|
2018-05-31 14:26:43 -04:00
|
|
|
return setTimer(cb, delay, true, args);
|
2018-05-23 14:53:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function clearTimer(id: number) {
|
2018-06-09 05:08:23 -04:00
|
|
|
timers.delete(id);
|
2018-06-05 04:10:27 -04:00
|
|
|
pubInternal("timers", {
|
2018-05-25 15:36:13 -04:00
|
|
|
command: pb.Msg.Command.TIMER_CLEAR,
|
|
|
|
timerClearId: id
|
2018-05-23 14:53:41 -04:00
|
|
|
});
|
|
|
|
}
|