import { DebouncedFunction , DebounceOptions } from './types' ; param fn param debounceTimeInMs param debounceOptions returns example public export const debounce = < Params extends any [ ] > ( fn , debounceTimeInMs , { leading = false , trailing = true } = { } ) : DebouncedFunction < Params > => { let timer ; const debouncedFn = ( ... args ) => { const isFirstLeadingCall = leading && ! timer ; if ( isFirstLeadingCall ) { fn ( ... args ) ; } const trailingFn = ( ) : void => { timer = undefined ; if ( ! isFirstLeadingCall && trailing ) { fn ( ... args ) ; } } ; if ( timer ) { clearTimeout ( timer ) ; } timer = setTimeout ( ( ) => trailingFn ( ) , debounceTimeInMs ) ; } ; debouncedFn . cancel = ( ) => { if ( timer ) { clearTimeout ( timer ) ; } } ; return debouncedFn ; } ;