TitleSearchWidget: Only update results if the query is current
authorDavid Lynch <dlynch@wikimedia.org>
Tue, 10 Nov 2015 23:59:27 +0000 (15:59 -0800)
committerDavid Lynch <dlynch@wikimedia.org>
Wed, 11 Nov 2015 01:31:35 +0000 (17:31 -0800)
With near-simultaneous inputs (e.g. holding down the backspace key
while deleting text) you can cause the suggestion promise to resolve
out-of-order. This causes apparently incorrect suggestions to be
displayed for the current input.

To fix this, abort the existing promise if it exists.

Bug: T114178
Change-Id: I9332452fd914b54e7c564284da2a8a00865ae806

resources/src/mediawiki.widgets/mw.widgets.TitleSearchWidget.js

index 0e2546f..546fbf8 100644 (file)
        mw.widgets.TitleSearchWidget.prototype.onQueryChange = function () {
                var widget = this;
 
-               this.getSuggestionsPromise().done( function ( response ) {
+               if ( this.currentRequest ) {
+                       this.currentRequest.abort();
+               }
+
+               this.currentRequest = this.getSuggestionsPromise();
+               this.currentRequest.done( function ( response ) {
                        // Parent method
                        mw.widgets.TitleSearchWidget.parent.prototype.onQueryChange.call( widget );
 
                        widget.results.addItems( widget.getOptionsFromData( response.query || {} ) );
+
+                       widget.currentRequest = false;
                } );
        };