import type { DebounceSettings } from '@dura/types' ; import debounce from 'lodash.debounce' ; export function dispatchDebounce ( cache , type , debounceSettings , fn ) { const debounceKey = `${ type }/debounce` ; const clearKey = `${ debounceKey }/clear` ; const has = cache . has ( debounceKey ) ; const wait = typeof debounceSettings === 'number' ? debounceSettings : debounceSettings . wait ; const debounceFn = typeof debounceSettings === 'number' ? debounce ( fn , debounceSettings ) : debounce ( fn , debounceSettings . wait , debounceSettings ) ; function resetClearTime ( ) { cache . get ( clearKey ) && clearTimeout ( cache . get ( clearKey ) ) ; const timeId = setTimeout ( ( ) => { cache . delete ( debounceKey ) ; cache . delete ( clearKey ) ; } , wait + 10 ) ; cache . set ( clearKey , timeId ) ; } if ( has ) { resetClearTime ( ) ; return cache . get ( debounceKey ) ; } cache . set ( debounceKey , debounceFn ) ; resetClearTime ( ) ; return debounceFn ; }