Load Modules support initialization

Rather than obscurely loading instrumentation code as a side
effect of loading UI code, run it explicitly inside the index.js
initalization code. Instrumentation is moved to its own file
and Popups modules now support an init function.

Change-Id: I9d2643ec8fb4e1dedc7ab9534b2965272f12335f
This commit is contained in:
Jon Robson 2023-05-09 08:11:44 -07:00 committed by Jdlrobson
parent 12e823ddbf
commit d4376bf203
6 changed files with 34 additions and 18 deletions

Binary file not shown.

Binary file not shown.

View file

@ -29,6 +29,7 @@ import { previewTypes, getPreviewType,
import isReferencePreviewsEnabled from './isReferencePreviewsEnabled';
import setUserConfigFlags from './setUserConfigFlags';
import { registerGatewayForPreviewType, getGatewayForPreviewType } from './gateway';
import { initReferencePreviewsInstrumentation } from './instrumentation/referencePreviews';
const EXCLUDED_LINK_SELECTORS = [
'.extiw',
@ -236,7 +237,10 @@ function handleDOMEventIfEligible( handler ) {
type: previewTypes.TYPE_REFERENCE,
selector: '#mw-content-text .reference a[ href*="#" ]',
gateway: referenceGateway,
renderFn: createReferencePreview
renderFn: createReferencePreview,
init: () => {
initReferencePreviewsInstrumentation();
}
} );
}
if ( !isAnythingEligible() ) {

View file

@ -0,0 +1,21 @@
let isTracking = false;
export const LOGGING_SCHEMA = 'event.ReferencePreviewsPopups';
/**
* Run once the preview is initialized.
*/
export function initReferencePreviewsInstrumentation() {
if ( mw.config.get( 'wgPopupsReferencePreviews' ) &&
navigator.sendBeacon &&
mw.config.get( 'wgIsArticle' ) &&
!isTracking
) {
isTracking = true;
mw.track( LOGGING_SCHEMA, { action: 'pageview' } );
}
}
export function isTrackingEnabled() {
return isTracking;
}

View file

@ -57,7 +57,7 @@ export default function createMwPopups( store, registerModel, registerPreviewUI,
* @param {PopupModule} module
*/
register: function ( module ) {
const { type, selector, gateway, renderFn, subTypes } = module;
const { type, selector, gateway, renderFn, subTypes, init } = module;
if ( !type || !selector || !gateway ) {
throw new Error(
`Registration of Popups custom preview type "${type}" failed: You must specify a type, a selector, and a gateway.`
@ -71,6 +71,10 @@ export default function createMwPopups( store, registerModel, registerPreviewUI,
registerPreviewUI( subTypePreview.type, subTypePreview.renderFn );
} );
}
// Run initialization function if provided.
if ( typeof init === 'function' ) {
init();
}
}
};
}

View file

@ -1,7 +1,7 @@
/**
* @module referencePreview
*/
import { isTrackingEnabled, LOGGING_SCHEMA } from '../../../instrumentation/referencePreviews';
import { renderPopup } from '../popup/popup';
import { createNodeFromTemplate, escapeHTML } from '../templateUtil';
@ -22,19 +22,6 @@ const templateHTML = `
</footer>
</div>`;
const LOGGING_SCHEMA = 'event.ReferencePreviewsPopups';
let isTracking = false;
$( () => {
if ( mw.config.get( 'wgPopupsReferencePreviews' ) &&
navigator.sendBeacon &&
mw.config.get( 'wgIsArticle' ) &&
!isTracking
) {
isTracking = true;
mw.track( LOGGING_SCHEMA, { action: 'pageview' } );
}
} );
/**
* @param {ext.popups.ReferencePreviewModel} model
* @return {JQuery}
@ -108,7 +95,7 @@ export function renderReferencePreview(
$el.find( '.mwe-popups-container' ).addClass( 'footer-empty' );
}
if ( isTracking ) {
if ( isTrackingEnabled() ) {
$el.find( '.mw-parser-output' ).on( 'click', 'a', () => {
mw.track( LOGGING_SCHEMA, {
action: 'clickedReferencePreviewsContentLink'
@ -121,7 +108,7 @@ export function renderReferencePreview(
// We are dealing with floating point numbers here when the page is zoomed!
scrolledToBottom = element.scrollTop >= element.scrollHeight - element.clientHeight - 1;
if ( isTracking ) {
if ( isTrackingEnabled() ) {
if ( !element.isOpenRecorded ) {
mw.track( LOGGING_SCHEMA, {
action: 'poppedOpen',