@langgraph-js/sdk
Version:
The UI SDK for LangGraph - seamlessly integrate your AI agents with frontend interfaces
27 lines (26 loc) • 828 B
JavaScript
/**
* Creates a debounced function that executes once per animation frame
* @param callback - The function to debounce
* @returns A function that executes the callback on the next animation frame
*/
export function rafDebounce(callback) {
let rafId = null;
let lastArgs = null;
// Return the debounced function
return function (...args) {
// Store the most recent arguments
lastArgs = args;
// Cancel any pending animation frame
if (rafId !== null) {
cancelAnimationFrame(rafId);
}
// Schedule execution on the next animation frame
rafId = requestAnimationFrame(() => {
if (lastArgs !== null) {
callback(...lastArgs);
lastArgs = null;
}
rafId = null;
});
};
}