UNPKG

@monstermann/fn

Version:

A utility library for TypeScript.

33 lines (31 loc) 884 B
import { isIterable } from "../../function/isIterable.js"; //#region src/promise/internals/timers.ts function createTimer(timeout, callback) { return { callback, tid: void 0, timeout }; } function clearTimer(timer) { if (isIterable(timer)) return Array.from(timer).forEach(clearTimer); if (timer.timeout < 0) return; timer.tid = void clearTimeout(timer.tid); } function startTimer(timer) { if (isIterable(timer)) return Array.from(timer).forEach(startTimer); if (timer.timeout < 0) return; if (timer.tid != null) return; timer.tid = setTimeout(() => { timer.tid = void 0; timer.callback(); }, timer.timeout); } function restartTimer(timer) { if (isIterable(timer)) return Array.from(timer).forEach(restartTimer); if (timer.timeout < 0) return; clearTimer(timer); startTimer(timer); } //#endregion export { clearTimer, createTimer, restartTimer, startTimer };