UNPKG

vike

Version:

The Framework *You* Control - Next.js & Nuxt alternative for unprecedented flexibility and dependability.

32 lines (31 loc) 1.2 kB
export { catchInfiniteLoop }; import { assertUsage, assertWarning } from './assert.js'; import { humanizeTime } from './humanizeTime.js'; const trackers = {}; const maxCalls = 99; const time = 5 * 1000; function catchInfiniteLoop(functionName) { // Init const now = new Date().getTime(); // Clean all outdated trackers Object.keys(trackers).forEach((key) => { const tracker = trackers[key]; const elapsedTime = now - tracker.startTime; if (elapsedTime > time) delete trackers[key]; }); const tracker = (trackers[functionName] ?? (trackers[functionName] = { count: 0, startTime: now })); // Count tracker.count++; // Error const msg = `${functionName} called ${tracker.count} times within ${humanizeTime(time)} — infinite loop?`; if (tracker.count > maxCalls) { assertUsage(false, msg); } // Warning, at 50% threshold if (!tracker.warned && tracker.count > maxCalls * 0.5) { // Warning is shown upon 10 calls a second, on average during 5 seconds, given the default parameters assertWarning(false, msg, { onlyOnce: false, showStackTrace: true }); tracker.warned = true; } }