/
githubmirror
/
angular
Обзор
Документация
Войти
/
githubmirror
/
angular
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
devtools/projects/ng-devtools/src/lib/shared/utils/debouncer.ts
53 строки
1 KB
hawkgs
refactor(devtools): use a ResizeObserver for the responsive split (#61525)
12 июн 2025, 11:08
12 июн 2025, 11:08
966f96c
Код
Авторство
О чём код?
/** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.dev/license */ /** Debounces a callback/handler. */ export class Debouncer { private handle?: number; private initialized: boolean = false; /** * Debounces the wrapped handler by a provided timeout. * * Example: * * ```typescript * // Non-debounced * api((args) => { ... }); * * // Debounced * const d = new Debouncer(); * api(d.debounce( * (args) => { ... }, * 1000 * )); * ``` * * @param handler * @param timeout * @returns A debounced handler */ debounce<T extends (...args: any[]) => void>(handler: T, timeout: number): T { if (this.initialized) { throw new Error('The debouncer is already initialized and running.'); } this.initialized = true; return ((...lastArgs: any[]) => { this.cancel(); this.handle = setTimeout(() => handler(...lastArgs), timeout); }) as T; } /** Cancel the currently debounced handler, if not resolved. */ cancel() { if (this.handle) { clearTimeout(this.handle); } } }