Skip to content

Commit

Permalink
Merge pull request #1675 from b1ink0/fix/activating-multiple-features…
Browse files Browse the repository at this point in the history
…-fails-activation

Activating multiple features / plugins sequentially fails to activate some features / plugins
  • Loading branch information
felixarntz authored Nov 19, 2024
2 parents 20a038f + 9e98fca commit d4f0938
Showing 1 changed file with 39 additions and 13 deletions.
52 changes: 39 additions & 13 deletions plugins/performance-lab/includes/admin/plugin-activate-ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,21 @@
const { i18n, a11y, apiFetch } = wp;
const { __ } = i18n;

// Queue to hold pending activation requests.
const activationQueue = [];
let isProcessingActivation = false;

/**
* Handles click events on elements with the class 'perflab-install-active-plugin'.
*
* This asynchronous function listens for click events on the document and executes
* the provided callback function if triggered.
* Enqueues plugin activation requests and starts processing if not already in progress.
*
* @param {MouseEvent} event - The click event object that is triggered when the user clicks on the document.
*
* @return {Promise<void>} The asynchronous function returns a promise that resolves to void.
* @param {MouseEvent} event - The click event object.
*/
async function handlePluginActivationClick( event ) {
const target = /** @type {HTMLElement} */ ( event.target );

function enqueuePluginActivation( event ) {
// Prevent the default link behavior.
event.preventDefault();

const target = /** @type {HTMLElement} */ ( event.target );

if (
target.classList.contains( 'updating-message' ) ||
target.classList.contains( 'disabled' )
Expand All @@ -31,12 +30,37 @@
}

target.classList.add( 'updating-message' );
target.textContent = __( 'Waiting…', 'performance-lab' );

const pluginSlug = target.dataset.pluginSlug;

activationQueue.push( { target, pluginSlug } );

// Start processing the queue if not already doing so.
if ( ! isProcessingActivation ) {
handlePluginActivation();
}
}

/**
* Handles activation of feature/plugin using queue based approach.
*
* @return {Promise<void>} The asynchronous function returns a promise that resolves to void.
*/
async function handlePluginActivation() {
if ( 0 === activationQueue.length ) {
isProcessingActivation = false;
return;
}

isProcessingActivation = true;

const { target, pluginSlug } = activationQueue.shift();

target.textContent = __( 'Activating…', 'performance-lab' );

a11y.speak( __( 'Activating…', 'performance-lab' ) );

const pluginSlug = target.dataset.pluginSlug;

try {
// Activate the plugin/feature via the REST API.
await apiFetch( {
Expand Down Expand Up @@ -76,13 +100,15 @@

target.classList.remove( 'updating-message' );
target.textContent = __( 'Activate', 'performance-lab' );
} finally {
handlePluginActivation();
}
}

// Attach the event listeners.
document
.querySelectorAll( '.perflab-install-active-plugin' )
.forEach( ( item ) => {
item.addEventListener( 'click', handlePluginActivationClick );
item.addEventListener( 'click', enqueuePluginActivation );
} );
} )();

0 comments on commit d4f0938

Please sign in to comment.