HEX
Server: LiteSpeed
System: Linux server342.web-hosting.com 4.18.0-553.124.4.lve.el8.x86_64 #1 SMP Fri May 15 13:02:13 UTC 2026 x86_64
User: ksonpoau (1099)
PHP: 8.2.31
Disabled: NONE
Upload Files
File: //home/ksonpoau/www/wp-content/plugins/extendify/src/AutoLaunch/hooks/useRateLimitedCursor.js
import { useEffect, useRef } from '@wordpress/element';

export const useRateLimitedCursor = (cb, intervalMs = 2000, deps = []) => {
	const lastAtRef = useRef(0);
	const timerRef = useRef(null);

	useEffect(() => {
		const schedule = () => {
			clearTimeout(timerRef.current);

			const now = Date.now();
			const wait = Math.max(0, lastAtRef.current + intervalMs - now);

			const run = () => {
				lastAtRef.current = Date.now();
				const hasMore = cb();
				if (hasMore) schedule();
			};

			if (wait === 0) run();
			else timerRef.current = setTimeout(run, wait);
		};

		schedule();

		return () => {
			clearTimeout(timerRef.current);
			timerRef.current = null;
		};
	}, [cb, intervalMs, ...deps]);
};