Edit stash: Simplify with postWithToken
authorMatt Russell <public@matt-russell.com>
Mon, 27 Jun 2016 08:14:30 +0000 (18:14 +1000)
committerDerk-Jan Hartman <hartman.wiki@gmail.com>
Sun, 25 Dec 2016 13:11:52 +0000 (14:11 +0100)
The original aborting issue in T102863 was fixed in 4a61bb8fb318.

Also moved early return to the top and added early return if the form is
missing, as well as moving code from the useless `onFormLoaded` function into
the main body.

Change-Id: Ie05d267f19ac2801754860fca1cec677167cd5d8

resources/src/mediawiki.action/mediawiki.action.edit.stash.js

index c8d3fad..7e966ea 100644 (file)
@@ -11,7 +11,7 @@
                var idleTimeout = 3000,
                        api = new mw.Api(),
                        timer,
-                       pending,
+                       stashReq,
                        lastText,
                        lastSummary,
                        lastTextHash,
                        PRIORITY_LOW = 1,
                        PRIORITY_HIGH = 2;
 
+               // We don't attempt to stash new section edits because in such cases the parser output
+               // varies on the edit summary (since it determines the new section's name).
+               if ( !$form.length || section === 'new' ) {
+                       return;
+               }
+
                // Send a request to stash the edit to the API.
                // If a request is in progress, abort it since its payload is stale and the API
                // may limit concurrent stash parses.
                function stashEdit() {
-                       api.getToken( 'csrf' ).then( function ( token ) {
-                               var req, params,
-                                       textChanged = isTextChanged(),
-                                       priority = textChanged ? PRIORITY_HIGH : PRIORITY_LOW;
-
-                               if ( pending ) {
-                                       if ( lastPriority > priority ) {
-                                               // Stash request for summary change should wait on pending text change stash
-                                               pending.then( checkStash );
-                                               return;
-                                       }
-                                       pending.abort();
+                       var req, params,
+                               textChanged = isTextChanged(),
+                               priority = textChanged ? PRIORITY_HIGH : PRIORITY_LOW;
+
+                       if ( stashReq ) {
+                               if ( lastPriority > priority ) {
+                                       // Stash request for summary change should wait on pending text change stash
+                                       stashReq.then( checkStash );
+                                       return;
                                }
+                               stashReq.abort();
+                       }
 
-                               // Update the "last" tracking variables
-                               lastSummary = $summary.textSelection( 'getContents' );
-                               lastPriority = priority;
-                               if ( textChanged ) {
-                                       lastText = $text.textSelection( 'getContents' );
-                                       // Reset hash
-                                       lastTextHash = null;
-                               }
+                       // Update the "last" tracking variables
+                       lastSummary = $summary.textSelection( 'getContents' );
+                       lastPriority = priority;
+                       if ( textChanged ) {
+                               lastText = $text.textSelection( 'getContents' );
+                               // Reset hash
+                               lastTextHash = null;
+                       }
+
+                       params = {
+                               action: 'stashedit',
+                               title: mw.config.get( 'wgPageName' ),
+                               section: section,
+                               sectiontitle: '',
+                               summary: lastSummary,
+                               contentmodel: model,
+                               contentformat: format,
+                               baserevid: revId
+                       };
+                       if ( lastTextHash ) {
+                               params.stashedtexthash = lastTextHash;
+                       } else {
+                               params.text = lastText;
+                       }
 
-                               params = {
-                                       action: 'stashedit',
-                                       token: token,
-                                       title: mw.config.get( 'wgPageName' ),
-                                       section: section,
-                                       sectiontitle: '',
-                                       summary: lastSummary,
-                                       contentmodel: model,
-                                       contentformat: format,
-                                       baserevid: revId
-                               };
-                               if ( lastTextHash ) {
-                                       params.stashedtexthash = lastTextHash;
+                       req = api.postWithToken( 'csrf', params );
+                       stashReq = req;
+                       req.then( function ( data ) {
+                               if ( req === stashReq ) {
+                                       stashReq = null;
+                               }
+                               if ( data.stashedit && data.stashedit.texthash ) {
+                                       lastTextHash = data.stashedit.texthash;
                                } else {
-                                       params.text = lastText;
+                                       // Request failed or text hash expired;
+                                       // include the text in a future stash request.
+                                       lastTextHash = null;
                                }
-
-                               req = api.post( params );
-                               pending = req;
-                               req.then( function ( data ) {
-                                       if ( req === pending ) {
-                                               pending = null;
-                                       }
-                                       if ( data.stashedit && data.stashedit.texthash ) {
-                                               lastTextHash = data.stashedit.texthash;
-                                       } else {
-                                               // Request failed or text hash expired;
-                                               // include the text in a future stash request.
-                                               lastTextHash = null;
-                                       }
-                               } );
                        } );
                }
 
                        idleTimeout = 3000;
                }
 
-               function onFormLoaded() {
-                       if (
-                               // Reverts may involve use (undo) links; stash as they review the diff.
-                               // Since the form has a pre-filled summary, stash the edit immediately.
-                               mw.util.getParamValue( 'undo' ) !== null ||
-                               // Pressing "show changes" and "preview" also signify that the user will
-                               // probably save the page soon
-                               $.inArray( $form.find( '#mw-edit-mode' ).val(), [ 'preview', 'diff' ] ) > -1
-                       ) {
-                               checkStash();
-                       }
-               }
-
-               // We don't attempt to stash new section edits because in such cases the parser output
-               // varies on the edit summary (since it determines the new section's name).
-               if ( $form.find( 'input[name=wpSection]' ).val() === 'new' ) {
-                       return;
-               }
-
                $text.on( {
-                       change: checkStash,
                        keyup: onKeyUp,
-                       focus: onTextFocus
+                       focus: onTextFocus,
+                       change: checkStash
                } );
                $summary.on( {
+                       keyup: onKeyUp,
                        focus: onSummaryFocus,
-                       focusout: checkStash,
-                       keyup: onKeyUp
+                       focusout: checkStash
                } );
-               onFormLoaded();
+
+               if (
+                       // Reverts may involve use (undo) links; stash as they review the diff.
+                       // Since the form has a pre-filled summary, stash the edit immediately.
+                       mw.util.getParamValue( 'undo' ) !== null ||
+                       // Pressing "show changes" and "preview" also signify that the user will
+                       // probably save the page soon
+                       $.inArray( $form.find( '#mw-edit-mode' ).val(), [ 'preview', 'diff' ] ) > -1
+               ) {
+                       checkStash();
+               }
        } );
 }( mediaWiki, jQuery ) );