'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var utils = require('@solid-primitives/utils'); var solidJs = require('solid-js'); var web = require('solid-js/web'); // src/index.ts function createRAF(callback) { if (web.isServer) { return [() => false, utils.noop, utils.noop]; } const [running, setRunning] = solidJs.createSignal(false); let requestID = 0; const loop = (timeStamp) => { requestID = requestAnimationFrame(loop); callback(timeStamp); }; const start = () => { if (running()) return; setRunning(true); requestID = requestAnimationFrame(loop); }; const stop = () => { setRunning(false); cancelAnimationFrame(requestID); }; solidJs.onCleanup(stop); return [running, start, stop]; } function targetFPS(callback, fps) { if (web.isServer) { return callback; } const interval = typeof fps === "function" ? solidJs.createMemo(() => Math.floor(1e3 / fps())) : (() => { const newInterval = Math.floor(1e3 / fps); return () => newInterval; })(); let elapsed = 0; let lastRun = 0; let missedBy = 0; return (timeStamp) => { elapsed = timeStamp - lastRun; if (Math.ceil(elapsed + missedBy) >= interval()) { lastRun = timeStamp; missedBy = Math.max(elapsed - interval(), 0); callback(timeStamp); } }; } function createMs(fps, limit) { const [ms, setMs] = solidJs.createSignal(0); let initialTs = 0; const reset = () => { initialTs = 0; }; const [running, start, stop] = createRAF( targetFPS((ts) => { initialTs ||= ts; const ms2 = ts - initialTs; setMs(ts - initialTs); if (ms2 === (typeof limit === "function" ? limit() : limit)) reset(); }, fps) ); start(); solidJs.onCleanup(stop); return Object.assign(ms, { reset, running, start, stop }); } exports.createMs = createMs; exports.createRAF = createRAF; exports.default = createRAF; exports.targetFPS = targetFPS;