From 036cb99775511000ae584418d6344d8d0d046566 Mon Sep 17 00:00:00 2001 From: WMDE-Fisch Date: Mon, 16 Dec 2024 13:36:38 +0100 Subject: [PATCH] [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 --- .../ext.cite.referencePreviews/constants.js | 4 -- .../createReferenceGateway.js | 2 +- modules/ext.cite.referencePreviews/index.js | 61 ++++++++++++++----- .../isReferencePreviewsEnabled.js | 29 --------- src/Hooks/CiteHooks.php | 2 - 5 files changed, 47 insertions(+), 51 deletions(-) delete mode 100644 modules/ext.cite.referencePreviews/constants.js delete mode 100644 modules/ext.cite.referencePreviews/isReferencePreviewsEnabled.js diff --git a/modules/ext.cite.referencePreviews/constants.js b/modules/ext.cite.referencePreviews/constants.js deleted file mode 100644 index 8d0d2cd7f..000000000 --- a/modules/ext.cite.referencePreviews/constants.js +++ /dev/null @@ -1,4 +0,0 @@ -module.exports = { - TYPE_REFERENCE: 'reference', - FETCH_DELAY_REFERENCE_TYPE: 150 -}; diff --git a/modules/ext.cite.referencePreviews/createReferenceGateway.js b/modules/ext.cite.referencePreviews/createReferenceGateway.js index 8d165c26b..4a53ee2e9 100644 --- a/modules/ext.cite.referencePreviews/createReferenceGateway.js +++ b/modules/ext.cite.referencePreviews/createReferenceGateway.js @@ -2,7 +2,7 @@ * @module gateway/reference */ -const { TYPE_REFERENCE } = require( './constants.js' ); +const TYPE_REFERENCE = 'reference'; /** * @return {Gateway} diff --git a/modules/ext.cite.referencePreviews/index.js b/modules/ext.cite.referencePreviews/index.js index 7b2fbc109..1099ab287 100644 --- a/modules/ext.cite.referencePreviews/index.js +++ b/modules/ext.cite.referencePreviews/index.js @@ -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 } }; } diff --git a/modules/ext.cite.referencePreviews/isReferencePreviewsEnabled.js b/modules/ext.cite.referencePreviews/isReferencePreviewsEnabled.js deleted file mode 100644 index 2c22ce404..000000000 --- a/modules/ext.cite.referencePreviews/isReferencePreviewsEnabled.js +++ /dev/null @@ -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; diff --git a/src/Hooks/CiteHooks.php b/src/Hooks/CiteHooks.php index 5e35a9c61..87f78f57d 100644 --- a/src/Hooks/CiteHooks.php +++ b/src/Hooks/CiteHooks.php @@ -125,10 +125,8 @@ class CiteHooks implements ], 'packageFiles' => [ 'index.js', - 'constants.js', 'createReferenceGateway.js', 'createReferencePreview.js', - 'isReferencePreviewsEnabled.js', 'referencePreviewsInstrumentation.js' ] ]