mirror of
https://github.com/denoland/deno.git
synced 2025-03-04 01:44:26 -05:00

Refactors handlers.rs The idea is that all Deno "ops" (aka bindings) should map onto a Rust Future. By setting the "sync" flag in the Base message users can determine if the future is executed immediately or put on the event loop. In the case of async futures, a promise is automatically created. Errors are automatically forwarded and raised. TODO: - The file system ops in src/handler.rs are not using the thread pool yet. This will be done in the future using tokio_threadpool::blocking. That is, if you try to call them asynchronously, you will get a promise and it will act asynchronous, but currently it will be blocking. - Handlers in src/handler.rs returned boxed futures. This was to make it easy while developing. We should try to remove this allocation.
98 lines
2.4 KiB
TypeScript
98 lines
2.4 KiB
TypeScript
// 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);
|
|
}
|