mirror of
https://github.com/denoland/deno.git
synced 2025-08-03 02:22:40 +00:00
Import ts file from prototype without change
From commit 559453cf6c
Excluding v8worker.d.ts, main.ts, and deno.d.ts.
Updates tslint.json to be original settings.
This commit is contained in:
parent
21e1425656
commit
fe404dfce9
16 changed files with 1374 additions and 73 deletions
89
js/timers.ts
Normal file
89
js/timers.ts
Normal file
|
@ -0,0 +1,89 @@
|
|||
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
||||
// All rights reserved. MIT License.
|
||||
import { deno as pb } from "./msg.pb";
|
||||
import { pubInternal, sub } from "./dispatch";
|
||||
import { assert } from "./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
|
||||
}
|
||||
|
||||
const timers = new Map<number, Timer>();
|
||||
|
||||
export function initTimers() {
|
||||
sub("timers", onMessage);
|
||||
}
|
||||
|
||||
function onMessage(payload: Uint8Array) {
|
||||
const msg = pb.Msg.decode(payload);
|
||||
assert(msg.command === pb.Msg.Command.TIMER_READY);
|
||||
const { timerReadyId, timerReadyDone } = msg;
|
||||
const timer = timers.get(timerReadyId);
|
||||
if (!timer) {
|
||||
return;
|
||||
}
|
||||
timer.cb(...timer.args);
|
||||
if (timerReadyDone) {
|
||||
timers.delete(timerReadyId);
|
||||
}
|
||||
}
|
||||
|
||||
function setTimer(
|
||||
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);
|
||||
pubInternal("timers", {
|
||||
command: pb.Msg.Command.TIMER_START,
|
||||
timerStartId: timer.id,
|
||||
timerStartInterval: timer.interval,
|
||||
timerStartDelay: timer.delay
|
||||
});
|
||||
return timer.id;
|
||||
}
|
||||
|
||||
export function setTimeout(
|
||||
cb: TimerCallback,
|
||||
delay: number,
|
||||
// tslint:disable-next-line:no-any
|
||||
...args: any[]
|
||||
): number {
|
||||
return setTimer(cb, delay, false, args);
|
||||
}
|
||||
|
||||
export function setInterval(
|
||||
cb: TimerCallback,
|
||||
delay: number,
|
||||
// tslint:disable-next-line:no-any
|
||||
...args: any[]
|
||||
): number {
|
||||
return setTimer(cb, delay, true, args);
|
||||
}
|
||||
|
||||
export function clearTimer(id: number) {
|
||||
timers.delete(id);
|
||||
pubInternal("timers", {
|
||||
command: pb.Msg.Command.TIMER_CLEAR,
|
||||
timerClearId: id
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue