From: Matt Russell Date: Thu, 30 Jun 2016 06:57:24 +0000 (+1000) Subject: mediawiki.api: Don't abort token requests in postWithToken() X-Git-Tag: 1.31.0-rc.0~6438^2 X-Git-Url: https://git.cyclocoop.org/%28%28?a=commitdiff_plain;h=c0eea2ba5716ffcbbeb6532827c9caf3af7f46bd;p=lhc%2Fweb%2Fwiklou.git mediawiki.api: Don't abort token requests in postWithToken() 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 --- diff --git a/resources/src/mediawiki/api.js b/resources/src/mediawiki/api.js index 3e010d0d45..a8ee4c7a82 100644 --- a/resources/src/mediawiki/api.js +++ b/resources/src/mediawiki/api.js @@ -289,10 +289,20 @@ */ 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, @@ -302,9 +312,14 @@ 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 ) ); } ); } @@ -313,7 +328,11 @@ } ); } ).promise( { abort: function () { - abortable.abort(); + if ( abortable ) { + abortable.abort(); + } else { + aborted = true; + } } } ); },