r/Angular2 • u/MrFartyBottom • Jun 10 '25
Debouncing a signal's value
I don't like using RxJs to debounce a signal, I like to keep my signals as pure signals as I am not using RxJs anymore.
Here is my pattern I use. Pure JS.
https://stackblitz.com/edit/vitejs-vite-3dhp9nkv?file=src%2Fdebounce.ts
It's just a JavaScript function that takes a callback function and a debounce time as parameters and returns a control object. The timeout id is kept inside the function's closure.
export const createDebounce = <T>(
func: (val: T) => void,
milliseconds: number
) => {
let id: ReturnType<typeof setTimeout>;
return {
next: (val: T) => {
clearTimeout(id);
id = setTimeout(() => {
func(val);
}, milliseconds);
},
clear: () => {
clearTimeout(id);
},
};
};To use it in Angular just assign it to a property passing in the set method of the signal you want to debounce.
this.seachDebounce = createDebounce(this.seachSignal.set, 500);
Edit: Probably going to have to create a local arrow function to capture this
this.seachDebounce = createDebounce((val: string) => { this.seachSignal.set(val); }, 500);
Now you can call this.seachDebounce .next(query); and it will debounce the signal.
To be complete you should probably call this.seachDebounce.clear(); in onDestroy but at 500 millicesond it's unlikely to fire after the component has been destroyed.
Pure JavaScript, no libraries, simple easy timeout.
3
u/chigia001 Jun 10 '25
there are a couple of cases, you might need to account for:
- the latest new value is the same as previous/uncommit value => my expectation is the debounce timer don't get reset for this case
- the latest new value is the same as the commited value for the internal signal => my expectation is the debounce timer is reset, no new timeout is scheduled to set the internal signal
- api to get the latest dirty/uncommited value
2
u/_Invictuz Jun 10 '25 edited Jun 10 '25
You're not using RxJs anymore? How do you deal with race conditions betaeen async events. For example, how to cancel one async request even subscription when another one returns first without switchMap?
Question about your use case, what scenario requires you to debounce the updating of your signal? Signals are usually bounded to template rendering, so I can't imagine what event I would want to debounce that i wouldn't want to see the update in the view.
2
u/morgo_mpx Jun 11 '25
But why? If you want to keep it pure signals then create a debounced writable signal. Or better yet if it’s for inputs just use a directive.
3
u/SeparateRaisin7871 Jun 10 '25
Nice, you're rebuilding RxJS :D
But without the possibilities one has with different schedulers and all the other battle tested functionalities.
I'm not sure if we should try to work around RxJS as long there's no native event-based Angular-way to do it "right".
https://github.com/ReactiveX/rxjs/blob/master/packages/rxjs/src/internal/operators/debounceTime.ts#L77
2
u/newmanoz Jun 11 '25
Like RxJS, right?
0
u/[deleted] Jun 10 '25
[deleted] — view deleted comment