Add abort method to mw.api
authorjdlrobson <jdlrobson@gmail.com>
Fri, 4 Sep 2015 18:08:14 +0000 (11:08 -0700)
committerBartosz Dziewoński <matma.rex@gmail.com>
Fri, 9 Oct 2015 12:58:34 +0000 (14:58 +0200)
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

resources/src/mediawiki/api.js
tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js

index 43b20b8..79aba77 100644 (file)
                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
                                        }
                                } );
 
+                       this.requests.push( xhr );
                        // Return the Promise
                        return apiDeferred.promise( { abort: xhr.abort } ).fail( function ( code, details ) {
                                if ( !( code === 'http' && details && details.textStatus === 'abort' ) ) {
index 8033458..56a346f 100644 (file)
@@ -1,4 +1,4 @@
-( function ( mw ) {
+( function ( mw, $ ) {
        QUnit.module( 'mediawiki.api', QUnit.newMwEnvironment( {
                setup: function () {
                        this.server = this.sandbox.useFakeServer();
                                assert.deepEqual( data, { example: { value: 'B' } } );
                        } );
        } );
-}( mediaWiki ) );
+
+       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, jQuery ) );