UNPKG

vike

Version:

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

33 lines (32 loc) 1.21 kB
export { catchInfiniteLoop }; import { assertUsage, assertWarning } from './assert.js'; const trackers = {}; function catchInfiniteLoop(functionName, maxNumberOfCalls = 100, withinSeconds = 5) { // Init const now = new Date(); let tracker = (trackers[functionName] ?? (trackers[functionName] = createTracker(now))); // Reset const elapsedTime = now.getTime() - tracker.start.getTime(); if (elapsedTime > withinSeconds * 1000) tracker = trackers[functionName] = createTracker(now); // Count tracker.count++; // Error const msg = `[Infinite Loop] Rendering ${tracker.count} times within ${withinSeconds} seconds [${functionName}]`; if (tracker.count > maxNumberOfCalls) { assertUsage(false, msg); } // Warning, at 50% threshold if (!tracker.warned && tracker.count > maxNumberOfCalls * 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; } } function createTracker(now) { const tracker = { count: 0, start: now, }; return tracker; }