Skip to content

Commit d4f0938

Browse files
authored
Merge pull request #1675 from b1ink0/fix/activating-multiple-features-fails-activation
Activating multiple features / plugins sequentially fails to activate some features / plugins
2 parents 20a038f + 9e98fca commit d4f0938

File tree

1 file changed

+39
-13
lines changed

1 file changed

+39
-13
lines changed

plugins/performance-lab/includes/admin/plugin-activate-ajax.js

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,21 @@
77
const { i18n, a11y, apiFetch } = wp;
88
const { __ } = i18n;
99

10+
// Queue to hold pending activation requests.
11+
const activationQueue = [];
12+
let isProcessingActivation = false;
13+
1014
/**
11-
* Handles click events on elements with the class 'perflab-install-active-plugin'.
12-
*
13-
* This asynchronous function listens for click events on the document and executes
14-
* the provided callback function if triggered.
15+
* Enqueues plugin activation requests and starts processing if not already in progress.
1516
*
16-
* @param {MouseEvent} event - The click event object that is triggered when the user clicks on the document.
17-
*
18-
* @return {Promise<void>} The asynchronous function returns a promise that resolves to void.
17+
* @param {MouseEvent} event - The click event object.
1918
*/
20-
async function handlePluginActivationClick( event ) {
21-
const target = /** @type {HTMLElement} */ ( event.target );
22-
19+
function enqueuePluginActivation( event ) {
2320
// Prevent the default link behavior.
2421
event.preventDefault();
2522

23+
const target = /** @type {HTMLElement} */ ( event.target );
24+
2625
if (
2726
target.classList.contains( 'updating-message' ) ||
2827
target.classList.contains( 'disabled' )
@@ -31,12 +30,37 @@
3130
}
3231

3332
target.classList.add( 'updating-message' );
33+
target.textContent = __( 'Waiting…', 'performance-lab' );
34+
35+
const pluginSlug = target.dataset.pluginSlug;
36+
37+
activationQueue.push( { target, pluginSlug } );
38+
39+
// Start processing the queue if not already doing so.
40+
if ( ! isProcessingActivation ) {
41+
handlePluginActivation();
42+
}
43+
}
44+
45+
/**
46+
* Handles activation of feature/plugin using queue based approach.
47+
*
48+
* @return {Promise<void>} The asynchronous function returns a promise that resolves to void.
49+
*/
50+
async function handlePluginActivation() {
51+
if ( 0 === activationQueue.length ) {
52+
isProcessingActivation = false;
53+
return;
54+
}
55+
56+
isProcessingActivation = true;
57+
58+
const { target, pluginSlug } = activationQueue.shift();
59+
3460
target.textContent = __( 'Activating…', 'performance-lab' );
3561

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

38-
const pluginSlug = target.dataset.pluginSlug;
39-
4064
try {
4165
// Activate the plugin/feature via the REST API.
4266
await apiFetch( {
@@ -76,13 +100,15 @@
76100

77101
target.classList.remove( 'updating-message' );
78102
target.textContent = __( 'Activate', 'performance-lab' );
103+
} finally {
104+
handlePluginActivation();
79105
}
80106
}
81107

82108
// Attach the event listeners.
83109
document
84110
.querySelectorAll( '.perflab-install-active-plugin' )
85111
.forEach( ( item ) => {
86-
item.addEventListener( 'click', handlePluginActivationClick );
112+
item.addEventListener( 'click', enqueuePluginActivation );
87113
} );
88114
} )();

0 commit comments

Comments
 (0)