mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 17:34:47 -05:00
optimize the timer ts impl, and correct the duration if less than 10
This commit is contained in:
parent
942daac1da
commit
502662476a
2 changed files with 21 additions and 20 deletions
|
@ -31,6 +31,10 @@ func InitTimers() {
|
||||||
Duration: msg.TimerStartDuration,
|
Duration: msg.TimerStartDuration,
|
||||||
Cleared: false,
|
Cleared: false,
|
||||||
}
|
}
|
||||||
|
// If this parameter is less than 10, a value of 10 is used
|
||||||
|
if t.Duration < 10 {
|
||||||
|
t.Duration = 10
|
||||||
|
}
|
||||||
t.StartTimer()
|
t.StartTimer()
|
||||||
timers[id] = t
|
timers[id] = t
|
||||||
return nil
|
return nil
|
||||||
|
|
37
timers.ts
37
timers.ts
|
@ -39,15 +39,16 @@ function onMessage(payload: Uint8Array) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setTimeout(
|
function setTimer(
|
||||||
cb: TimerCallback,
|
cb: TimerCallback,
|
||||||
duration: number,
|
duration: number,
|
||||||
|
interval: boolean,
|
||||||
// tslint:disable-next-line:no-any
|
// tslint:disable-next-line:no-any
|
||||||
...args: any[]
|
args: any[]
|
||||||
): number {
|
): number {
|
||||||
const timer = {
|
const timer = {
|
||||||
id: nextTimerId++,
|
id: nextTimerId++,
|
||||||
interval: false,
|
interval,
|
||||||
duration,
|
duration,
|
||||||
args,
|
args,
|
||||||
cb
|
cb
|
||||||
|
@ -56,34 +57,28 @@ export function setTimeout(
|
||||||
dispatch.sendMsg("timers", {
|
dispatch.sendMsg("timers", {
|
||||||
command: pb.Msg.Command.TIMER_START,
|
command: pb.Msg.Command.TIMER_START,
|
||||||
timerStartId: timer.id,
|
timerStartId: timer.id,
|
||||||
timerStartInterval: false,
|
timerStartInterval: interval,
|
||||||
timerStartDuration: duration
|
timerStartDuration: duration
|
||||||
});
|
});
|
||||||
return timer.id;
|
return timer.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO DRY with setTimeout
|
export function setTimeout(
|
||||||
|
cb: TimerCallback,
|
||||||
|
duration: number,
|
||||||
|
// tslint:disable-next-line:no-any
|
||||||
|
...args: any[]
|
||||||
|
): number {
|
||||||
|
return setTimer(cb, duration, false, args);
|
||||||
|
}
|
||||||
|
|
||||||
export function setInterval(
|
export function setInterval(
|
||||||
cb: TimerCallback,
|
cb: TimerCallback,
|
||||||
repeat: number,
|
repeat: number,
|
||||||
// tslint:disable-next-line:no-any
|
// tslint:disable-next-line:no-any
|
||||||
...args: any[]
|
...args: any[]
|
||||||
): number {
|
): number {
|
||||||
const timer = {
|
return setTimer(cb, duration, true, args);
|
||||||
id: nextTimerId++,
|
|
||||||
interval: true,
|
|
||||||
duration: repeat,
|
|
||||||
args,
|
|
||||||
cb
|
|
||||||
};
|
|
||||||
timers.set(timer.id, timer);
|
|
||||||
dispatch.sendMsg("timers", {
|
|
||||||
command: pb.Msg.Command.TIMER_START,
|
|
||||||
timerStartId: timer.id,
|
|
||||||
timerStartInterval: true,
|
|
||||||
timerStartDuration: repeat
|
|
||||||
});
|
|
||||||
return timer.id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function clearTimer(id: number) {
|
export function clearTimer(id: number) {
|
||||||
|
@ -92,3 +87,5 @@ export function clearTimer(id: number) {
|
||||||
timerClearId: id
|
timerClearId: id
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue