truckingsim / Ajax-Bootstrap-Select

This uses the bootstrap-select plugin and extends it so that you can use a remote source to search.
MIT License
280 stars 97 forks source link

Cancel the previous request if another one has been sent. #198

Open radekg12 opened 3 years ago

radekg12 commented 3 years ago

Please add the option to cancel the previous request, if it is in the Pending status, and in the meantime, another request has been sent (similar to switchMap in RxJS).

Problem: Now, when I send the request, the text "Searching ..." is displayed, which is correct. However, when I enter a few more letters while I haven't received the response for my first request yet, a bug may appear. The results from the first request will be displayed while the second request will be in the Pending status and there is no information about it. "Searching ..." word is not visible on the Select element. This is especially problematic if you have to wait a long time for a response. Additionally, there may be a potential race condition of requests.

Example (race condition): Example, when I type ABC the request with query param q=ABC, will be sent. Meanwhile, after we paused for a bit (more than the debounce time), we decide to type in another letter (the letter D) our app sends the request to the server with ABCD string. The results for string ABCD was cached so server replies very quickly. A few seconds later, however, the server finally replies with the response for the ABC string and overwriting results for string ABCD.

Diagram to illustrate the issue: // A1: Request for ABC // A2: Response for ABC // B1: Request for ABCD // B2: Response for ABCD

--A1----------A2--> ------B1--B2------>

Nitrodist commented 3 years ago

FYI this is called debouncing