mediawiki.api: Don't abort token requests in postWithToken()
authorMatt Russell <public@matt-russell.com>
Thu, 30 Jun 2016 06:57:24 +0000 (16:57 +1000)
committerMatt Russell <public@matt-russell.com>
Wed, 6 Jul 2016 02:12:24 +0000 (12:12 +1000)
Per Ie05d267f1.

Other requests can still make use of the token, even if this one won't, so
only abort the actual request if it is currently running, otherwise abort it
as soon as the token request finishes.

The only caveat with this method is an abort while a token request is running
isn't instantaneous, which perhaps isn't expected, but I doubt it matters.

Change-Id: Ib34e2e3a7f8ca5eb0832c4a6c1623a638381e6c8

resources/src/mediawiki/api.js

index 3e010d0..a8ee4c7 100644 (file)
                 */
                postWithToken: function ( tokenType, params, ajaxOptions ) {
                        var api = this,
-                               abortable;
+                               abortedPromise = $.Deferred().reject( 'http',
+                                       { textStatus: 'abort', exception: 'abort' } ).promise(),
+                               abortable,
+                               aborted;
 
-                       return ( abortable = api.getToken( tokenType, params.assert ) ).then( function ( token ) {
+                       return api.getToken( tokenType, params.assert ).then( function ( token ) {
                                params.token = token;
+                               // Request was aborted while token request was running, but we
+                               // don't want to unnecessarily abort token requests, so abort
+                               // a fake request instead
+                               if ( aborted ) {
+                                       return abortedPromise;
+                               }
+
                                return ( abortable = api.post( params, ajaxOptions ) ).then(
                                        // If no error, return to caller as-is
                                        null,
                                                        api.badToken( tokenType );
                                                        // Try again, once
                                                        params.token = undefined;
-                                                       return ( abortable = api.getToken( tokenType, params.assert ) ).then( function ( token ) {
+                                                       abortable = null;
+                                                       return api.getToken( tokenType, params.assert ).then( function ( token ) {
                                                                params.token = token;
-                                                               return ( abortable = api.post( params, ajaxOptions ) ).promise();
+                                                               if ( aborted ) {
+                                                                       return abortedPromise;
+                                                               }
+
+                                                               return ( abortable = api.post( params, ajaxOptions ) );
                                                        } );
                                                }
 
                                        }
                                );
                        } ).promise( { abort: function () {
-                               abortable.abort();
+                               if ( abortable ) {
+                                       abortable.abort();
+                               } else {
+                                       aborted = true;
+                               }
                        } } );
                },