MCPcopy Index your code
hub / github.com/angular/angular / Debouncer

Class Debouncer

devtools/projects/ng-devtools/src/lib/shared/utils/debouncer.ts:10–53  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8
9/** Debounces a callback/handler. */
10export class Debouncer {
11 private handle?: number;
12 private initialized: boolean = false;
13
14 /**
15 * Debounces the wrapped handler by a provided timeout.
16 *
17 * Example:
18 *
19 * ```typescript
20 * // Non-debounced
21 * api((args) => { ... });
22 *
23 * // Debounced
24 * const d = new Debouncer();
25 * api(d.debounce(
26 * (args) => { ... },
27 * 1000
28 * ));
29 * ```
30 *
31 * @param handler
32 * @param timeout
33 * @returns A debounced handler
34 */
35 debounce<T extends (...args: any[]) => void>(handler: T, timeout: number): T {
36 if (this.initialized) {
37 throw new Error('The debouncer is already initialized and running.');
38 }
39 this.initialized = true;
40
41 return ((...lastArgs: any[]) => {
42 this.cancel();
43 this.handle = setTimeout(() => handler(...lastArgs), timeout);
44 }) as T;
45 }
46
47 /** Cancel the currently debounced handler, if not resolved. */
48 cancel() {
49 if (this.handle) {
50 clearTimeout(this.handle);
51 }
52 }
53}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…