catstodon/app/javascript/flavours/glitch/utils/debounce.ts
Eugen Rochko ba7b1f06c1 [Glitch] Fix too many requests caused by relationship look-ups in web UI
Port 70988519df to glitch-soc

Co-authored-by: Claire <claire.github-309c@sitedethib.com>
Signed-off-by: Claire <claire.github-309c@sitedethib.com>
2024-09-24 19:42:30 +02:00

23 lines
585 B
TypeScript

import { debounce } from 'lodash';
import type { AppDispatch } from 'flavours/glitch/store';
export const debounceWithDispatchAndArguments = <T>(
fn: (dispatch: AppDispatch, ...args: T[]) => void,
{ delay = 100 },
) => {
let argumentBuffer: T[] = [];
let dispatchBuffer: AppDispatch;
const wrapped = debounce(() => {
const tmpBuffer = argumentBuffer;
argumentBuffer = [];
fn(dispatchBuffer, ...tmpBuffer);
}, delay);
return (dispatch: AppDispatch, ...args: T[]) => {
dispatchBuffer = dispatch;
argumentBuffer.push(...args);
wrapped();
};
};