Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error calculating rendered variable in async function. Succesive calls to this funcion not appending the suggestions: #249

Open
xelloss1012 opened this issue Sep 28, 2021 · 0 comments

Comments

@xelloss1012
Copy link

xelloss1012 commented Sep 28, 2021

Hello!,

I noticed an error in the async function when calculating the value for the rendered variable... This makes succesive calls to this funcion not appending the suggestions:

 function async(suggestions) {
     suggestions = suggestions || [];
     if (!canceled && rendered < that.limit) {
         that.cancel = $.noop;
         var idx = Math.abs(rendered - that.limit);
         rendered += idx;
         that._append(query, suggestions.slice(0, idx));
         that.async && that.trigger("asyncReceived", query, that.name);
     }
 }

This way, the result of rendered - limit it's always added to rendered, so rendered will always end up having the value of limit!... For example, let's say we have already rendered 10 suggestions in a previous call to async, and that the limit is set to 1000. Then idx will be calculated as 1000-10=990... and thus when adding this amount to rendered (whose value is 10) the result will be again 1000 (the limit). This would make the next call to async to be over the limit and the new suggestions will not be added.

In my Iocal file I have solved it this way:

function async(suggestions) {
    suggestions = suggestions || [];
    if (!canceled && rendered < that.limit) {
        that.cancel = $.noop;
        var idx = Math.min(Math.abs(rendered - that.limit), suggestions.length);
        rendered += idx;
        that._append(query, suggestions.slice(0, idx));
        that.async && that.trigger("asyncReceived", query, that.name);
    }
}

It is as simple as applying a Math.max between the result of rendered - limit and suggestion.length.

Hope this helps someone.

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant