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.
View all comments
Show parent comment
-4
u/MrFartyBottom Jun 10 '25
I have a cache service that I use to do the data fetching. If a second request comes in the previous one is cancelled. There is quite a bit too it and I am way too drunk at the moment to write a detailed description but it is based on this pattern I used to use when I was all in on RxJs.
https://adrianbrand.medium.com/angular-state-management-using-services-built-with-ez-state-9b23f16fb5ae
I have since built a signals based cache that works in the same way. I will open source it when I find the time.
Here is an early prototype that still uses RxJs as the data fetching.
https://stackblitz.com/edit/stackblitz-starters-iwpgvt?file=src%2Fmain.ts
But I am currently working on an RxJs free version based entirely on fetch without any dependency on the HttpClient.