mirror of
https://github.com/funamitech/mastodon
synced 2024-12-01 00:08:39 +09:00
70988519df
Co-authored-by: Claire <claire.github-309c@sitedethib.com>
24 lines
578 B
TypeScript
24 lines
578 B
TypeScript
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();
|
|
};
|
|
};
|