2018-07-23 14:46:30 -04:00
|
|
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
2018-07-06 11:20:35 -04:00
|
|
|
import { assert } from "./util";
|
2018-08-09 15:17:08 -04:00
|
|
|
import * as util from "./util";
|
|
|
|
import { deno as fbs } from "gen/msg_generated";
|
|
|
|
import { flatbuffers } from "flatbuffers";
|
2018-08-30 15:35:51 -04:00
|
|
|
import { send } from "./fbs_util";
|
2018-07-06 11:20:35 -04:00
|
|
|
|
|
|
|
let nextTimerId = 1;
|
|
|
|
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
export type TimerCallback = (...args: any[]) => void;
|
|
|
|
|
|
|
|
interface Timer {
|
|
|
|
id: number;
|
|
|
|
cb: TimerCallback;
|
|
|
|
interval: boolean;
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
args: any[];
|
|
|
|
delay: number; // milliseconds
|
|
|
|
}
|
|
|
|
|
|
|
|
const timers = new Map<number, Timer>();
|
|
|
|
|
2018-08-12 14:27:54 -04:00
|
|
|
/** @internal */
|
2018-08-09 15:17:08 -04:00
|
|
|
export function onMessage(msg: fbs.TimerReady) {
|
|
|
|
const timerReadyId = msg.id();
|
|
|
|
const timerReadyDone = msg.done();
|
2018-07-06 11:20:35 -04:00
|
|
|
const timer = timers.get(timerReadyId);
|
|
|
|
if (!timer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
timer.cb(...timer.args);
|
|
|
|
if (timerReadyDone) {
|
|
|
|
timers.delete(timerReadyId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-09 15:17:08 -04:00
|
|
|
function startTimer(
|
2018-07-06 11:20:35 -04:00
|
|
|
cb: TimerCallback,
|
|
|
|
delay: number,
|
|
|
|
interval: boolean,
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
args: any[]
|
|
|
|
): number {
|
|
|
|
const timer = {
|
|
|
|
id: nextTimerId++,
|
|
|
|
interval,
|
|
|
|
delay,
|
|
|
|
args,
|
|
|
|
cb
|
|
|
|
};
|
|
|
|
timers.set(timer.id, timer);
|
2018-08-09 15:17:08 -04:00
|
|
|
|
|
|
|
util.log("timers.ts startTimer");
|
|
|
|
|
|
|
|
// Send TimerStart message
|
|
|
|
const builder = new flatbuffers.Builder();
|
|
|
|
fbs.TimerStart.startTimerStart(builder);
|
|
|
|
fbs.TimerStart.addId(builder, timer.id);
|
|
|
|
fbs.TimerStart.addInterval(builder, timer.interval);
|
|
|
|
fbs.TimerStart.addDelay(builder, timer.delay);
|
|
|
|
const msg = fbs.TimerStart.endTimerStart(builder);
|
2018-08-30 15:35:51 -04:00
|
|
|
const baseRes = send(builder, fbs.Any.TimerStart, msg);
|
|
|
|
assert(baseRes == null);
|
2018-07-06 11:20:35 -04:00
|
|
|
return timer.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setTimeout(
|
|
|
|
cb: TimerCallback,
|
|
|
|
delay: number,
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
...args: any[]
|
|
|
|
): number {
|
2018-08-09 15:17:08 -04:00
|
|
|
return startTimer(cb, delay, false, args);
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function setInterval(
|
|
|
|
cb: TimerCallback,
|
|
|
|
delay: number,
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
...args: any[]
|
|
|
|
): number {
|
2018-08-09 15:17:08 -04:00
|
|
|
return startTimer(cb, delay, true, args);
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function clearTimer(id: number) {
|
|
|
|
timers.delete(id);
|
2018-08-09 15:17:08 -04:00
|
|
|
|
|
|
|
const builder = new flatbuffers.Builder();
|
|
|
|
fbs.TimerClear.startTimerClear(builder);
|
|
|
|
fbs.TimerClear.addId(builder, id);
|
|
|
|
const msg = fbs.TimerClear.endTimerClear(builder);
|
2018-08-30 15:35:51 -04:00
|
|
|
const res = send(builder, fbs.Any.TimerClear, msg);
|
|
|
|
assert(res == null);
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|