0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-31 09:14:20 -04:00
denoland-deno/js/timers.ts

99 lines
2.4 KiB
TypeScript
Raw Normal View History

// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import { assert } from "./util";
import * as util from "./util";
import { deno as fbs } from "gen/msg_generated";
import { flatbuffers } from "flatbuffers";
import { send, sendAsync } from "./fbs_util";
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
}
function startTimer(
id: number,
cb: TimerCallback,
delay: number,
interval: boolean,
// tslint:disable-next-line:no-any
args: any[]
): void {
const timer: Timer = {
id,
interval,
delay,
args,
cb
};
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.addDelay(builder, timer.delay);
const msg = fbs.TimerStart.endTimerStart(builder);
sendAsync(builder, fbs.Any.TimerStart, msg).then(
baseRes => {
assert(fbs.Any.TimerReady === baseRes!.msgType());
const msg = new fbs.TimerReady();
assert(baseRes!.msg(msg) != null);
assert(msg.id() === timer.id);
if (msg.canceled()) {
util.log("timer canceled message");
} else {
cb(...args);
if (interval) {
// TODO Faking setInterval with setTimeout.
// We need a new timer implementation, this is just a stopgap.
startTimer(id, cb, delay, true, args);
}
}
},
error => {
throw error;
}
);
}
export function setTimeout(
cb: TimerCallback,
delay: number,
// tslint:disable-next-line:no-any
...args: any[]
): number {
const id = nextTimerId++;
startTimer(id, cb, delay, false, args);
return id;
}
export function setInterval(
cb: TimerCallback,
delay: number,
// tslint:disable-next-line:no-any
...args: any[]
): number {
const id = nextTimerId++;
startTimer(id, cb, delay, true, args);
return id;
}
export function clearTimer(id: number) {
const builder = new flatbuffers.Builder();
fbs.TimerClear.startTimerClear(builder);
fbs.TimerClear.addId(builder, id);
const msg = fbs.TimerClear.endTimerClear(builder);
const res = send(builder, fbs.Any.TimerClear, msg);
assert(res == null);
}