1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00
denoland-deno/js/timers.ts
Ryan Dahl b088b58f76 Add SetGlobalTimeout().
To be used for a timers implementation soon.
2018-09-25 17:02:49 -04:00

107 lines
2.7 KiB
TypeScript

// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import { assert } from "./util";
import * as util from "./util";
import * as fbs from "gen/msg_generated";
import { flatbuffers } from "flatbuffers";
import { sendSync, sendAsync } from "./dispatch";
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
}
export function setGlobalTimeout(timeout: number) {
const builder = new flatbuffers.Builder();
fbs.SetTimeout.startSetTimeout(builder);
fbs.SetTimeout.addTimeout(builder, timeout);
const msg = fbs.SetTimeout.endSetTimeout(builder);
const res = sendSync(builder, fbs.Any.SetTimeout, msg);
assert(res == null);
}
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 = sendSync(builder, fbs.Any.TimerClear, msg);
assert(res == null);
}