Create controller.getApi method with defaults

Only share API objects when it is safe to do so.

Change-Id: I4810f59ce41c43f78e0b42db6b36620b2aca1cec
This commit is contained in:
Ed Sanders 2020-10-23 12:02:18 +01:00
parent 9add814c4f
commit 3b31aa669d
4 changed files with 30 additions and 18 deletions

View file

@ -1,5 +1,4 @@
var
api = new mw.Api( { parameters: { formatversion: 2 } } ),
controller = require( './controller.js' ),
modifier = require( './modifier.js' ),
logger = require( './logger.js' ),
@ -48,7 +47,7 @@ OO.initClass( CommentController );
* @return {jQuery.Promise}
*/
function getLatestRevId( pageName ) {
return api.get( {
return controller.getApi().get( {
action: 'query',
prop: 'revisions',
rvprop: 'ids',
@ -155,7 +154,7 @@ CommentController.prototype.setup = function ( mode ) {
commentController.teardown();
OO.ui.alert(
code instanceof Error ? code.toString() : api.getErrorMessage( data ),
code instanceof Error ? code.toString() : controller.getApi().getErrorMessage( data ),
{ size: 'medium' }
);
@ -242,7 +241,8 @@ CommentController.prototype.save = function ( comment, pageName ) {
commentController = this;
return this.replyWidget.checkboxesPromise.then( function ( checkboxes ) {
var captchaInput = commentController.replyWidget.captchaInput,
var defaults, noTimeoutApi,
captchaInput = commentController.replyWidget.captchaInput,
data = {
action: 'discussiontoolsedit',
paction: 'addcomment',
@ -251,6 +251,7 @@ CommentController.prototype.save = function ( comment, pageName ) {
summary: replyWidget.getEditSummary(),
assert: mw.user.isAnon() ? 'anon' : 'user',
assertuser: mw.user.getName() || undefined,
uselang: mw.config.get( 'wgUserLanguage' ),
dttags: [
'discussiontools',
'discussiontools-reply',
@ -275,13 +276,14 @@ CommentController.prototype.save = function ( comment, pageName ) {
'unwatch';
}
// No timeout. Huge talk pages can take a long time to save, and falsely reporting an error
// could result in duplicate messages if the user retries. (T249071)
defaults = OO.copy( controller.getApi().defaults );
defaults.timeout = 0;
noTimeoutApi = new mw.Api( defaults );
return mw.libs.ve.targetSaver.postContent(
data,
{
// No timeout. Huge talk pages take a long time to save, and falsely reporting an error can
// result in duplicate messages when the user retries. (T249071)
api: new mw.Api( { ajax: { timeout: 0 }, parameters: { formatversion: 2 } } )
}
data, { api: noTimeoutApi }
).catch( function ( code, responseData ) {
// Better user-facing error messages
if ( code === 'editconflict' ) {
@ -371,7 +373,7 @@ CommentController.prototype.switchToVisual = function () {
} ).join( '\n' );
// Based on ve.init.mw.Target#parseWikitextFragment
parsePromise = api.post( {
parsePromise = controller.getApi().post( {
action: 'visualeditor',
paction: 'parsefragment',
page: oldWidget.pageName,

View file

@ -1,7 +1,6 @@
'use strict';
var
api = new mw.Api( { parameters: { formatversion: 2 } } ),
$pageContainer,
Parser = require( './Parser.js' ),
ThreadItem = require( './ThreadItem.js' ),
@ -10,6 +9,10 @@ var
mw.messages.set( require( './controller/contLangMessages.json' ) );
function getApi() {
return new mw.Api( { parameters: { formatversion: 2 } } );
}
function highlight( comment ) {
var padding = 5,
// $container must be position:relative/absolute
@ -47,7 +50,9 @@ function highlight( comment ) {
* @return {jQuery.Promise}
*/
function getPageData( pageName, oldId ) {
var lintPromise, transcludedFromPromise, veMetadataPromise;
var lintPromise, transcludedFromPromise, veMetadataPromise,
api = getApi();
pageDataCache[ pageName ] = pageDataCache[ pageName ] || {};
if ( pageDataCache[ pageName ][ oldId ] ) {
return pageDataCache[ pageName ][ oldId ];
@ -252,6 +257,7 @@ function init( $container, state ) {
function update( data, comment, pageName, replyWidget ) {
var watch,
api = getApi(),
pageUpdated = $.Deferred();
// We posted a new comment, clear the cache, because wgCurRevisionId will not change if we posted
@ -288,6 +294,7 @@ function update( data, comment, pageName, replyWidget ) {
// HACK: 'useskin' triggers a different code path that runs our OutputPageBeforeHTML hook,
// adding our reply links in the HTML (T266195)
useskin: mw.config.get( 'skin' ),
uselang: mw.config.get( 'wgUserLanguage' ),
prop: [ 'text', 'modules', 'jsconfigvars' ],
page: mw.config.get( 'wgRelevantPageName' )
} );
@ -348,5 +355,6 @@ module.exports = {
init: init,
update: update,
checkCommentOnPage: checkCommentOnPage,
getCheckboxesPromise: getCheckboxesPromise
getCheckboxesPromise: getCheckboxesPromise,
getApi: getApi
};

View file

@ -4,6 +4,8 @@
* @copyright 2011-2019 VisualEditor Team and others; see http://ve.mit-license.org
*/
var controller = require( 'ext.discussionTools.init' ).controller;
/**
* MWUsernameCompletionAction action.
*
@ -20,7 +22,8 @@ function MWUsernameCompletionAction( surface ) {
// Parent constructor
MWUsernameCompletionAction.super.call( this, surface );
this.api = new mw.Api( { parameters: { formatversion: 2 } } );
// Shared API object so previous requests can be aborted
this.api = controller.getApi();
this.searchedPrefixes = {};
this.localUsers = [];
this.ipUsers = [];

View file

@ -157,7 +157,6 @@ function ReplyWidget( commentController, comment, pageName, oldId, config ) {
this.editSummaryInput.connect( this, { change: 'onEditSummaryChange' } );
this.editSummaryInput.$input.on( 'keydown', this.onKeyDown.bind( this, false ) );
this.api = new mw.Api( { parameters: { formatversion: 2 } } );
this.onInputChangeThrottled = OO.ui.throttle( this.onInputChange.bind( this ), 1000 );
// Initialization
@ -275,7 +274,7 @@ ReplyWidget.prototype.setPending = function ( pending ) {
};
ReplyWidget.prototype.saveEditMode = function ( mode ) {
this.api.saveOption( 'discussiontools-editmode', mode ).then( function () {
controller.getApi().saveOption( 'discussiontools-editmode', mode ).then( function () {
mw.user.options.set( 'discussiontools-editmode', mode );
} );
};
@ -523,7 +522,7 @@ ReplyWidget.prototype.preparePreview = function ( wikitext ) {
wikitext = wikitext + '<span style="opacity: 0.6;">' + mw.msg( 'discussiontools-signature-prefix' ) + '~~~~</span>';
}
wikitext = indent + wikitext.replace( /\n/g, '\n' + indent );
this.previewRequest = parsePromise = this.api.post( {
this.previewRequest = parsePromise = controller.getApi().post( {
action: 'parse',
text: wikitext,
pst: true,