You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
23 lines
578 B
23 lines
578 B
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(); |
|
}; |
|
};
|
|
|