|
| 1 | +declaremodule'use-debounce'{ |
| 2 | +declaretypeCallOptions={| |
| 3 | +/** |
| 4 | + * Controls if the function should be invoked on the leading edge of the timeout. |
| 5 | + */ |
| 6 | +leading?:boolean, |
| 7 | +/** |
| 8 | + * Controls if the function should be invoked on the trailing edge of the timeout. |
| 9 | + */ |
| 10 | +trailing?:boolean, |
| 11 | +|}; |
| 12 | + |
| 13 | +declaretypeOptions={| |
| 14 | + ...CallOptions, |
| 15 | +/** |
| 16 | + * The maximum time the given function is allowed to be delayed before it's invoked. |
| 17 | + */ |
| 18 | +maxWait?:number, |
| 19 | +|}; |
| 20 | + |
| 21 | +declaretypeControlFunctions={| |
| 22 | +/** |
| 23 | + * Cancel pending function invocations |
| 24 | + */ |
| 25 | +cancel:()=>void, |
| 26 | +/** |
| 27 | + * Immediately invoke pending function invocations |
| 28 | + */ |
| 29 | +flush:()=>void, |
| 30 | +/** |
| 31 | + * Returns `true` if there are any pending function invocations |
| 32 | + */ |
| 33 | +isPending:()=>boolean, |
| 34 | +|}; |
| 35 | + |
| 36 | +/** |
| 37 | + * Subsequent calls to the debounced function `debounced.callback` return the result of the last func invocation. |
| 38 | + * Note, that if there are no previous invocations it's mean you will get undefined. You should check it in your code properly. |
| 39 | + */ |
| 40 | +declaretypeDebouncedState<R,T:(...args:any)=>R>={| |
| 41 | + ...ControlFunctions, |
| 42 | +(...args:Parameters<T>):R|void, |
| 43 | +|}; |
| 44 | + |
| 45 | +declaremodule.exports:{| |
| 46 | +useDebounce:<T>(value: T, delay: number, options?:{| |
| 47 | +maxWait?:number, |
| 48 | +leading?:boolean, |
| 49 | +trailing?:boolean, |
| 50 | +equalityFn?:(left:T,right:T)=>boolean, |
| 51 | +|}) =>[T,DebouncedState<void,(value:T)=>void>], |
| 52 | + /** |
| 53 | + * Creates a debounced function that delays invoking `func` until after `wait` |
| 54 | + * milliseconds have elapsed since the last time the debounced function was |
| 55 | + * invoked, or until the next browser frame is drawn. |
| 56 | + * |
| 57 | + * The debounced function comes with a `cancel` method to cancel delayed `func` |
| 58 | + * invocations and a `flush` method to immediately invoke them. |
| 59 | + * |
| 60 | + * Provide `options` to indicate whether `func` should be invoked on the leading |
| 61 | + * and/or trailing edge of the `wait` timeout. The `func` is invoked with the |
| 62 | + * last arguments provided to the debounced function. |
| 63 | + * |
| 64 | + * Subsequent calls to the debounced function return the result of the last |
| 65 | + * `func` invocation. |
| 66 | + * |
| 67 | + * **Note:** If `leading` and `trailing` options are `true`, `func` is |
| 68 | + * invoked on the trailing edge of the timeout only if the debounced function |
| 69 | + * is invoked more than once during the `wait` timeout. |
| 70 | + * |
| 71 | + * If `wait` is `0` and `leading` is `false`, `func` invocation is deferred |
| 72 | + * until the next tick, similar to `setTimeout` with a timeout of `0`. |
| 73 | + * |
| 74 | + * If `wait` is omitted in an environment with `requestAnimationFrame`, `func` |
| 75 | + * invocation will be deferred until the next frame is drawn (typically about |
| 76 | + * 16ms). |
| 77 | + * |
| 78 | + * See [David Corbacho's article](https://css-tricks.com/debouncing-throttling-explained-examples/) |
| 79 | + * for details over the differences between `debounce` and `throttle`. |
| 80 | + * |
| 81 | + * @category Function |
| 82 | + * @param{Function} func The function to debounce. |
| 83 | + * @param{number} [wait=0] |
| 84 | + * The number of milliseconds to delay; if omitted, `requestAnimationFrame` is |
| 85 | + * used (if available, otherwise it will be setTimeout(...,0)). |
| 86 | + * @param{Object} [options={}] The options object. |
| 87 | + * Controls if `func` should be invoked on the leading edge of the timeout. |
| 88 | + * @param{boolean} [options.leading=false] |
| 89 | + * The maximum time `func` is allowed to be delayed before it's invoked. |
| 90 | + * @param{number} [options.maxWait] |
| 91 | + * Controls if `func` should be invoked the trailing edge of the timeout. |
| 92 | + * @param{boolean} [options.trailing=true] |
| 93 | + * @returns{Function} Returns the new debounced function. |
| 94 | + * @example |
| 95 | + * |
| 96 | + * // Avoid costly calculations while the window size is in flux. |
| 97 | + * const resizeHandler = useDebouncedCallback(calculateLayout, 150); |
| 98 | + * window.addEventListener('resize', resizeHandler) |
| 99 | + * |
| 100 | + * // Invoke `sendMail` when clicked, debouncing subsequent calls. |
| 101 | + * const clickHandler = useDebouncedCallback(sendMail, 300,{ |
| 102 | +*leading:true, |
| 103 | +*trailing:false, |
| 104 | +*}) |
| 105 | + *<buttononClick={clickHandler}>click me</button> |
| 106 | + * |
| 107 | + * // Ensure `batchLog` is invoked once after 1 second of debounced calls. |
| 108 | + * const debounced = useDebouncedCallback(batchLog, 250,{'maxWait':1000}) |
| 109 | + * const source = new EventSource('/stream') |
| 110 | + * source.addEventListener('message', debounced) |
| 111 | + * |
| 112 | + * // Cancel the trailing debounced invocation. |
| 113 | + * window.addEventListener('popstate', debounced.cancel) |
| 114 | + * |
| 115 | + * // Check for pending invocations. |
| 116 | + * const status = debounced.pending() ? "Pending..." : "Ready" |
| 117 | + */ |
| 118 | + useDebouncedCallback:<R,T:(...args:any)=>R>( |
| 119 | + func: T, wait?: number, options?: Options, |
| 120 | + ) =>DebouncedState<R,T>, |
| 121 | + /** |
| 122 | + * Creates a throttled function that only invokes `func` at most once per |
| 123 | + * every `wait` milliseconds (or once per browser frame). |
| 124 | + * |
| 125 | + * The throttled function comes with a `cancel` method to cancel delayed `func` |
| 126 | + * invocations and a `flush` method to immediately invoke them. |
| 127 | + * |
| 128 | + * Provide `options` to indicate whether `func` should be invoked on the leading |
| 129 | + * and/or trailing edge of the `wait` timeout. The `func` is invoked with the |
| 130 | + * last arguments provided to the throttled function. |
| 131 | + * |
| 132 | + * Subsequent calls to the throttled function return the result of the last |
| 133 | + * `func` invocation. |
| 134 | + * |
| 135 | + * **Note:** If `leading` and `trailing` options are `true`, `func` is |
| 136 | + * invoked on the trailing edge of the timeout only if the throttled function |
| 137 | + * is invoked more than once during the `wait` timeout. |
| 138 | + * |
| 139 | + * If `wait` is `0` and `leading` is `false`, `func` invocation is deferred |
| 140 | + * until the next tick, similar to `setTimeout` with a timeout of `0`. |
| 141 | + * |
| 142 | + * If `wait` is omitted in an environment with `requestAnimationFrame`, `func` |
| 143 | + * invocation will be deferred until the next frame is drawn (typically about |
| 144 | + * 16ms). |
| 145 | + * |
| 146 | + * See [David Corbacho's article](https://css-tricks.com/debouncing-throttling-explained-examples/) |
| 147 | + * for details over the differences between `throttle` and `debounce`. |
| 148 | + * |
| 149 | + * @category Function |
| 150 | + * @param{Function} func The function to throttle. |
| 151 | + * @param{number} [wait=0] |
| 152 | + * The number of milliseconds to throttle invocations to; if omitted, |
| 153 | + * `requestAnimationFrame` is used (if available, otherwise it will be setTimeout(...,0)). |
| 154 | + * @param{Object} [options={}] The options object. |
| 155 | + * @param{boolean} [options.leading=true] |
| 156 | + * Specify invoking on the leading edge of the timeout. |
| 157 | + * @param{boolean} [options.trailing=true] |
| 158 | + * Specify invoking on the trailing edge of the timeout. |
| 159 | + * @returns{Function} Returns the new throttled function. |
| 160 | + * @example |
| 161 | + * |
| 162 | + * // Avoid excessively updating the position while scrolling. |
| 163 | + * const scrollHandler = useThrottledCallback(updatePosition, 100) |
| 164 | + * window.addEventListener('scroll', scrollHandler) |
| 165 | + * |
| 166 | + * // Invoke `renewToken` when the click event is fired, but not more than once every 5 minutes. |
| 167 | + * const{callback} = useThrottledCallback(renewToken, 300000,{'trailing':false}) |
| 168 | + *<buttononClick={callback}>click</button> |
| 169 | + * |
| 170 | + * // Cancel the trailing throttled invocation. |
| 171 | + * window.addEventListener('popstate', throttled.cancel); |
| 172 | + */ |
| 173 | + useThrottledCallback:<R,T:(...args:any)=>R>( |
| 174 | + func: T, wait: number, options?: CallOptions, |
| 175 | + ) =>DebouncedState<R,T>, |
| 176 | + |}; |
| 177 | +} |