Table of Contents
Standard Module: timeout
To use the timeout module and the Timeout interface in a HipHop program,
import the JavaScript module with:
import { timeout, Timeout } from "@hop/hiphop/modules/timeout.js";The timeout HipHop module implements asynchronous timeouts that are
controlled by Hiphop programs. Once started, a timeout can be
paused, reset, or aborted. When the timeout expires, it emits
a signal to notify the other components of the HipHop program.
Timeout
The Timeout interface declares the following HipHop signals:
in reset;in pause;out elapsed.
These signals are used to control a running instance of the
timeout module.
timeout(duration) implements Timeout
A HipHop timeout module is an asynchronous tasks that run in the
background of the application. When it expires, it emits its signal
elapsed.
A timeout is created by running the timeout module passing
it a numerical value, which is the timeout in millisecond before
the signal elapsed is emitted. Example:
run timeout(2000) { e as elapsed }; // wait for two seconds before emitting `e`.A timeout instance is paused by emitting its pause signals. It is
resumed by emitting the same signal again. When a timeout is paused,
it stops counting elapsed time. For instance, if a timeout is spawned
to wait for 1000 ms and if it is suspended after 100ms, when resumed,
and whenever resumed, it will wait for an extra 900ms before emitting
its elapsed signal.
If a timeout receives its reset signal, it resets its internal
counter and waits again for the full duration before emiting elapsed.
★ Example: timeout-mod.hh.js
import * as hh from "@hop/hiphop";
import { timeout, Timeout } from "@hop/hiphop/modules/timeout.js";
const delay = 100;
hiphop module prg(resolve) {
out DUR;
signal implements Timeout;
let now0 = Date.now();
fork {
run timeout(delay) { * };
} par {
// pause the timer
yield;
emit pause(true);
// wait for a an extra delay
run timeout(delay) { };
// resume the timer
emit pause(false);
} par {
await immediate (elapsed.now);
}
emit DUR((Date.now() - now0) > delay);
pragma { resolve(false); }
}
export const mach = new hh.ReactiveMachine(prg);
mach.batchPromise = new Promise((res, rej) => mach.init(res));
mach.react();
mach.react();
Implementation
- JavaScript implementation: timeout.hh.js
- TypeScript type declaration: timeout.d.ts