diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2018-09-05 22:13:36 -0400 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2018-09-09 18:47:22 -0400 |
commit | 0d03fafbfec4545098023b7147c5f8fb6ae06f99 (patch) | |
tree | f7c3e10ac0956c4a1ae19c91a711de54de677965 /js/timers.ts | |
parent | ff6eefdf87560986274799132d44b00e0a288c21 (diff) |
Map promises onto futures.
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.
Diffstat (limited to 'js/timers.ts')
-rw-r--r-- | js/timers.ts | 63 |
1 files changed, 33 insertions, 30 deletions
diff --git a/js/timers.ts b/js/timers.ts index d09af0295..6b23c64f1 100644 --- a/js/timers.ts +++ b/js/timers.ts @@ -3,7 +3,7 @@ import { assert } from "./util"; import * as util from "./util"; import { deno as fbs } from "gen/msg_generated"; import { flatbuffers } from "flatbuffers"; -import { send } from "./fbs_util"; +import { send, sendAsync } from "./fbs_util"; let nextTimerId = 1; @@ -19,50 +19,51 @@ interface Timer { delay: number; // milliseconds } -const timers = new Map<number, Timer>(); - -/** @internal */ -export function onMessage(msg: fbs.TimerReady) { - const timerReadyId = msg.id(); - const timerReadyDone = msg.done(); - const timer = timers.get(timerReadyId); - if (!timer) { - return; - } - timer.cb(...timer.args); - if (timerReadyDone) { - timers.delete(timerReadyId); - } -} - function startTimer( + id: number, cb: TimerCallback, delay: number, interval: boolean, // tslint:disable-next-line:no-any args: any[] -): number { - const timer = { - id: nextTimerId++, +): void { + const timer: Timer = { + id, interval, delay, args, cb }; - timers.set(timer.id, timer); - 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.addInterval(builder, timer.interval); fbs.TimerStart.addDelay(builder, timer.delay); const msg = fbs.TimerStart.endTimerStart(builder); - const baseRes = send(builder, fbs.Any.TimerStart, msg); - assert(baseRes == null); - return timer.id; + + 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( @@ -71,7 +72,9 @@ export function setTimeout( // tslint:disable-next-line:no-any ...args: any[] ): number { - return startTimer(cb, delay, false, args); + const id = nextTimerId++; + startTimer(id, cb, delay, false, args); + return id; } export function setInterval( @@ -80,12 +83,12 @@ export function setInterval( // tslint:disable-next-line:no-any ...args: any[] ): number { - return startTimer(cb, delay, true, args); + const id = nextTimerId++; + startTimer(id, cb, delay, true, args); + return id; } export function clearTimer(id: number) { - timers.delete(id); - const builder = new flatbuffers.Builder(); fbs.TimerClear.startTimerClear(builder); fbs.TimerClear.addId(builder, id); |