Search code examples
angularngrx

can ngrx store.dispatch be throttled / debounced or can ngrx actions be throttled / debounced?


I am using ngrx 6 with angular 6

In my ngrx store devtools I see that one of my actions is firing more often than I want. It's not breaking the app, its just cluttering the devtools output.

I am dispatching the action with:

this.store.dispatch(new RecentSearchesRequestedAction())

How can I best debounce / throttle this so that it does not dispatch more than once per second for example? Does ngrx have a built in solution? I know I could spend time working out why the action is firing so often but I don't really have time for that. What's best practice to achieve this? Thanks


Solution

  • The best solution would be to debounce whatever is calling this.store.dispatch, which would be outside ngrx.

    You can throttle the output side of ngrx, for example:

    this.store.select((x) => x.property)
    .pipe(
        debounceTime(1000)
    ).subscribe((v) => console.log(v));
    

    ... would log the value of x.property at most once per second. But to stop the inputs, you need to throttle before the calls are made to dispatch.