1
0
mirror of https://github.com/funamitech/mastodon synced 2024-12-18 00:29:13 +09:00
YuruToot/app/javascript/flavours/glitch/utils/debounce.ts

24 lines
585 B
TypeScript
Raw Normal View History

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();
};
};