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

View file

@ -1,7 +1,6 @@
'use strict'; 'use strict';
var var
api = new mw.Api( { parameters: { formatversion: 2 } } ),
$pageContainer, $pageContainer,
Parser = require( './Parser.js' ), Parser = require( './Parser.js' ),
ThreadItem = require( './ThreadItem.js' ), ThreadItem = require( './ThreadItem.js' ),
@ -10,6 +9,10 @@ var
mw.messages.set( require( './controller/contLangMessages.json' ) ); mw.messages.set( require( './controller/contLangMessages.json' ) );
function getApi() {
return new mw.Api( { parameters: { formatversion: 2 } } );
}
function highlight( comment ) { function highlight( comment ) {
var padding = 5, var padding = 5,
// $container must be position:relative/absolute // $container must be position:relative/absolute
@ -47,7 +50,9 @@ function highlight( comment ) {
* @return {jQuery.Promise} * @return {jQuery.Promise}
*/ */
function getPageData( pageName, oldId ) { function getPageData( pageName, oldId ) {
var lintPromise, transcludedFromPromise, veMetadataPromise; var lintPromise, transcludedFromPromise, veMetadataPromise,
api = getApi();
pageDataCache[ pageName ] = pageDataCache[ pageName ] || {}; pageDataCache[ pageName ] = pageDataCache[ pageName ] || {};
if ( pageDataCache[ pageName ][ oldId ] ) { if ( pageDataCache[ pageName ][ oldId ] ) {
return pageDataCache[ pageName ][ oldId ]; return pageDataCache[ pageName ][ oldId ];
@ -252,6 +257,7 @@ function init( $container, state ) {
function update( data, comment, pageName, replyWidget ) { function update( data, comment, pageName, replyWidget ) {
var watch, var watch,
api = getApi(),
pageUpdated = $.Deferred(); pageUpdated = $.Deferred();
// We posted a new comment, clear the cache, because wgCurRevisionId will not change if we posted // 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, // HACK: 'useskin' triggers a different code path that runs our OutputPageBeforeHTML hook,
// adding our reply links in the HTML (T266195) // adding our reply links in the HTML (T266195)
useskin: mw.config.get( 'skin' ), useskin: mw.config.get( 'skin' ),
uselang: mw.config.get( 'wgUserLanguage' ),
prop: [ 'text', 'modules', 'jsconfigvars' ], prop: [ 'text', 'modules', 'jsconfigvars' ],
page: mw.config.get( 'wgRelevantPageName' ) page: mw.config.get( 'wgRelevantPageName' )
} ); } );
@ -348,5 +355,6 @@ module.exports = {
init: init, init: init,
update: update, update: update,
checkCommentOnPage: checkCommentOnPage, 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 * @copyright 2011-2019 VisualEditor Team and others; see http://ve.mit-license.org
*/ */
var controller = require( 'ext.discussionTools.init' ).controller;
/** /**
* MWUsernameCompletionAction action. * MWUsernameCompletionAction action.
* *
@ -20,7 +22,8 @@ function MWUsernameCompletionAction( surface ) {
// Parent constructor // Parent constructor
MWUsernameCompletionAction.super.call( this, surface ); 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.searchedPrefixes = {};
this.localUsers = []; this.localUsers = [];
this.ipUsers = []; this.ipUsers = [];

View file

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