'use strict'; var solidJs = require('solid-js'); var web = require('solid-js/web'); // src/index.ts exports.makeTimer = (fn, delay, timer) => { if (web.isServer) { return () => void 0; } const intervalId = timer(fn, delay); const clear = () => clearInterval(intervalId); return solidJs.onCleanup(clear); }; exports.createTimer = (fn, delay, timer) => { if (web.isServer) { return void 0; } if (typeof delay === "number") { exports.makeTimer(fn, delay, timer); return; } let done = false; let prevTime = performance.now(); let fractionDone = 0; let shouldHandleFraction = false; const callHandler = () => { solidJs.untrack(fn); prevTime = performance.now(); done = timer === setTimeout; }; solidJs.createEffect((prevDelay) => { if (done) return; const currDelay = delay(); if (currDelay === false) { if (prevDelay) fractionDone += (performance.now() - prevTime) / prevDelay; return currDelay; } if (prevDelay === false) prevTime = performance.now(); if (shouldHandleFraction) { if (prevDelay) fractionDone += (performance.now() - prevTime) / prevDelay; prevTime = performance.now(); if (fractionDone >= 1) { fractionDone = 0; callHandler(); } else if (fractionDone > 0) { const [listen, rerunEffect] = solidJs.createSignal(void 0, { equals: false }); listen(); exports.makeTimer( () => { fractionDone = 0; shouldHandleFraction = false; rerunEffect(); callHandler(); }, (1 - fractionDone) * currDelay, setTimeout ); return currDelay; } } shouldHandleFraction = true; exports.makeTimer(callHandler, currDelay, timer); return currDelay; }); }; exports.createTimeoutLoop = (handler, timeout) => { if (web.isServer) { return void 0; } if (typeof timeout === "number") { exports.makeTimer(handler, timeout, setInterval); return; } const [currentTimeout, setCurrentTimeout] = solidJs.createSignal(solidJs.untrack(timeout)); solidJs.createEffect(() => { const currTimeout = currentTimeout(); if (currTimeout === false) return; exports.makeTimer( () => { handler(); setCurrentTimeout(timeout); }, currTimeout, setInterval ); }); }; function createPolled(fn, timeout, value, options) { if (web.isServer) { return fn; } const memo = solidJs.createMemo(() => solidJs.createSignal(fn(value), options), options); exports.createTimer(() => memo()[1](fn), timeout, setInterval); return () => memo()[0](); } exports.createIntervalCounter = (timeout, options) => { if (web.isServer) { return () => 0; } return createPolled((prev) => prev + 1, timeout, -1, options); }; exports.createPolled = createPolled;