nvisionative / nvQuickComponents

nvQuickComponents is a collection of versatile web components, based 100% on web standards, to help developers break free from framework churn.
GNU General Public License v3.0
12 stars 5 forks source link

[AutoComplete] Race Condition on Remote Long Queries #10

Open SkyeHoefling opened 5 years ago

SkyeHoefling commented 5 years ago

If you are using the autocomplete control with a remote endpoint to populate the results there can be a race condition if it takes your result some time to respond. See the example below with steps to reproduce

  1. Start typing into auto complete (the first char or first 2 char may be an expensive query)
  2. Continue typing until you get to 3 or 4+ chars where the query is much faster
  3. Wait

Expected Result

Actual Result

Root Cause There is a race condition happening with the getRemoteResults invocation which overrides results. We should add some code to the callback (maybe promise handling) to drop requests if they are not the latest.

The problem has to do with this invocation and callback

getRemoteResults(input, (remoteItemsSource) => {
    let results:HTMLElement[] = [];
    for (let index in remoteItemsSource) {
        let currentElement:HTMLElement = buildAutoCompleteItem(remoteItemsSource[index]);
        results.push(currentElement);
    }

    hydrateAutocomplete(results);
});
SkyeHoefling commented 5 years ago

I have a fix for this in https://github.com/HoeflingSoftware/nvQuickComponents/tree/autocomplete-race-condition.

Once #9 is merged I will create a PR for this since it is dependent on that code change

david-poindexter commented 5 years ago

9 was merged. With this PR go ahead and include the build so we will have a working /dist folder. Then we can circle back to #3 and set a path forward.