Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Debounce user input to avoid repeated Ajax requests

    redux-observableredux-observable

    When accepting user input and converting it into a request that talks to the outside world, we can often end up in a situation where responses are no longer relevant. This is a common case in type-ahead style input fields - we would only want to call an external search API after the user has stopped typing for a short amount of time. In this lesson we’ll see how we can begin to combine RxJS operators to solve such problems.

    Code

    Code

    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: In this application, if I type a single character, it makes a network request to an API. It returns some data. We display it on the screen, which all looks good.

    If I type "skull," it gives us one search result, but we had an additional four requests that we didn't need to make. Why is this happening? Back in the component, this input field has an unchanged handler that for every change will call our search function.

    This is an action creator that we have bound using React Redux. It ends up producing an action of type search.

    The way our epic works right now is that for every single action that comes through of this type, it makes it through to this switchMap, which will keep executing this function, returning no observables, and unsubscribing to previous ones until we get to the point that one completes and we end up with the search results here.

    This can end up flooding a backend with API requests that are not really needed. Instead, what we'd like to do is wait for a small window of time where the user has stopped typing, then go off and make a network request there.

    As they start typing again, we may want to cancel. The point is we shouldn't be making any requests at all if they are typing really quickly. This type of operation is what RxJS was designed for.

    This particular problem can be solved with something called debounceTime. We'll import this from rxjs/operators. You see we place it directly after the OfType filter. This will handle filtering the action stream to only include actions of type search.

    Then this will subscribe to that new stream, but it will have an internal timer. Every time it sees a value, it will reset its timer. The timer's based on the number we give here. It's in milliseconds.

    What will happen here is if we only search for a single character, the element will make it into this observable. After 500 milliseconds, we'll make it down here.

    If another action makes it through here before our internal timer has elapsed and debounceTime resets, it drops the previous value. It waits to see if 500 milliseconds passes again.

    This is what's known as a lossy strategy, meaning we're not aggregating anything. We're not keeping a buffer of any previous values. We're just waiting until this window of time has passed where there have been no events.

    Only at that point we then emit the previous event that we saw. That's it. With a single line, we can go back to the browser. We can try the same experiment again. We try and search for "skull." You can see only got a single network request.