Skip to content

Commit c3b3236

Browse files
committed
Update JavaScript plugin activation to use queue based approach
1 parent 20a038f commit c3b3236

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,36 +7,60 @@
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' )
2928
) {
3029
return;
3130
}
3231

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