mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2025-01-08 03:04:21 +00:00
Use abort signals in mw.Api code
Depends-On: Iec338e9f595b452c19ce8e74eb81339fbce11640 Change-Id: Ia803b4eab766768c2c8a096c308958b48eb34af2
This commit is contained in:
parent
ebd9610900
commit
584137bb07
|
@ -107,25 +107,14 @@ ve.ce.MWSignatureNode.prototype.onTeardown = function () {
|
||||||
*/
|
*/
|
||||||
ve.ce.MWSignatureNode.prototype.generateContents = function () {
|
ve.ce.MWSignatureNode.prototype.generateContents = function () {
|
||||||
const doc = this.getModel().getDocument();
|
const doc = this.getModel().getDocument();
|
||||||
let abortable, aborted;
|
const api = ve.init.target.getContentApi( doc );
|
||||||
const abortedPromise = ve.createDeferred().reject( 'http',
|
const ajaxOptions = {};
|
||||||
{ textStatus: 'abort', exception: 'abort' } ).promise();
|
const abortable = api.makeAbortablePromise( ajaxOptions );
|
||||||
|
|
||||||
function abort() {
|
|
||||||
aborted = true;
|
|
||||||
if ( abortable && abortable.abort ) {
|
|
||||||
abortable.abort();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Acquire a temporary user username before previewing, so that signatures
|
// Acquire a temporary user username before previewing, so that signatures
|
||||||
// display the temp user instead of IP user. (T331397)
|
// display the temp user instead of IP user. (T331397)
|
||||||
return mw.user.acquireTempUserName()
|
return mw.user.acquireTempUserName()
|
||||||
.then( () => {
|
.then( () => {
|
||||||
if ( aborted ) {
|
|
||||||
return abortedPromise;
|
|
||||||
}
|
|
||||||
|
|
||||||
// We must have only one top-level node, this is the easiest way.
|
// We must have only one top-level node, this is the easiest way.
|
||||||
const wikitext = '<span>~~~~</span>';
|
const wikitext = '<span>~~~~</span>';
|
||||||
|
|
||||||
|
@ -133,35 +122,29 @@ ve.ce.MWSignatureNode.prototype.generateContents = function () {
|
||||||
// meta attributes (that may or may not be required).
|
// 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
|
// We could try hacking up one (or even both) of these, but just calling the two parsers
|
||||||
// in order seems slightly saner.
|
// in order seems slightly saner.
|
||||||
return ( abortable = ve.init.target.getContentApi( doc ).post( {
|
return api.post( {
|
||||||
action: 'parse',
|
action: 'parse',
|
||||||
text: wikitext,
|
text: wikitext,
|
||||||
contentmodel: 'wikitext',
|
contentmodel: 'wikitext',
|
||||||
prop: 'text',
|
prop: 'text',
|
||||||
onlypst: true
|
onlypst: true
|
||||||
} ) );
|
}, ajaxOptions );
|
||||||
} )
|
} )
|
||||||
.then( ( pstResponse ) => {
|
.then( ( pstResponse ) => {
|
||||||
if ( aborted ) {
|
|
||||||
return abortedPromise;
|
|
||||||
}
|
|
||||||
const wikitext = ve.getProp( pstResponse, 'parse', 'text' );
|
const wikitext = ve.getProp( pstResponse, 'parse', 'text' );
|
||||||
if ( !wikitext ) {
|
if ( !wikitext ) {
|
||||||
return ve.createDeferred().reject();
|
return ve.createDeferred().reject();
|
||||||
}
|
}
|
||||||
return ( abortable = ve.init.target.parseWikitextFragment( wikitext, true, doc ) );
|
return ve.init.target.parseWikitextFragment( wikitext, true, doc, ajaxOptions );
|
||||||
} )
|
} )
|
||||||
.then( ( parseResponse ) => {
|
.then( ( parseResponse ) => {
|
||||||
if ( aborted ) {
|
|
||||||
return abortedPromise;
|
|
||||||
}
|
|
||||||
if ( ve.getProp( parseResponse, 'visualeditor', 'result' ) !== 'success' ) {
|
if ( ve.getProp( parseResponse, 'visualeditor', 'result' ) !== 'success' ) {
|
||||||
return ve.createDeferred().reject();
|
return ve.createDeferred().reject();
|
||||||
}
|
}
|
||||||
// Simplified case of template rendering, don't need to worry about filtering etc
|
// Simplified case of template rendering, don't need to worry about filtering etc
|
||||||
return $( parseResponse.visualeditor.content ).contents().toArray();
|
return $( parseResponse.visualeditor.content ).contents().toArray();
|
||||||
} )
|
} )
|
||||||
.promise( { abort: abort } );
|
.promise( abortable );
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Registration */
|
/* Registration */
|
||||||
|
|
|
@ -601,19 +601,13 @@ ve.init.mw.Target.prototype.getWikitextFragment = function ( doc, useRevision )
|
||||||
* @param {string} wikitext
|
* @param {string} wikitext
|
||||||
* @param {boolean} pst Perform pre-save transform
|
* @param {boolean} pst Perform pre-save transform
|
||||||
* @param {ve.dm.Document} [doc] Parse for a specific document, defaults to current surface's
|
* @param {ve.dm.Document} [doc] Parse for a specific document, defaults to current surface's
|
||||||
|
* @param {Object} [ajaxOptions]
|
||||||
* @return {jQuery.Promise} Abortable promise
|
* @return {jQuery.Promise} Abortable promise
|
||||||
*/
|
*/
|
||||||
ve.init.mw.Target.prototype.parseWikitextFragment = function ( wikitext, pst, doc ) {
|
ve.init.mw.Target.prototype.parseWikitextFragment = function ( wikitext, pst, doc, ajaxOptions ) {
|
||||||
let abortable, aborted;
|
const api = this.getContentApi( doc );
|
||||||
const abortedPromise = ve.createDeferred().reject( 'http',
|
ajaxOptions = ajaxOptions || {};
|
||||||
{ textStatus: 'abort', exception: 'abort' } ).promise();
|
const abortable = api.makeAbortablePromise( ajaxOptions );
|
||||||
|
|
||||||
function abort() {
|
|
||||||
aborted = true;
|
|
||||||
if ( abortable && abortable.abort ) {
|
|
||||||
abortable.abort();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Acquire a temporary user username before previewing or diffing, so that signatures and
|
// 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)
|
// 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
|
return tempUserNamePromise
|
||||||
.then( () => {
|
.then( () => api.post( {
|
||||||
if ( aborted ) {
|
action: 'visualeditor',
|
||||||
return abortedPromise;
|
paction: 'parsefragment',
|
||||||
}
|
page: this.getPageName( doc ),
|
||||||
return ( abortable = this.getContentApi( doc ).post( {
|
wikitext: wikitext,
|
||||||
action: 'visualeditor',
|
pst: pst
|
||||||
paction: 'parsefragment',
|
}, ajaxOptions ) )
|
||||||
page: this.getPageName( doc ),
|
.promise( abortable );
|
||||||
wikitext: wikitext,
|
|
||||||
pst: pst
|
|
||||||
} ) );
|
|
||||||
} )
|
|
||||||
.promise( { abort: abort } );
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue