mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-14 15:24:52 +09:00
27 lines
713 B
TypeScript
27 lines
713 B
TypeScript
/**
|
|
* Run a task now and again intervalMs after each run finishes, so runs never overlap.
|
|
* setInterval can't be used as an async task may take longer than the interval.
|
|
* The task handles its own errors.
|
|
*
|
|
* @returns Function that stops polling, a run in progress still finishes
|
|
*/
|
|
export function poll(task: () => Promise<void>, intervalMs: number) {
|
|
let stopped = false;
|
|
let timeout: ReturnType<typeof setTimeout> | undefined;
|
|
async function run() {
|
|
if (stopped) {
|
|
return;
|
|
}
|
|
await task();
|
|
// Stopping while the task ran must not schedule another run
|
|
if (!stopped) {
|
|
timeout = setTimeout(run, intervalMs);
|
|
}
|
|
}
|
|
run();
|
|
return () => {
|
|
stopped = true;
|
|
clearTimeout(timeout);
|
|
};
|
|
}
|