[refactor] Merge some ReferencePreviews code

It's not making much sense to have the things merged in seprate
files IMO. I'll try to merge what's needed for the init to see
what could be delayed and lazy loaded.

Bug: T361670
Change-Id: Ia0bc9b68bbd9cfeed385520a868fe8726f7aa872
This commit is contained in:
WMDE-Fisch 2024-12-16 13:36:38 +01:00
parent c2dc9ca49d
commit 036cb99775
5 changed files with 47 additions and 51 deletions

View file

@ -1,4 +0,0 @@
module.exports = {
TYPE_REFERENCE: 'reference',
FETCH_DELAY_REFERENCE_TYPE: 150
};

View file

@ -2,7 +2,7 @@
* @module gateway/reference
*/
const { TYPE_REFERENCE } = require( './constants.js' );
const TYPE_REFERENCE = 'reference';
/**
* @return {Gateway}

View file

@ -1,15 +1,36 @@
const isReferencePreviewsEnabled = require( './isReferencePreviewsEnabled.js' );
const { initReferencePreviewsInstrumentation, LOGGING_SCHEMA } = require( './referencePreviewsInstrumentation.js' );
const createReferenceGateway = require( './createReferenceGateway.js' );
const renderFn = require( './createReferencePreview.js' );
const { TYPE_REFERENCE, FETCH_DELAY_REFERENCE_TYPE } = require( './constants.js' );
const createReferencePreview = require( './createReferencePreview.js' );
const TYPE_REFERENCE = 'reference';
/**
* Given the global state of the application, creates a function that gets
* whether or not the user should have Reference Previews enabled.
*
* @param {mw.user} user The `mw.user` singleton instance
* @param {Function} isPreviewTypeEnabled check whether preview has been disabled or enabled.
* @param {mw.Map} config
*
* @return {boolean|null} Null when there is no way the popup type can be enabled at run-time.
* @memberof module:ext.cite.referencePreviews
*/
function isReferencePreviewsEnabled( user, isPreviewTypeEnabled, config ) {
if ( !config.get( 'wgCiteReferencePreviewsActive' ) ) {
return null;
}
if ( user.isAnon() ) {
return isPreviewTypeEnabled( TYPE_REFERENCE );
}
return true;
}
const referencePreviewsState = isReferencePreviewsEnabled(
mw.user,
mw.popups.isEnabled,
mw.config
);
const gateway = createReferenceGateway();
// For tracking baseline stats in the Cite extension https://phabricator.wikimedia.org/T353798
// FIXME: This might be obsolete when the code moves to the Cite extension and the tracking there
@ -22,22 +43,32 @@ mw.trackSubscribe( 'Popups.SettingChange', ( data ) => {
}
} );
module.exports = referencePreviewsState !== null ? {
type: TYPE_REFERENCE,
selector: '#mw-content-text .reference a[ href*="#" ]',
delay: FETCH_DELAY_REFERENCE_TYPE,
gateway,
renderFn,
init: () => {
initReferencePreviewsInstrumentation();
}
} : null;
/**
* Create the relevant config to register the preview type in the Popups extension.
*
* @see mw.popups.register()
* @memberof module:ext.cite.referencePreviews
*/
function createReferencePreviewsType() {
return {
type: TYPE_REFERENCE,
selector: '#mw-content-text .reference a[ href*="#" ]',
delay: 150,
gateway: createReferenceGateway(),
renderFn: createReferencePreview,
init: () => {
initReferencePreviewsInstrumentation();
}
};
}
module.exports = referencePreviewsState !== null ? createReferencePreviewsType() : null;
// Expose private methods for QUnit tests
if ( typeof QUnit !== 'undefined' ) {
module.exports = { private: {
createReferenceGateway: require( './createReferenceGateway.js' ),
createReferencePreview: require( './createReferencePreview.js' ),
isReferencePreviewsEnabled: require( './isReferencePreviewsEnabled.js' )
isReferencePreviewsEnabled
} };
}

View file

@ -1,29 +0,0 @@
const { TYPE_REFERENCE } = require( './constants.js' );
/**
* @module isReferencePreviewsEnabled
*/
/**
* Given the global state of the application, creates a function that gets
* whether or not the user should have Reference Previews enabled.
*
* @param {mw.user} user The `mw.user` singleton instance
* @param {Function} isPreviewTypeEnabled check whether preview has been disabled or enabled.
* @param {mw.Map} config
*
* @return {boolean|null} Null when there is no way the popup type can be enabled at run-time.
*/
function isReferencePreviewsEnabled( user, isPreviewTypeEnabled, config ) {
if ( !config.get( 'wgCiteReferencePreviewsActive' ) ) {
return null;
}
if ( user.isAnon() ) {
return isPreviewTypeEnabled( TYPE_REFERENCE );
}
return true;
}
module.exports = isReferencePreviewsEnabled;

View file

@ -125,10 +125,8 @@ class CiteHooks implements
],
'packageFiles' => [
'index.js',
'constants.js',
'createReferenceGateway.js',
'createReferencePreview.js',
'isReferencePreviewsEnabled.js',
'referencePreviewsInstrumentation.js'
]
]