autocomplete icon indicating copy to clipboard operation
autocomplete copied to clipboard

debounce support?

Open gergelyke opened this issue 10 years ago • 17 comments

Hey,

do you plan to add it, or is it already possible?

gergelyke avatar Jan 19 '16 13:01 gergelyke

Hey @gergelyke,

Right now it doesn't support debounce. Could you share the use-case of introducing such debounce capability? One of the cool features of Algolia is its native support of prefix search; retrieving relevant results from the very 1st keystroke -> you shouldn't need to "wait" to have a few chars before calling the API to ensure the results are relevant.

redox avatar Jan 19 '16 14:01 redox

Hey @redox,

i get it - still, I'd like to add debounce, as I do believe firing up 10 parallel requests is not a good practice. If you do not want to add it, could you point me to the part of the project where it should be added?

Thanks a lot, Gergely

gergelyke avatar Jan 19 '16 15:01 gergelyke

Hey,

The old typeahead.js had Bloodhound which provided rateLimitBy (debounce by default, or throttle) and rateLimitWait (defaults to 300 miliseconds) for queries.

My current implementation with this plugin seems to be hammering the server with every key type (not using Algolia's service atm).

So basically:

  • No point in making requests when the user is still typing.
  • Not everybody use it with Algolia's service.

@gergelyke Did you find a neat solution for this?

I'd hate to switch back to the buggy and unmaintained typeahead.js just for this lonely albeit crucial feature.

kobykorman avatar Apr 20 '16 23:04 kobykorman

Hi, did you tried to debounce the source calls like this:

source: lodash.debounce(function(query, cb) {
  // do a query with algolia, then call
  cb(suggestions);
}, 200);

or

source: lodash.debounce(autocomplete.sources.hits, 200);

?

https://github.com/algolia/autocomplete.js#datasets

source is a simple function so maybe try that.

vvo avatar Apr 21 '16 08:04 vvo

@vvo's recommendation is probably the way to go 👍

redox avatar Apr 21 '16 08:04 redox

Thanks @vvo!

I didn't want to incorporate yet another dependency, so I eventually used a function:

var myObject.debounce = function(func, wait, immediate) {
    var timeout;
    return function() {
        var context = this, args = arguments;
        var later = function() {
            timeout = null;
            if (!immediate) func.apply(context, args);
        };
        var callNow = immediate && !timeout;
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
        if (callNow) func.apply(context, args);
    };
};

Which is, apparently, underscore.js' debounce.

And the implementation looks like this:

source: myObject.debounce(function (query, cb) {
    $.ajax(url,
            {
                data: {search: query},
                // spinner
            })
            .done(function (suggestions) {
                cb(suggestions);
            });
}, 300),

@redox - If my 2 cents are worth anything, I do want to express that this feature incorporated in the plugin would be greatly appreciated, as it seems integral to it, and implementers would have one less thing to worry about.

kobykorman avatar Apr 21 '16 11:04 kobykorman

+1 on baking this into autocomplete.js and thanks a lot you other guys for providing a custom fix!

@redox

Could you share the use-case of introducing such debounce capability? One of the cool features of Algolia is its native support of prefix search; retrieving relevant results from the very 1st keystroke -> you shouldn't need to "wait" to have a few chars before calling the API to ensure the results are relevant.

Our use case right now is to stop these warnings:

Your current plan (Essential) allows you to perform a maximum of 75 queries per second. Your application has reached 99 queries per second in the past 24 hours.

..which is a result of following the recommended convention of having multiple indexes, which again is what the WP plugin creates automatically for you.

I totally agree it's supercool to have the snappy search with no debounce, but that just dosn't mix well with your queries pr second policy

fractalf avatar Apr 26 '18 12:04 fractalf

Hey @fractalf,

thank you for sharing your concerns. Any chance you could use the solution provided by @legshooter ? In the meantime, I'm gonna try to bake it in the library (so the sources.hits) automatically.

redox avatar Apr 26 '18 12:04 redox

In the meantime, I'm gonna try to bake it in the library (so the sources.hits) automatically.

Arg, I might rather want to go a level deeper and try to debounce the source.

redox avatar Apr 26 '18 12:04 redox

@fractalf @legshooter what do you think about #226 ?

redox avatar Apr 26 '18 13:04 redox

@redox Cheers for grabbing this so quick! On first sight the PR looks good to me. I see you rewrote the underscore code a bit, but as long as you tested it, looks fine by me. I'm slightly confused by the lodash's way of handling this, looks way more complex https://github.com/lodash/lodash/blob/master/debounce.js

In the meantime, yes I use the quickfix by legshooter no problem :)

fractalf avatar Apr 27 '18 06:04 fractalf

@redox It's what I wanted all along 👍

As for the inner workings of debounce, I am also not sure about it, and currently using lodash's version.

kobykorman avatar Apr 27 '18 10:04 kobykorman

Implemented in 0.30.0

redox avatar Apr 30 '18 09:04 redox

@redox I think your implementation might not work as expected. I had to go into the logic details when I needed to create a simular custom debounce for vue-instantsearch.

Your code have no concept of immediate (look at underscore debounce function arguments and condition for this) which basically makes it fire a request right after first character is pressed and not wait for the timeout.

I ended up with an extremely easy custom solution which at least does what I want: "After any keypress, wait x ms before doing a request. If a new key is pressed before the request, reset the timeout and init a new"

Basically

                clearTimeout(this.debounceTimeout);
                this.debounceTimeout =  setTimeout(() => {
                    this.debounceTimeout = null;
                    doJob();
                }, this.debounceTime);

I dunno, I might miss out on something, but can it be so simple? :)

fractalf avatar May 02 '18 10:05 fractalf

That looks like that same as in the PR @fractalf, do you have a suggested diff?

Haroenv avatar May 02 '18 10:05 Haroenv

Nope it's not exactly the same, in the current implementation of the debounce we create a callNow variable that will always be true on the first render since we don't have a debounceTimeout yet. I think we can get rid of the value and the condition on callNow.

Note also that with the current implementation we don't always debounce the call. When the values are in the cache we don't wait, we render them immediately. It's maybe what we want but it can lead to some inconsistencies in the UI.

https://github.com/algolia/autocomplete.js/blob/48b597f68b7beb3afb4a703310813e13e28491f0/src/autocomplete/dataset.js#L212-L222

samouss avatar May 02 '18 10:05 samouss

@samouss Yes good to point that out, when a result is found in the cache it should definitely not be debounced.

The callNow only makes sense if you include the immediate argument (look underscore code), but in this case I think it's not necessary and even wrong because of first keypress always triggering a query

fractalf avatar May 03 '18 07:05 fractalf