Jdlrobson has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/236193

Change subject: Add abort method to mw.api
......................................................................

Add abort method to mw.api

The abort method allows you to cancel any requests currently pending
in the current class. Useful for cancelling no longer needed requests
due some user action (for example a user switching back to edit mode
from preview or a user cancelling multiple searches across different
lookup widgets)

Bug: T111245
Change-Id: Ie614b05fbfbddca38ea201e90053bebdd58da949
---
M resources/src/mediawiki.api/mediawiki.api.js
M tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js
2 files changed, 40 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/93/236193/1

diff --git a/resources/src/mediawiki.api/mediawiki.api.js 
b/resources/src/mediawiki.api/mediawiki.api.js
index cae65c5..136c433 100644
--- a/resources/src/mediawiki.api/mediawiki.api.js
+++ b/resources/src/mediawiki.api/mediawiki.api.js
@@ -79,9 +79,19 @@
                options.ajax = $.extend( {}, defaultOptions.ajax, options.ajax 
);
 
                this.defaults = options;
+               this.requests = [];
        };
 
        mw.Api.prototype = {
+               /**
+                * Abort all unfinished requests issued by this Api object.
+                * @method
+                */
+               abort: function () {
+                       $.each( this.requests, function ( index, request ) {
+                               request.abort();
+                       } );
+               },
 
                /**
                 * Perform API get request
@@ -222,6 +232,7 @@
                                        }
                                } );
 
+                       this.requests.push( xhr );
                        // Return the Promise
                        return apiDeferred.promise( { abort: xhr.abort } 
).fail( function ( code, details ) {
                                if ( !( code === 'http' && details && 
details.textStatus === 'abort' ) ) {
diff --git a/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js 
b/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js
index 26b6f57..478b6b9 100644
--- a/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js
+++ b/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js
@@ -346,4 +346,33 @@
                                assert.deepEqual( data, { example: { value: 'B' 
} } );
                        } );
        } );
+
+       QUnit.module( 'mediawiki.api (2)', {
+               setup: function () {
+                       var self = this,
+                               requests = this.requests = [];
+                       this.api = new mw.Api();
+                       this.sandbox.stub( jQuery, 'ajax', function () {
+                               var request = $.extend( {
+                                       abort: self.sandbox.spy()
+                               }, $.Deferred() );
+                               requests.push( request );
+                               return request;
+                       } );
+               }
+       } );
+
+       QUnit.test( '#abort', 3, function ( assert ) {
+               this.api.get( {
+                       a: 1
+               } );
+               this.api.post( {
+                       b: 2
+               } );
+               this.api.abort();
+               assert.ok( this.requests.length === 2, 'Check both requests 
triggered' );
+               $.each( this.requests, function ( i, request ) {
+                       assert.ok( request.abort.calledOnce, 'abort request 
number ' + i );
+               } );
+       } );
 }( mediaWiki ) );

-- 
To view, visit https://gerrit.wikimedia.org/r/236193
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie614b05fbfbddca38ea201e90053bebdd58da949
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Jdlrobson <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to