Created
December 20, 2025 21:01
-
-
Save vs-borodin/7f4b356e0e59eb1549235356cde5c617 to your computer and use it in GitHub Desktop.
debounced
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { | |
| assertInInjectionContext, | |
| assertNotInReactiveContext, | |
| inject, | |
| INJECTOR, | |
| type Injector, | |
| type Signal, | |
| } from '@angular/core'; | |
| import { toObservable, toSignal } from '@angular/core/rxjs-interop'; | |
| import { debounceTime } from 'rxjs'; | |
| export function debounced<T>( | |
| signal: Signal<T>, | |
| options: DebouncedOptions = { time: 300 } | |
| ): Signal<T> { | |
| if (ngDevMode) { | |
| assertNotInReactiveContext(debounced); | |
| } | |
| if (ngDevMode && !options.injector) { | |
| assertInInjectionContext(debounced); | |
| } | |
| const injector = options.injector || inject(INJECTOR); | |
| return toSignal(toObservable(signal, { injector }).pipe(debounceTime(options.time)), { | |
| initialValue: signal(), | |
| injector, | |
| }); | |
| } | |
| export interface DebouncedOptions { | |
| readonly time: number; | |
| readonly injector?: Injector; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment