| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | export type DebouncedFunction<Args extends Array<unknown>> = { |
| b69ab31 | | | 9 | (...args: Args): void; |
| b69ab31 | | | 10 | reset: () => void; |
| b69ab31 | | | 11 | isPending: () => boolean; |
| b69ab31 | | | 12 | dispose: () => void; |
| b69ab31 | | | 13 | }; |
| b69ab31 | | | 14 | |
| b69ab31 | | | 15 | /** |
| b69ab31 | | | 16 | * This is a rate limiting mechanism, used to invoke a function after a repeated |
| b69ab31 | | | 17 | * action has completed. This creates and returns a debounced version of |
| b69ab31 | | | 18 | * the function passed in that will postpone its execution until after `wait` |
| b69ab31 | | | 19 | * milliseconds have elapsed since the last time it was invoked. |
| b69ab31 | | | 20 | * |
| b69ab31 | | | 21 | * For example, if you wanted to update a preview after the user stops typing |
| b69ab31 | | | 22 | * you could do the following: |
| b69ab31 | | | 23 | * |
| b69ab31 | | | 24 | * elem.addEventListener('keyup', debounce(this.updatePreview, 250), false); |
| b69ab31 | | | 25 | * |
| b69ab31 | | | 26 | * The returned function has a reset method which can be called to cancel a |
| b69ab31 | | | 27 | * pending invocation. |
| b69ab31 | | | 28 | * |
| b69ab31 | | | 29 | * var debouncedUpdatePreview = debounce(this.updatePreview, 250); |
| b69ab31 | | | 30 | * elem.addEventListener('keyup', debouncedUpdatePreview, false); |
| b69ab31 | | | 31 | * |
| b69ab31 | | | 32 | * // later, to cancel pending calls |
| b69ab31 | | | 33 | * debouncedUpdatePreview.reset(); |
| b69ab31 | | | 34 | * |
| b69ab31 | | | 35 | * @param func - the function to debounce |
| b69ab31 | | | 36 | * @param wait - how long to wait in milliseconds |
| b69ab31 | | | 37 | * @param context - optional context to invoke the function in |
| b69ab31 | | | 38 | * @param leading - cause debounce to trigger the function on |
| b69ab31 | | | 39 | * the leading edge instead of the trailing edge of the wait interval |
| b69ab31 | | | 40 | */ |
| b69ab31 | | | 41 | export function debounce<Args extends Array<unknown>>( |
| b69ab31 | | | 42 | func: (...args: Args) => unknown, |
| b69ab31 | | | 43 | wait: number, |
| b69ab31 | | | 44 | context: unknown = undefined, |
| b69ab31 | | | 45 | leading = false, |
| b69ab31 | | | 46 | ): DebouncedFunction<Args> { |
| b69ab31 | | | 47 | let timeout: NodeJS.Timeout | undefined; |
| b69ab31 | | | 48 | let shouldCallLeading = true; |
| b69ab31 | | | 49 | |
| b69ab31 | | | 50 | function debouncer(...args: Args) { |
| b69ab31 | | | 51 | let callback: () => void; |
| b69ab31 | | | 52 | |
| b69ab31 | | | 53 | if (leading) { |
| b69ab31 | | | 54 | callback = function () { |
| b69ab31 | | | 55 | shouldCallLeading = true; |
| b69ab31 | | | 56 | clearTimeout(timeout); |
| b69ab31 | | | 57 | timeout = undefined; |
| b69ab31 | | | 58 | }; |
| b69ab31 | | | 59 | |
| b69ab31 | | | 60 | if (!shouldCallLeading) { |
| b69ab31 | | | 61 | clearTimeout(timeout); |
| b69ab31 | | | 62 | timeout = setTimeout(callback, wait); |
| b69ab31 | | | 63 | return; |
| b69ab31 | | | 64 | } |
| b69ab31 | | | 65 | |
| b69ab31 | | | 66 | shouldCallLeading = false; |
| b69ab31 | | | 67 | func.apply(context, args); |
| b69ab31 | | | 68 | } else { |
| b69ab31 | | | 69 | debouncer.reset(); |
| b69ab31 | | | 70 | callback = function () { |
| b69ab31 | | | 71 | clearTimeout(timeout); |
| b69ab31 | | | 72 | timeout = undefined; |
| b69ab31 | | | 73 | func.apply(context, args); |
| b69ab31 | | | 74 | }; |
| b69ab31 | | | 75 | } |
| b69ab31 | | | 76 | |
| b69ab31 | | | 77 | clearTimeout(timeout); |
| b69ab31 | | | 78 | timeout = setTimeout(callback, wait); |
| b69ab31 | | | 79 | } |
| b69ab31 | | | 80 | |
| b69ab31 | | | 81 | debouncer.reset = function () { |
| b69ab31 | | | 82 | clearTimeout(timeout); |
| b69ab31 | | | 83 | timeout = undefined; |
| b69ab31 | | | 84 | shouldCallLeading = true; |
| b69ab31 | | | 85 | }; |
| b69ab31 | | | 86 | |
| b69ab31 | | | 87 | debouncer.dispose = function () { |
| b69ab31 | | | 88 | clearTimeout(timeout); |
| b69ab31 | | | 89 | timeout = undefined; |
| b69ab31 | | | 90 | }; |
| b69ab31 | | | 91 | |
| b69ab31 | | | 92 | debouncer.isPending = function () { |
| b69ab31 | | | 93 | return timeout != null; |
| b69ab31 | | | 94 | }; |
| b69ab31 | | | 95 | |
| b69ab31 | | | 96 | return debouncer; |
| b69ab31 | | | 97 | } |