mediawiki.api: Don't hold references to finished requests forever
authorBartosz Dziewoński <matma.rex@gmail.com>
Tue, 20 Oct 2015 22:50:20 +0000 (00:50 +0200)
committerBartosz Dziewoński <matma.rex@gmail.com>
Wed, 21 Oct 2015 19:30:16 +0000 (21:30 +0200)
Follow-up to Ie614b05fbfbddca38ea201e90053bebdd58da949.

Also removed a TODO comment which no longers makes sense. (We can't
share API objects because users could abort each others' requests.)

Change-Id: I106139d6e463d7e5e3473035835eb74b18829b87

resources/src/mediawiki/api.js

index 481bd89..5f82b18 100644 (file)
@@ -67,7 +67,6 @@
         *  each individual request by passing them to #get or #post (or directly #ajax) later on.
         */
        mw.Api = function ( options ) {
-               // TODO: Share API objects with exact same config.
                options = options || {};
 
                // Force a string if we got a mw.Uri object
@@ -90,7 +89,9 @@
                 */
                abort: function () {
                        $.each( this.requests, function ( index, request ) {
-                               request.abort();
+                               if ( request ) {
+                                       request.abort();
+                               }
                        } );
                },
 
                 *  Fail: Error code
                 */
                ajax: function ( parameters, ajaxOptions ) {
-                       var token,
+                       var token, requestIndex,
+                               api = this,
                                apiDeferred = $.Deferred(),
                                xhr, key, formData;
 
                                        }
                                } );
 
+                       requestIndex = this.requests.length;
                        this.requests.push( xhr );
+                       xhr.always( function () {
+                               api.requests[ requestIndex ] = null;
+                       } );
                        // Return the Promise
                        return apiDeferred.promise( { abort: xhr.abort } ).fail( function ( code, details ) {
                                if ( !( code === 'http' && details && details.textStatus === 'abort' ) ) {