|
7 | 7 | const { i18n, a11y, apiFetch } = wp; |
8 | 8 | const { __ } = i18n; |
9 | 9 |
|
| 10 | + // Queue to hold pending activation requests. |
| 11 | + const activationQueue = []; |
| 12 | + let isProcessingActivation = false; |
| 13 | + |
10 | 14 | /** |
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. |
15 | 16 | * |
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. |
19 | 18 | */ |
20 | | - async function handlePluginActivationClick( event ) { |
21 | | - const target = /** @type {HTMLElement} */ ( event.target ); |
22 | | - |
| 19 | + function enqueuePluginActivation( event ) { |
23 | 20 | // Prevent the default link behavior. |
24 | 21 | event.preventDefault(); |
25 | 22 |
|
| 23 | + const target = /** @type {HTMLElement} */ ( event.target ); |
| 24 | + |
26 | 25 | if ( |
27 | 26 | target.classList.contains( 'updating-message' ) || |
28 | 27 | target.classList.contains( 'disabled' ) |
|
31 | 30 | } |
32 | 31 |
|
33 | 32 | 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 | + |
34 | 60 | target.textContent = __( 'Activating…', 'performance-lab' ); |
35 | 61 |
|
36 | 62 | a11y.speak( __( 'Activating…', 'performance-lab' ) ); |
37 | 63 |
|
38 | | - const pluginSlug = target.dataset.pluginSlug; |
39 | | - |
40 | 64 | try { |
41 | 65 | // Activate the plugin/feature via the REST API. |
42 | 66 | await apiFetch( { |
|
76 | 100 |
|
77 | 101 | target.classList.remove( 'updating-message' ); |
78 | 102 | target.textContent = __( 'Activate', 'performance-lab' ); |
| 103 | + } finally { |
| 104 | + handlePluginActivation(); |
79 | 105 | } |
80 | 106 | } |
81 | 107 |
|
82 | 108 | // Attach the event listeners. |
83 | 109 | document |
84 | 110 | .querySelectorAll( '.perflab-install-active-plugin' ) |
85 | 111 | .forEach( ( item ) => { |
86 | | - item.addEventListener( 'click', handlePluginActivationClick ); |
| 112 | + item.addEventListener( 'click', enqueuePluginActivation ); |
87 | 113 | } ); |
88 | 114 | } )(); |
0 commit comments