Fix too many requests caused by relationship look-ups in web UI (#32042)

Co-authored-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
Eugen Rochko 2024-09-24 19:02:36 +02:00 committed by GitHub
parent f1b6a611aa
commit 70988519df
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 40 additions and 19 deletions

View file

@ -0,0 +1,23 @@
import { debounce } from 'lodash';
import type { AppDispatch } from 'mastodon/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();
};
};