Use abort signals in mw.Api code

Depends-On: Iec338e9f595b452c19ce8e74eb81339fbce11640
Change-Id: Ia803b4eab766768c2c8a096c308958b48eb34af2
This commit is contained in:
Bartosz Dziewoński 2023-09-21 00:07:30 +02:00 committed by Bartosz Dziewoński
parent ebd9610900
commit 584137bb07
2 changed files with 20 additions and 48 deletions

View file

@ -107,25 +107,14 @@ ve.ce.MWSignatureNode.prototype.onTeardown = function () {
*/
ve.ce.MWSignatureNode.prototype.generateContents = function () {
const doc = this.getModel().getDocument();
let abortable, aborted;
const abortedPromise = ve.createDeferred().reject( 'http',
{ textStatus: 'abort', exception: 'abort' } ).promise();
function abort() {
aborted = true;
if ( abortable && abortable.abort ) {
abortable.abort();
}
}
const api = ve.init.target.getContentApi( doc );
const ajaxOptions = {};
const abortable = api.makeAbortablePromise( ajaxOptions );
// Acquire a temporary user username before previewing, so that signatures
// display the temp user instead of IP user. (T331397)
return mw.user.acquireTempUserName()
.then( () => {
if ( aborted ) {
return abortedPromise;
}
// We must have only one top-level node, this is the easiest way.
const wikitext = '<span>~~~~</span>';
@ -133,35 +122,29 @@ ve.ce.MWSignatureNode.prototype.generateContents = function () {
// meta attributes (that may or may not be required).
// We could try hacking up one (or even both) of these, but just calling the two parsers
// in order seems slightly saner.
return ( abortable = ve.init.target.getContentApi( doc ).post( {
return api.post( {
action: 'parse',
text: wikitext,
contentmodel: 'wikitext',
prop: 'text',
onlypst: true
} ) );
}, ajaxOptions );
} )
.then( ( pstResponse ) => {
if ( aborted ) {
return abortedPromise;
}
const wikitext = ve.getProp( pstResponse, 'parse', 'text' );
if ( !wikitext ) {
return ve.createDeferred().reject();
}
return ( abortable = ve.init.target.parseWikitextFragment( wikitext, true, doc ) );
return ve.init.target.parseWikitextFragment( wikitext, true, doc, ajaxOptions );
} )
.then( ( parseResponse ) => {
if ( aborted ) {
return abortedPromise;
}
if ( ve.getProp( parseResponse, 'visualeditor', 'result' ) !== 'success' ) {
return ve.createDeferred().reject();
}
// Simplified case of template rendering, don't need to worry about filtering etc
return $( parseResponse.visualeditor.content ).contents().toArray();
} )
.promise( { abort: abort } );
.promise( abortable );
};
/* Registration */

View file

@ -601,19 +601,13 @@ ve.init.mw.Target.prototype.getWikitextFragment = function ( doc, useRevision )
* @param {string} wikitext
* @param {boolean} pst Perform pre-save transform
* @param {ve.dm.Document} [doc] Parse for a specific document, defaults to current surface's
* @param {Object} [ajaxOptions]
* @return {jQuery.Promise} Abortable promise
*/
ve.init.mw.Target.prototype.parseWikitextFragment = function ( wikitext, pst, doc ) {
let abortable, aborted;
const abortedPromise = ve.createDeferred().reject( 'http',
{ textStatus: 'abort', exception: 'abort' } ).promise();
function abort() {
aborted = true;
if ( abortable && abortable.abort ) {
abortable.abort();
}
}
ve.init.mw.Target.prototype.parseWikitextFragment = function ( wikitext, pst, doc, ajaxOptions ) {
const api = this.getContentApi( doc );
ajaxOptions = ajaxOptions || {};
const abortable = api.makeAbortablePromise( ajaxOptions );
// Acquire a temporary user username before previewing or diffing, so that signatures and
// user-related magic words display the temp user instead of IP user in the preview. (T331397)
@ -625,19 +619,14 @@ ve.init.mw.Target.prototype.parseWikitextFragment = function ( wikitext, pst, do
}
return tempUserNamePromise
.then( () => {
if ( aborted ) {
return abortedPromise;
}
return ( abortable = this.getContentApi( doc ).post( {
action: 'visualeditor',
paction: 'parsefragment',
page: this.getPageName( doc ),
wikitext: wikitext,
pst: pst
} ) );
} )
.promise( { abort: abort } );
.then( () => api.post( {
action: 'visualeditor',
paction: 'parsefragment',
page: this.getPageName( doc ),
wikitext: wikitext,
pst: pst
}, ajaxOptions ) )
.promise( abortable );
};
/**